blob: 464cd52526add8b802f797762ff7747d4b0fe228 [file] [log] [blame]
Christopher Wiley89eaab52015-09-15 14:46:46 -07001/*
2 * Copyright (C) 2015, 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 */
Adam Lesinskiffa16862014-01-23 18:17:42 -080016
Christopher Wileyf690be52015-09-14 15:19:10 -070017#include "aidl.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080018
Christopher Wileyf690be52015-09-14 15:19:10 -070019#include <fcntl.h>
Christopher Wileyc16e5e72015-09-16 10:49:40 -070020#include <iostream>
Christopher Wileyf690be52015-09-14 15:19:10 -070021#include <map>
Adam Lesinskiffa16862014-01-23 18:17:42 -080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Christopher Wileyf690be52015-09-14 15:19:10 -070025#include <sys/param.h>
26#include <sys/stat.h>
27#include <unistd.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -080028
Elliott Hughes549b6e22015-08-17 12:41:46 -070029#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -080030#include <io.h>
Andrew Hsieh15ce9942014-05-07 20:14:30 +080031#include <direct.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -080032#include <sys/stat.h>
33#endif
34
Christopher Wileyf690be52015-09-14 15:19:10 -070035
Christopher Wileyf690be52015-09-14 15:19:10 -070036#include "aidl_language.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070037#include "generate_cpp.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070038#include "generate_java.h"
Christopher Wiley72877ac2015-10-06 14:41:42 -070039#include "import_resolver.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070040#include "logging.h"
41#include "options.h"
42#include "os.h"
Christopher Wileye3550c62015-09-29 13:26:10 -070043#include "type_cpp.h"
Christopher Wiley775fa1f2015-09-22 15:00:12 -070044#include "type_java.h"
Christopher Wiley84c1eac2015-09-23 13:29:28 -070045#include "type_namespace.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070046
Adam Lesinskiffa16862014-01-23 18:17:42 -080047#ifndef O_BINARY
48# define O_BINARY 0
49#endif
50
Christopher Wileyc16e5e72015-09-16 10:49:40 -070051using std::cerr;
52using std::endl;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070053using std::map;
54using std::set;
55using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070056using std::unique_ptr;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070057using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080058
Christopher Wileyf690be52015-09-14 15:19:10 -070059namespace android {
60namespace aidl {
61namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080062
Christopher Wileyf690be52015-09-14 15:19:10 -070063// The following are gotten as the offset from the allowable id's between
64// android.os.IBinder.FIRST_CALL_TRANSACTION=1 and
65// android.os.IBinder.LAST_CALL_TRANSACTION=16777215
66const int kMinUserSetMethodId = 0;
67const int kMaxUserSetMethodId = 16777214;
Adam Lesinskiffa16862014-01-23 18:17:42 -080068
Casey Dahlin42727f82015-10-12 19:23:40 -070069bool check_filename(const std::string& filename,
70 const std::string& package,
71 const std::string& name,
72 unsigned line) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080073 const char* p;
74 string expected;
75 string fn;
76 size_t len;
77 char cwd[MAXPATHLEN];
78 bool valid = false;
79
Elliott Hughesce310da2015-07-29 08:44:17 -070080#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -080081 if (isalpha(filename[0]) && filename[1] == ':'
82 && filename[2] == OS_PATH_SEPARATOR) {
83#else
84 if (filename[0] == OS_PATH_SEPARATOR) {
85#endif
86 fn = filename;
87 } else {
88 fn = getcwd(cwd, sizeof(cwd));
89 len = fn.length();
90 if (fn[len-1] != OS_PATH_SEPARATOR) {
91 fn += OS_PATH_SEPARATOR;
92 }
93 fn += filename;
94 }
95
Casey Dahlinfb7da2e2015-10-08 17:26:09 -070096 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080097 expected = package;
98 expected += '.';
99 }
100
101 len = expected.length();
102 for (size_t i=0; i<len; i++) {
103 if (expected[i] == '.') {
104 expected[i] = OS_PATH_SEPARATOR;
105 }
106 }
107
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700108 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700109
Adam Lesinskiffa16862014-01-23 18:17:42 -0800110 expected += ".aidl";
111
112 len = fn.length();
113 valid = (len >= expected.length());
114
115 if (valid) {
116 p = fn.c_str() + (len - expected.length());
117
Elliott Hughesce310da2015-07-29 08:44:17 -0700118#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800119 if (OS_PATH_SEPARATOR != '/') {
120 // Input filename under cygwin most likely has / separators
121 // whereas the expected string uses \\ separators. Adjust
122 // them accordingly.
123 for (char *c = const_cast<char *>(p); *c; ++c) {
124 if (*c == '/') *c = OS_PATH_SEPARATOR;
125 }
126 }
127#endif
128
Yabin Cui482eefb2014-11-10 15:01:43 -0800129 // aidl assumes case-insensitivity on Mac Os and Windows.
130#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800131 valid = (expected == p);
132#else
133 valid = !strcasecmp(expected.c_str(), p);
134#endif
135 }
136
137 if (!valid) {
138 fprintf(stderr, "%s:%d interface %s should be declared in a file"
139 " called %s.\n",
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700140 filename.c_str(), line, name.c_str(), expected.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800141 }
142
Casey Dahlin42727f82015-10-12 19:23:40 -0700143 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800144}
145
Casey Dahlin42727f82015-10-12 19:23:40 -0700146bool check_filenames(const std::string& filename, const AidlDocumentItem* items) {
147 if (! items)
148 return true;
149
150 if (items->item_type == INTERFACE_TYPE_BINDER) {
151 const AidlInterface* c = reinterpret_cast<const AidlInterface*>(items);
152 return check_filename(filename, c->GetPackage(), c->GetName(), c->GetLine());
153 }
154
155 bool success = true;
156
157 for (const AidlParcelable* p = reinterpret_cast<const AidlParcelable*>(items);
158 p; p = p->next)
159 success &= check_filename(filename, p->GetPackage(), p->GetName(),
160 p->GetLine());
161
162 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800163}
164
Casey Dahlin0edf3422015-10-07 12:34:59 -0700165bool gather_types(const std::string& filename,
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700166 const AidlDocumentItem* all_items,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700167 TypeNamespace* types) {
168 bool success = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800169
Casey Dahlin42727f82015-10-12 19:23:40 -0700170 if (! all_items)
171 return true;
172
173 if (all_items->item_type == INTERFACE_TYPE_BINDER)
174 return types->AddBinderType(reinterpret_cast<const AidlInterface *>(all_items), filename);
175
176 for (const AidlParcelable* item =
177 reinterpret_cast<const AidlParcelable *>(all_items);
178 item; item = item->next) {
179 success &= types->AddParcelableType(item, filename);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700180 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700181
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700182 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800183}
184
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700185int check_types(const string& filename,
Casey Dahlin98a544b2015-10-14 14:22:55 -0700186 const AidlInterface* c,
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700187 TypeNamespace* types) {
188 int err = 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700189
190 // Has to be a pointer due to deleting copy constructor. No idea why.
191 map<string, const AidlMethod*> method_names;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700192 for (const auto& m : c->GetMethods()) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700193 if (!types->AddContainerType(m->GetType().GetName()) ||
194 !types->IsValidReturnType(m->GetType(), filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700195 err = 1; // return type is invalid
Adam Lesinskiffa16862014-01-23 18:17:42 -0800196 }
197
198 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700199 for (const auto& arg : m->GetArguments()) {
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700200 if (!types->AddContainerType(arg->GetType().GetName()) ||
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700201 !types->IsValidArg(*arg, index, filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700202 err = 1;
203 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800204 }
205
Casey Dahlinf4a93112015-10-05 16:58:09 -0700206 auto it = method_names.find(m->GetName());
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700207 // prevent duplicate methods
Casey Dahlinf4a93112015-10-05 16:58:09 -0700208 if (it == method_names.end()) {
209 method_names[m->GetName()] = m.get();
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700210 } else {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700211 cerr << filename << ":" << m->GetLine()
212 << " attempt to redefine method " << m->GetName() << "," << endl
213 << filename << ":" << it->second->GetLine()
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700214 << " previously defined here." << endl;
215 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800216 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700217 }
218 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800219}
220
Christopher Wileyf690be52015-09-14 15:19:10 -0700221void generate_dep_file(const JavaOptions& options,
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700222 const AidlDocumentItem* items,
Casey Dahlin0edf3422015-10-07 12:34:59 -0700223 const std::vector<std::unique_ptr<AidlImport>>& imports) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800224 /* we open the file in binary mode to ensure that the same output is
225 * generated on all platforms !!
226 */
227 FILE* to = NULL;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700228 string fileName;
229
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700230 if (options.auto_dep_file_) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700231 fileName = options.output_file_name_ + ".d";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800232 } else {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700233 fileName = options.dep_file_name_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800234 }
235
Casey Dahlin0edf3422015-10-07 12:34:59 -0700236 string output_file_name;
237
238 // TODO: Mock IO and remove this weird stuff (b/24816077)
239 if (!options.output_file_name_for_deps_test_.empty())
240 output_file_name = options.output_file_name_for_deps_test_;
241 else
242 output_file_name = options.output_file_name_;
243
244 to = fopen(fileName.c_str(), "wb");
245
Adam Lesinskiffa16862014-01-23 18:17:42 -0800246 if (to == NULL) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700247 cerr << "Could not open " << fileName << endl;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800248 return;
249 }
250
Casey Dahlin88868fc2015-09-01 13:21:26 -0700251 if (items->item_type == INTERFACE_TYPE_BINDER) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700252 fprintf(to, "%s: \\\n", output_file_name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800253 } else {
254 // parcelable: there's no output file.
255 fprintf(to, " : \\\n");
256 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700257 fprintf(to, " %s %s\n", options.input_file_name_.c_str(), imports.empty() ? "" : "\\");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800258
Casey Dahlin0edf3422015-10-07 12:34:59 -0700259 bool first = true;
260 for (const auto& import : imports) {
261 if (! first) {
262 fprintf(to, " \\\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800263 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700264 first = false;
265
266 if (! import->GetFilename().empty()) {
267 fprintf(to, " %s", import->GetFilename().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800268 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800269 }
270
Casey Dahlin0edf3422015-10-07 12:34:59 -0700271 fprintf(to, first ? "\n" : "\n\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800272
Ying Wang0e4861a2015-07-22 17:42:35 -0700273 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
274 // has been deleted, moved or renamed in incremental build.
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700275 fprintf(to, "%s :\n", options.input_file_name_.c_str());
Ying Wang0e4861a2015-07-22 17:42:35 -0700276
Adam Lesinskiffa16862014-01-23 18:17:42 -0800277 // Output "<imported_file>: " so make won't fail if the imported file has
278 // been deleted, moved or renamed in incremental build.
Casey Dahlin0edf3422015-10-07 12:34:59 -0700279 for (const auto& import : imports) {
280 if (! import->GetFilename().empty()) {
281 fprintf(to, "%s :\n", import->GetFilename().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800282 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800283 }
284
285 fclose(to);
286}
287
Christopher Wileyf690be52015-09-14 15:19:10 -0700288string generate_outputFileName2(const JavaOptions& options,
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700289 const std::string& name,
290 const std::string& package) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800291 string result;
292
293 // create the path to the destination folder based on the
294 // interface package name
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700295 result = options.output_base_folder_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800296 result += OS_PATH_SEPARATOR;
297
298 string packageStr = package;
299 size_t len = packageStr.length();
300 for (size_t i=0; i<len; i++) {
301 if (packageStr[i] == '.') {
302 packageStr[i] = OS_PATH_SEPARATOR;
303 }
304 }
305
306 result += packageStr;
307
308 // add the filename by replacing the .aidl extension to .java
Adam Lesinskiffa16862014-01-23 18:17:42 -0800309 result += OS_PATH_SEPARATOR;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700310 result.append(name, 0, name.find('.'));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800311 result += ".java";
312
313 return result;
314}
315
Christopher Wileyf690be52015-09-14 15:19:10 -0700316string generate_outputFileName(const JavaOptions& options,
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700317 const AidlDocumentItem* items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800318 // items has already been checked to have only one interface.
Casey Dahlin88868fc2015-09-01 13:21:26 -0700319 if (items->item_type == INTERFACE_TYPE_BINDER) {
Casey Dahlin5ac90202015-10-09 15:16:43 -0700320 const AidlInterface* type = reinterpret_cast<const AidlInterface*>(items);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800321
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700322 return generate_outputFileName2(options, type->GetName(), type->GetPackage());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800323 } else if (items->item_type == USER_DATA_TYPE) {
Casey Dahlin5ac90202015-10-09 15:16:43 -0700324 const AidlParcelable* type = reinterpret_cast<const AidlParcelable*>(items);
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700325
Casey Dahlin59401da2015-10-09 18:16:45 -0700326 return generate_outputFileName2(options, type->GetName(), type->GetPackage());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800327 }
328
329 // I don't think we can come here, but safer than returning NULL.
330 string result;
331 return result;
332}
333
334
Christopher Wileyf690be52015-09-14 15:19:10 -0700335void check_outputFilePath(const string& path) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800336 size_t len = path.length();
337 for (size_t i=0; i<len ; i++) {
338 if (path[i] == OS_PATH_SEPARATOR) {
339 string p = path.substr(0, i);
340 if (access(path.data(), F_OK) != 0) {
Elliott Hughes549b6e22015-08-17 12:41:46 -0700341#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800342 _mkdir(p.data());
343#else
344 mkdir(p.data(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
345#endif
346 }
347 }
348 }
349}
350
351
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700352int parse_preprocessed_file(const string& filename, TypeNamespace* types) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800353 FILE* f = fopen(filename.c_str(), "rb");
354 if (f == NULL) {
355 fprintf(stderr, "aidl: can't open preprocessed file: %s\n",
356 filename.c_str());
357 return 1;
358 }
359
360 int lineno = 1;
361 char line[1024];
362 char type[1024];
363 char fullname[1024];
364 while (fgets(line, sizeof(line), f)) {
365 // skip comments and empty lines
366 if (!line[0] || strncmp(line, "//", 2) == 0) {
367 continue;
368 }
369
370 sscanf(line, "%s %[^; \r\n\t];", type, fullname);
371
372 char* packagename;
Casey Dahlin624358c2015-10-12 19:29:51 -0700373 char* classname = strrchr(fullname, '.');
Adam Lesinskiffa16862014-01-23 18:17:42 -0800374 if (classname != NULL) {
375 *classname = '\0';
376 classname++;
377 packagename = fullname;
378 } else {
379 classname = fullname;
380 packagename = NULL;
381 }
382
383 //printf("%s:%d:...%s...%s...%s...\n", filename.c_str(), lineno,
384 // type, packagename, classname);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700385 AidlDocumentItem* doc;
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700386
Adam Lesinskiffa16862014-01-23 18:17:42 -0800387 if (0 == strcmp("parcelable", type)) {
Casey Dahlin98a544b2015-10-14 14:22:55 -0700388 doc = new AidlParcelable(classname, lineno, packagename);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800389 }
390 else if (0 == strcmp("interface", type)) {
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700391 auto temp = new std::vector<std::unique_ptr<AidlMethod>>();
Casey Dahlin98a544b2015-10-14 14:22:55 -0700392 doc = new AidlInterface(classname, lineno, "", false, temp,
393 packagename ?: "");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800394 }
395 else {
396 fprintf(stderr, "%s:%d: bad type in line: %s\n",
397 filename.c_str(), lineno, line);
Elliott Hughes5cd06072013-10-29 15:25:52 -0700398 fclose(f);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800399 return 1;
400 }
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700401 if (!gather_types(filename.c_str(), doc, types)) {
402 fprintf(stderr, "Failed to gather types for preprocessed aidl.\n");
403 fclose(f);
404 return 1;
405 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800406 lineno++;
407 }
408
409 if (!feof(f)) {
410 fprintf(stderr, "%s:%d: error reading file, line to long.\n",
411 filename.c_str(), lineno);
412 return 1;
413 }
414
415 fclose(f);
416 return 0;
417}
418
Christopher Wileyf690be52015-09-14 15:19:10 -0700419int check_and_assign_method_ids(const char * filename,
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700420 const std::vector<std::unique_ptr<AidlMethod>>& items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800421 // Check whether there are any methods with manually assigned id's and any that are not.
422 // Either all method id's must be manually assigned or all of them must not.
423 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
424 set<int> usedIds;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800425 bool hasUnassignedIds = false;
426 bool hasAssignedIds = false;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700427 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700428 if (item->HasId()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700429 hasAssignedIds = true;
Casey Dahlindff80e52015-09-29 13:57:06 -0700430 // Ensure that the user set id is not duplicated.
Casey Dahlinf4a93112015-10-05 16:58:09 -0700431 if (usedIds.find(item->GetId()) != usedIds.end()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700432 // We found a duplicate id, so throw an error.
Adam Lesinskiffa16862014-01-23 18:17:42 -0800433 fprintf(stderr,
Casey Dahlindff80e52015-09-29 13:57:06 -0700434 "%s:%d Found duplicate method id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700435 filename, item->GetLine(),
436 item->GetId(), item->GetName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800437 return 1;
438 }
Casey Dahlindff80e52015-09-29 13:57:06 -0700439 // Ensure that the user set id is within the appropriate limits
Casey Dahlinf4a93112015-10-05 16:58:09 -0700440 if (item->GetId() < kMinUserSetMethodId ||
441 item->GetId() > kMaxUserSetMethodId) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700442 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700443 filename, item->GetLine(),
444 item->GetId(), item->GetName().c_str());
Casey Dahlindff80e52015-09-29 13:57:06 -0700445 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
446 kMinUserSetMethodId, kMaxUserSetMethodId);
447 return 1;
448 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700449 usedIds.insert(item->GetId());
Casey Dahlindff80e52015-09-29 13:57:06 -0700450 } else {
451 hasUnassignedIds = true;
452 }
453 if (hasAssignedIds && hasUnassignedIds) {
454 fprintf(stderr,
455 "%s: You must either assign id's to all methods or to none of them.\n",
456 filename);
457 return 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800458 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800459 }
460
461 // In the case that all methods have unassigned id's, set a unique id for them.
462 if (hasUnassignedIds) {
463 int newId = 0;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700464 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700465 item->SetId(newId++);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800466 }
467 }
468
469 // success
470 return 0;
471}
472
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700473} // namespace
474
475namespace internals {
476
477int load_and_validate_aidl(const std::vector<std::string> preprocessed_files,
478 const std::vector<std::string> import_paths,
479 const std::string& input_file_name,
480 const IoDelegate& io_delegate,
481 TypeNamespace* types,
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700482 AidlInterface** returned_interface,
Casey Dahlin0edf3422015-10-07 12:34:59 -0700483 std::vector<std::unique_ptr<AidlImport>>* returned_imports) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700484 int err = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800485
Casey Dahlin624358c2015-10-12 19:29:51 -0700486 std::map<AidlImport*,std::unique_ptr<AidlDocumentItem>> docs;
487
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700488 // import the preprocessed file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700489 for (const string& s : preprocessed_files) {
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700490 err |= parse_preprocessed_file(s, types);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700491 }
492 if (err != 0) {
493 return err;
494 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800495
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700496 // parse the input file
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700497 Parser p{io_delegate};
498 if (!p.ParseFile(input_file_name)) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700499 return 1;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700500 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700501
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700502 AidlDocumentItem* parsed_doc = p.GetDocument();
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700503 // We could in theory declare parcelables in the same file as the interface.
504 // In practice, those parcelables would have to have the same name as
505 // the interface, since this was originally written to support Java, with its
506 // packages and names that correspond to file system structure.
507 // Since we can't have two distinct classes with the same name and package,
508 // we can't actually declare parcelables in the same file.
509 if (parsed_doc == nullptr ||
Casey Dahlin42727f82015-10-12 19:23:40 -0700510 parsed_doc->item_type != INTERFACE_TYPE_BINDER) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700511 cerr << "aidl expects exactly one interface per input file";
Christopher Wileya2f516d2015-09-24 10:12:31 -0700512 return 1;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700513 }
Casey Dahlin5ac90202015-10-09 15:16:43 -0700514 AidlInterface* interface = reinterpret_cast<AidlInterface*>(parsed_doc);
Casey Dahlin42727f82015-10-12 19:23:40 -0700515 if (!check_filename(input_file_name.c_str(), interface->GetPackage(),
516 interface->GetName(), interface->GetLine()))
517 err |= 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800518
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700519 // parse the imports of the input file
Christopher Wiley72877ac2015-10-06 14:41:42 -0700520 ImportResolver import_resolver{io_delegate, import_paths};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700521 for (auto& import : p.GetImports()) {
522 if (types->HasType(import->GetNeededClass())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700523 // There are places in the Android tree where an import doesn't resolve,
524 // but we'll pick the type up through the preprocessed types.
525 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700526 continue;
527 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700528 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700529 if (import_path.empty()) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700530 cerr << import->GetFileFrom() << ":" << import->GetLine()
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700531 << ": couldn't find import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700532 << import->GetNeededClass() << endl;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700533 err |= 1;
534 continue;
535 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700536 import->SetFilename(import_path);
Casey Dahlin2cc93162015-10-02 16:14:17 -0700537
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700538 Parser p{io_delegate};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700539 if (!p.ParseFile(import->GetFilename())) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700540 cerr << "error while parsing import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700541 << import->GetNeededClass() << endl;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700542 err |= 1;
543 continue;
544 }
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700545
Casey Dahlin624358c2015-10-12 19:29:51 -0700546 AidlDocumentItem* document = p.GetDocument();
547 if (!check_filenames(import->GetFilename(), document))
Casey Dahlin42727f82015-10-12 19:23:40 -0700548 err |= 1;
Casey Dahlin624358c2015-10-12 19:29:51 -0700549 docs[import.get()] = std::unique_ptr<AidlDocumentItem>(document);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700550 }
551 if (err != 0) {
552 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700553 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800554
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700555 // gather the types that have been declared
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700556 if (!gather_types(input_file_name.c_str(), parsed_doc, types)) {
557 err |= 1;
558 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700559 for (const auto& import : p.GetImports()) {
Casey Dahlin624358c2015-10-12 19:29:51 -0700560 if (!gather_types(import->GetFilename(), docs[import.get()].get(), types)) {
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700561 err |= 1;
562 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700563 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800564
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700565 // check the referenced types in parsed_doc to make sure we've imported them
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700566 err |= check_types(input_file_name, interface, types);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800567
Adam Lesinskiffa16862014-01-23 18:17:42 -0800568
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700569 // assign method ids and validate.
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700570 err |= check_and_assign_method_ids(input_file_name.c_str(),
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700571 interface->GetMethods());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800572
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700573 // after this, there shouldn't be any more errors because of the
574 // input.
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700575 if (err != 0) {
576 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700577 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800578
Casey Dahlin0edf3422015-10-07 12:34:59 -0700579 if (returned_interface)
580 *returned_interface = interface;
581 else
582 delete interface;
583
584 if (returned_imports)
585 p.ReleaseImports(returned_imports);
586
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700587 return 0;
588}
589
Casey Dahlin2cc93162015-10-02 16:14:17 -0700590} // namespace internals
591
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700592int compile_aidl_to_cpp(const CppOptions& options,
593 const IoDelegate& io_delegate) {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700594 AidlInterface* interface = nullptr;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700595 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileye3550c62015-09-29 13:26:10 -0700596 unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace());
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700597 int err = internals::load_and_validate_aidl(
598 std::vector<std::string>{}, // no preprocessed files
599 options.ImportPaths(),
600 options.InputFileName(),
601 io_delegate,
602 types.get(),
603 &interface,
604 &imports);
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700605 if (err != 0) {
606 return err;
607 }
608
609 // TODO(wiley) b/23600457 generate a dependency file if requested with -b
610
Christopher Wileye3550c62015-09-29 13:26:10 -0700611 return (cpp::GenerateCpp(options, *types, *interface)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700612}
613
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700614int compile_aidl_to_java(const JavaOptions& options,
615 const IoDelegate& io_delegate) {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700616 AidlInterface* interface = nullptr;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700617 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileydb154a52015-09-28 16:32:25 -0700618 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700619 int err = internals::load_and_validate_aidl(
620 options.preprocessed_files_,
621 options.import_paths_,
622 options.input_file_name_,
623 io_delegate,
624 types.get(),
625 &interface,
626 &imports);
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700627 if (err != 0) {
628 return err;
629 }
Casey Dahlin5ac90202015-10-09 15:16:43 -0700630 AidlDocumentItem* parsed_doc = reinterpret_cast<AidlDocumentItem*>(interface);
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700631
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700632 string output_file_name = options.output_file_name_;
633 // if needed, generate the output file name from the base folder
634 if (output_file_name.length() == 0 &&
635 options.output_base_folder_.length() > 0) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700636 output_file_name = generate_outputFileName(options, parsed_doc);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700637 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800638
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700639 // if we were asked to, generate a make dependency file
640 // unless it's a parcelable *and* it's supposed to fail on parcelable
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700641 if (options.auto_dep_file_ || options.dep_file_name_ != "") {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800642 // make sure the folders of the output file all exists
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700643 check_outputFilePath(output_file_name);
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700644 generate_dep_file(options, parsed_doc, imports);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700645 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800646
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700647 // make sure the folders of the output file all exists
648 check_outputFilePath(output_file_name);
649
650 err = generate_java(output_file_name, options.input_file_name_.c_str(),
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700651 interface, types.get());
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700652
653 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800654}
655
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700656int preprocess_aidl(const JavaOptions& options,
657 const IoDelegate& io_delegate) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800658 vector<string> lines;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800659
660 // read files
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700661 int N = options.files_to_preprocess_.size();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800662 for (int i=0; i<N; i++) {
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700663 Parser p{io_delegate};
664 if (!p.ParseFile(options.files_to_preprocess_[i]))
665 return 1;
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700666 AidlDocumentItem* doc = p.GetDocument();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800667 string line;
668 if (doc->item_type == USER_DATA_TYPE) {
Casey Dahlin5ac90202015-10-09 15:16:43 -0700669 AidlParcelable* parcelable = reinterpret_cast<AidlParcelable*>(doc);
Casey Dahlin59401da2015-10-09 18:16:45 -0700670
671 line = "parcelable ";
672
673 if (! parcelable->GetPackage().empty()) {
674 line += parcelable->GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800675 line += '.';
676 }
Casey Dahlin59401da2015-10-09 18:16:45 -0700677 line += parcelable->GetName();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800678 } else {
679 line = "interface ";
Casey Dahlin5ac90202015-10-09 15:16:43 -0700680 AidlInterface* iface = reinterpret_cast<AidlInterface*>(doc);
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700681 if (!iface->GetPackage().empty()) {
682 line += iface->GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800683 line += '.';
684 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700685 line += iface->GetName();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800686 }
687 line += ";\n";
688 lines.push_back(line);
689 }
690
691 // write preprocessed file
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700692 int fd = open( options.output_file_name_.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800693 O_RDWR|O_CREAT|O_TRUNC|O_BINARY,
Elliott Hughes549b6e22015-08-17 12:41:46 -0700694#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800695 _S_IREAD|_S_IWRITE);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700696#else
Adam Lesinskiffa16862014-01-23 18:17:42 -0800697 S_IRUSR|S_IWUSR|S_IRGRP);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700698#endif
Adam Lesinskiffa16862014-01-23 18:17:42 -0800699 if (fd == -1) {
700 fprintf(stderr, "aidl: could not open file for write: %s\n",
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700701 options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800702 return 1;
703 }
704
705 N = lines.size();
706 for (int i=0; i<N; i++) {
707 const string& s = lines[i];
708 int len = s.length();
709 if (len != write(fd, s.c_str(), len)) {
710 fprintf(stderr, "aidl: error writing to file %s\n",
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700711 options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800712 close(fd);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700713 unlink(options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800714 return 1;
715 }
716 }
717
718 close(fd);
719 return 0;
720}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700721
722} // namespace android
723} // namespace aidl