blob: f61dfd0f21ce51db11ee3b2526adb0db5f8e93f4 [file] [log] [blame]
Inseob Kim5d3e6882019-07-25 13:55:48 +09001/*
2 * Copyright (C) 2019 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_api_dump_main"
18
19#include <android-base/file.h>
20#include <android-base/logging.h>
21#include <google/protobuf/text_format.h>
22
23#include <algorithm>
24#include <cstdio>
25#include <cstdlib>
26#include <map>
27#include <string>
28
29#include "Common.h"
30
31namespace {
32
33[[noreturn]] void PrintUsage(const char* exe_name) {
34 std::printf("Usage: %s output_file sysprop_files...\n", exe_name);
35 std::exit(EXIT_FAILURE);
36}
37
38} // namespace
39
40int main(int argc, char* argv[]) {
41 if (argc < 3) {
42 std::fprintf(stderr, "%s needs at least 2 arguments\n", argv[0]);
43 PrintUsage(argv[0]);
44 }
45
46 sysprop::SyspropLibraryApis api;
47 std::map<std::string, sysprop::Properties> modules;
48
49 for (int i = 2; i < argc; ++i) {
Bernie Innocenti7ac8da42020-02-06 23:30:12 +090050 if (auto res = ParseProps(argv[i]); res.ok()) {
Inseob Kim5d3e6882019-07-25 13:55:48 +090051 if (!modules.emplace(res->module(), *res).second) {
52 LOG(FATAL) << "duplicated module name " << res->module();
53 }
54 } else {
55 LOG(FATAL) << "parsing sysprop file " << argv[i]
56 << " failed: " << res.error();
57 }
58 }
59
60 for (auto& [name, props] : modules) {
Inseob Kim656ac0f2020-10-14 16:14:48 +090061 auto& prop_list = *props.mutable_prop();
62
63 // remove internals
64 auto is_internal = [](auto& prop) {
65 return prop.scope() == sysprop::Internal;
66 };
67 prop_list.erase(
68 std::remove_if(prop_list.begin(), prop_list.end(), is_internal),
69 prop_list.end());
70
71 if (prop_list.empty()) continue;
72
73 // ... and then sort to normalize
74 std::sort(prop_list.begin(), prop_list.end(),
Inseob Kim5d3e6882019-07-25 13:55:48 +090075 [](auto& a, auto& b) { return a.api_name() < b.api_name(); });
76 *api.add_props() = std::move(props);
77 }
78
79 std::string res;
80 if (!google::protobuf::TextFormat::PrintToString(api, &res)) {
81 LOG(FATAL) << "dumping API failed";
82 }
83
84 if (!android::base::WriteStringToFile(res, argv[1])) {
85 PLOG(FATAL) << "writing API file to " << argv[1] << " failed";
86 }
87}