/* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "sysprop_api_dump_main" #include #include #include #include #include #include #include #include #include "Common.h" namespace { [[noreturn]] void PrintUsage(const char* exe_name) { std::printf("Usage: %s output_file sysprop_files...\n", exe_name); std::exit(EXIT_FAILURE); } } // namespace int main(int argc, char* argv[]) { if (argc < 3) { std::fprintf(stderr, "%s needs at least 2 arguments\n", argv[0]); PrintUsage(argv[0]); } sysprop::SyspropLibraryApis api; std::map modules; for (int i = 2; i < argc; ++i) { if (auto res = ParseProps(argv[i]); res.ok()) { if (!modules.emplace(res->module(), *res).second) { LOG(FATAL) << "duplicated module name " << res->module(); } } else { LOG(FATAL) << "parsing sysprop file " << argv[i] << " failed: " << res.error(); } } for (auto& [name, props] : modules) { auto& prop_list = *props.mutable_prop(); // remove internals auto is_internal = [](auto& prop) { return prop.scope() == sysprop::Internal; }; prop_list.erase( std::remove_if(prop_list.begin(), prop_list.end(), is_internal), prop_list.end()); if (prop_list.empty()) continue; // ... and then sort to normalize std::sort(prop_list.begin(), prop_list.end(), [](auto& a, auto& b) { return a.api_name() < b.api_name(); }); *api.add_props() = std::move(props); } std::string res; if (!google::protobuf::TextFormat::PrintToString(api, &res)) { LOG(FATAL) << "dumping API failed"; } if (!android::base::WriteStringToFile(res, argv[1])) { PLOG(FATAL) << "writing API file to " << argv[1] << " failed"; } }