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