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