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> |
| 20 | #include <cstdio> |
| 21 | #include <cstdlib> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <getopt.h> |
| 25 | |
| 26 | #include "JavaGen.h" |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | struct Arguments { |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 31 | std::string input_file_path; |
| 32 | std::string java_output_dir; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | [[noreturn]] void PrintUsage(const char* exe_name) { |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 36 | std::printf("Usage: %s [--java-output-dir dir] sysprop_file\n", exe_name); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 37 | std::exit(EXIT_FAILURE); |
| 38 | } |
| 39 | |
| 40 | bool ParseArgs(int argc, char* argv[], Arguments* args, std::string* err) { |
| 41 | for (;;) { |
| 42 | static struct option long_options[] = { |
| 43 | {"java-output-dir", required_argument, 0, 'j'}, |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | int opt = getopt_long_only(argc, argv, "", long_options, nullptr); |
| 47 | if (opt == -1) break; |
| 48 | |
| 49 | switch (opt) { |
| 50 | case 'j': |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 51 | args->java_output_dir = optarg; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 52 | break; |
| 53 | default: |
| 54 | PrintUsage(argv[0]); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (optind >= argc) { |
| 59 | *err = "No input file specified"; |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | if (optind + 1 < argc) { |
| 64 | *err = "More than one input file"; |
| 65 | return false; |
| 66 | } |
| 67 | |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 68 | args->input_file_path = argv[optind]; |
| 69 | if (args->java_output_dir.empty()) args->java_output_dir = "."; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | } // namespace |
| 75 | |
| 76 | int main(int argc, char* argv[]) { |
| 77 | Arguments args; |
| 78 | std::string err; |
| 79 | if (!ParseArgs(argc, argv, &args, &err)) { |
| 80 | std::fprintf(stderr, "%s: %s\n", argv[0], err.c_str()); |
| 81 | PrintUsage(argv[0]); |
| 82 | } |
| 83 | |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 84 | if (!GenerateJavaLibrary(args.input_file_path, args.java_output_dir, &err)) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 85 | LOG(FATAL) << "Error during generating java sysprop from " |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 86 | << args.input_file_path << ": " << err; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 87 | } |
| 88 | } |