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