• 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_java"
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 "JavaGen.h"
28 #include "sysprop.pb.h"
29 
30 using android::base::Result;
31 
32 namespace {
33 
34 struct Arguments {
35   std::string input_file_path;
36   std::string java_output_dir;
37   sysprop::Scope scope;
38 };
39 
PrintUsage(const char * exe_name)40 [[noreturn]] void PrintUsage(const char* exe_name) {
41   std::printf(
42       "Usage: %s --scope (internal|public) --java-output-dir dir "
43       "sysprop_file\n",
44       exe_name);
45   std::exit(EXIT_FAILURE);
46 }
47 
ParseArgs(int argc,char * argv[],Arguments * args)48 Result<void> ParseArgs(int argc, char* argv[], Arguments* args) {
49   for (;;) {
50     static struct option long_options[] = {
51         {"java-output-dir", required_argument, 0, 'j'},
52         {"scope", required_argument, 0, 's'},
53     };
54 
55     int opt = getopt_long_only(argc, argv, "", long_options, nullptr);
56     if (opt == -1) break;
57 
58     switch (opt) {
59       case 'j':
60         args->java_output_dir = optarg;
61         break;
62       case 's':
63         if (strcmp(optarg, "public") == 0) {
64           args->scope = sysprop::Scope::Public;
65         } else if (strcmp(optarg, "internal") == 0) {
66           args->scope = sysprop::Scope::Internal;
67         } else {
68           return Errorf("Invalid option {} for scope", optarg);
69         }
70         break;
71       default:
72         PrintUsage(argv[0]);
73     }
74   }
75 
76   if (optind >= argc) {
77     return Errorf("No input file specified");
78   }
79 
80   if (optind + 1 < argc) {
81     return Errorf("More than one input file");
82   }
83 
84   args->input_file_path = argv[optind];
85   if (args->java_output_dir.empty()) args->java_output_dir = ".";
86 
87   return {};
88 }
89 
90 }  // namespace
91 
main(int argc,char * argv[])92 int main(int argc, char* argv[]) {
93   Arguments args;
94   std::string err;
95   if (auto res = ParseArgs(argc, argv, &args); !res.ok()) {
96     LOG(ERROR) << res.error();
97     PrintUsage(argv[0]);
98   }
99 
100   if (auto res = GenerateJavaLibrary(args.input_file_path, args.scope,
101                                      args.java_output_dir);
102       !res.ok()) {
103     LOG(FATAL) << "Error during generating java sysprop from "
104                << args.input_file_path << ": " << res.error();
105   }
106 }
107