blob: 6f280fc1873d244878391fe5a8371098f43698ff [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::Result;
30
Inseob Kim5f8f32c2018-08-24 11:10:44 +090031namespace {
32
33struct Arguments {
Inseob Kimdbc2c802018-09-13 14:16:48 +090034 std::string input_file_path;
Inseob Kimcb71b082019-02-08 21:04:49 +090035 std::string header_dir;
Inseob Kim1ca03f32019-06-08 16:31:59 +090036 std::string public_header_dir;
Inseob Kimcb71b082019-02-08 21:04:49 +090037 std::string source_dir;
Inseob Kimdbc2c802018-09-13 14:16:48 +090038 std::string include_name;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090039};
40
41[[noreturn]] void PrintUsage(const char* exe_name) {
42 std::printf(
Inseob Kimcb71b082019-02-08 21:04:49 +090043 "Usage: %s --header-dir dir --source-dir dir "
Inseob Kim1ca03f32019-06-08 16:31:59 +090044 "--include-name name --public-header-dir dir "
Inseob Kimcb71b082019-02-08 21:04:49 +090045 "sysprop_file\n",
Inseob Kim5f8f32c2018-08-24 11:10:44 +090046 exe_name);
47 std::exit(EXIT_FAILURE);
48}
49
Inseob Kim053b83d2019-06-26 13:41:51 +090050Result<Arguments> ParseArgs(int argc, char* argv[]) {
51 Arguments ret;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090052 for (;;) {
53 static struct option long_options[] = {
Inseob Kimcb71b082019-02-08 21:04:49 +090054 {"header-dir", required_argument, 0, 'h'},
Inseob Kim1ca03f32019-06-08 16:31:59 +090055 {"public-header-dir", required_argument, 0, 'p'},
Inseob Kimcb71b082019-02-08 21:04:49 +090056 {"source-dir", required_argument, 0, 'c'},
Inseob Kimdbc2c802018-09-13 14:16:48 +090057 {"include-name", required_argument, 0, 'n'},
Inseob Kim5f8f32c2018-08-24 11:10:44 +090058 };
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 Kim053b83d2019-06-26 13:41:51 +090065 ret.header_dir = optarg;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090066 break;
Inseob Kim1ca03f32019-06-08 16:31:59 +090067 case 'p':
Inseob Kim053b83d2019-06-26 13:41:51 +090068 ret.public_header_dir = optarg;
Inseob Kimcb71b082019-02-08 21:04:49 +090069 break;
70 case 'c':
Inseob Kim053b83d2019-06-26 13:41:51 +090071 ret.source_dir = optarg;
Inseob Kimdbc2c802018-09-13 14:16:48 +090072 break;
73 case 'n':
Inseob Kim053b83d2019-06-26 13:41:51 +090074 ret.include_name = optarg;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090075 break;
76 default:
77 PrintUsage(argv[0]);
78 }
79 }
80
81 if (optind >= argc) {
Inseob Kim053b83d2019-06-26 13:41:51 +090082 return Errorf("No input file specified");
Inseob Kim5f8f32c2018-08-24 11:10:44 +090083 }
84
85 if (optind + 1 < argc) {
Inseob Kim053b83d2019-06-26 13:41:51 +090086 return Errorf("More than one input file");
Inseob Kim5f8f32c2018-08-24 11:10:44 +090087 }
88
Inseob Kim053b83d2019-06-26 13:41:51 +090089 if (ret.header_dir.empty() || ret.public_header_dir.empty() ||
90 ret.source_dir.empty() || ret.include_name.empty()) {
Inseob Kimcb71b082019-02-08 21:04:49 +090091 PrintUsage(argv[0]);
92 }
93
Inseob Kim053b83d2019-06-26 13:41:51 +090094 ret.input_file_path = argv[optind];
Inseob Kim5f8f32c2018-08-24 11:10:44 +090095
Inseob Kim053b83d2019-06-26 13:41:51 +090096 return ret;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090097}
98
99} // namespace
100
101int main(int argc, char* argv[]) {
102 Arguments args;
Bernie Innocenti7ac8da42020-02-06 23:30:12 +0900103 if (auto res = ParseArgs(argc, argv); res.ok()) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900104 args = std::move(*res);
105 } else {
106 LOG(ERROR) << argv[0] << ": " << res.error();
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900107 PrintUsage(argv[0]);
108 }
109
Inseob Kim053b83d2019-06-26 13:41:51 +0900110 if (auto res = GenerateCppFiles(args.input_file_path, args.header_dir,
111 args.public_header_dir, args.source_dir,
112 args.include_name);
Bernie Innocenti7ac8da42020-02-06 23:30:12 +0900113 !res.ok()) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900114 LOG(FATAL) << "Error during generating cpp sysprop from "
Inseob Kim053b83d2019-06-26 13:41:51 +0900115 << args.input_file_path << ": " << res.error();
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900116 }
117}