blob: 5f066698460967898ee79b6ddd51fb21baf11526 [file] [log] [blame]
Inseob Kim5f8f32c2018-08-24 11:10:44 +09001/*
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 Kim053b83d2019-06-26 13:41:51 +090020#include <android-base/result.h>
Inseob Kim5f8f32c2018-08-24 11:10:44 +090021#include <cstdio>
22#include <cstdlib>
23#include <string>
24
25#include <getopt.h>
26
27#include "CppGen.h"
28
Inseob Kim053b83d2019-06-26 13:41:51 +090029using android::base::Errorf;
30using android::base::Result;
31
Inseob Kim5f8f32c2018-08-24 11:10:44 +090032namespace {
33
34struct Arguments {
Inseob Kimdbc2c802018-09-13 14:16:48 +090035 std::string input_file_path;
Inseob Kimcb71b082019-02-08 21:04:49 +090036 std::string header_dir;
Inseob Kim1ca03f32019-06-08 16:31:59 +090037 std::string public_header_dir;
Inseob Kimcb71b082019-02-08 21:04:49 +090038 std::string source_dir;
Inseob Kimdbc2c802018-09-13 14:16:48 +090039 std::string include_name;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090040};
41
42[[noreturn]] void PrintUsage(const char* exe_name) {
43 std::printf(
Inseob Kimcb71b082019-02-08 21:04:49 +090044 "Usage: %s --header-dir dir --source-dir dir "
Inseob Kim1ca03f32019-06-08 16:31:59 +090045 "--include-name name --public-header-dir dir "
Inseob Kimcb71b082019-02-08 21:04:49 +090046 "sysprop_file\n",
Inseob Kim5f8f32c2018-08-24 11:10:44 +090047 exe_name);
48 std::exit(EXIT_FAILURE);
49}
50
Inseob Kim053b83d2019-06-26 13:41:51 +090051Result<Arguments> ParseArgs(int argc, char* argv[]) {
52 Arguments ret;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090053 for (;;) {
54 static struct option long_options[] = {
Inseob Kimcb71b082019-02-08 21:04:49 +090055 {"header-dir", required_argument, 0, 'h'},
Inseob Kim1ca03f32019-06-08 16:31:59 +090056 {"public-header-dir", required_argument, 0, 'p'},
Inseob Kimcb71b082019-02-08 21:04:49 +090057 {"source-dir", required_argument, 0, 'c'},
Inseob Kimdbc2c802018-09-13 14:16:48 +090058 {"include-name", required_argument, 0, 'n'},
Inseob Kim5f8f32c2018-08-24 11:10:44 +090059 };
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 Kim053b83d2019-06-26 13:41:51 +090066 ret.header_dir = optarg;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090067 break;
Inseob Kim1ca03f32019-06-08 16:31:59 +090068 case 'p':
Inseob Kim053b83d2019-06-26 13:41:51 +090069 ret.public_header_dir = optarg;
Inseob Kimcb71b082019-02-08 21:04:49 +090070 break;
71 case 'c':
Inseob Kim053b83d2019-06-26 13:41:51 +090072 ret.source_dir = optarg;
Inseob Kimdbc2c802018-09-13 14:16:48 +090073 break;
74 case 'n':
Inseob Kim053b83d2019-06-26 13:41:51 +090075 ret.include_name = optarg;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090076 break;
77 default:
78 PrintUsage(argv[0]);
79 }
80 }
81
82 if (optind >= argc) {
Inseob Kim053b83d2019-06-26 13:41:51 +090083 return Errorf("No input file specified");
Inseob Kim5f8f32c2018-08-24 11:10:44 +090084 }
85
86 if (optind + 1 < argc) {
Inseob Kim053b83d2019-06-26 13:41:51 +090087 return Errorf("More than one input file");
Inseob Kim5f8f32c2018-08-24 11:10:44 +090088 }
89
Inseob Kim053b83d2019-06-26 13:41:51 +090090 if (ret.header_dir.empty() || ret.public_header_dir.empty() ||
91 ret.source_dir.empty() || ret.include_name.empty()) {
Inseob Kimcb71b082019-02-08 21:04:49 +090092 PrintUsage(argv[0]);
93 }
94
Inseob Kim053b83d2019-06-26 13:41:51 +090095 ret.input_file_path = argv[optind];
Inseob Kim5f8f32c2018-08-24 11:10:44 +090096
Inseob Kim053b83d2019-06-26 13:41:51 +090097 return ret;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090098}
99
100} // namespace
101
102int main(int argc, char* argv[]) {
103 Arguments args;
Inseob Kim053b83d2019-06-26 13:41:51 +0900104 if (auto res = ParseArgs(argc, argv); res) {
105 args = std::move(*res);
106 } else {
107 LOG(ERROR) << argv[0] << ": " << res.error();
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900108 PrintUsage(argv[0]);
109 }
110
Inseob Kim053b83d2019-06-26 13:41:51 +0900111 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 Kim5f8f32c2018-08-24 11:10:44 +0900115 LOG(FATAL) << "Error during generating cpp sysprop from "
Inseob Kim053b83d2019-06-26 13:41:51 +0900116 << args.input_file_path << ": " << res.error();
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900117 }
118}