blob: fe3ae80fe455525128b1b6586220ef227a5175e3 [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_java"
18
19#include <android-base/logging.h>
20#include <cstdio>
21#include <cstdlib>
22#include <string>
23
24#include <getopt.h>
25
26#include "JavaGen.h"
27
28namespace {
29
30struct Arguments {
Inseob Kim0773b942018-10-04 19:29:27 +090031 std::string input_file_path;
32 std::string java_output_dir;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090033};
34
35[[noreturn]] void PrintUsage(const char* exe_name) {
Inseob Kim0773b942018-10-04 19:29:27 +090036 std::printf("Usage: %s [--java-output-dir dir] sysprop_file\n", exe_name);
Inseob Kim5f8f32c2018-08-24 11:10:44 +090037 std::exit(EXIT_FAILURE);
38}
39
40bool ParseArgs(int argc, char* argv[], Arguments* args, std::string* err) {
41 for (;;) {
42 static struct option long_options[] = {
43 {"java-output-dir", required_argument, 0, 'j'},
Inseob Kim5f8f32c2018-08-24 11:10:44 +090044 };
45
46 int opt = getopt_long_only(argc, argv, "", long_options, nullptr);
47 if (opt == -1) break;
48
49 switch (opt) {
50 case 'j':
Inseob Kim0773b942018-10-04 19:29:27 +090051 args->java_output_dir = optarg;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090052 break;
53 default:
54 PrintUsage(argv[0]);
55 }
56 }
57
58 if (optind >= argc) {
59 *err = "No input file specified";
60 return false;
61 }
62
63 if (optind + 1 < argc) {
64 *err = "More than one input file";
65 return false;
66 }
67
Inseob Kim0773b942018-10-04 19:29:27 +090068 args->input_file_path = argv[optind];
69 if (args->java_output_dir.empty()) args->java_output_dir = ".";
Inseob Kim5f8f32c2018-08-24 11:10:44 +090070
71 return true;
72}
73
74} // namespace
75
76int main(int argc, char* argv[]) {
77 Arguments args;
78 std::string err;
79 if (!ParseArgs(argc, argv, &args, &err)) {
80 std::fprintf(stderr, "%s: %s\n", argv[0], err.c_str());
81 PrintUsage(argv[0]);
82 }
83
Inseob Kim0773b942018-10-04 19:29:27 +090084 if (!GenerateJavaLibrary(args.input_file_path, args.java_output_dir, &err)) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +090085 LOG(FATAL) << "Error during generating java sysprop from "
Inseob Kim0773b942018-10-04 19:29:27 +090086 << args.input_file_path << ": " << err;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090087 }
88}