blob: dc8058cfa66b72a47aa64313f74c49822bdc6600 [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::Result;
31
Inseob Kim5f8f32c2018-08-24 11:10:44 +090032namespace {
33
34struct Arguments {
Inseob Kim0773b942018-10-04 19:29:27 +090035 std::string input_file_path;
36 std::string java_output_dir;
Inseob Kim38569c72019-07-30 18:36:28 +090037 sysprop::Scope scope;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090038};
39
40[[noreturn]] void PrintUsage(const char* exe_name) {
Inseob Kim38569c72019-07-30 18:36:28 +090041 std::printf(
42 "Usage: %s --scope (internal|public) --java-output-dir dir "
43 "sysprop_file\n",
44 exe_name);
Inseob Kim5f8f32c2018-08-24 11:10:44 +090045 std::exit(EXIT_FAILURE);
46}
47
Inseob Kim053b83d2019-06-26 13:41:51 +090048Result<void> ParseArgs(int argc, char* argv[], Arguments* args) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +090049 for (;;) {
50 static struct option long_options[] = {
51 {"java-output-dir", required_argument, 0, 'j'},
Inseob Kim38569c72019-07-30 18:36:28 +090052 {"scope", required_argument, 0, 's'},
Inseob Kim5f8f32c2018-08-24 11:10:44 +090053 };
54
55 int opt = getopt_long_only(argc, argv, "", long_options, nullptr);
56 if (opt == -1) break;
57
58 switch (opt) {
59 case 'j':
Inseob Kim0773b942018-10-04 19:29:27 +090060 args->java_output_dir = optarg;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090061 break;
Inseob Kim38569c72019-07-30 18:36:28 +090062 case 's':
63 if (strcmp(optarg, "public") == 0) {
64 args->scope = sysprop::Scope::Public;
65 } else if (strcmp(optarg, "internal") == 0) {
66 args->scope = sysprop::Scope::Internal;
67 } else {
68 return Errorf("Invalid option {} for scope", optarg);
69 }
70 break;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090071 default:
72 PrintUsage(argv[0]);
73 }
74 }
75
76 if (optind >= argc) {
Inseob Kim053b83d2019-06-26 13:41:51 +090077 return Errorf("No input file specified");
Inseob Kim5f8f32c2018-08-24 11:10:44 +090078 }
79
80 if (optind + 1 < argc) {
Inseob Kim053b83d2019-06-26 13:41:51 +090081 return Errorf("More than one input file");
Inseob Kim5f8f32c2018-08-24 11:10:44 +090082 }
83
Inseob Kim0773b942018-10-04 19:29:27 +090084 args->input_file_path = argv[optind];
85 if (args->java_output_dir.empty()) args->java_output_dir = ".";
Inseob Kim5f8f32c2018-08-24 11:10:44 +090086
Inseob Kim053b83d2019-06-26 13:41:51 +090087 return {};
Inseob Kim5f8f32c2018-08-24 11:10:44 +090088}
89
90} // namespace
91
92int main(int argc, char* argv[]) {
93 Arguments args;
94 std::string err;
Bernie Innocenti7ac8da42020-02-06 23:30:12 +090095 if (auto res = ParseArgs(argc, argv, &args); !res.ok()) {
Inseob Kim053b83d2019-06-26 13:41:51 +090096 LOG(ERROR) << res.error();
Inseob Kim5f8f32c2018-08-24 11:10:44 +090097 PrintUsage(argv[0]);
98 }
99
Inseob Kim38569c72019-07-30 18:36:28 +0900100 if (auto res = GenerateJavaLibrary(args.input_file_path, args.scope,
101 args.java_output_dir);
Bernie Innocenti7ac8da42020-02-06 23:30:12 +0900102 !res.ok()) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900103 LOG(FATAL) << "Error during generating java sysprop from "
Inseob Kim053b83d2019-06-26 13:41:51 +0900104 << args.input_file_path << ": " << res.error();
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900105 }
106}