blob: 6c799c5dde1e269012ad99513c3017f6ed515b41 [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'},
Kiyoung Kim722dad62019-12-19 15:59:55 +090038 {"recovery", no_argument, 0, 'y'},
Jooyung Hana3d5d092019-09-26 23:23:50 +090039#endif
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090040 {"help", no_argument, 0, 'h'},
41 {0, 0, 0, 0}};
42
43struct ProgramArgs {
44 std::string target_file;
Jooyung Hana3d5d092019-09-26 23:23:50 +090045 std::string root;
46 std::string vndk_version;
Kiyoung Kim722dad62019-12-19 15:59:55 +090047 bool is_recovery;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090048};
49
50[[noreturn]] void PrintUsage(int status = EXIT_SUCCESS) {
Jooyung Hana3d5d092019-09-26 23:23:50 +090051 std::cerr << "Usage : linkerconfig [--target <target_file>]"
52#ifndef __ANDROID__
53 " --root <root dir>"
54 " --vndk <vndk version>"
Kiyoung Kim722dad62019-12-19 15:59:55 +090055 " --recovery"
Jooyung Hana3d5d092019-09-26 23:23:50 +090056#endif
Kiyoung Kim09cbb082019-12-05 16:44:34 +090057 " [--recovery]"
Jooyung Hana3d5d092019-09-26 23:23:50 +090058 " [--help]"
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090059 << std::endl;
60 exit(status);
61}
62
63bool ParseArgs(int argc, char* argv[], ProgramArgs* args) {
64 int parse_result;
Jooyung Hana3d5d092019-09-26 23:23:50 +090065 while ((parse_result = getopt_long(
Kiyoung Kim722dad62019-12-19 15:59:55 +090066 argc, argv, "t:r:v:hy", program_options, NULL)) != -1) {
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090067 switch (parse_result) {
68 case 't':
69 args->target_file = optarg;
70 break;
Jooyung Hana3d5d092019-09-26 23:23:50 +090071 case 'r':
72 args->root = optarg;
73 break;
74 case 'v':
75 args->vndk_version = optarg;
76 break;
Kiyoung Kim722dad62019-12-19 15:59:55 +090077 case 'y':
78 args->is_recovery = true;
79 break;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090080 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 Kim722dad62019-12-19 15:59:55 +090094android::linkerconfig::modules::Configuration GetConfiguration(ProgramArgs args) {
95 // Recovery mode does not require environmental variables.
96 if (args.is_recovery || android::linkerconfig::modules::IsRecoveryMode()) {
Kiyoung Kim09cbb082019-12-05 16:44:34 +090097 return android::linkerconfig::contents::CreateRecoveryConfiguration();
98 }
99
Kiyoung Kim722dad62019-12-19 15:59:55 +0900100#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 Kime6558072019-09-30 16:58:39 +0900110 if (android::linkerconfig::modules::IsLegacyDevice()) {
111 return android::linkerconfig::contents::CreateLegacyConfiguration();
112 }
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900113
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900114 // Use base configuration in default
115 return android::linkerconfig::contents::CreateBaseConfiguration();
116}
Kiyoung Kime3bad042019-07-23 13:38:34 +0900117
Jooyung Hana3d5d092019-09-26 23:23:50 +0900118#ifdef __ANDROID__
Kiyoung Kime3bad042019-07-23 13:38:34 +0900119struct 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 Hana3d5d092019-09-26 23:23:50 +0900129#endif
Kiyoung Kime3bad042019-07-23 13:38:34 +0900130
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900131} // namespace
132
133int main(int argc, char* argv[]) {
Jooyung Hana3d5d092019-09-26 23:23:50 +0900134 android::base::InitLogging(argv
135#ifdef __ANDROID__
136 ,
137 CombinedLogger()
138#endif
139 );
Kiyoung Kime3bad042019-07-23 13:38:34 +0900140
Kiyoung Kim722dad62019-12-19 15:59:55 +0900141 ProgramArgs args = {.is_recovery = false};
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900142
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 Kim853438d2019-07-16 09:51:14 +0900153 PLOG(FATAL) << "Failed to open file " << args.target_file;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900154 return EXIT_FAILURE;
155 }
156 out = &file_out;
157 }
158
Kiyoung Kim722dad62019-12-19 15:59:55 +0900159 auto config = GetConfiguration(args);
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900160 android::linkerconfig::modules::ConfigWriter config_writer;
161
162 config.WriteConfig(config_writer);
163 *out << config_writer.ToString();
164 if (!out->good()) {
Kiyoung Kim853438d2019-07-16 09:51:14 +0900165 PLOG(FATAL) << "Failed to write content to " << args.target_file;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900166 return EXIT_FAILURE;
167 }
168
169 return EXIT_SUCCESS;
Josh Gao76614bb2019-12-11 15:28:30 -0800170}