blob: 536217595843911afcd0aac17199e7dae8c9cbca [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"
Kiyoung Kim853438d2019-07-16 09:51:14 +090024#include "linkerconfig/log.h"
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090025#include "linkerconfig/variableloader.h"
26
27namespace {
28const static struct option program_options[] = {
29 {"target", required_argument, 0, 't'},
30 {"help", no_argument, 0, 'h'},
31 {0, 0, 0, 0}};
32
33struct 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
43bool 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
65android::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 Kime3bad042019-07-23 13:38:34 +090075
76struct 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 Kime9a77fe2019-05-23 11:04:20 +090087} // namespace
88
89int main(int argc, char* argv[]) {
Kiyoung Kime3bad042019-07-23 13:38:34 +090090 android::base::InitLogging(argv, CombinedLogger());
91
Kiyoung Kime9a77fe2019-05-23 11:04:20 +090092 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 Kim853438d2019-07-16 09:51:14 +0900104 PLOG(FATAL) << "Failed to open file " << args.target_file;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900105 return EXIT_FAILURE;
106 }
107 out = &file_out;
108 }
109
Kiyoung Kim853438d2019-07-16 09:51:14 +0900110 android::linkerconfig::generator::LoadVariables();
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900111 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 Kim853438d2019-07-16 09:51:14 +0900117 PLOG(FATAL) << "Failed to write content to " << args.target_file;
Kiyoung Kime9a77fe2019-05-23 11:04:20 +0900118 return EXIT_FAILURE;
119 }
120
121 return EXIT_SUCCESS;
122}