blob: 4e5574edb14cae26573d7c543e9496813253927b [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>
20#include <cstdio>
21#include <cstdlib>
22#include <string>
23
24#include <getopt.h>
25
26#include "CppGen.h"
27
28namespace {
29
30struct Arguments {
Inseob Kimdbc2c802018-09-13 14:16:48 +090031 std::string input_file_path;
Inseob Kimcb71b082019-02-08 21:04:49 +090032 std::string header_dir;
33 std::string system_header_dir;
34 std::string source_dir;
Inseob Kimdbc2c802018-09-13 14:16:48 +090035 std::string include_name;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090036};
37
38[[noreturn]] void PrintUsage(const char* exe_name) {
39 std::printf(
Inseob Kimcb71b082019-02-08 21:04:49 +090040 "Usage: %s --header-dir dir --source-dir dir "
41 "--include-name name --system-header-dir dir "
42 "sysprop_file\n",
Inseob Kim5f8f32c2018-08-24 11:10:44 +090043 exe_name);
44 std::exit(EXIT_FAILURE);
45}
46
47bool ParseArgs(int argc, char* argv[], Arguments* args, std::string* err) {
48 for (;;) {
49 static struct option long_options[] = {
Inseob Kimcb71b082019-02-08 21:04:49 +090050 {"header-dir", required_argument, 0, 'h'},
51 {"system-header-dir", required_argument, 0, 's'},
52 {"source-dir", required_argument, 0, 'c'},
Inseob Kimdbc2c802018-09-13 14:16:48 +090053 {"include-name", required_argument, 0, 'n'},
Inseob Kim5f8f32c2018-08-24 11:10:44 +090054 };
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 Kimcb71b082019-02-08 21:04:49 +090061 args->header_dir = optarg;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090062 break;
63 case 's':
Inseob Kimcb71b082019-02-08 21:04:49 +090064 args->system_header_dir = optarg;
65 break;
66 case 'c':
67 args->source_dir = optarg;
Inseob Kimdbc2c802018-09-13 14:16:48 +090068 break;
69 case 'n':
70 args->include_name = optarg;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090071 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 Kimcb71b082019-02-08 21:04:49 +090087 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 Kimdbc2c802018-09-13 14:16:48 +090092 args->input_file_path = argv[optind];
Inseob Kim5f8f32c2018-08-24 11:10:44 +090093
94 return true;
95}
96
97} // namespace
98
99int 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 Kimcb71b082019-02-08 21:04:49 +0900107 if (!GenerateCppFiles(args.input_file_path, args.header_dir,
108 args.system_header_dir, args.source_dir,
109 args.include_name, &err)) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900110 LOG(FATAL) << "Error during generating cpp sysprop from "
Inseob Kimdbc2c802018-09-13 14:16:48 +0900111 << args.input_file_path << ": " << err;
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900112 }
113}