• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
20 #include <android-base/result.h>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <string>
24 
25 #include <getopt.h>
26 
27 #include "CppGen.h"
28 
29 using android::base::Result;
30 
31 namespace {
32 
33 struct Arguments {
34   std::string input_file_path;
35   std::string header_dir;
36   std::string public_header_dir;
37   std::string source_dir;
38   std::string include_name;
39 };
40 
PrintUsage(const char * exe_name)41 [[noreturn]] void PrintUsage(const char* exe_name) {
42   std::printf(
43       "Usage: %s --header-dir dir --source-dir dir "
44       "--include-name name --public-header-dir dir "
45       "sysprop_file\n",
46       exe_name);
47   std::exit(EXIT_FAILURE);
48 }
49 
ParseArgs(int argc,char * argv[])50 Result<Arguments> ParseArgs(int argc, char* argv[]) {
51   Arguments ret;
52   for (;;) {
53     static struct option long_options[] = {
54         {"header-dir", required_argument, 0, 'h'},
55         {"public-header-dir", required_argument, 0, 'p'},
56         {"source-dir", required_argument, 0, 'c'},
57         {"include-name", required_argument, 0, 'n'},
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':
65         ret.header_dir = optarg;
66         break;
67       case 'p':
68         ret.public_header_dir = optarg;
69         break;
70       case 'c':
71         ret.source_dir = optarg;
72         break;
73       case 'n':
74         ret.include_name = optarg;
75         break;
76       default:
77         PrintUsage(argv[0]);
78     }
79   }
80 
81   if (optind >= argc) {
82     return Errorf("No input file specified");
83   }
84 
85   if (optind + 1 < argc) {
86     return Errorf("More than one input file");
87   }
88 
89   if (ret.header_dir.empty() || ret.public_header_dir.empty() ||
90       ret.source_dir.empty() || ret.include_name.empty()) {
91     PrintUsage(argv[0]);
92   }
93 
94   ret.input_file_path = argv[optind];
95 
96   return ret;
97 }
98 
99 }  // namespace
100 
main(int argc,char * argv[])101 int main(int argc, char* argv[]) {
102   Arguments args;
103   if (auto res = ParseArgs(argc, argv); res.ok()) {
104     args = std::move(*res);
105   } else {
106     LOG(ERROR) << argv[0] << ": " << res.error();
107     PrintUsage(argv[0]);
108   }
109 
110   if (auto res = GenerateCppFiles(args.input_file_path, args.header_dir,
111                                   args.public_header_dir, args.source_dir,
112                                   args.include_name);
113       !res.ok()) {
114     LOG(FATAL) << "Error during generating cpp sysprop from "
115                << args.input_file_path << ": " << res.error();
116   }
117 }
118