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> |
Kiyoung Kim | e655807 | 2019-09-30 16:58:39 +0900 | [diff] [blame] | 18 | |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 19 | #include <cstring> |
| 20 | #include <fstream> |
| 21 | #include <iostream> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "linkerconfig/baseconfig.h" |
Kiyoung Kim | e655807 | 2019-09-30 16:58:39 +0900 | [diff] [blame] | 25 | #include "linkerconfig/environment.h" |
| 26 | #include "linkerconfig/legacy.h" |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame] | 27 | #include "linkerconfig/log.h" |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 28 | #include "linkerconfig/variableloader.h" |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 29 | #include "linkerconfig/variables.h" |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | const static struct option program_options[] = { |
| 33 | {"target", required_argument, 0, 't'}, |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 34 | #ifndef __ANROID__ |
| 35 | {"root", required_argument, 0, 'r'}, |
| 36 | {"vndk", required_argument, 0, 'v'}, |
| 37 | #endif |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 38 | {"help", no_argument, 0, 'h'}, |
| 39 | {0, 0, 0, 0}}; |
| 40 | |
| 41 | struct ProgramArgs { |
| 42 | std::string target_file; |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 43 | std::string root; |
| 44 | std::string vndk_version; |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | [[noreturn]] void PrintUsage(int status = EXIT_SUCCESS) { |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 48 | std::cerr << "Usage : linkerconfig [--target <target_file>]" |
| 49 | #ifndef __ANDROID__ |
| 50 | " --root <root dir>" |
| 51 | " --vndk <vndk version>" |
| 52 | #endif |
| 53 | " [--help]" |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 54 | << std::endl; |
| 55 | exit(status); |
| 56 | } |
| 57 | |
| 58 | bool ParseArgs(int argc, char* argv[], ProgramArgs* args) { |
| 59 | int parse_result; |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 60 | while ((parse_result = getopt_long( |
| 61 | argc, argv, "t:r:v:h", program_options, NULL)) != -1) { |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 62 | switch (parse_result) { |
| 63 | case 't': |
| 64 | args->target_file = optarg; |
| 65 | break; |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 66 | case 'r': |
| 67 | args->root = optarg; |
| 68 | break; |
| 69 | case 'v': |
| 70 | args->vndk_version = optarg; |
| 71 | break; |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 72 | case 'h': |
| 73 | PrintUsage(); |
| 74 | default: |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (optind < argc) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | android::linkerconfig::modules::Configuration GetConfiguration() { |
Kiyoung Kim | e655807 | 2019-09-30 16:58:39 +0900 | [diff] [blame] | 87 | if (android::linkerconfig::modules::IsLegacyDevice()) { |
| 88 | return android::linkerconfig::contents::CreateLegacyConfiguration(); |
| 89 | } |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 90 | |
| 91 | // TODO : Use recovery if needed |
| 92 | |
| 93 | // Use base configuration in default |
| 94 | return android::linkerconfig::contents::CreateBaseConfiguration(); |
| 95 | } |
Kiyoung Kim | e3bad04 | 2019-07-23 13:38:34 +0900 | [diff] [blame] | 96 | |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 97 | #ifdef __ANDROID__ |
Kiyoung Kim | e3bad04 | 2019-07-23 13:38:34 +0900 | [diff] [blame] | 98 | struct CombinedLogger { |
| 99 | android::base::LogdLogger logd; |
| 100 | |
| 101 | void operator()(android::base::LogId id, android::base::LogSeverity severity, |
| 102 | const char* tag, const char* file, unsigned int line, |
| 103 | const char* message) { |
| 104 | logd(id, severity, tag, file, line, message); |
| 105 | KernelLogger(id, severity, tag, file, line, message); |
| 106 | } |
| 107 | }; |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 108 | #endif |
Kiyoung Kim | e3bad04 | 2019-07-23 13:38:34 +0900 | [diff] [blame] | 109 | |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 110 | } // namespace |
| 111 | |
| 112 | int main(int argc, char* argv[]) { |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 113 | android::base::InitLogging(argv |
| 114 | #ifdef __ANDROID__ |
| 115 | , |
| 116 | CombinedLogger() |
| 117 | #endif |
| 118 | ); |
Kiyoung Kim | e3bad04 | 2019-07-23 13:38:34 +0900 | [diff] [blame] | 119 | |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 120 | ProgramArgs args; |
| 121 | |
| 122 | if (!ParseArgs(argc, argv, &args)) { |
| 123 | PrintUsage(EXIT_FAILURE); |
| 124 | } |
| 125 | |
| 126 | std::ostream* out = &std::cout; |
| 127 | std::ofstream file_out; |
| 128 | |
| 129 | if (args.target_file != "") { |
| 130 | file_out.open(args.target_file); |
| 131 | if (file_out.fail()) { |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame] | 132 | PLOG(FATAL) << "Failed to open file " << args.target_file; |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 133 | return EXIT_FAILURE; |
| 134 | } |
| 135 | out = &file_out; |
| 136 | } |
| 137 | |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 138 | #ifndef __ANDROID__ |
| 139 | if (args.root == "" || args.vndk_version == "") { |
| 140 | PrintUsage(); |
| 141 | } |
| 142 | android::linkerconfig::modules::Variables::AddValue("ro.vndk.version", |
| 143 | args.vndk_version); |
| 144 | #endif |
| 145 | |
| 146 | android::linkerconfig::generator::LoadVariables(args.root); |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 147 | auto config = GetConfiguration(); |
| 148 | android::linkerconfig::modules::ConfigWriter config_writer; |
| 149 | |
| 150 | config.WriteConfig(config_writer); |
| 151 | *out << config_writer.ToString(); |
| 152 | if (!out->good()) { |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame] | 153 | PLOG(FATAL) << "Failed to write content to " << args.target_file; |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 154 | return EXIT_FAILURE; |
| 155 | } |
| 156 | |
| 157 | return EXIT_SUCCESS; |
| 158 | } |