Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 1 | /* |
| 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 Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 20 | #include <android-base/result.h> |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 21 | #include <cstdio> |
| 22 | #include <cstdlib> |
| 23 | #include <string> |
| 24 | |
| 25 | #include <getopt.h> |
| 26 | |
| 27 | #include "JavaGen.h" |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 28 | #include "sysprop.pb.h" |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 29 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 30 | using android::base::Result; |
| 31 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | struct Arguments { |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 35 | std::string input_file_path; |
| 36 | std::string java_output_dir; |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 37 | sysprop::Scope scope; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | [[noreturn]] void PrintUsage(const char* exe_name) { |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 41 | std::printf( |
| 42 | "Usage: %s --scope (internal|public) --java-output-dir dir " |
| 43 | "sysprop_file\n", |
| 44 | exe_name); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 45 | std::exit(EXIT_FAILURE); |
| 46 | } |
| 47 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 48 | Result<void> ParseArgs(int argc, char* argv[], Arguments* args) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 49 | for (;;) { |
| 50 | static struct option long_options[] = { |
| 51 | {"java-output-dir", required_argument, 0, 'j'}, |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 52 | {"scope", required_argument, 0, 's'}, |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 53 | }; |
| 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 Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 60 | args->java_output_dir = optarg; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 61 | break; |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 62 | 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 Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 71 | default: |
| 72 | PrintUsage(argv[0]); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (optind >= argc) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 77 | return Errorf("No input file specified"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (optind + 1 < argc) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 81 | return Errorf("More than one input file"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 82 | } |
| 83 | |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 84 | args->input_file_path = argv[optind]; |
| 85 | if (args->java_output_dir.empty()) args->java_output_dir = "."; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 86 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 87 | return {}; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | int main(int argc, char* argv[]) { |
| 93 | Arguments args; |
| 94 | std::string err; |
Bernie Innocenti | 7ac8da4 | 2020-02-06 23:30:12 +0900 | [diff] [blame] | 95 | if (auto res = ParseArgs(argc, argv, &args); !res.ok()) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 96 | LOG(ERROR) << res.error(); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 97 | PrintUsage(argv[0]); |
| 98 | } |
| 99 | |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 100 | if (auto res = GenerateJavaLibrary(args.input_file_path, args.scope, |
| 101 | args.java_output_dir); |
Bernie Innocenti | 7ac8da4 | 2020-02-06 23:30:12 +0900 | [diff] [blame] | 102 | !res.ok()) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 103 | LOG(FATAL) << "Error during generating java sysprop from " |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 104 | << args.input_file_path << ": " << res.error(); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 105 | } |
| 106 | } |