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.ok()) { 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 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(), 75 [](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 } 88