blob: 723e2d38103484f797e77cf9de1584740b3e71d0 [file] [log] [blame]
Inseob Kim5f8f32c2018-08-24 11:10:44 +09001/*
2 * Copyright (C) 2018 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#define LOG_TAG "sysprop_java"
18
19#include <android-base/logging.h>
Inseob Kim053b83d2019-06-26 13:41:51 +090020#include <android-base/result.h>
Inseob Kim5f8f32c2018-08-24 11:10:44 +090021#include <cstdio>
22#include <cstdlib>
23#include <string>
24
25#include <getopt.h>
26
27#include "JavaGen.h"
Inseob Kim38569c72019-07-30 18:36:28 +090028#include "sysprop.pb.h"
Inseob Kim5f8f32c2018-08-24 11:10:44 +090029
Inseob Kim053b83d2019-06-26 13:41:51 +090030using android::base::Errorf;
31using android::base::Result;
32
Inseob Kim5f8f32c2018-08-24 11:10:44 +090033namespace {
34
35struct Arguments {
Inseob Kim0773b942018-10-04 19:29:27 +090036 std::string input_file_path;
37 std::string java_output_dir;
Inseob Kim38569c72019-07-30 18:36:28 +090038 sysprop::Scope scope;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090039};
40
41[[noreturn]] void PrintUsage(const char* exe_name) {
Inseob Kim38569c72019-07-30 18:36:28 +090042 std::printf(
43 "Usage: %s --scope (internal|public) --java-output-dir dir "
44 "sysprop_file\n",
45 exe_name);
Inseob Kim5f8f32c2018-08-24 11:10:44 +090046 std::exit(EXIT_FAILURE);
47}
48
Inseob Kim053b83d2019-06-26 13:41:51 +090049Result<void> ParseArgs(int argc, char* argv[], Arguments* args) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +090050 for (;;) {
51 static struct option long_options[] = {
52 {"java-output-dir", required_argument, 0, 'j'},
Inseob Kim38569c72019-07-30 18:36:28 +090053 {"scope", required_argument, 0, 's'},
Inseob Kim5f8f32c2018-08-24 11:10:44 +090054 };
55
56 int opt = getopt_long_only(argc, argv, "", long_options, nullptr);
57 if (opt == -1) break;
58
59 switch (opt) {
60 case 'j':
Inseob Kim0773b942018-10-04 19:29:27 +090061 args->java_output_dir = optarg;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090062 break;
Inseob Kim38569c72019-07-30 18:36:28 +090063 case 's':
64 if (strcmp(optarg, "public") == 0) {
65 args->scope = sysprop::Scope::Public;
66 } else if (strcmp(optarg, "internal") == 0) {
67 args->scope = sysprop::Scope::Internal;
68 } else {
69 return Errorf("Invalid option {} for scope", optarg);
70 }
71 break;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090072 default:
73 PrintUsage(argv[0]);
74 }
75 }
76
77 if (optind >= argc) {
Inseob Kim053b83d2019-06-26 13:41:51 +090078 return Errorf("No input file specified");
Inseob Kim5f8f32c2018-08-24 11:10:44 +090079 }
80
81 if (optind + 1 < argc) {
Inseob Kim053b83d2019-06-26 13:41:51 +090082 return Errorf("More than one input file");
Inseob Kim5f8f32c2018-08-24 11:10:44 +090083 }
84
Inseob Kim0773b942018-10-04 19:29:27 +090085 args->input_file_path = argv[optind];
86 if (args->java_output_dir.empty()) args->java_output_dir = ".";
Inseob Kim5f8f32c2018-08-24 11:10:44 +090087
Inseob Kim053b83d2019-06-26 13:41:51 +090088 return {};
Inseob Kim5f8f32c2018-08-24 11:10:44 +090089}
90
91} // namespace
92
93int main(int argc, char* argv[]) {
94 Arguments args;
95 std::string err;
Inseob Kim053b83d2019-06-26 13:41:51 +090096 if (auto res = ParseArgs(argc, argv, &args); !res) {
97 LOG(ERROR) << res.error();
Inseob Kim5f8f32c2018-08-24 11:10:44 +090098 PrintUsage(argv[0]);
99 }
100
Inseob Kim38569c72019-07-30 18:36:28 +0900101 if (auto res = GenerateJavaLibrary(args.input_file_path, args.scope,
102 args.java_output_dir);
Inseob Kim053b83d2019-06-26 13:41:51 +0900103 !res) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900104 LOG(FATAL) << "Error during generating java sysprop from "
Inseob Kim053b83d2019-06-26 13:41:51 +0900105 << args.input_file_path << ": " << res.error();
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900106 }
107}