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