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> |
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 "CppGen.h" |
| 28 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 29 | using android::base::Result; |
| 30 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | struct Arguments { |
Inseob Kim | dbc2c80 | 2018-09-13 14:16:48 +0900 | [diff] [blame] | 34 | std::string input_file_path; |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 35 | std::string header_dir; |
Inseob Kim | 1ca03f3 | 2019-06-08 16:31:59 +0900 | [diff] [blame] | 36 | std::string public_header_dir; |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 37 | std::string source_dir; |
Inseob Kim | dbc2c80 | 2018-09-13 14:16:48 +0900 | [diff] [blame] | 38 | std::string include_name; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | [[noreturn]] void PrintUsage(const char* exe_name) { |
| 42 | std::printf( |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 43 | "Usage: %s --header-dir dir --source-dir dir " |
Inseob Kim | 1ca03f3 | 2019-06-08 16:31:59 +0900 | [diff] [blame] | 44 | "--include-name name --public-header-dir dir " |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 45 | "sysprop_file\n", |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 46 | exe_name); |
| 47 | std::exit(EXIT_FAILURE); |
| 48 | } |
| 49 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 50 | Result<Arguments> ParseArgs(int argc, char* argv[]) { |
| 51 | Arguments ret; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 52 | for (;;) { |
| 53 | static struct option long_options[] = { |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 54 | {"header-dir", required_argument, 0, 'h'}, |
Inseob Kim | 1ca03f3 | 2019-06-08 16:31:59 +0900 | [diff] [blame] | 55 | {"public-header-dir", required_argument, 0, 'p'}, |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 56 | {"source-dir", required_argument, 0, 'c'}, |
Inseob Kim | dbc2c80 | 2018-09-13 14:16:48 +0900 | [diff] [blame] | 57 | {"include-name", required_argument, 0, 'n'}, |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | int opt = getopt_long_only(argc, argv, "", long_options, nullptr); |
| 61 | if (opt == -1) break; |
| 62 | |
| 63 | switch (opt) { |
| 64 | case 'h': |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 65 | ret.header_dir = optarg; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 66 | break; |
Inseob Kim | 1ca03f3 | 2019-06-08 16:31:59 +0900 | [diff] [blame] | 67 | case 'p': |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 68 | ret.public_header_dir = optarg; |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 69 | break; |
| 70 | case 'c': |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 71 | ret.source_dir = optarg; |
Inseob Kim | dbc2c80 | 2018-09-13 14:16:48 +0900 | [diff] [blame] | 72 | break; |
| 73 | case 'n': |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 74 | ret.include_name = optarg; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 75 | break; |
| 76 | default: |
| 77 | PrintUsage(argv[0]); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (optind >= argc) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 82 | return Errorf("No input file specified"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (optind + 1 < argc) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 86 | return Errorf("More than one input file"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 87 | } |
| 88 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 89 | if (ret.header_dir.empty() || ret.public_header_dir.empty() || |
| 90 | ret.source_dir.empty() || ret.include_name.empty()) { |
Inseob Kim | cb71b08 | 2019-02-08 21:04:49 +0900 | [diff] [blame] | 91 | PrintUsage(argv[0]); |
| 92 | } |
| 93 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 94 | ret.input_file_path = argv[optind]; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 95 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 96 | return ret; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | } // namespace |
| 100 | |
| 101 | int main(int argc, char* argv[]) { |
| 102 | Arguments args; |
Bernie Innocenti | 7ac8da4 | 2020-02-06 23:30:12 +0900 | [diff] [blame] | 103 | if (auto res = ParseArgs(argc, argv); res.ok()) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 104 | args = std::move(*res); |
| 105 | } else { |
| 106 | LOG(ERROR) << argv[0] << ": " << res.error(); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 107 | PrintUsage(argv[0]); |
| 108 | } |
| 109 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 110 | if (auto res = GenerateCppFiles(args.input_file_path, args.header_dir, |
| 111 | args.public_header_dir, args.source_dir, |
| 112 | args.include_name); |
Bernie Innocenti | 7ac8da4 | 2020-02-06 23:30:12 +0900 | [diff] [blame] | 113 | !res.ok()) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 114 | LOG(FATAL) << "Error during generating cpp sysprop from " |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 115 | << args.input_file_path << ": " << res.error(); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 116 | } |
| 117 | } |