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_cpp" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <cstdio> |
| 21 | #include <cstdlib> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <getopt.h> |
| 25 | |
| 26 | #include "CppGen.h" |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | struct Arguments { |
Inseob Kim | dbc2c80 | 2018-09-13 14:16:48 +0900 | [diff] [blame] | 31 | std::string input_file_path; |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 32 | std::string header_dir; |
| 33 | std::string system_header_dir; |
| 34 | std::string source_dir; |
Inseob Kim | dbc2c80 | 2018-09-13 14:16:48 +0900 | [diff] [blame] | 35 | std::string include_name; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | [[noreturn]] void PrintUsage(const char* exe_name) { |
| 39 | std::printf( |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 40 | "Usage: %s --header-dir dir --source-dir dir " |
| 41 | "--include-name name --system-header-dir dir " |
| 42 | "sysprop_file\n", |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 43 | exe_name); |
| 44 | std::exit(EXIT_FAILURE); |
| 45 | } |
| 46 | |
| 47 | bool ParseArgs(int argc, char* argv[], Arguments* args, std::string* err) { |
| 48 | for (;;) { |
| 49 | static struct option long_options[] = { |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 50 | {"header-dir", required_argument, 0, 'h'}, |
| 51 | {"system-header-dir", required_argument, 0, 's'}, |
| 52 | {"source-dir", required_argument, 0, 'c'}, |
Inseob Kim | dbc2c80 | 2018-09-13 14:16:48 +0900 | [diff] [blame] | 53 | {"include-name", required_argument, 0, 'n'}, |
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 'h': |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 61 | args->header_dir = optarg; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 62 | break; |
| 63 | case 's': |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 64 | args->system_header_dir = optarg; |
| 65 | break; |
| 66 | case 'c': |
| 67 | args->source_dir = optarg; |
Inseob Kim | dbc2c80 | 2018-09-13 14:16:48 +0900 | [diff] [blame] | 68 | break; |
| 69 | case 'n': |
| 70 | args->include_name = optarg; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 71 | break; |
| 72 | default: |
| 73 | PrintUsage(argv[0]); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (optind >= argc) { |
| 78 | *err = "No input file specified"; |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (optind + 1 < argc) { |
| 83 | *err = "More than one input file"; |
| 84 | return false; |
| 85 | } |
| 86 | |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 87 | if (args->header_dir.empty() || args->system_header_dir.empty() || |
| 88 | args->source_dir.empty() || args->include_name.empty()) { |
| 89 | PrintUsage(argv[0]); |
| 90 | } |
| 91 | |
Inseob Kim | dbc2c80 | 2018-09-13 14:16:48 +0900 | [diff] [blame] | 92 | args->input_file_path = argv[optind]; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | } // namespace |
| 98 | |
| 99 | int main(int argc, char* argv[]) { |
| 100 | Arguments args; |
| 101 | std::string err; |
| 102 | if (!ParseArgs(argc, argv, &args, &err)) { |
| 103 | std::fprintf(stderr, "%s: %s\n", argv[0], err.c_str()); |
| 104 | PrintUsage(argv[0]); |
| 105 | } |
| 106 | |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 107 | if (!GenerateCppFiles(args.input_file_path, args.header_dir, |
| 108 | args.system_header_dir, args.source_dir, |
| 109 | args.include_name, &err)) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 110 | LOG(FATAL) << "Error during generating cpp sysprop from " |
Inseob Kim | dbc2c80 | 2018-09-13 14:16:48 +0900 | [diff] [blame] | 111 | << args.input_file_path << ": " << err; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 112 | } |
| 113 | } |