1 //
2 // Copyright (C) 2023 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 #include <gflags/gflags.h>
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "catalog.h"
25 #include "codegen_java.h"
26 
27 constexpr char DEFAULT_CONFIG_DIR[] = "frameworks/proto_logging/stats/express/catalog";
28 
29 DEFINE_string(configDir, DEFAULT_CONFIG_DIR, "path to cfg files");
30 DEFINE_string(java, "", "path to the generated Java class file");
31 DEFINE_string(javaPackage, "", "generated Java package name");
32 DEFINE_string(javaClass, "", "generated Java class name");
33 
34 namespace android {
35 namespace express {
36 
createCodeGenerators()37 std::vector<std::unique_ptr<CodeGenerator>> createCodeGenerators() {
38     std::vector<std::unique_ptr<CodeGenerator>> result;
39 
40     if (FLAGS_java.size()) {
41         result.push_back(std::make_unique<CodeGeneratorJava>(
42                 CodeGeneratorJava(FLAGS_java, FLAGS_javaPackage, FLAGS_javaClass)));
43     }
44 
45     return result;
46 }
47 
generateLoggingCode(const MetricInfoMap & metricsIds)48 bool generateLoggingCode(const MetricInfoMap& metricsIds) {
49     const auto codeGenerators = createCodeGenerators();
50     for (const auto& codeGen : codeGenerators) {
51         if (!codeGen->generateCode(metricsIds)) return false;
52     }
53     return true;
54 }
55 
56 /**
57  * Do the argument parsing and execute the tasks.
58  */
run()59 static int run() {
60     std::map<std::string, ExpressMetric> metrics;
61     if (!readCatalog(FLAGS_configDir.c_str(), metrics)) {
62         return -1;
63     }
64 
65     MetricInfoMap metricsIds;
66     if (!generateMetricsIds(metrics, metricsIds)) {
67         return -1;
68     }
69 
70     if (!generateLoggingCode(metricsIds)) {
71         return -1;
72     }
73 
74     return 0;
75 }
76 
77 }  // namespace express
78 }  // namespace android
79 
main(int argc,char ** argv)80 int main(int argc, char** argv) {
81     GOOGLE_PROTOBUF_VERIFY_VERSION;
82     google::ParseCommandLineFlags(&argc, &argv, false);
83     return android::express::run();
84 }
85