blob: a721485a6f3092821febfbc877c2515e6b490926 [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 Kime9a77fe2019-05-23 11:04:20 +090028#include "linkerconfig/variableloader.h"
Jooyung Hana3d5d092019-09-26 23:23:50 +090029#include "linkerconfig/variables.h"
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090030
31namespace {
32const static struct option program_options[] = {
33 {"target", required_argument, 0, 't'},
Jooyung Hana3d5d092019-09-26 23:23:50 +090034#ifndef __ANROID__
35 {"root", required_argument, 0, 'r'},
36 {"vndk", required_argument, 0, 'v'},
37#endif
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090038 {"help", no_argument, 0, 'h'},
39 {0, 0, 0, 0}};
40
41struct ProgramArgs {
42 std::string target_file;
Jooyung Hana3d5d092019-09-26 23:23:50 +090043 std::string root;
44 std::string vndk_version;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090045};
46
47[[noreturn]] void PrintUsage(int status = EXIT_SUCCESS) {
Jooyung Hana3d5d092019-09-26 23:23:50 +090048 std::cerr << "Usage : linkerconfig [--target <target_file>]"
49#ifndef __ANDROID__
50 " --root <root dir>"
51 " --vndk <vndk version>"
52#endif
53 " [--help]"
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090054 << std::endl;
55 exit(status);
56}
57
58bool ParseArgs(int argc, char* argv[], ProgramArgs* args) {
59 int parse_result;
Jooyung Hana3d5d092019-09-26 23:23:50 +090060 while ((parse_result = getopt_long(
61 argc, argv, "t:r:v:h", program_options, NULL)) != -1) {
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090062 switch (parse_result) {
63 case 't':
64 args->target_file = optarg;
65 break;
Jooyung Hana3d5d092019-09-26 23:23:50 +090066 case 'r':
67 args->root = optarg;
68 break;
69 case 'v':
70 args->vndk_version = optarg;
71 break;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090072 case 'h':
73 PrintUsage();
74 default:
75 return false;
76 }
77 }
78
79 if (optind < argc) {
80 return false;
81 }
82
83 return true;
84}
85
86android::linkerconfig::modules::Configuration GetConfiguration() {
Kiyoung Kime6558072019-09-30 16:58:39 +090087 if (android::linkerconfig::modules::IsLegacyDevice()) {
88 return android::linkerconfig::contents::CreateLegacyConfiguration();
89 }
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090090
91 // TODO : Use recovery if needed
92
93 // Use base configuration in default
94 return android::linkerconfig::contents::CreateBaseConfiguration();
95}
Kiyoung Kime3bad042019-07-23 13:38:34 +090096
Jooyung Hana3d5d092019-09-26 23:23:50 +090097#ifdef __ANDROID__
Kiyoung Kime3bad042019-07-23 13:38:34 +090098struct CombinedLogger {
99 android::base::LogdLogger logd;
100
101 void operator()(android::base::LogId id, android::base::LogSeverity severity,
102 const char* tag, const char* file, unsigned int line,
103 const char* message) {
104 logd(id, severity, tag, file, line, message);
105 KernelLogger(id, severity, tag, file, line, message);
106 }
107};
Jooyung Hana3d5d092019-09-26 23:23:50 +0900108#endif
Kiyoung Kime3bad042019-07-23 13:38:34 +0900109
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900110} // namespace
111
112int main(int argc, char* argv[]) {
Jooyung Hana3d5d092019-09-26 23:23:50 +0900113 android::base::InitLogging(argv
114#ifdef __ANDROID__
115 ,
116 CombinedLogger()
117#endif
118 );
Kiyoung Kime3bad042019-07-23 13:38:34 +0900119
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900120 ProgramArgs args;
121
122 if (!ParseArgs(argc, argv, &args)) {
123 PrintUsage(EXIT_FAILURE);
124 }
125
126 std::ostream* out = &std::cout;
127 std::ofstream file_out;
128
129 if (args.target_file != "") {
130 file_out.open(args.target_file);
131 if (file_out.fail()) {
Kiyoung Kim853438d2019-07-16 09:51:14 +0900132 PLOG(FATAL) << "Failed to open file " << args.target_file;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900133 return EXIT_FAILURE;
134 }
135 out = &file_out;
136 }
137
Jooyung Hana3d5d092019-09-26 23:23:50 +0900138#ifndef __ANDROID__
139 if (args.root == "" || args.vndk_version == "") {
140 PrintUsage();
141 }
142 android::linkerconfig::modules::Variables::AddValue("ro.vndk.version",
143 args.vndk_version);
144#endif
145
146 android::linkerconfig::generator::LoadVariables(args.root);
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900147 auto config = GetConfiguration();
148 android::linkerconfig::modules::ConfigWriter config_writer;
149
150 config.WriteConfig(config_writer);
151 *out << config_writer.ToString();
152 if (!out->good()) {
Kiyoung Kim853438d2019-07-16 09:51:14 +0900153 PLOG(FATAL) << "Failed to write content to " << args.target_file;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900154 return EXIT_FAILURE;
155 }
156
157 return EXIT_SUCCESS;
158}