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