blob: 136933b8cfb2d2f87bf8c8f872eb13f0374e8e7a [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"
12#include "atoms_info_writer.h"
13#include "frameworks/base/cmds/statsd/src/atoms.pb.h"
14#include "java_writer.h"
15#include "java_writer_q.h"
16#include "native_writer.h"
17#include "utils.h"
Yao Chend54f9dd2017-10-17 17:37:48 +000018
19using namespace google::protobuf;
20using namespace std;
21
22namespace android {
23namespace stats_log_api_gen {
24
Stefan Lafonae2df012017-11-14 09:17:21 -080025using android::os::statsd::Atom;
Yao Chend54f9dd2017-10-17 17:37:48 +000026
Muhammad Qureshia345af92020-03-24 17:05:14 -070027static void print_usage() {
Yao Chend54f9dd2017-10-17 17:37:48 +000028 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
29 fprintf(stderr, "\n");
30 fprintf(stderr, "OPTIONS\n");
Muhammad Qureshic29064e2019-11-20 17:18:03 -080031 fprintf(stderr, " --cpp FILENAME the header file to output for write helpers\n");
32 fprintf(stderr, " --header FILENAME the cpp file to output for write helpers\n");
33 fprintf(stderr,
Muhammad Qureshia345af92020-03-24 17:05:14 -070034 " --atomsInfoCpp FILENAME the header file to output for "
35 "statsd metadata\n");
36 fprintf(stderr,
37 " --atomsInfoHeader FILENAME the cpp file to output for statsd "
38 "metadata\n");
Yao Chend54f9dd2017-10-17 17:37:48 +000039 fprintf(stderr, " --help this message\n");
40 fprintf(stderr, " --java FILENAME the java file to output\n");
Tej Singh810eeb32019-03-07 19:08:52 -080041 fprintf(stderr, " --module NAME optional, module name to generate outputs for\n");
Muhammad Qureshia345af92020-03-24 17:05:14 -070042 fprintf(stderr,
43 " --namespace COMMA,SEP,NAMESPACE required for cpp/header with "
44 "module\n");
45 fprintf(stderr,
46 " comma separated namespace of "
47 "the files\n");
48 fprintf(stderr,
49 " --importHeader NAME required for cpp/jni to say which header to "
50 "import "
Muhammad Qureshic29064e2019-11-20 17:18:03 -080051 "for write helpers\n");
Muhammad Qureshia345af92020-03-24 17:05:14 -070052 fprintf(stderr,
53 " --atomsInfoImportHeader NAME required for cpp to say which "
54 "header to import "
Muhammad Qureshic29064e2019-11-20 17:18:03 -080055 "for statsd metadata\n");
Tej Singh2910d5a2019-03-23 17:26:32 -070056 fprintf(stderr, " --javaPackage PACKAGE the package for the java file.\n");
57 fprintf(stderr, " required for java with module\n");
58 fprintf(stderr, " --javaClass CLASS the class name of the java class.\n");
59 fprintf(stderr, " Optional for Java with module.\n");
Muhammad Qureshi6f658812019-11-24 22:14:38 -080060 fprintf(stderr, " Default is \"StatsLogInternal\"\n");
Muhammad Qureshibb699b62020-01-29 11:09:43 -080061 fprintf(stderr, " --supportQ Include runtime support for Android Q.\n");
Muhammad Qureshia345af92020-03-24 17:05:14 -070062 fprintf(stderr,
63 " --worksource Include support for logging WorkSource "
64 "objects.\n");
65 fprintf(stderr,
66 " --compileQ Include compile-time support for Android Q "
Muhammad Qureshibb699b62020-01-29 11:09:43 -080067 "(Java only).\n");
Muhammad Qureshi6f658812019-11-24 22:14:38 -080068}
Yao Chend54f9dd2017-10-17 17:37:48 +000069
70/**
71 * Do the argument parsing and execute the tasks.
72 */
Muhammad Qureshia345af92020-03-24 17:05:14 -070073static int run(int argc, char const* const* argv) {
Yao Chend54f9dd2017-10-17 17:37:48 +000074 string cppFilename;
75 string headerFilename;
76 string javaFilename;
Muhammad Qureshic29064e2019-11-20 17:18:03 -080077 string atomsInfoCppFilename;
78 string atomsInfoHeaderFilename;
Muhammad Qureshi4ef3c3b2020-02-28 14:22:47 -080079 string javaPackage;
80 string javaClass;
Yao Chend54f9dd2017-10-17 17:37:48 +000081
Tej Singh810eeb32019-03-07 19:08:52 -080082 string moduleName = DEFAULT_MODULE_NAME;
83 string cppNamespace = DEFAULT_CPP_NAMESPACE;
84 string cppHeaderImport = DEFAULT_CPP_HEADER_IMPORT;
Muhammad Qureshic29064e2019-11-20 17:18:03 -080085 string atomsInfoCppHeaderImport = DEFAULT_ATOMS_INFO_CPP_HEADER_IMPORT;
Muhammad Qureshi6f658812019-11-24 22:14:38 -080086 bool supportQ = false;
Muhammad Qureshib11e00d2020-01-10 14:03:13 -080087 bool supportWorkSource = false;
Muhammad Qureshibb699b62020-01-29 11:09:43 -080088 bool compileQ = false;
Tej Singh810eeb32019-03-07 19:08:52 -080089
Yao Chend54f9dd2017-10-17 17:37:48 +000090 int index = 1;
91 while (index < argc) {
92 if (0 == strcmp("--help", argv[index])) {
93 print_usage();
94 return 0;
95 } else if (0 == strcmp("--cpp", argv[index])) {
96 index++;
97 if (index >= argc) {
98 print_usage();
99 return 1;
100 }
101 cppFilename = argv[index];
102 } else if (0 == strcmp("--header", argv[index])) {
103 index++;
104 if (index >= argc) {
105 print_usage();
106 return 1;
107 }
108 headerFilename = argv[index];
109 } else if (0 == strcmp("--java", argv[index])) {
110 index++;
111 if (index >= argc) {
112 print_usage();
113 return 1;
114 }
115 javaFilename = argv[index];
Tej Singh810eeb32019-03-07 19:08:52 -0800116 } else if (0 == strcmp("--module", argv[index])) {
117 index++;
118 if (index >= argc) {
119 print_usage();
120 return 1;
121 }
122 moduleName = argv[index];
123 } else if (0 == strcmp("--namespace", argv[index])) {
124 index++;
125 if (index >= argc) {
126 print_usage();
127 return 1;
128 }
129 cppNamespace = argv[index];
130 } else if (0 == strcmp("--importHeader", argv[index])) {
131 index++;
132 if (index >= argc) {
133 print_usage();
134 return 1;
135 }
136 cppHeaderImport = argv[index];
Tej Singh2910d5a2019-03-23 17:26:32 -0700137 } else if (0 == strcmp("--javaPackage", argv[index])) {
138 index++;
139 if (index >= argc) {
140 print_usage();
141 return 1;
142 }
143 javaPackage = argv[index];
144 } else if (0 == strcmp("--javaClass", argv[index])) {
145 index++;
146 if (index >= argc) {
147 print_usage();
148 return 1;
149 }
150 javaClass = argv[index];
Muhammad Qureshic29064e2019-11-20 17:18:03 -0800151 } else if (0 == strcmp("--atomsInfoHeader", argv[index])) {
152 index++;
153 if (index >= argc) {
154 print_usage();
155 return 1;
156 }
157 atomsInfoHeaderFilename = argv[index];
158 } else if (0 == strcmp("--atomsInfoCpp", argv[index])) {
159 index++;
160 if (index >= argc) {
161 print_usage();
162 return 1;
163 }
164 atomsInfoCppFilename = argv[index];
165 } else if (0 == strcmp("--atomsInfoImportHeader", argv[index])) {
166 index++;
167 if (index >= argc) {
168 print_usage();
169 return 1;
170 }
171 atomsInfoCppHeaderImport = argv[index];
Muhammad Qureshi6f658812019-11-24 22:14:38 -0800172 } else if (0 == strcmp("--supportQ", argv[index])) {
173 supportQ = true;
Muhammad Qureshib11e00d2020-01-10 14:03:13 -0800174 } else if (0 == strcmp("--worksource", argv[index])) {
175 supportWorkSource = true;
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800176 } else if (0 == strcmp("--compileQ", argv[index])) {
177 compileQ = true;
Yao Chend54f9dd2017-10-17 17:37:48 +0000178 }
Muhammad Qureshic29064e2019-11-20 17:18:03 -0800179
Yao Chend54f9dd2017-10-17 17:37:48 +0000180 index++;
181 }
182
Muhammad Qureshia345af92020-03-24 17:05:14 -0700183 if (cppFilename.size() == 0 && headerFilename.size() == 0 && javaFilename.size() == 0 &&
184 atomsInfoHeaderFilename.size() == 0 && atomsInfoCppFilename.size() == 0) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000185 print_usage();
186 return 1;
187 }
188
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800189 if (DEFAULT_MODULE_NAME == moduleName && (supportQ || compileQ)) {
Muhammad Qureshi6f658812019-11-24 22:14:38 -0800190 // Support for Q schema is not needed for default module.
191 fprintf(stderr, "%s cannot support Q schema\n", moduleName.c_str());
192 return 1;
193 }
194
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800195 if (supportQ && compileQ) {
196 // Runtime Q support is redundant if compile-time Q support is required.
197 fprintf(stderr, "Cannot specify compileQ and supportQ simultaneously.\n");
198 return 1;
199 }
200
Yao Chend54f9dd2017-10-17 17:37:48 +0000201 // Collate the parameters
202 Atoms atoms;
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700203 int errorCount = collate_atoms(Atom::descriptor(), moduleName, &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +0000204 if (errorCount != 0) {
205 return 1;
206 }
207
Yangster-mac7604aea2017-12-11 22:55:49 -0800208 AtomDecl attributionDecl;
209 vector<java_type_t> attributionSignature;
Muhammad Qureshia345af92020-03-24 17:05:14 -0700210 collate_atom(android::os::statsd::AttributionNode::descriptor(), &attributionDecl,
211 &attributionSignature);
Yangster-mac7604aea2017-12-11 22:55:49 -0800212
Muhammad Qureshic29064e2019-11-20 17:18:03 -0800213 // Write the atoms info .cpp file
214 if (atomsInfoCppFilename.size() != 0) {
215 FILE* out = fopen(atomsInfoCppFilename.c_str(), "w");
216 if (out == NULL) {
217 fprintf(stderr, "Unable to open file for write: %s\n", atomsInfoCppFilename.c_str());
218 return 1;
219 }
Muhammad Qureshia345af92020-03-24 17:05:14 -0700220 errorCount = android::stats_log_api_gen::write_atoms_info_cpp(out, atoms, cppNamespace,
221 atomsInfoCppHeaderImport);
Muhammad Qureshic29064e2019-11-20 17:18:03 -0800222 fclose(out);
223 }
224
225 // Write the atoms info .h file
226 if (atomsInfoHeaderFilename.size() != 0) {
227 FILE* out = fopen(atomsInfoHeaderFilename.c_str(), "w");
228 if (out == NULL) {
229 fprintf(stderr, "Unable to open file for write: %s\n", atomsInfoHeaderFilename.c_str());
230 return 1;
231 }
Muhammad Qureshicdf291d2020-04-15 17:13:12 -0700232 errorCount = android::stats_log_api_gen::write_atoms_info_header(out, cppNamespace);
Muhammad Qureshic29064e2019-11-20 17:18:03 -0800233 fclose(out);
234 }
235
Yao Chend54f9dd2017-10-17 17:37:48 +0000236 // Write the .cpp file
237 if (cppFilename.size() != 0) {
238 FILE* out = fopen(cppFilename.c_str(), "w");
239 if (out == NULL) {
240 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
241 return 1;
242 }
Tej Singh810eeb32019-03-07 19:08:52 -0800243 // If this is for a specific module, the namespace must also be provided.
244 if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) {
245 fprintf(stderr, "Must supply --namespace if supplying a specific module\n");
246 return 1;
247 }
Muhammad Qureshia345af92020-03-24 17:05:14 -0700248 // If this is for a specific module, the header file to import must also be
249 // provided.
Tej Singh810eeb32019-03-07 19:08:52 -0800250 if (moduleName != DEFAULT_MODULE_NAME && cppHeaderImport == DEFAULT_CPP_HEADER_IMPORT) {
251 fprintf(stderr, "Must supply --headerImport if supplying a specific module\n");
252 return 1;
253 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800254 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
Muhammad Qureshia345af92020-03-24 17:05:14 -0700255 out, atoms, attributionDecl, cppNamespace, cppHeaderImport, supportQ);
Yao Chend54f9dd2017-10-17 17:37:48 +0000256 fclose(out);
257 }
258
259 // Write the .h file
260 if (headerFilename.size() != 0) {
261 FILE* out = fopen(headerFilename.c_str(), "w");
262 if (out == NULL) {
263 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
264 return 1;
265 }
Tej Singh810eeb32019-03-07 19:08:52 -0800266 // If this is for a specific module, the namespace must also be provided.
267 if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) {
268 fprintf(stderr, "Must supply --namespace if supplying a specific module\n");
269 }
Muhammad Qureshia345af92020-03-24 17:05:14 -0700270 errorCount = android::stats_log_api_gen::write_stats_log_header(out, atoms, attributionDecl,
271 cppNamespace);
Yao Chend54f9dd2017-10-17 17:37:48 +0000272 fclose(out);
273 }
274
275 // Write the .java file
276 if (javaFilename.size() != 0) {
Muhammad Qureshi4ef3c3b2020-02-28 14:22:47 -0800277 if (javaClass.size() == 0) {
278 fprintf(stderr, "Must supply --javaClass if supplying a Java filename");
279 return 1;
280 }
281
282 if (javaPackage.size() == 0) {
283 fprintf(stderr, "Must supply --javaPackage if supplying a Java filename");
284 return 1;
285 }
286
287 if (moduleName.size() == 0) {
288 fprintf(stderr, "Must supply --module if supplying a Java filename");
289 return 1;
290 }
291
Yao Chend54f9dd2017-10-17 17:37:48 +0000292 FILE* out = fopen(javaFilename.c_str(), "w");
293 if (out == NULL) {
294 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
295 return 1;
296 }
Muhammad Qureshief19b082020-01-03 18:38:01 -0800297
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800298 if (compileQ) {
299 errorCount = android::stats_log_api_gen::write_stats_log_java_q_for_module(
Muhammad Qureshia345af92020-03-24 17:05:14 -0700300 out, atoms, attributionDecl, javaClass, javaPackage, supportWorkSource);
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800301 } else {
302 errorCount = android::stats_log_api_gen::write_stats_log_java(
Muhammad Qureshib13a3212020-03-12 07:37:13 -0700303 out, atoms, attributionDecl, javaClass, javaPackage, supportQ,
Muhammad Qureshibb699b62020-01-29 11:09:43 -0800304 supportWorkSource);
305 }
Muhammad Qureshief19b082020-01-03 18:38:01 -0800306
Yao Chend54f9dd2017-10-17 17:37:48 +0000307 fclose(out);
308 }
309
Tej Singh2910d5a2019-03-23 17:26:32 -0700310 return errorCount;
Yao Chend54f9dd2017-10-17 17:37:48 +0000311}
312
Muhammad Qureshief19b082020-01-03 18:38:01 -0800313} // namespace stats_log_api_gen
314} // namespace android
Yao Chend54f9dd2017-10-17 17:37:48 +0000315
316/**
317 * Main.
318 */
Muhammad Qureshia345af92020-03-24 17:05:14 -0700319int main(int argc, char const* const* argv) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000320 GOOGLE_PROTOBUF_VERIFY_VERSION;
321
322 return android::stats_log_api_gen::run(argc, argv);
Yao Chencf3829a2018-06-05 14:20:35 -0700323}