Inseob Kim | 5d3e688 | 2019-07-25 13:55:48 +0900 | [diff] [blame^] | 1 | /* |
| 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 | |
| 31 | namespace { |
| 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 | |
| 40 | int 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) { |
| 50 | if (auto res = ParseProps(argv[i]); res) { |
| 51 | 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) { |
| 61 | // Sort properties to normalize |
| 62 | std::sort(props.mutable_prop()->begin(), props.mutable_prop()->end(), |
| 63 | [](auto& a, auto& b) { return a.api_name() < b.api_name(); }); |
| 64 | *api.add_props() = std::move(props); |
| 65 | } |
| 66 | |
| 67 | std::string res; |
| 68 | if (!google::protobuf::TextFormat::PrintToString(api, &res)) { |
| 69 | LOG(FATAL) << "dumping API failed"; |
| 70 | } |
| 71 | |
| 72 | if (!android::base::WriteStringToFile(res, argv[1])) { |
| 73 | PLOG(FATAL) << "writing API file to " << argv[1] << " failed"; |
| 74 | } |
| 75 | } |