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" |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame^] | 24 | #include "linkerconfig/log.h" |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 25 | #include "linkerconfig/variableloader.h" |
| 26 | |
| 27 | namespace { |
| 28 | const static struct option program_options[] = { |
| 29 | {"target", required_argument, 0, 't'}, |
| 30 | {"help", no_argument, 0, 'h'}, |
| 31 | {0, 0, 0, 0}}; |
| 32 | |
| 33 | struct ProgramArgs { |
| 34 | std::string target_file; |
| 35 | }; |
| 36 | |
| 37 | [[noreturn]] void PrintUsage(int status = EXIT_SUCCESS) { |
| 38 | std::cerr << "Usage : linkerconfig [--target <target_file>] [--help]" |
| 39 | << std::endl; |
| 40 | exit(status); |
| 41 | } |
| 42 | |
| 43 | bool ParseArgs(int argc, char* argv[], ProgramArgs* args) { |
| 44 | int parse_result; |
| 45 | while ((parse_result = |
| 46 | getopt_long(argc, argv, "th:", program_options, NULL)) != -1) { |
| 47 | switch (parse_result) { |
| 48 | case 't': |
| 49 | args->target_file = optarg; |
| 50 | break; |
| 51 | case 'h': |
| 52 | PrintUsage(); |
| 53 | default: |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (optind < argc) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | android::linkerconfig::modules::Configuration GetConfiguration() { |
| 66 | // TODO : Use legacy if needed |
| 67 | |
| 68 | // TODO : Use vndk lite if needed |
| 69 | |
| 70 | // TODO : Use recovery if needed |
| 71 | |
| 72 | // Use base configuration in default |
| 73 | return android::linkerconfig::contents::CreateBaseConfiguration(); |
| 74 | } |
Kiyoung Kim | e3bad04 | 2019-07-23 13:38:34 +0900 | [diff] [blame] | 75 | |
| 76 | struct CombinedLogger { |
| 77 | android::base::LogdLogger logd; |
| 78 | |
| 79 | void operator()(android::base::LogId id, android::base::LogSeverity severity, |
| 80 | const char* tag, const char* file, unsigned int line, |
| 81 | const char* message) { |
| 82 | logd(id, severity, tag, file, line, message); |
| 83 | KernelLogger(id, severity, tag, file, line, message); |
| 84 | } |
| 85 | }; |
| 86 | |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 87 | } // namespace |
| 88 | |
| 89 | int main(int argc, char* argv[]) { |
Kiyoung Kim | e3bad04 | 2019-07-23 13:38:34 +0900 | [diff] [blame] | 90 | android::base::InitLogging(argv, CombinedLogger()); |
| 91 | |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 92 | ProgramArgs args; |
| 93 | |
| 94 | if (!ParseArgs(argc, argv, &args)) { |
| 95 | PrintUsage(EXIT_FAILURE); |
| 96 | } |
| 97 | |
| 98 | std::ostream* out = &std::cout; |
| 99 | std::ofstream file_out; |
| 100 | |
| 101 | if (args.target_file != "") { |
| 102 | file_out.open(args.target_file); |
| 103 | if (file_out.fail()) { |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame^] | 104 | PLOG(FATAL) << "Failed to open file " << args.target_file; |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 105 | return EXIT_FAILURE; |
| 106 | } |
| 107 | out = &file_out; |
| 108 | } |
| 109 | |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame^] | 110 | android::linkerconfig::generator::LoadVariables(); |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 111 | auto config = GetConfiguration(); |
| 112 | android::linkerconfig::modules::ConfigWriter config_writer; |
| 113 | |
| 114 | config.WriteConfig(config_writer); |
| 115 | *out << config_writer.ToString(); |
| 116 | if (!out->good()) { |
Kiyoung Kim | 853438d | 2019-07-16 09:51:14 +0900 | [diff] [blame^] | 117 | PLOG(FATAL) << "Failed to write content to " << args.target_file; |
Kiyoung Kim | e9a77fe | 2019-05-23 11:04:20 +0900 | [diff] [blame] | 118 | return EXIT_FAILURE; |
| 119 | } |
| 120 | |
| 121 | return EXIT_SUCCESS; |
| 122 | } |