Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 16 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 17 | #include "aidl.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 18 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 20 | #include <iostream> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 21 | #include <map> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 25 | #include <sys/param.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <unistd.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 29 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 30 | #include <io.h> |
Andrew Hsieh | 15ce994 | 2014-05-07 20:14:30 +0800 | [diff] [blame] | 31 | #include <direct.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #endif |
| 34 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 35 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 36 | #include "aidl_language.h" |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 37 | #include "generate_cpp.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 38 | #include "generate_java.h" |
| 39 | #include "logging.h" |
| 40 | #include "options.h" |
| 41 | #include "os.h" |
| 42 | #include "parse_helpers.h" |
| 43 | #include "search_path.h" |
Christopher Wiley | 775fa1f | 2015-09-22 15:00:12 -0700 | [diff] [blame^] | 44 | #include "type_java.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 45 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 46 | #ifndef O_BINARY |
| 47 | # define O_BINARY 0 |
| 48 | #endif |
| 49 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 50 | using std::cerr; |
| 51 | using std::endl; |
Christopher Wiley | 9f4c7ae | 2015-08-24 14:07:32 -0700 | [diff] [blame] | 52 | using std::map; |
| 53 | using std::set; |
| 54 | using std::string; |
| 55 | using std::vector; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 56 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 57 | namespace android { |
| 58 | namespace aidl { |
| 59 | namespace { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 60 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 61 | // The following are gotten as the offset from the allowable id's between |
| 62 | // android.os.IBinder.FIRST_CALL_TRANSACTION=1 and |
| 63 | // android.os.IBinder.LAST_CALL_TRANSACTION=16777215 |
| 64 | const int kMinUserSetMethodId = 0; |
| 65 | const int kMaxUserSetMethodId = 16777214; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 66 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 67 | int check_filename(const char* filename, |
| 68 | const char* package, |
| 69 | buffer_type* name) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 70 | const char* p; |
| 71 | string expected; |
| 72 | string fn; |
| 73 | size_t len; |
| 74 | char cwd[MAXPATHLEN]; |
| 75 | bool valid = false; |
| 76 | |
Elliott Hughes | ce310da | 2015-07-29 08:44:17 -0700 | [diff] [blame] | 77 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 78 | if (isalpha(filename[0]) && filename[1] == ':' |
| 79 | && filename[2] == OS_PATH_SEPARATOR) { |
| 80 | #else |
| 81 | if (filename[0] == OS_PATH_SEPARATOR) { |
| 82 | #endif |
| 83 | fn = filename; |
| 84 | } else { |
| 85 | fn = getcwd(cwd, sizeof(cwd)); |
| 86 | len = fn.length(); |
| 87 | if (fn[len-1] != OS_PATH_SEPARATOR) { |
| 88 | fn += OS_PATH_SEPARATOR; |
| 89 | } |
| 90 | fn += filename; |
| 91 | } |
| 92 | |
| 93 | if (package) { |
| 94 | expected = package; |
| 95 | expected += '.'; |
| 96 | } |
| 97 | |
| 98 | len = expected.length(); |
| 99 | for (size_t i=0; i<len; i++) { |
| 100 | if (expected[i] == '.') { |
| 101 | expected[i] = OS_PATH_SEPARATOR; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | p = strchr(name->data, '.'); |
| 106 | len = p ? p-name->data : strlen(name->data); |
| 107 | expected.append(name->data, len); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 108 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 109 | expected += ".aidl"; |
| 110 | |
| 111 | len = fn.length(); |
| 112 | valid = (len >= expected.length()); |
| 113 | |
| 114 | if (valid) { |
| 115 | p = fn.c_str() + (len - expected.length()); |
| 116 | |
Elliott Hughes | ce310da | 2015-07-29 08:44:17 -0700 | [diff] [blame] | 117 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 118 | if (OS_PATH_SEPARATOR != '/') { |
| 119 | // Input filename under cygwin most likely has / separators |
| 120 | // whereas the expected string uses \\ separators. Adjust |
| 121 | // them accordingly. |
| 122 | for (char *c = const_cast<char *>(p); *c; ++c) { |
| 123 | if (*c == '/') *c = OS_PATH_SEPARATOR; |
| 124 | } |
| 125 | } |
| 126 | #endif |
| 127 | |
Yabin Cui | 482eefb | 2014-11-10 15:01:43 -0800 | [diff] [blame] | 128 | // aidl assumes case-insensitivity on Mac Os and Windows. |
| 129 | #if defined(__linux__) |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 130 | valid = (expected == p); |
| 131 | #else |
| 132 | valid = !strcasecmp(expected.c_str(), p); |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | if (!valid) { |
| 137 | fprintf(stderr, "%s:%d interface %s should be declared in a file" |
| 138 | " called %s.\n", |
| 139 | filename, name->lineno, name->data, expected.c_str()); |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 146 | int check_filenames(const char* filename, document_item_type* items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 147 | int err = 0; |
| 148 | while (items) { |
| 149 | if (items->item_type == USER_DATA_TYPE) { |
| 150 | user_data_type* p = (user_data_type*)items; |
| 151 | err |= check_filename(filename, p->package, &p->name); |
| 152 | } |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 153 | else if (items->item_type == INTERFACE_TYPE_BINDER) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 154 | interface_type* c = (interface_type*)items; |
| 155 | err |= check_filename(filename, c->package, &c->name); |
| 156 | } |
| 157 | else { |
| 158 | fprintf(stderr, "aidl: internal error unkown document type %d.\n", |
| 159 | items->item_type); |
| 160 | return 1; |
| 161 | } |
| 162 | items = items->next; |
| 163 | } |
| 164 | return err; |
| 165 | } |
| 166 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 167 | char* rfind(char* str, char c) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 168 | char* p = str + strlen(str) - 1; |
| 169 | while (p >= str) { |
| 170 | if (*p == c) { |
| 171 | return p; |
| 172 | } |
| 173 | p--; |
| 174 | } |
| 175 | return NULL; |
| 176 | } |
| 177 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 178 | int gather_types(const char* filename, document_item_type* items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 179 | int err = 0; |
| 180 | while (items) { |
| 181 | Type* type; |
| 182 | if (items->item_type == USER_DATA_TYPE) { |
| 183 | user_data_type* p = (user_data_type*)items; |
| 184 | type = new UserDataType(p->package ? p->package : "", p->name.data, |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 185 | false, p->parcelable, filename, p->name.lineno); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 186 | } |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 187 | else if (items->item_type == INTERFACE_TYPE_BINDER) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 188 | interface_type* c = (interface_type*)items; |
| 189 | type = new InterfaceType(c->package ? c->package : "", |
| 190 | c->name.data, false, c->oneway, |
| 191 | filename, c->name.lineno); |
| 192 | } |
| 193 | else { |
| 194 | fprintf(stderr, "aidl: internal error %s:%d\n", __FILE__, __LINE__); |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | Type* old = NAMES.Find(type->QualifiedName()); |
| 199 | if (old == NULL) { |
| 200 | NAMES.Add(type); |
| 201 | |
| 202 | if (items->item_type == INTERFACE_TYPE_BINDER) { |
| 203 | // for interfaces, also add the stub and proxy types, we don't |
| 204 | // bother checking these for duplicates, because the parser |
| 205 | // won't let us do it. |
| 206 | interface_type* c = (interface_type*)items; |
| 207 | |
| 208 | string name = c->name.data; |
| 209 | name += ".Stub"; |
| 210 | Type* stub = new Type(c->package ? c->package : "", |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 211 | name, Type::GENERATED, false, false, |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 212 | filename, c->name.lineno); |
| 213 | NAMES.Add(stub); |
| 214 | |
| 215 | name = c->name.data; |
| 216 | name += ".Stub.Proxy"; |
| 217 | Type* proxy = new Type(c->package ? c->package : "", |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 218 | name, Type::GENERATED, false, false, |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 219 | filename, c->name.lineno); |
| 220 | NAMES.Add(proxy); |
| 221 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 222 | } else { |
| 223 | if (old->Kind() == Type::BUILT_IN) { |
| 224 | fprintf(stderr, "%s:%d attempt to redefine built in class %s\n", |
| 225 | filename, type->DeclLine(), |
| 226 | type->QualifiedName().c_str()); |
| 227 | err = 1; |
| 228 | } |
| 229 | else if (type->Kind() != old->Kind()) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 230 | fprintf(stderr, "%s:%d attempt to redefine %s as %s,\n", |
| 231 | filename, type->DeclLine(), |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 232 | type->QualifiedName().c_str(), |
| 233 | type->HumanReadableKind().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 234 | fprintf(stderr, "%s:%d previously defined here as %s.\n", |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 235 | old->DeclFile().c_str(), old->DeclLine(), |
| 236 | old->HumanReadableKind().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 237 | err = 1; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | items = items->next; |
| 242 | } |
| 243 | return err; |
| 244 | } |
| 245 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 246 | int check_method(const char* filename, method_type* m) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 247 | int err = 0; |
| 248 | |
| 249 | // return type |
| 250 | Type* returnType = NAMES.Search(m->type.type.data); |
| 251 | if (returnType == NULL) { |
| 252 | fprintf(stderr, "%s:%d unknown return type %s\n", filename, |
| 253 | m->type.type.lineno, m->type.type.data); |
| 254 | err = 1; |
| 255 | return err; |
| 256 | } |
| 257 | |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 258 | if (!returnType->CanWriteToParcel()) { |
| 259 | fprintf(stderr, "%s:%d return type %s can't be marshalled.\n", filename, |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 260 | m->type.type.lineno, m->type.type.data); |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 261 | err = 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | if (m->type.dimension > 0 && !returnType->CanBeArray()) { |
| 265 | fprintf(stderr, "%s:%d return type %s%s can't be an array.\n", filename, |
| 266 | m->type.array_token.lineno, m->type.type.data, |
| 267 | m->type.array_token.data); |
| 268 | err = 1; |
| 269 | } |
| 270 | |
| 271 | if (m->type.dimension > 1) { |
| 272 | fprintf(stderr, "%s:%d return type %s%s only one" |
| 273 | " dimensional arrays are supported\n", filename, |
| 274 | m->type.array_token.lineno, m->type.type.data, |
| 275 | m->type.array_token.data); |
| 276 | err = 1; |
| 277 | } |
| 278 | |
| 279 | int index = 1; |
| 280 | |
| 281 | arg_type* arg = m->args; |
| 282 | while (arg) { |
| 283 | Type* t = NAMES.Search(arg->type.type.data); |
| 284 | |
| 285 | // check the arg type |
| 286 | if (t == NULL) { |
| 287 | fprintf(stderr, "%s:%d parameter %s (%d) unknown type %s\n", |
| 288 | filename, m->type.type.lineno, arg->name.data, index, |
| 289 | arg->type.type.data); |
| 290 | err = 1; |
| 291 | goto next; |
| 292 | } |
| 293 | |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 294 | if (!t->CanWriteToParcel()) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 295 | fprintf(stderr, "%s:%d parameter %d: '%s %s' can't be marshalled.\n", |
| 296 | filename, m->type.type.lineno, index, |
| 297 | arg->type.type.data, arg->name.data); |
| 298 | err = 1; |
| 299 | } |
| 300 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 301 | if (arg->direction.data == NULL |
| 302 | && (arg->type.dimension != 0 || t->CanBeOutParameter())) { |
| 303 | fprintf(stderr, "%s:%d parameter %d: '%s %s' can be an out" |
| 304 | " parameter, so you must declare it as in," |
| 305 | " out or inout.\n", |
| 306 | filename, m->type.type.lineno, index, |
| 307 | arg->type.type.data, arg->name.data); |
| 308 | err = 1; |
| 309 | } |
| 310 | |
| 311 | if (convert_direction(arg->direction.data) != IN_PARAMETER |
| 312 | && !t->CanBeOutParameter() |
| 313 | && arg->type.dimension == 0) { |
| 314 | fprintf(stderr, "%s:%d parameter %d: '%s %s %s' can only be an in" |
| 315 | " parameter.\n", |
| 316 | filename, m->type.type.lineno, index, |
| 317 | arg->direction.data, arg->type.type.data, |
| 318 | arg->name.data); |
| 319 | err = 1; |
| 320 | } |
| 321 | |
| 322 | if (arg->type.dimension > 0 && !t->CanBeArray()) { |
| 323 | fprintf(stderr, "%s:%d parameter %d: '%s %s%s %s' can't be an" |
| 324 | " array.\n", filename, |
| 325 | m->type.array_token.lineno, index, arg->direction.data, |
| 326 | arg->type.type.data, arg->type.array_token.data, |
| 327 | arg->name.data); |
| 328 | err = 1; |
| 329 | } |
| 330 | |
| 331 | if (arg->type.dimension > 1) { |
| 332 | fprintf(stderr, "%s:%d parameter %d: '%s %s%s %s' only one" |
| 333 | " dimensional arrays are supported\n", filename, |
| 334 | m->type.array_token.lineno, index, arg->direction.data, |
| 335 | arg->type.type.data, arg->type.array_token.data, |
| 336 | arg->name.data); |
| 337 | err = 1; |
| 338 | } |
| 339 | |
| 340 | // check that the name doesn't match a keyword |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 341 | if (is_java_keyword(arg->name.data)) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 342 | fprintf(stderr, "%s:%d parameter %d %s is named the same as a" |
| 343 | " Java or aidl keyword\n", |
| 344 | filename, m->name.lineno, index, arg->name.data); |
| 345 | err = 1; |
| 346 | } |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 347 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 348 | next: |
| 349 | index++; |
| 350 | arg = arg->next; |
| 351 | } |
| 352 | |
| 353 | return err; |
| 354 | } |
| 355 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 356 | int check_types(const char* filename, document_item_type* items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 357 | int err = 0; |
| 358 | while (items) { |
| 359 | // (nothing to check for USER_DATA_TYPE) |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 360 | if (items->item_type == INTERFACE_TYPE_BINDER) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 361 | map<string,method_type*> methodNames; |
| 362 | interface_type* c = (interface_type*)items; |
| 363 | |
| 364 | interface_item_type* member = c->interface_items; |
| 365 | while (member) { |
| 366 | if (member->item_type == METHOD_TYPE) { |
| 367 | method_type* m = (method_type*)member; |
| 368 | |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 369 | err |= check_method(filename, m); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 370 | |
| 371 | // prevent duplicate methods |
| 372 | if (methodNames.find(m->name.data) == methodNames.end()) { |
| 373 | methodNames[m->name.data] = m; |
| 374 | } else { |
| 375 | fprintf(stderr,"%s:%d attempt to redefine method %s,\n", |
| 376 | filename, m->name.lineno, m->name.data); |
| 377 | method_type* old = methodNames[m->name.data]; |
| 378 | fprintf(stderr, "%s:%d previously defined here.\n", |
| 379 | filename, old->name.lineno); |
| 380 | err = 1; |
| 381 | } |
| 382 | } |
| 383 | member = member->next; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | items = items->next; |
| 388 | } |
| 389 | return err; |
| 390 | } |
| 391 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 392 | void generate_dep_file(const JavaOptions& options, |
| 393 | const document_item_type* items, |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 394 | import_info* import_head) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 395 | /* we open the file in binary mode to ensure that the same output is |
| 396 | * generated on all platforms !! |
| 397 | */ |
| 398 | FILE* to = NULL; |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 399 | if (options.auto_dep_file_) { |
| 400 | string fileName = options.output_file_name_ + ".d"; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 401 | to = fopen(fileName.c_str(), "wb"); |
| 402 | } else { |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 403 | to = fopen(options.dep_file_name_.c_str(), "wb"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | if (to == NULL) { |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | const char* slash = "\\"; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 411 | import_info* import = import_head; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 412 | if (import == NULL) { |
| 413 | slash = ""; |
| 414 | } |
| 415 | |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 416 | if (items->item_type == INTERFACE_TYPE_BINDER) { |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 417 | fprintf(to, "%s: \\\n", options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 418 | } else { |
| 419 | // parcelable: there's no output file. |
| 420 | fprintf(to, " : \\\n"); |
| 421 | } |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 422 | fprintf(to, " %s %s\n", options.input_file_name_.c_str(), slash); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 423 | |
| 424 | while (import) { |
| 425 | if (import->next == NULL) { |
| 426 | slash = ""; |
| 427 | } |
| 428 | if (import->filename) { |
| 429 | fprintf(to, " %s %s\n", import->filename, slash); |
| 430 | } |
| 431 | import = import->next; |
| 432 | } |
| 433 | |
| 434 | fprintf(to, "\n"); |
| 435 | |
Ying Wang | 0e4861a | 2015-07-22 17:42:35 -0700 | [diff] [blame] | 436 | // Output "<input_aidl_file>: " so make won't fail if the input .aidl file |
| 437 | // has been deleted, moved or renamed in incremental build. |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 438 | fprintf(to, "%s :\n", options.input_file_name_.c_str()); |
Ying Wang | 0e4861a | 2015-07-22 17:42:35 -0700 | [diff] [blame] | 439 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 440 | // Output "<imported_file>: " so make won't fail if the imported file has |
| 441 | // been deleted, moved or renamed in incremental build. |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 442 | import = import_head; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 443 | while (import) { |
| 444 | if (import->filename) { |
| 445 | fprintf(to, "%s :\n", import->filename); |
| 446 | } |
| 447 | import = import->next; |
| 448 | } |
| 449 | |
| 450 | fclose(to); |
| 451 | } |
| 452 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 453 | string generate_outputFileName2(const JavaOptions& options, |
| 454 | const buffer_type& name, |
| 455 | const char* package) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 456 | string result; |
| 457 | |
| 458 | // create the path to the destination folder based on the |
| 459 | // interface package name |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 460 | result = options.output_base_folder_; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 461 | result += OS_PATH_SEPARATOR; |
| 462 | |
| 463 | string packageStr = package; |
| 464 | size_t len = packageStr.length(); |
| 465 | for (size_t i=0; i<len; i++) { |
| 466 | if (packageStr[i] == '.') { |
| 467 | packageStr[i] = OS_PATH_SEPARATOR; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | result += packageStr; |
| 472 | |
| 473 | // add the filename by replacing the .aidl extension to .java |
| 474 | const char* p = strchr(name.data, '.'); |
| 475 | len = p ? p-name.data : strlen(name.data); |
| 476 | |
| 477 | result += OS_PATH_SEPARATOR; |
| 478 | result.append(name.data, len); |
| 479 | result += ".java"; |
| 480 | |
| 481 | return result; |
| 482 | } |
| 483 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 484 | string generate_outputFileName(const JavaOptions& options, |
| 485 | const document_item_type* items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 486 | // items has already been checked to have only one interface. |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 487 | if (items->item_type == INTERFACE_TYPE_BINDER) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 488 | interface_type* type = (interface_type*)items; |
| 489 | |
| 490 | return generate_outputFileName2(options, type->name, type->package); |
| 491 | } else if (items->item_type == USER_DATA_TYPE) { |
| 492 | user_data_type* type = (user_data_type*)items; |
| 493 | return generate_outputFileName2(options, type->name, type->package); |
| 494 | } |
| 495 | |
| 496 | // I don't think we can come here, but safer than returning NULL. |
| 497 | string result; |
| 498 | return result; |
| 499 | } |
| 500 | |
| 501 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 502 | void check_outputFilePath(const string& path) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 503 | size_t len = path.length(); |
| 504 | for (size_t i=0; i<len ; i++) { |
| 505 | if (path[i] == OS_PATH_SEPARATOR) { |
| 506 | string p = path.substr(0, i); |
| 507 | if (access(path.data(), F_OK) != 0) { |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 508 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 509 | _mkdir(p.data()); |
| 510 | #else |
| 511 | mkdir(p.data(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); |
| 512 | #endif |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 519 | int parse_preprocessed_file(const string& filename) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 520 | int err; |
| 521 | |
| 522 | FILE* f = fopen(filename.c_str(), "rb"); |
| 523 | if (f == NULL) { |
| 524 | fprintf(stderr, "aidl: can't open preprocessed file: %s\n", |
| 525 | filename.c_str()); |
| 526 | return 1; |
| 527 | } |
| 528 | |
| 529 | int lineno = 1; |
| 530 | char line[1024]; |
| 531 | char type[1024]; |
| 532 | char fullname[1024]; |
| 533 | while (fgets(line, sizeof(line), f)) { |
| 534 | // skip comments and empty lines |
| 535 | if (!line[0] || strncmp(line, "//", 2) == 0) { |
| 536 | continue; |
| 537 | } |
| 538 | |
| 539 | sscanf(line, "%s %[^; \r\n\t];", type, fullname); |
| 540 | |
| 541 | char* packagename; |
| 542 | char* classname = rfind(fullname, '.'); |
| 543 | if (classname != NULL) { |
| 544 | *classname = '\0'; |
| 545 | classname++; |
| 546 | packagename = fullname; |
| 547 | } else { |
| 548 | classname = fullname; |
| 549 | packagename = NULL; |
| 550 | } |
| 551 | |
| 552 | //printf("%s:%d:...%s...%s...%s...\n", filename.c_str(), lineno, |
| 553 | // type, packagename, classname); |
| 554 | document_item_type* doc; |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 555 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 556 | if (0 == strcmp("parcelable", type)) { |
| 557 | user_data_type* parcl = (user_data_type*)malloc( |
| 558 | sizeof(user_data_type)); |
| 559 | memset(parcl, 0, sizeof(user_data_type)); |
| 560 | parcl->document_item.item_type = USER_DATA_TYPE; |
| 561 | parcl->keyword_token.lineno = lineno; |
| 562 | parcl->keyword_token.data = strdup(type); |
| 563 | parcl->package = packagename ? strdup(packagename) : NULL; |
| 564 | parcl->name.lineno = lineno; |
| 565 | parcl->name.data = strdup(classname); |
| 566 | parcl->semicolon_token.lineno = lineno; |
| 567 | parcl->semicolon_token.data = strdup(";"); |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 568 | parcl->parcelable = true; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 569 | doc = (document_item_type*)parcl; |
| 570 | } |
| 571 | else if (0 == strcmp("interface", type)) { |
| 572 | interface_type* iface = (interface_type*)malloc( |
| 573 | sizeof(interface_type)); |
| 574 | memset(iface, 0, sizeof(interface_type)); |
| 575 | iface->document_item.item_type = INTERFACE_TYPE_BINDER; |
| 576 | iface->interface_token.lineno = lineno; |
| 577 | iface->interface_token.data = strdup(type); |
| 578 | iface->package = packagename ? strdup(packagename) : NULL; |
| 579 | iface->name.lineno = lineno; |
| 580 | iface->name.data = strdup(classname); |
| 581 | iface->open_brace_token.lineno = lineno; |
| 582 | iface->open_brace_token.data = strdup("{"); |
| 583 | iface->close_brace_token.lineno = lineno; |
| 584 | iface->close_brace_token.data = strdup("}"); |
| 585 | doc = (document_item_type*)iface; |
| 586 | } |
| 587 | else { |
| 588 | fprintf(stderr, "%s:%d: bad type in line: %s\n", |
| 589 | filename.c_str(), lineno, line); |
Elliott Hughes | 5cd0607 | 2013-10-29 15:25:52 -0700 | [diff] [blame] | 590 | fclose(f); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 591 | return 1; |
| 592 | } |
| 593 | err = gather_types(filename.c_str(), doc); |
| 594 | lineno++; |
| 595 | } |
| 596 | |
| 597 | if (!feof(f)) { |
| 598 | fprintf(stderr, "%s:%d: error reading file, line to long.\n", |
| 599 | filename.c_str(), lineno); |
| 600 | return 1; |
| 601 | } |
| 602 | |
| 603 | fclose(f); |
| 604 | return 0; |
| 605 | } |
| 606 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 607 | int check_and_assign_method_ids(const char * filename, |
| 608 | interface_item_type* first_item) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 609 | // Check whether there are any methods with manually assigned id's and any that are not. |
| 610 | // Either all method id's must be manually assigned or all of them must not. |
| 611 | // Also, check for duplicates of user set id's and that the id's are within the proper bounds. |
| 612 | set<int> usedIds; |
| 613 | interface_item_type* item = first_item; |
| 614 | bool hasUnassignedIds = false; |
| 615 | bool hasAssignedIds = false; |
| 616 | while (item != NULL) { |
| 617 | if (item->item_type == METHOD_TYPE) { |
| 618 | method_type* method_item = (method_type*)item; |
| 619 | if (method_item->hasId) { |
| 620 | hasAssignedIds = true; |
| 621 | method_item->assigned_id = atoi(method_item->id.data); |
| 622 | // Ensure that the user set id is not duplicated. |
| 623 | if (usedIds.find(method_item->assigned_id) != usedIds.end()) { |
| 624 | // We found a duplicate id, so throw an error. |
| 625 | fprintf(stderr, |
| 626 | "%s:%d Found duplicate method id (%d) for method: %s\n", |
| 627 | filename, method_item->id.lineno, |
| 628 | method_item->assigned_id, method_item->name.data); |
| 629 | return 1; |
| 630 | } |
| 631 | // Ensure that the user set id is within the appropriate limits |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 632 | if (method_item->assigned_id < kMinUserSetMethodId || |
| 633 | method_item->assigned_id > kMaxUserSetMethodId) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 634 | fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n", |
| 635 | filename, method_item->id.lineno, |
| 636 | method_item->assigned_id, method_item->name.data); |
| 637 | fprintf(stderr, " Value for id must be between %d and %d inclusive.\n", |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 638 | kMinUserSetMethodId, kMaxUserSetMethodId); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 639 | return 1; |
| 640 | } |
| 641 | usedIds.insert(method_item->assigned_id); |
| 642 | } else { |
| 643 | hasUnassignedIds = true; |
| 644 | } |
| 645 | if (hasAssignedIds && hasUnassignedIds) { |
| 646 | fprintf(stderr, |
| 647 | "%s: You must either assign id's to all methods or to none of them.\n", |
| 648 | filename); |
| 649 | return 1; |
| 650 | } |
| 651 | } |
| 652 | item = item->next; |
| 653 | } |
| 654 | |
| 655 | // In the case that all methods have unassigned id's, set a unique id for them. |
| 656 | if (hasUnassignedIds) { |
| 657 | int newId = 0; |
| 658 | item = first_item; |
| 659 | while (item != NULL) { |
| 660 | if (item->item_type == METHOD_TYPE) { |
| 661 | method_type* method_item = (method_type*)item; |
| 662 | method_item->assigned_id = newId++; |
| 663 | } |
| 664 | item = item->next; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | // success |
| 669 | return 0; |
| 670 | } |
| 671 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 672 | int load_and_validate_aidl(const std::vector<std::string> preprocessed_files, |
| 673 | const std::vector<std::string> import_paths, |
| 674 | const std::string& input_file_name, |
| 675 | interface_type** returned_interface, |
| 676 | import_info** returned_imports) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 677 | int err = 0; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 678 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 679 | set_import_paths(import_paths); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 680 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 681 | register_base_types(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 682 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 683 | // import the preprocessed file |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 684 | for (const string& s : preprocessed_files) { |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 685 | err |= parse_preprocessed_file(s); |
| 686 | } |
| 687 | if (err != 0) { |
| 688 | return err; |
| 689 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 690 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 691 | // parse the input file |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 692 | Parser p{input_file_name}; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 693 | if (!p.OpenFileFromDisk() || !p.RunParser()) { |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 694 | return 1; |
| 695 | } |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 696 | document_item_type* parsed_doc = p.GetDocument(); |
| 697 | // We could in theory declare parcelables in the same file as the interface. |
| 698 | // In practice, those parcelables would have to have the same name as |
| 699 | // the interface, since this was originally written to support Java, with its |
| 700 | // packages and names that correspond to file system structure. |
| 701 | // Since we can't have two distinct classes with the same name and package, |
| 702 | // we can't actually declare parcelables in the same file. |
| 703 | if (parsed_doc == nullptr || |
| 704 | parsed_doc->item_type != INTERFACE_TYPE_BINDER || |
| 705 | parsed_doc->next != nullptr) { |
| 706 | cerr << "aidl expects exactly one interface per input file"; |
| 707 | err |= 1; |
| 708 | } |
| 709 | interface_type* interface = (interface_type*)parsed_doc; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 710 | err |= check_filename(input_file_name.c_str(), |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 711 | interface->package, &interface->name); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 712 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 713 | // parse the imports of the input file |
| 714 | for (import_info* import = p.GetImports(); import; import = import->next) { |
| 715 | if (NAMES.Find(import->neededClass) != NULL) { |
| 716 | continue; |
| 717 | } |
| 718 | import->filename = find_import_file(import->neededClass); |
| 719 | if (!import->filename) { |
| 720 | cerr << import->from << ":" << import->statement.lineno |
| 721 | << ": couldn't find import for class " |
| 722 | << import->neededClass << endl; |
| 723 | err |= 1; |
| 724 | continue; |
| 725 | } |
| 726 | Parser p{import->filename}; |
| 727 | if (!p.OpenFileFromDisk() || !p.RunParser() || p.GetDocument() == nullptr) { |
| 728 | cerr << "error while parsing import for class " |
| 729 | << import->neededClass << endl; |
| 730 | err |= 1; |
| 731 | continue; |
| 732 | } |
| 733 | import->doc = p.GetDocument(); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 734 | err |= check_filenames(import->filename, import->doc); |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 735 | } |
| 736 | if (err != 0) { |
| 737 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 738 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 739 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 740 | // gather the types that have been declared |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 741 | err |= gather_types(input_file_name.c_str(), parsed_doc); |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 742 | for (import_info* import = p.GetImports(); import; import = import->next) { |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 743 | err |= gather_types(import->filename, import->doc); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 744 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 745 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 746 | // check the referenced types in parsed_doc to make sure we've imported them |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 747 | err |= check_types(input_file_name.c_str(), parsed_doc); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 748 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 749 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 750 | // assign method ids and validate. |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 751 | err |= check_and_assign_method_ids(input_file_name.c_str(), |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 752 | interface->interface_items); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 753 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 754 | // after this, there shouldn't be any more errors because of the |
| 755 | // input. |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 756 | if (err != 0) { |
| 757 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 758 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 759 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 760 | *returned_interface = interface; |
| 761 | *returned_imports = p.GetImports(); |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | } // namespace |
| 766 | |
| 767 | int compile_aidl_to_cpp(const CppOptions& options) { |
| 768 | interface_type* interface = nullptr; |
| 769 | import_info* imports = nullptr; |
| 770 | int err = load_and_validate_aidl(std::vector<std::string>{}, |
| 771 | options.ImportPaths(), |
| 772 | options.InputFileName(), |
| 773 | &interface, |
| 774 | &imports); |
| 775 | if (err != 0) { |
| 776 | return err; |
| 777 | } |
| 778 | |
| 779 | // TODO(wiley) b/23600457 generate a dependency file if requested with -b |
| 780 | |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 781 | return (GenerateCpp(options, interface)) ? 0 : 1; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | int compile_aidl_to_java(const JavaOptions& options) { |
| 785 | interface_type* interface = nullptr; |
| 786 | import_info* imports = nullptr; |
| 787 | int err = load_and_validate_aidl(options.preprocessed_files_, |
| 788 | options.import_paths_, |
| 789 | options.input_file_name_, |
| 790 | &interface, |
| 791 | &imports); |
| 792 | if (err != 0) { |
| 793 | return err; |
| 794 | } |
| 795 | document_item_type* parsed_doc = (document_item_type*)interface; |
| 796 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 797 | string output_file_name = options.output_file_name_; |
| 798 | // if needed, generate the output file name from the base folder |
| 799 | if (output_file_name.length() == 0 && |
| 800 | options.output_base_folder_.length() > 0) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 801 | output_file_name = generate_outputFileName(options, parsed_doc); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 802 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 803 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 804 | // if we were asked to, generate a make dependency file |
| 805 | // unless it's a parcelable *and* it's supposed to fail on parcelable |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 806 | if (options.auto_dep_file_ || options.dep_file_name_ != "") { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 807 | // make sure the folders of the output file all exists |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 808 | check_outputFilePath(output_file_name); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 809 | generate_dep_file(options, parsed_doc, imports); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 810 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 811 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 812 | // make sure the folders of the output file all exists |
| 813 | check_outputFilePath(output_file_name); |
| 814 | |
| 815 | err = generate_java(output_file_name, options.input_file_name_.c_str(), |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 816 | interface); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 817 | |
| 818 | return err; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 821 | int preprocess_aidl(const JavaOptions& options) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 822 | vector<string> lines; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 823 | |
| 824 | // read files |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 825 | int N = options.files_to_preprocess_.size(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 826 | for (int i=0; i<N; i++) { |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 827 | Parser p{options.files_to_preprocess_[i]}; |
Casey Dahlin | 9980181 | 2015-09-15 18:19:25 -0700 | [diff] [blame] | 828 | if (!p.OpenFileFromDisk()) |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 829 | return 1; |
Casey Dahlin | 9980181 | 2015-09-15 18:19:25 -0700 | [diff] [blame] | 830 | if (!p.RunParser()) |
| 831 | return 1; |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 832 | document_item_type* doc = p.GetDocument(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 833 | string line; |
| 834 | if (doc->item_type == USER_DATA_TYPE) { |
| 835 | user_data_type* parcelable = (user_data_type*)doc; |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 836 | if (parcelable->parcelable) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 837 | line = "parcelable "; |
| 838 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 839 | if (parcelable->package) { |
| 840 | line += parcelable->package; |
| 841 | line += '.'; |
| 842 | } |
| 843 | line += parcelable->name.data; |
| 844 | } else { |
| 845 | line = "interface "; |
| 846 | interface_type* iface = (interface_type*)doc; |
| 847 | if (iface->package) { |
| 848 | line += iface->package; |
| 849 | line += '.'; |
| 850 | } |
| 851 | line += iface->name.data; |
| 852 | } |
| 853 | line += ";\n"; |
| 854 | lines.push_back(line); |
| 855 | } |
| 856 | |
| 857 | // write preprocessed file |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 858 | int fd = open( options.output_file_name_.c_str(), |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 859 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 860 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 861 | _S_IREAD|_S_IWRITE); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 862 | #else |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 863 | S_IRUSR|S_IWUSR|S_IRGRP); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 864 | #endif |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 865 | if (fd == -1) { |
| 866 | fprintf(stderr, "aidl: could not open file for write: %s\n", |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 867 | options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 868 | return 1; |
| 869 | } |
| 870 | |
| 871 | N = lines.size(); |
| 872 | for (int i=0; i<N; i++) { |
| 873 | const string& s = lines[i]; |
| 874 | int len = s.length(); |
| 875 | if (len != write(fd, s.c_str(), len)) { |
| 876 | fprintf(stderr, "aidl: error writing to file %s\n", |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 877 | options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 878 | close(fd); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 879 | unlink(options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 880 | return 1; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | close(fd); |
| 885 | return 0; |
| 886 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 887 | |
| 888 | } // namespace android |
| 889 | } // namespace aidl |