Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <getopt.h> |
| 18 | #include <cstring> |
| 19 | #include <fstream> |
| 20 | #include <iostream> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "linkerconfig/baseconfig.h" |
| 24 | #include "linkerconfig/variableloader.h" |
| 25 | |
| 26 | namespace { |
| 27 | const static struct option program_options[] = { |
| 28 | {"target", required_argument, 0, 't'}, |
| 29 | {"help", no_argument, 0, 'h'}, |
| 30 | {0, 0, 0, 0}}; |
| 31 | |
| 32 | struct ProgramArgs { |
| 33 | std::string target_file; |
| 34 | }; |
| 35 | |
| 36 | [[noreturn]] void PrintUsage(int status = EXIT_SUCCESS) { |
| 37 | std::cerr << "Usage : linkerconfig [--target <target_file>] [--help]" |
| 38 | << std::endl; |
| 39 | exit(status); |
| 40 | } |
| 41 | |
| 42 | bool ParseArgs(int argc, char* argv[], ProgramArgs* args) { |
| 43 | int parse_result; |
| 44 | while ((parse_result = |
| 45 | getopt_long(argc, argv, "th:", program_options, NULL)) != -1) { |
| 46 | switch (parse_result) { |
| 47 | case 't': |
| 48 | args->target_file = optarg; |
| 49 | break; |
| 50 | case 'h': |
| 51 | PrintUsage(); |
| 52 | default: |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (optind < argc) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | android::linkerconfig::modules::Configuration GetConfiguration() { |
| 65 | // TODO : Use legacy if needed |
| 66 | |
| 67 | // TODO : Use vndk lite if needed |
| 68 | |
| 69 | // TODO : Use recovery if needed |
| 70 | |
| 71 | // Use base configuration in default |
| 72 | return android::linkerconfig::contents::CreateBaseConfiguration(); |
| 73 | } |
| 74 | } // namespace |
| 75 | |
| 76 | int main(int argc, char* argv[]) { |
| 77 | ProgramArgs args; |
| 78 | |
| 79 | if (!ParseArgs(argc, argv, &args)) { |
| 80 | PrintUsage(EXIT_FAILURE); |
| 81 | } |
| 82 | |
| 83 | std::ostream* out = &std::cout; |
| 84 | std::ofstream file_out; |
| 85 | |
| 86 | if (args.target_file != "") { |
| 87 | file_out.open(args.target_file); |
| 88 | if (file_out.fail()) { |
| 89 | std::cerr << "Failed to open file " << args.target_file << " : " |
| 90 | << std::strerror(errno) << std::endl; |
| 91 | return EXIT_FAILURE; |
| 92 | } |
| 93 | out = &file_out; |
| 94 | } |
| 95 | |
| 96 | android::linkerconfig::generator::LoadVariable(); |
| 97 | auto config = GetConfiguration(); |
| 98 | android::linkerconfig::modules::ConfigWriter config_writer; |
| 99 | |
| 100 | config.WriteConfig(config_writer); |
| 101 | *out << config_writer.ToString(); |
| 102 | if (!out->good()) { |
| 103 | std::cerr << "Failed to write content to " << args.target_file << " : " |
| 104 | << std::strerror(errno) << std::endl; |
| 105 | return EXIT_FAILURE; |
| 106 | } |
| 107 | |
| 108 | return EXIT_SUCCESS; |
| 109 | } |