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 | */ |
Kiyoung Kim | e655807 | 2019-09-30 16:58:39 +0900 | [diff] [blame] | 16 | |
Jooyung Han | 471aeb9 | 2020-02-20 18:21:03 +0900 | [diff] [blame] | 17 | #include <climits> |
| 18 | #include <cstdlib> |
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 | |
Jooyung Han | a59fbd5 | 2020-01-30 12:57:34 +0900 | [diff] [blame] | 24 | #include <errno.h> |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 25 | #include <fcntl.h> |
Jooyung Han | 471aeb9 | 2020-02-20 18:21:03 +0900 | [diff] [blame] | 26 | #include <getopt.h> |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 27 | #include <sys/stat.h> |
Jooyung Han | a59fbd5 | 2020-01-30 12:57:34 +0900 | [diff] [blame] | 28 | #include <sys/types.h> |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 29 | |
Jooyung Han | 471aeb9 | 2020-02-20 18:21:03 +0900 | [diff] [blame] | 30 | #include <android-base/result.h> |
Jooyung Han | 8e5cc8d | 2020-03-03 00:34:32 +0900 | [diff] [blame] | 31 | #include <android-base/strings.h> |
Jooyung Han | 471aeb9 | 2020-02-20 18:21:03 +0900 | [diff] [blame] | 32 | |
Kiyoung Kim | 284bc72 | 2020-01-20 13:33:00 +0900 | [diff] [blame] | 33 | #include "linkerconfig/apex.h" |
| 34 | #include "linkerconfig/apexconfig.h" |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 35 | #include "linkerconfig/baseconfig.h" |
Kiyoung Kim | d7fb1c1 | 2020-10-21 09:59:12 +0900 | [diff] [blame] | 36 | #include "linkerconfig/configparser.h" |
Kiyoung Kim | 284bc72 | 2020-01-20 13:33:00 +0900 | [diff] [blame] | 37 | #include "linkerconfig/context.h" |
Kiyoung Kim | e655807 | 2019-09-30 16:58:39 +0900 | [diff] [blame] | 38 | #include "linkerconfig/environment.h" |
| 39 | #include "linkerconfig/legacy.h" |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame] | 40 | #include "linkerconfig/log.h" |
Jooyung Han | 9960aca | 2020-02-06 20:17:53 +0900 | [diff] [blame] | 41 | #include "linkerconfig/namespacebuilder.h" |
Kiyoung Kim | 09cbb08 | 2019-12-05 16:44:34 +0900 | [diff] [blame] | 42 | #include "linkerconfig/recovery.h" |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 43 | #include "linkerconfig/variableloader.h" |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 44 | #include "linkerconfig/variables.h" |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 45 | |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 46 | using android::base::ErrnoError; |
| 47 | using android::base::Error; |
Jooyung Han | 8e5cc8d | 2020-03-03 00:34:32 +0900 | [diff] [blame] | 48 | using android::base::Join; |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 49 | using android::base::Result; |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 50 | using android::linkerconfig::contents::Context; |
Kiyoung Kim | 284bc72 | 2020-01-20 13:33:00 +0900 | [diff] [blame] | 51 | using android::linkerconfig::modules::ApexInfo; |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 52 | using android::linkerconfig::modules::Configuration; |
| 53 | |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 54 | namespace { |
| 55 | const static struct option program_options[] = { |
| 56 | {"target", required_argument, 0, 't'}, |
Jooyung Han | b4af747 | 2020-02-03 18:07:12 +0900 | [diff] [blame] | 57 | {"strict", no_argument, 0, 's'}, |
Josh Gao | 76614bb | 2019-12-11 15:28:30 -0800 | [diff] [blame] | 58 | #ifndef __ANDROID__ |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 59 | {"root", required_argument, 0, 'r'}, |
| 60 | {"vndk", required_argument, 0, 'v'}, |
Jooyung Han | 0441fd3 | 2020-03-31 16:14:56 +0900 | [diff] [blame] | 61 | {"product_vndk", required_argument, 0, 'p'}, |
Kiyoung Kim | 722dad6 | 2019-12-19 15:59:55 +0900 | [diff] [blame] | 62 | {"recovery", no_argument, 0, 'y'}, |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 63 | #endif |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 64 | {"help", no_argument, 0, 'h'}, |
| 65 | {0, 0, 0, 0}}; |
| 66 | |
| 67 | struct ProgramArgs { |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 68 | std::string target_directory; |
Jooyung Han | b4af747 | 2020-02-03 18:07:12 +0900 | [diff] [blame] | 69 | bool strict; |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 70 | std::string root; |
| 71 | std::string vndk_version; |
Jooyung Han | 0441fd3 | 2020-03-31 16:14:56 +0900 | [diff] [blame] | 72 | std::string product_vndk_version; |
Kiyoung Kim | 722dad6 | 2019-12-19 15:59:55 +0900 | [diff] [blame] | 73 | bool is_recovery; |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | [[noreturn]] void PrintUsage(int status = EXIT_SUCCESS) { |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 77 | std::cerr << "Usage : linkerconfig [--target <target_directory>]" |
Jooyung Han | b4af747 | 2020-02-03 18:07:12 +0900 | [diff] [blame] | 78 | " [--strict]" |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 79 | #ifndef __ANDROID__ |
| 80 | " --root <root dir>" |
| 81 | " --vndk <vndk version>" |
Jooyung Han | 0441fd3 | 2020-03-31 16:14:56 +0900 | [diff] [blame] | 82 | " --product_vndk <product vndk version>" |
Kiyoung Kim | 722dad6 | 2019-12-19 15:59:55 +0900 | [diff] [blame] | 83 | " --recovery" |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 84 | #endif |
| 85 | " [--help]" |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 86 | << std::endl; |
| 87 | exit(status); |
| 88 | } |
| 89 | |
Jooyung Han | 471aeb9 | 2020-02-20 18:21:03 +0900 | [diff] [blame] | 90 | std::string RealPath(std::string_view path) { |
| 91 | char resolved_path[PATH_MAX]; |
| 92 | if (realpath(path.data(), resolved_path) != nullptr) { |
| 93 | return resolved_path; |
| 94 | } |
| 95 | PrintUsage(-1); |
| 96 | } |
| 97 | |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 98 | bool ParseArgs(int argc, char* argv[], ProgramArgs* args) { |
| 99 | int parse_result; |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 100 | while ((parse_result = getopt_long( |
Jooyung Han | 0441fd3 | 2020-03-31 16:14:56 +0900 | [diff] [blame] | 101 | argc, argv, "t:sr:v:ep:hyl", program_options, NULL)) != -1) { |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 102 | switch (parse_result) { |
| 103 | case 't': |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 104 | args->target_directory = optarg; |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 105 | break; |
Jooyung Han | b4af747 | 2020-02-03 18:07:12 +0900 | [diff] [blame] | 106 | case 's': |
| 107 | args->strict = true; |
| 108 | break; |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 109 | case 'r': |
Jooyung Han | 471aeb9 | 2020-02-20 18:21:03 +0900 | [diff] [blame] | 110 | args->root = RealPath(optarg); |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 111 | break; |
| 112 | case 'v': |
| 113 | args->vndk_version = optarg; |
| 114 | break; |
Jooyung Han | 0441fd3 | 2020-03-31 16:14:56 +0900 | [diff] [blame] | 115 | case 'p': |
| 116 | args->product_vndk_version = optarg; |
| 117 | break; |
Kiyoung Kim | 722dad6 | 2019-12-19 15:59:55 +0900 | [diff] [blame] | 118 | case 'y': |
| 119 | args->is_recovery = true; |
| 120 | break; |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 121 | case 'h': |
| 122 | PrintUsage(); |
| 123 | default: |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (optind < argc) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 135 | void LoadVariables(ProgramArgs args) { |
Kiyoung Kim | 722dad6 | 2019-12-19 15:59:55 +0900 | [diff] [blame] | 136 | #ifndef __ANDROID__ |
Jooyung Han | 71b4bf3 | 2020-04-02 15:40:41 +0900 | [diff] [blame] | 137 | if (!args.is_recovery && args.root == "") { |
Kiyoung Kim | 722dad6 | 2019-12-19 15:59:55 +0900 | [diff] [blame] | 138 | PrintUsage(); |
| 139 | } |
| 140 | android::linkerconfig::modules::Variables::AddValue("ro.vndk.version", |
| 141 | args.vndk_version); |
Jooyung Han | 0441fd3 | 2020-03-31 16:14:56 +0900 | [diff] [blame] | 142 | android::linkerconfig::modules::Variables::AddValue( |
| 143 | "ro.product.vndk.version", args.product_vndk_version); |
Kiyoung Kim | 722dad6 | 2019-12-19 15:59:55 +0900 | [diff] [blame] | 144 | #endif |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 145 | if (!args.is_recovery) { |
| 146 | android::linkerconfig::generator::LoadVariables(args.root); |
| 147 | } |
| 148 | } |
Kiyoung Kim | 722dad6 | 2019-12-19 15:59:55 +0900 | [diff] [blame] | 149 | |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 150 | Result<void> WriteConfigurationToFile(Configuration& conf, |
| 151 | std::string file_path) { |
| 152 | std::ostream* out = &std::cout; |
| 153 | std::ofstream file_out; |
| 154 | |
| 155 | if (file_path != "") { |
| 156 | file_out.open(file_path); |
| 157 | if (file_out.fail()) { |
| 158 | return ErrnoError() << "Failed to open file " << file_path; |
| 159 | } |
| 160 | out = &file_out; |
| 161 | } |
| 162 | |
| 163 | android::linkerconfig::modules::ConfigWriter config_writer; |
| 164 | |
| 165 | conf.WriteConfig(config_writer); |
| 166 | *out << config_writer.ToString(); |
| 167 | if (!out->good()) { |
| 168 | return ErrnoError() << "Failed to write content to " << file_path; |
| 169 | } |
| 170 | |
| 171 | return {}; |
| 172 | } |
| 173 | |
Jooyung Han | 5f24636 | 2020-01-28 15:44:14 +0900 | [diff] [blame] | 174 | Result<void> UpdatePermission([[maybe_unused]] const std::string& file_path) { |
| 175 | #ifdef __ANDROID__ |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 176 | if (fchmodat(AT_FDCWD, |
| 177 | file_path.c_str(), |
| 178 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, |
| 179 | AT_SYMLINK_NOFOLLOW) < 0) { |
| 180 | return ErrnoError() << "Failed to update permission of " << file_path; |
| 181 | } |
Jooyung Han | 5f24636 | 2020-01-28 15:44:14 +0900 | [diff] [blame] | 182 | #endif |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 183 | |
| 184 | return {}; |
| 185 | } |
| 186 | |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 187 | Context GetContext(ProgramArgs args) { |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 188 | Context ctx; |
Jooyung Han | b4af747 | 2020-02-03 18:07:12 +0900 | [diff] [blame] | 189 | if (args.strict) { |
| 190 | ctx.SetStrictMode(true); |
| 191 | } |
Jooyung Han | b632392 | 2020-08-11 08:56:29 +0000 | [diff] [blame] | 192 | if (!args.is_recovery) { |
| 193 | auto apex_list = android::linkerconfig::modules::ScanActiveApexes(args.root); |
Kiyoung Kim | b972763 | 2020-12-08 16:14:57 +0900 | [diff] [blame] | 194 | if (apex_list.ok()) { |
| 195 | for (auto const& apex_item : *apex_list) { |
| 196 | auto apex_info = apex_item.second; |
| 197 | if (apex_info.has_bin || apex_info.has_lib) { |
| 198 | ctx.AddApexModule(std::move(apex_info)); |
| 199 | } |
Jooyung Han | b632392 | 2020-08-11 08:56:29 +0000 | [diff] [blame] | 200 | } |
Kiyoung Kim | b972763 | 2020-12-08 16:14:57 +0900 | [diff] [blame] | 201 | } else { |
| 202 | LOG(ERROR) << "Failed to scan APEX modules : " << apex_list.error(); |
Jooyung Han | b632392 | 2020-08-11 08:56:29 +0000 | [diff] [blame] | 203 | } |
Jooyung Han | b632392 | 2020-08-11 08:56:29 +0000 | [diff] [blame] | 204 | } |
Kiyoung Kim | d7fb1c1 | 2020-10-21 09:59:12 +0900 | [diff] [blame] | 205 | |
| 206 | std::string system_config_path = args.root + "/system/etc/linker.config.pb"; |
| 207 | if (access(system_config_path.c_str(), F_OK) == 0) { |
| 208 | auto system_config = |
| 209 | android::linkerconfig::modules::ParseLinkerConfig(system_config_path); |
| 210 | if (system_config.ok()) { |
| 211 | ctx.SetSystemConfig(*system_config); |
| 212 | } else { |
| 213 | LOG(ERROR) << "Failed to read system config : " << system_config.error(); |
| 214 | } |
| 215 | } |
Kiyoung Kim | 6f084a6 | 2021-05-27 15:51:16 +0900 | [diff] [blame] | 216 | |
| 217 | std::string vendor_config_path = args.root + "/vendor/etc/linker.config.pb"; |
| 218 | if (access(vendor_config_path.c_str(), F_OK) == 0) { |
| 219 | auto vendor_config = |
| 220 | android::linkerconfig::modules::ParseLinkerConfig(vendor_config_path); |
| 221 | if (vendor_config.ok()) { |
| 222 | ctx.SetVendorConfig(*vendor_config); |
| 223 | } else { |
| 224 | LOG(ERROR) << "Failed to read vendor config : " << vendor_config.error(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | std::string product_config_path = args.root + "/product/etc/linker.config.pb"; |
| 229 | if (access(product_config_path.c_str(), F_OK) == 0) { |
| 230 | auto product_config = |
| 231 | android::linkerconfig::modules::ParseLinkerConfig(product_config_path); |
| 232 | if (product_config.ok()) { |
| 233 | ctx.SetProductConfig(*product_config); |
| 234 | } else { |
| 235 | LOG(ERROR) << "Failed to read product config : " << product_config.error(); |
| 236 | } |
| 237 | } |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 238 | return ctx; |
| 239 | } |
| 240 | |
| 241 | Configuration GetConfiguration(Context& ctx) { |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 242 | if (android::linkerconfig::modules::IsRecoveryMode()) { |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 243 | return android::linkerconfig::contents::CreateRecoveryConfiguration(ctx); |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | if (android::linkerconfig::modules::IsLegacyDevice()) { |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 247 | return android::linkerconfig::contents::CreateLegacyConfiguration(ctx); |
Kiyoung Kim | e655807 | 2019-09-30 16:58:39 +0900 | [diff] [blame] | 248 | } |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 249 | |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 250 | // Use base configuration in default |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 251 | return android::linkerconfig::contents::CreateBaseConfiguration(ctx); |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 252 | } |
Kiyoung Kim | e3bad04 | 2019-07-23 13:38:34 +0900 | [diff] [blame] | 253 | |
Kiyoung Kim | 32e9380 | 2020-08-03 23:15:52 +0900 | [diff] [blame] | 254 | Result<void> GenerateConfiguration(Configuration config, |
| 255 | const std::string& dir_path, |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 256 | bool update_permission) { |
| 257 | std::string file_path = ""; |
| 258 | if (dir_path != "") { |
| 259 | file_path = dir_path + "/ld.config.txt"; |
| 260 | } |
| 261 | |
| 262 | auto write_config = WriteConfigurationToFile(config, file_path); |
Bernie Innocenti | 5c32556 | 2020-02-06 23:16:23 +0900 | [diff] [blame] | 263 | if (!write_config.ok()) { |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 264 | return write_config; |
| 265 | } else if (update_permission && file_path != "") { |
| 266 | return UpdatePermission(file_path); |
| 267 | } |
| 268 | |
| 269 | return {}; |
| 270 | } |
| 271 | |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 272 | Result<void> GenerateBaseLinkerConfiguration(Context& ctx, |
| 273 | const std::string& dir_path) { |
| 274 | return GenerateConfiguration(GetConfiguration(ctx), dir_path, true); |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 275 | } |
| 276 | |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 277 | Result<void> GenerateRecoveryLinkerConfiguration(Context& ctx, |
| 278 | const std::string& dir_path) { |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 279 | return GenerateConfiguration( |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 280 | android::linkerconfig::contents::CreateRecoveryConfiguration(ctx), |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 281 | dir_path, |
| 282 | false); |
| 283 | } |
| 284 | |
Kiyoung Kim | 284bc72 | 2020-01-20 13:33:00 +0900 | [diff] [blame] | 285 | Result<void> GenerateApexConfiguration( |
| 286 | const std::string& base_dir, android::linkerconfig::contents::Context& ctx, |
| 287 | const android::linkerconfig::modules::ApexInfo& target_apex) { |
| 288 | std::string dir_path = base_dir + "/" + target_apex.name; |
Jooyung Han | a59fbd5 | 2020-01-30 12:57:34 +0900 | [diff] [blame] | 289 | if (auto ret = mkdir(dir_path.c_str(), 0755); ret != 0 && errno != EEXIST) { |
Kiyoung Kim | 284bc72 | 2020-01-20 13:33:00 +0900 | [diff] [blame] | 290 | return ErrnoError() << "Failed to create directory " << dir_path; |
| 291 | } |
| 292 | |
| 293 | return GenerateConfiguration( |
| 294 | android::linkerconfig::contents::CreateApexConfiguration(ctx, target_apex), |
| 295 | dir_path, |
| 296 | true); |
| 297 | } |
| 298 | |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 299 | void GenerateApexConfigurations(Context& ctx, const std::string& dir_path) { |
| 300 | for (auto const& apex_item : ctx.GetApexModules()) { |
| 301 | if (apex_item.has_bin) { |
| 302 | auto result = GenerateApexConfiguration(dir_path, ctx, apex_item); |
Bernie Innocenti | 5c32556 | 2020-02-06 23:16:23 +0900 | [diff] [blame] | 303 | if (!result.ok()) { |
Kiyoung Kim | 284bc72 | 2020-01-20 13:33:00 +0900 | [diff] [blame] | 304 | LOG(WARNING) << result.error(); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Jooyung Han | 52ccfa7 | 2020-09-01 13:49:27 +0900 | [diff] [blame] | 310 | void GenerateApexLibrariesConfig(Context& ctx, const std::string& dir_path) { |
Jooyung Han | 8e5cc8d | 2020-03-03 00:34:32 +0900 | [diff] [blame] | 311 | if (dir_path == "") { |
| 312 | return; |
| 313 | } |
Jooyung Han | 52ccfa7 | 2020-09-01 13:49:27 +0900 | [diff] [blame] | 314 | const std::string file_path = dir_path + "/apex.libraries.config.txt"; |
Jooyung Han | 8e5cc8d | 2020-03-03 00:34:32 +0900 | [diff] [blame] | 315 | std::ofstream out(file_path); |
| 316 | for (auto const& apex_item : ctx.GetApexModules()) { |
| 317 | if (!apex_item.jni_libs.empty()) { |
Jooyung Han | 52ccfa7 | 2020-09-01 13:49:27 +0900 | [diff] [blame] | 318 | out << "jni " << apex_item.namespace_name << " " |
| 319 | << Join(apex_item.jni_libs, ":") << '\n'; |
| 320 | } |
| 321 | if (!apex_item.public_libs.empty()) { |
| 322 | out << "public " << apex_item.namespace_name << " " |
| 323 | << Join(apex_item.public_libs, ":") << '\n'; |
Jooyung Han | 8e5cc8d | 2020-03-03 00:34:32 +0900 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | out.close(); |
| 327 | UpdatePermission(file_path); |
| 328 | } |
| 329 | |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 330 | void ExitOnFailure(Result<void> task) { |
Bernie Innocenti | 5c32556 | 2020-02-06 23:16:23 +0900 | [diff] [blame] | 331 | if (!task.ok()) { |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 332 | LOG(FATAL) << task.error(); |
| 333 | exit(EXIT_FAILURE); |
| 334 | } |
| 335 | } |
| 336 | |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 337 | #ifdef __ANDROID__ |
Kiyoung Kim | e3bad04 | 2019-07-23 13:38:34 +0900 | [diff] [blame] | 338 | struct CombinedLogger { |
| 339 | android::base::LogdLogger logd; |
| 340 | |
| 341 | void operator()(android::base::LogId id, android::base::LogSeverity severity, |
| 342 | const char* tag, const char* file, unsigned int line, |
| 343 | const char* message) { |
| 344 | logd(id, severity, tag, file, line, message); |
| 345 | KernelLogger(id, severity, tag, file, line, message); |
| 346 | } |
| 347 | }; |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 348 | #endif |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 349 | } // namespace |
| 350 | |
| 351 | int main(int argc, char* argv[]) { |
Jooyung Han | a3d5d09 | 2019-09-26 23:23:50 +0900 | [diff] [blame] | 352 | android::base::InitLogging(argv |
| 353 | #ifdef __ANDROID__ |
| 354 | , |
| 355 | CombinedLogger() |
| 356 | #endif |
| 357 | ); |
Kiyoung Kim | e3bad04 | 2019-07-23 13:38:34 +0900 | [diff] [blame] | 358 | |
Jooyung Han | 5bbb123 | 2019-12-23 13:35:00 +0900 | [diff] [blame] | 359 | ProgramArgs args = {}; |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 360 | |
| 361 | if (!ParseArgs(argc, argv, &args)) { |
| 362 | PrintUsage(EXIT_FAILURE); |
| 363 | } |
| 364 | |
Richard Fung | 28d91b5 | 2020-06-25 12:38:13 -0700 | [diff] [blame] | 365 | if (!android::linkerconfig::modules::IsLegacyDevice() && |
| 366 | android::linkerconfig::modules::IsVndkLiteDevice()) { |
Kiyoung Kim | 2006ea7 | 2020-06-23 16:15:47 +0900 | [diff] [blame] | 367 | LOG(ERROR) << "Linkerconfig no longer supports VNDK-Lite configuration"; |
| 368 | exit(EXIT_FAILURE); |
| 369 | } |
| 370 | |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 371 | LoadVariables(args); |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 372 | Context ctx = GetContext(args); |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 373 | |
Jooyung Han | 82bda26 | 2020-06-11 16:58:24 +0900 | [diff] [blame] | 374 | // when exec'ed from init, this is 077, which makes the subdirectories |
| 375 | // inaccessible for others. set umask to 022 so that they can be |
Jiyong Park | d601820 | 2020-02-21 13:56:52 +0900 | [diff] [blame] | 376 | // accessible. |
Jooyung Han | 82bda26 | 2020-06-11 16:58:24 +0900 | [diff] [blame] | 377 | umask(022); |
Jiyong Park | d601820 | 2020-02-21 13:56:52 +0900 | [diff] [blame] | 378 | |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 379 | if (args.is_recovery) { |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 380 | ExitOnFailure( |
| 381 | GenerateRecoveryLinkerConfiguration(ctx, args.target_directory)); |
Kiyoung Kim | 5c8366d | 2019-12-30 18:43:00 +0900 | [diff] [blame] | 382 | } else { |
Jooyung Han | 87e7df8 | 2020-02-03 14:23:06 +0900 | [diff] [blame] | 383 | ExitOnFailure(GenerateBaseLinkerConfiguration(ctx, args.target_directory)); |
| 384 | GenerateApexConfigurations(ctx, args.target_directory); |
Jooyung Han | 52ccfa7 | 2020-09-01 13:49:27 +0900 | [diff] [blame] | 385 | GenerateApexLibrariesConfig(ctx, args.target_directory); |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | return EXIT_SUCCESS; |
Kiyoung Kim | 32e9380 | 2020-08-03 23:15:52 +0900 | [diff] [blame] | 389 | } |