blob: b888ce904b31c87a7b093fb5bc0f3dce13ecdf5a [file] [log] [blame]
Yao Chend54f9dd2017-10-17 17:37:48 +00001
Muhammad Qureshia345af92020-03-24 17:05:14 -07002#include <getopt.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
Yao Chend54f9dd2017-10-17 17:37:48 +00006
Muhammad Qureshief19b082020-01-03 18:38:01 -08007#include <map>
Yao Chend54f9dd2017-10-17 17:37:48 +00008#include <set>
9#include <vector>
10
Muhammad Qureshia345af92020-03-24 17:05:14 -070011#include "Collation.h"
Muhammad Qureshia345af92020-03-24 17:05:14 -070012#include "frameworks/base/cmds/statsd/src/atoms.pb.h"
13#include "java_writer.h"
14#include "java_writer_q.h"
15#include "native_writer.h"
16#include "utils.h"
Yao Chend54f9dd2017-10-17 17:37:48 +000017
18using namespace google::protobuf;
19using namespace std;
20
21namespace android {
22namespace stats_log_api_gen {
23
Stefan Lafonae2df012017-11-14 09:17:21 -080024using android::os::statsd::Atom;
Yao Chend54f9dd2017-10-17 17:37:48 +000025
Muhammad Qureshia345af92020-03-24 17:05:14 -070026static void print_usage() {
Yao Chend54f9dd2017-10-17 17:37:48 +000027 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
28 fprintf(stderr, "\n");
29 fprintf(stderr, "OPTIONS\n");
Muhammad Qureshic29064e2019-11-20 17:18:03 -080030 fprintf(stderr, " --cpp FILENAME the header file to output for write helpers\n");
31 fprintf(stderr, " --header FILENAME the cpp file to output for write helpers\n");
Yao Chend54f9dd2017-10-17 17:37:48 +000032 fprintf(stderr, " --help this message\n");
33 fprintf(stderr, " --java FILENAME the java file to output\n");
Tej Singh810eeb32019-03-07 19:08:52 -080034 fprintf(stderr, " --module NAME optional, module name to generate outputs for\n");
Muhammad Qureshia345af92020-03-24 17:05:14 -070035 fprintf(stderr,
36 " --namespace COMMA,SEP,NAMESPACE required for cpp/header with "
37 "module\n");
38 fprintf(stderr,
39 " comma separated namespace of "
40 "the files\n");
41 fprintf(stderr,
42 " --importHeader NAME required for cpp/jni to say which header to "
43 "import "
Muhammad Qureshic29064e2019-11-20 17:18:03 -080044 "for write helpers\n");
Tej Singh2910d5a2019-03-23 17:26:32 -070045 fprintf(stderr, " --javaPackage PACKAGE the package for the java file.\n");
46 fprintf(stderr, " required for java with module\n");
47 fprintf(stderr, " --javaClass CLASS the class name of the java class.\n");
48 fprintf(stderr, " Optional for Java with module.\n");
Muhammad Qureshi6f658812019-11-24 22:14:38 -080049 fprintf(stderr, " Default is \"StatsLogInternal\"\n");
Muhammad Qureshibb699b62020-01-29 11:09:43 -080050 fprintf(stderr, " --supportQ Include runtime support for Android Q.\n");
Muhammad Qureshia345af92020-03-24 17:05:14 -070051 fprintf(stderr,
52 " --worksource Include support for logging WorkSource "
53 "objects.\n");
54 fprintf(stderr,
55 " --compileQ Include compile-time support for Android Q "
Muhammad Qureshibb699b62020-01-29 11:09:43 -080056 "(Java only).\n");
Muhammad Qureshi6f658812019-11-24 22:14:38 -080057}
Yao Chend54f9dd2017-10-17 17:37:48 +000058
59/**
60 * Do the argument parsing and execute the tasks.
61 */
Muhammad Qureshia345af92020-03-24 17:05:14 -070062static int run(int argc, char const* const* argv) {
Yao Chend54f9dd2017-10-17 17:37:48 +000063 string cppFilename;
64 string headerFilename;
65 string javaFilename;
Muhammad Qureshi4ef3c3b2020-02-28 14:22:47 -080066 string javaPackage;
67 string javaClass;
Yao Chend54f9dd2017-10-17 17:37:48 +000068
Tej Singh810eeb32019-03-07 19:08:52 -080069 string moduleName = DEFAULT_MODULE_NAME;
70 string cppNamespace = DEFAULT_CPP_NAMESPACE;
71 string cppHeaderImport = DEFAULT_CPP_HEADER_IMPORT;
Muhammad Qureshi6f658812019-11-24 22:14:38 -080072 bool supportQ = false;
Muhammad Qureshib11e00d2020-01-10 14:03:13 -080073 bool supportWorkSource = false;
Muhammad Qureshibb699b62020-01-29 11:09:43 -080074 bool compileQ = false;
Tej Singh810eeb32019-03-07 19:08:52 -080075
Yao Chend54f9dd2017-10-17 17:37:48 +000076 int index = 1;
77 while (index < argc) {
78 if (0 == strcmp("--help", argv[index])) {
79 print_usage();
80 return 0;
81 } else if (0 == strcmp("--cpp", argv[index])) {
82 index++;
83 if (index >= argc) {
84 print_usage();
85 return 1;
86 }
87 cppFilename = argv[index];
88 } else if (0 == strcmp("--header", argv[index])) {
89 index++;
90 if (index >= argc) {
91 print_usage();
92 return 1;
93 }
94 headerFilename = argv[index];
95 } else if (0 == strcmp("--java", argv[index])) {
96 index++;
97 if (index >= argc) {
98 print_usage();
99 return 1;
100 }
101 javaFilename = argv[index];
Tej Singh810eeb32019-03-07 19:08:52 -0800102 } else if (0 == strcmp("--module", argv[index])) {
103 index++;
104 if (index >= argc) {
105 print_usage();
106 return 1;
107 }
108 moduleName = argv[index];
109 } else if (0 == strcmp("--namespace", argv[index])) {
110 index++;
111 if (index >= argc) {
112 print_usage();
113 return 1;
114 }
115 cppNamespace = argv[index];
116 } else if (0 == strcmp("--importHeader", argv[index])) {
117 index++;
118 if (index >= argc) {
119 print_usage();
120 return 1;
121 }
122 cppHeaderImport = argv[index];
Tej Singh2910d5a2019-03-23 17:26:32 -0700123 } else if (0 == strcmp("--javaPackage", argv[index])) {
124 index++;
125 if (index >= argc) {
126 print_usage();
127 return 1;
128 }
129 javaPackage = argv[index];
130 } else if (0 == strcmp("--javaClass", argv[index])) {
131 index++;
132 if (index >= argc) {
133 print_usage();
134 return 1;
135 }
136 javaClass = argv[index];
Muhammad Qureshi6f658812019-11-24 22:14:38 -0800137 } else if (0 == strcmp("--supportQ", argv[index])) {
138 supportQ = true;
Muhammad Qureshib11e00d2020-01-10 14:03:13 -0800139 } else if (0 == strcmp("--worksource", argv[index])) {
140 supportWorkSource = true;
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800141 } else if (0 == strcmp("--compileQ", argv[index])) {
142 compileQ = true;
Yao Chend54f9dd2017-10-17 17:37:48 +0000143 }
Muhammad Qureshic29064e2019-11-20 17:18:03 -0800144
Yao Chend54f9dd2017-10-17 17:37:48 +0000145 index++;
146 }
147
Muhammad Qureshi741d3182020-06-18 18:28:21 -0700148 if (cppFilename.size() == 0 && headerFilename.size() == 0 && javaFilename.size() == 0) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000149 print_usage();
150 return 1;
151 }
152
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800153 if (DEFAULT_MODULE_NAME == moduleName && (supportQ || compileQ)) {
Muhammad Qureshi6f658812019-11-24 22:14:38 -0800154 // Support for Q schema is not needed for default module.
155 fprintf(stderr, "%s cannot support Q schema\n", moduleName.c_str());
156 return 1;
157 }
158
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800159 if (supportQ && compileQ) {
160 // Runtime Q support is redundant if compile-time Q support is required.
161 fprintf(stderr, "Cannot specify compileQ and supportQ simultaneously.\n");
162 return 1;
163 }
164
Yao Chend54f9dd2017-10-17 17:37:48 +0000165 // Collate the parameters
166 Atoms atoms;
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700167 int errorCount = collate_atoms(Atom::descriptor(), moduleName, &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +0000168 if (errorCount != 0) {
169 return 1;
170 }
171
Yangster-mac7604aea2017-12-11 22:55:49 -0800172 AtomDecl attributionDecl;
173 vector<java_type_t> attributionSignature;
Muhammad Qureshia345af92020-03-24 17:05:14 -0700174 collate_atom(android::os::statsd::AttributionNode::descriptor(), &attributionDecl,
175 &attributionSignature);
Yangster-mac7604aea2017-12-11 22:55:49 -0800176
Yao Chend54f9dd2017-10-17 17:37:48 +0000177 // Write the .cpp file
178 if (cppFilename.size() != 0) {
179 FILE* out = fopen(cppFilename.c_str(), "w");
180 if (out == NULL) {
181 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
182 return 1;
183 }
Tej Singh810eeb32019-03-07 19:08:52 -0800184 // If this is for a specific module, the namespace must also be provided.
185 if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) {
186 fprintf(stderr, "Must supply --namespace if supplying a specific module\n");
187 return 1;
188 }
Muhammad Qureshia345af92020-03-24 17:05:14 -0700189 // If this is for a specific module, the header file to import must also be
190 // provided.
Tej Singh810eeb32019-03-07 19:08:52 -0800191 if (moduleName != DEFAULT_MODULE_NAME && cppHeaderImport == DEFAULT_CPP_HEADER_IMPORT) {
192 fprintf(stderr, "Must supply --headerImport if supplying a specific module\n");
193 return 1;
194 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800195 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
Muhammad Qureshia345af92020-03-24 17:05:14 -0700196 out, atoms, attributionDecl, cppNamespace, cppHeaderImport, supportQ);
Yao Chend54f9dd2017-10-17 17:37:48 +0000197 fclose(out);
198 }
199
200 // Write the .h file
201 if (headerFilename.size() != 0) {
202 FILE* out = fopen(headerFilename.c_str(), "w");
203 if (out == NULL) {
204 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
205 return 1;
206 }
Tej Singh810eeb32019-03-07 19:08:52 -0800207 // If this is for a specific module, the namespace must also be provided.
208 if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) {
209 fprintf(stderr, "Must supply --namespace if supplying a specific module\n");
210 }
Muhammad Qureshia345af92020-03-24 17:05:14 -0700211 errorCount = android::stats_log_api_gen::write_stats_log_header(out, atoms, attributionDecl,
212 cppNamespace);
Yao Chend54f9dd2017-10-17 17:37:48 +0000213 fclose(out);
214 }
215
216 // Write the .java file
217 if (javaFilename.size() != 0) {
Muhammad Qureshi4ef3c3b2020-02-28 14:22:47 -0800218 if (javaClass.size() == 0) {
219 fprintf(stderr, "Must supply --javaClass if supplying a Java filename");
220 return 1;
221 }
222
223 if (javaPackage.size() == 0) {
224 fprintf(stderr, "Must supply --javaPackage if supplying a Java filename");
225 return 1;
226 }
227
228 if (moduleName.size() == 0) {
229 fprintf(stderr, "Must supply --module if supplying a Java filename");
230 return 1;
231 }
232
Yao Chend54f9dd2017-10-17 17:37:48 +0000233 FILE* out = fopen(javaFilename.c_str(), "w");
234 if (out == NULL) {
235 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
236 return 1;
237 }
Muhammad Qureshief19b082020-01-03 18:38:01 -0800238
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800239 if (compileQ) {
240 errorCount = android::stats_log_api_gen::write_stats_log_java_q_for_module(
Muhammad Qureshia345af92020-03-24 17:05:14 -0700241 out, atoms, attributionDecl, javaClass, javaPackage, supportWorkSource);
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800242 } else {
243 errorCount = android::stats_log_api_gen::write_stats_log_java(
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700244 out, atoms, attributionDecl, javaClass, javaPackage, supportQ,
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800245 supportWorkSource);
246 }
Muhammad Qureshief19b082020-01-03 18:38:01 -0800247
Yao Chend54f9dd2017-10-17 17:37:48 +0000248 fclose(out);
249 }
250
Tej Singh2910d5a2019-03-23 17:26:32 -0700251 return errorCount;
Yao Chend54f9dd2017-10-17 17:37:48 +0000252}
253
Muhammad Qureshief19b082020-01-03 18:38:01 -0800254} // namespace stats_log_api_gen
255} // namespace android
Yao Chend54f9dd2017-10-17 17:37:48 +0000256
257/**
258 * Main.
259 */
Muhammad Qureshia345af92020-03-24 17:05:14 -0700260int main(int argc, char const* const* argv) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000261 GOOGLE_PROTOBUF_VERIFY_VERSION;
262
263 return android::stats_log_api_gen::run(argc, argv);
Yao Chencf3829a2018-06-05 14:20:35 -0700264}