blob: 28aabfa94daa63ad308ebd315d5cd9e0948e4d9d [file] [log] [blame]
Kiyoung Kime9a77fe2019-05-23 11:04:20 +09001/*
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 Kime6558072019-09-30 16:58:39 +090018
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090019#include <cstring>
20#include <fstream>
21#include <iostream>
22#include <string>
23
24#include "linkerconfig/baseconfig.h"
Kiyoung Kime6558072019-09-30 16:58:39 +090025#include "linkerconfig/environment.h"
26#include "linkerconfig/legacy.h"
Kiyoung Kim853438d2019-07-16 09:51:14 +090027#include "linkerconfig/log.h"
Kiyoung Kim09cbb082019-12-05 16:44:34 +090028#include "linkerconfig/recovery.h"
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090029#include "linkerconfig/variableloader.h"
Jooyung Hana3d5d092019-09-26 23:23:50 +090030#include "linkerconfig/variables.h"
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090031
32namespace {
33const static struct option program_options[] = {
34 {"target", required_argument, 0, 't'},
Josh Gao76614bb2019-12-11 15:28:30 -080035#ifndef __ANDROID__
Jooyung Hana3d5d092019-09-26 23:23:50 +090036 {"root", required_argument, 0, 'r'},
37 {"vndk", required_argument, 0, 'v'},
38#endif
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090039 {"help", no_argument, 0, 'h'},
40 {0, 0, 0, 0}};
41
42struct ProgramArgs {
43 std::string target_file;
Jooyung Hana3d5d092019-09-26 23:23:50 +090044 std::string root;
45 std::string vndk_version;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090046};
47
48[[noreturn]] void PrintUsage(int status = EXIT_SUCCESS) {
Jooyung Hana3d5d092019-09-26 23:23:50 +090049 std::cerr << "Usage : linkerconfig [--target <target_file>]"
50#ifndef __ANDROID__
51 " --root <root dir>"
52 " --vndk <vndk version>"
53#endif
Kiyoung Kim09cbb082019-12-05 16:44:34 +090054 " [--recovery]"
Jooyung Hana3d5d092019-09-26 23:23:50 +090055 " [--help]"
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090056 << std::endl;
57 exit(status);
58}
59
60bool ParseArgs(int argc, char* argv[], ProgramArgs* args) {
61 int parse_result;
Jooyung Hana3d5d092019-09-26 23:23:50 +090062 while ((parse_result = getopt_long(
63 argc, argv, "t:r:v:h", program_options, NULL)) != -1) {
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090064 switch (parse_result) {
65 case 't':
66 args->target_file = optarg;
67 break;
Jooyung Hana3d5d092019-09-26 23:23:50 +090068 case 'r':
69 args->root = optarg;
70 break;
71 case 'v':
72 args->vndk_version = optarg;
73 break;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090074 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
88android::linkerconfig::modules::Configuration GetConfiguration() {
Kiyoung Kim09cbb082019-12-05 16:44:34 +090089 if (android::linkerconfig::modules::IsRecoveryMode()) {
90 return android::linkerconfig::contents::CreateRecoveryConfiguration();
91 }
92
Kiyoung Kime6558072019-09-30 16:58:39 +090093 if (android::linkerconfig::modules::IsLegacyDevice()) {
94 return android::linkerconfig::contents::CreateLegacyConfiguration();
95 }
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090096
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090097 // Use base configuration in default
98 return android::linkerconfig::contents::CreateBaseConfiguration();
99}
Kiyoung Kime3bad042019-07-23 13:38:34 +0900100
Jooyung Hana3d5d092019-09-26 23:23:50 +0900101#ifdef __ANDROID__
Kiyoung Kime3bad042019-07-23 13:38:34 +0900102struct 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 Hana3d5d092019-09-26 23:23:50 +0900112#endif
Kiyoung Kime3bad042019-07-23 13:38:34 +0900113
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900114} // namespace
115
116int main(int argc, char* argv[]) {
Jooyung Hana3d5d092019-09-26 23:23:50 +0900117 android::base::InitLogging(argv
118#ifdef __ANDROID__
119 ,
120 CombinedLogger()
121#endif
122 );
Kiyoung Kime3bad042019-07-23 13:38:34 +0900123
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900124 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 Kim853438d2019-07-16 09:51:14 +0900136 PLOG(FATAL) << "Failed to open file " << args.target_file;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900137 return EXIT_FAILURE;
138 }
139 out = &file_out;
140 }
141
Jooyung Hana3d5d092019-09-26 23:23:50 +0900142#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 Kime9a77fe2019-05-23 11:04:20 +0900151 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 Kim853438d2019-07-16 09:51:14 +0900157 PLOG(FATAL) << "Failed to write content to " << args.target_file;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900158 return EXIT_FAILURE;
159 }
160
161 return EXIT_SUCCESS;
Josh Gao76614bb2019-12-11 15:28:30 -0800162}