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