• 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 <cstdio>
21 #include <cstdlib>
22 #include <string>
23 
24 #include <getopt.h>
25 
26 #include "CppGen.h"
27 
28 namespace {
29 
30 struct Arguments {
31   std::string input_file_path;
32   std::string header_dir;
33   std::string system_header_dir;
34   std::string source_dir;
35   std::string include_name;
36 };
37 
PrintUsage(const char * exe_name)38 [[noreturn]] void PrintUsage(const char* exe_name) {
39   std::printf(
40       "Usage: %s --header-dir dir --source-dir dir "
41       "--include-name name --system-header-dir dir "
42       "sysprop_file\n",
43       exe_name);
44   std::exit(EXIT_FAILURE);
45 }
46 
ParseArgs(int argc,char * argv[],Arguments * args,std::string * err)47 bool ParseArgs(int argc, char* argv[], Arguments* args, std::string* err) {
48   for (;;) {
49     static struct option long_options[] = {
50         {"header-dir", required_argument, 0, 'h'},
51         {"system-header-dir", required_argument, 0, 's'},
52         {"source-dir", required_argument, 0, 'c'},
53         {"include-name", required_argument, 0, 'n'},
54     };
55 
56     int opt = getopt_long_only(argc, argv, "", long_options, nullptr);
57     if (opt == -1) break;
58 
59     switch (opt) {
60       case 'h':
61         args->header_dir = optarg;
62         break;
63       case 's':
64         args->system_header_dir = optarg;
65         break;
66       case 'c':
67         args->source_dir = optarg;
68         break;
69       case 'n':
70         args->include_name = optarg;
71         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 
87   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 
92   args->input_file_path = argv[optind];
93 
94   return true;
95 }
96 
97 }  // namespace
98 
main(int argc,char * argv[])99 int 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 
107   if (!GenerateCppFiles(args.input_file_path, args.header_dir,
108                         args.system_header_dir, args.source_dir,
109                         args.include_name, &err)) {
110     LOG(FATAL) << "Error during generating cpp sysprop from "
111                << args.input_file_path << ": " << err;
112   }
113 }
114