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 | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 44 | #include "type_cpp.h" |
Christopher Wiley | 775fa1f | 2015-09-22 15:00:12 -0700 | [diff] [blame] | 45 | #include "type_java.h" |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 46 | #include "type_namespace.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 48 | #ifndef O_BINARY |
| 49 | # define O_BINARY 0 |
| 50 | #endif |
| 51 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 52 | using std::cerr; |
| 53 | using std::endl; |
Christopher Wiley | 9f4c7ae | 2015-08-24 14:07:32 -0700 | [diff] [blame] | 54 | using std::map; |
| 55 | using std::set; |
| 56 | using std::string; |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 57 | using std::unique_ptr; |
Christopher Wiley | 9f4c7ae | 2015-08-24 14:07:32 -0700 | [diff] [blame] | 58 | using std::vector; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 59 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 60 | namespace android { |
| 61 | namespace aidl { |
| 62 | namespace { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 63 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 64 | // The following are gotten as the offset from the allowable id's between |
| 65 | // android.os.IBinder.FIRST_CALL_TRANSACTION=1 and |
| 66 | // android.os.IBinder.LAST_CALL_TRANSACTION=16777215 |
| 67 | const int kMinUserSetMethodId = 0; |
| 68 | const int kMaxUserSetMethodId = 16777214; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 69 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 70 | int check_filename(const char* filename, |
| 71 | const char* package, |
| 72 | buffer_type* name) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 73 | const char* p; |
| 74 | string expected; |
| 75 | string fn; |
| 76 | size_t len; |
| 77 | char cwd[MAXPATHLEN]; |
| 78 | bool valid = false; |
| 79 | |
Elliott Hughes | ce310da | 2015-07-29 08:44:17 -0700 | [diff] [blame] | 80 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 81 | 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 | |
| 96 | if (package) { |
| 97 | 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 | |
| 108 | p = strchr(name->data, '.'); |
| 109 | len = p ? p-name->data : strlen(name->data); |
| 110 | expected.append(name->data, len); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 112 | expected += ".aidl"; |
| 113 | |
| 114 | len = fn.length(); |
| 115 | valid = (len >= expected.length()); |
| 116 | |
| 117 | if (valid) { |
| 118 | p = fn.c_str() + (len - expected.length()); |
| 119 | |
Elliott Hughes | ce310da | 2015-07-29 08:44:17 -0700 | [diff] [blame] | 120 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 121 | if (OS_PATH_SEPARATOR != '/') { |
| 122 | // Input filename under cygwin most likely has / separators |
| 123 | // whereas the expected string uses \\ separators. Adjust |
| 124 | // them accordingly. |
| 125 | for (char *c = const_cast<char *>(p); *c; ++c) { |
| 126 | if (*c == '/') *c = OS_PATH_SEPARATOR; |
| 127 | } |
| 128 | } |
| 129 | #endif |
| 130 | |
Yabin Cui | 482eefb | 2014-11-10 15:01:43 -0800 | [diff] [blame] | 131 | // aidl assumes case-insensitivity on Mac Os and Windows. |
| 132 | #if defined(__linux__) |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 133 | valid = (expected == p); |
| 134 | #else |
| 135 | valid = !strcasecmp(expected.c_str(), p); |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | if (!valid) { |
| 140 | fprintf(stderr, "%s:%d interface %s should be declared in a file" |
| 141 | " called %s.\n", |
| 142 | filename, name->lineno, name->data, expected.c_str()); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 149 | int check_filenames(const char* filename, document_item_type* items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 150 | int err = 0; |
| 151 | while (items) { |
| 152 | if (items->item_type == USER_DATA_TYPE) { |
| 153 | user_data_type* p = (user_data_type*)items; |
| 154 | err |= check_filename(filename, p->package, &p->name); |
| 155 | } |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 156 | else if (items->item_type == INTERFACE_TYPE_BINDER) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 157 | interface_type* c = (interface_type*)items; |
| 158 | err |= check_filename(filename, c->package, &c->name); |
| 159 | } |
| 160 | else { |
| 161 | fprintf(stderr, "aidl: internal error unkown document type %d.\n", |
| 162 | items->item_type); |
| 163 | return 1; |
| 164 | } |
| 165 | items = items->next; |
| 166 | } |
| 167 | return err; |
| 168 | } |
| 169 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 170 | char* rfind(char* str, char c) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 171 | char* p = str + strlen(str) - 1; |
| 172 | while (p >= str) { |
| 173 | if (*p == c) { |
| 174 | return p; |
| 175 | } |
| 176 | p--; |
| 177 | } |
| 178 | return NULL; |
| 179 | } |
| 180 | |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 181 | bool gather_types(const char* raw_filename, |
| 182 | document_item_type* all_items, |
| 183 | TypeNamespace* types) { |
| 184 | bool success = true; |
| 185 | if (raw_filename == nullptr) |
| 186 | raw_filename = ""; |
| 187 | const std::string filename{raw_filename}; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 188 | |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 189 | for (document_item_type* item = all_items; item; item = item->next) { |
| 190 | if (item->item_type == USER_DATA_TYPE) { |
| 191 | user_data_type* p = (user_data_type*)item; |
| 192 | success &= types->AddParcelableType(p, filename); |
| 193 | } else if (item->item_type == INTERFACE_TYPE_BINDER) { |
| 194 | interface_type* c = (interface_type*)item; |
| 195 | success &= types->AddBinderType(c, filename); |
| 196 | } else { |
| 197 | LOG(FATAL) << "internal error"; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 198 | } |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 199 | } |
| 200 | return success; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 203 | int check_types(const string& filename, |
| 204 | interface_type* c, |
| 205 | TypeNamespace* types) { |
| 206 | int err = 0; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame^] | 207 | set<string> method_names; |
| 208 | for (const auto& m : *c->methods) { |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 209 | if (!types->AddContainerType(m->type->type.data) || |
| 210 | !types->IsValidReturnType(*m->type, filename)) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 211 | err = 1; // return type is invalid |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | int index = 1; |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 215 | for (const std::unique_ptr<AidlArgument>& arg : *m->args) { |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 216 | if (!types->AddContainerType(arg->GetType().type.data) || |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 217 | !types->IsValidArg(*arg, index, filename)) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 218 | err = 1; |
| 219 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 222 | // prevent duplicate methods |
| 223 | if (method_names.find(m->name.data) == method_names.end()) { |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame^] | 224 | method_names.insert(m->name.data); |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 225 | } else { |
| 226 | cerr << filename << ":" << m->name.lineno |
| 227 | << " attempt to redefine method " << m->name.data << "," << endl |
| 228 | << filename << ":" << m->name.lineno |
| 229 | << " previously defined here." << endl; |
| 230 | err = 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 231 | } |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 232 | } |
| 233 | return err; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 236 | void generate_dep_file(const JavaOptions& options, |
| 237 | const document_item_type* items, |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 238 | import_info* import_head) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 239 | /* we open the file in binary mode to ensure that the same output is |
| 240 | * generated on all platforms !! |
| 241 | */ |
| 242 | FILE* to = NULL; |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 243 | if (options.auto_dep_file_) { |
| 244 | string fileName = options.output_file_name_ + ".d"; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 245 | to = fopen(fileName.c_str(), "wb"); |
| 246 | } else { |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 247 | to = fopen(options.dep_file_name_.c_str(), "wb"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | if (to == NULL) { |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | const char* slash = "\\"; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 255 | import_info* import = import_head; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 256 | if (import == NULL) { |
| 257 | slash = ""; |
| 258 | } |
| 259 | |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 260 | if (items->item_type == INTERFACE_TYPE_BINDER) { |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 261 | fprintf(to, "%s: \\\n", options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 262 | } else { |
| 263 | // parcelable: there's no output file. |
| 264 | fprintf(to, " : \\\n"); |
| 265 | } |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 266 | fprintf(to, " %s %s\n", options.input_file_name_.c_str(), slash); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 267 | |
| 268 | while (import) { |
| 269 | if (import->next == NULL) { |
| 270 | slash = ""; |
| 271 | } |
| 272 | if (import->filename) { |
| 273 | fprintf(to, " %s %s\n", import->filename, slash); |
| 274 | } |
| 275 | import = import->next; |
| 276 | } |
| 277 | |
| 278 | fprintf(to, "\n"); |
| 279 | |
Ying Wang | 0e4861a | 2015-07-22 17:42:35 -0700 | [diff] [blame] | 280 | // Output "<input_aidl_file>: " so make won't fail if the input .aidl file |
| 281 | // has been deleted, moved or renamed in incremental build. |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 282 | fprintf(to, "%s :\n", options.input_file_name_.c_str()); |
Ying Wang | 0e4861a | 2015-07-22 17:42:35 -0700 | [diff] [blame] | 283 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 284 | // Output "<imported_file>: " so make won't fail if the imported file has |
| 285 | // been deleted, moved or renamed in incremental build. |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 286 | import = import_head; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 287 | while (import) { |
| 288 | if (import->filename) { |
| 289 | fprintf(to, "%s :\n", import->filename); |
| 290 | } |
| 291 | import = import->next; |
| 292 | } |
| 293 | |
| 294 | fclose(to); |
| 295 | } |
| 296 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 297 | string generate_outputFileName2(const JavaOptions& options, |
| 298 | const buffer_type& name, |
| 299 | const char* package) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 300 | string result; |
| 301 | |
| 302 | // create the path to the destination folder based on the |
| 303 | // interface package name |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 304 | result = options.output_base_folder_; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 305 | result += OS_PATH_SEPARATOR; |
| 306 | |
| 307 | string packageStr = package; |
| 308 | size_t len = packageStr.length(); |
| 309 | for (size_t i=0; i<len; i++) { |
| 310 | if (packageStr[i] == '.') { |
| 311 | packageStr[i] = OS_PATH_SEPARATOR; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | result += packageStr; |
| 316 | |
| 317 | // add the filename by replacing the .aidl extension to .java |
| 318 | const char* p = strchr(name.data, '.'); |
| 319 | len = p ? p-name.data : strlen(name.data); |
| 320 | |
| 321 | result += OS_PATH_SEPARATOR; |
| 322 | result.append(name.data, len); |
| 323 | result += ".java"; |
| 324 | |
| 325 | return result; |
| 326 | } |
| 327 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 328 | string generate_outputFileName(const JavaOptions& options, |
| 329 | const document_item_type* items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 330 | // items has already been checked to have only one interface. |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 331 | if (items->item_type == INTERFACE_TYPE_BINDER) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 332 | interface_type* type = (interface_type*)items; |
| 333 | |
| 334 | return generate_outputFileName2(options, type->name, type->package); |
| 335 | } else if (items->item_type == USER_DATA_TYPE) { |
| 336 | user_data_type* type = (user_data_type*)items; |
| 337 | return generate_outputFileName2(options, type->name, type->package); |
| 338 | } |
| 339 | |
| 340 | // I don't think we can come here, but safer than returning NULL. |
| 341 | string result; |
| 342 | return result; |
| 343 | } |
| 344 | |
| 345 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 346 | void check_outputFilePath(const string& path) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 347 | size_t len = path.length(); |
| 348 | for (size_t i=0; i<len ; i++) { |
| 349 | if (path[i] == OS_PATH_SEPARATOR) { |
| 350 | string p = path.substr(0, i); |
| 351 | if (access(path.data(), F_OK) != 0) { |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 352 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 353 | _mkdir(p.data()); |
| 354 | #else |
| 355 | mkdir(p.data(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); |
| 356 | #endif |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 363 | int parse_preprocessed_file(const string& filename, TypeNamespace* types) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 364 | FILE* f = fopen(filename.c_str(), "rb"); |
| 365 | if (f == NULL) { |
| 366 | fprintf(stderr, "aidl: can't open preprocessed file: %s\n", |
| 367 | filename.c_str()); |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | int lineno = 1; |
| 372 | char line[1024]; |
| 373 | char type[1024]; |
| 374 | char fullname[1024]; |
| 375 | while (fgets(line, sizeof(line), f)) { |
| 376 | // skip comments and empty lines |
| 377 | if (!line[0] || strncmp(line, "//", 2) == 0) { |
| 378 | continue; |
| 379 | } |
| 380 | |
| 381 | sscanf(line, "%s %[^; \r\n\t];", type, fullname); |
| 382 | |
| 383 | char* packagename; |
| 384 | char* classname = rfind(fullname, '.'); |
| 385 | if (classname != NULL) { |
| 386 | *classname = '\0'; |
| 387 | classname++; |
| 388 | packagename = fullname; |
| 389 | } else { |
| 390 | classname = fullname; |
| 391 | packagename = NULL; |
| 392 | } |
| 393 | |
| 394 | //printf("%s:%d:...%s...%s...%s...\n", filename.c_str(), lineno, |
| 395 | // type, packagename, classname); |
| 396 | document_item_type* doc; |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 397 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 398 | if (0 == strcmp("parcelable", type)) { |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 399 | user_data_type* parcl = new user_data_type(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 400 | memset(parcl, 0, sizeof(user_data_type)); |
| 401 | parcl->document_item.item_type = USER_DATA_TYPE; |
| 402 | parcl->keyword_token.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 403 | parcl->keyword_token.data = cpp_strdup(type); |
| 404 | parcl->package = packagename ? cpp_strdup(packagename) : NULL; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 405 | parcl->name.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 406 | parcl->name.data = cpp_strdup(classname); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 407 | parcl->semicolon_token.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 408 | parcl->semicolon_token.data = cpp_strdup(";"); |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 409 | parcl->parcelable = true; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 410 | doc = (document_item_type*)parcl; |
| 411 | } |
| 412 | else if (0 == strcmp("interface", type)) { |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 413 | interface_type* iface = new interface_type(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 414 | memset(iface, 0, sizeof(interface_type)); |
| 415 | iface->document_item.item_type = INTERFACE_TYPE_BINDER; |
| 416 | iface->interface_token.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 417 | iface->interface_token.data = cpp_strdup(type); |
| 418 | iface->package = packagename ? cpp_strdup(packagename) : NULL; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 419 | iface->name.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 420 | iface->name.data = cpp_strdup(classname); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 421 | iface->open_brace_token.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 422 | iface->open_brace_token.data = cpp_strdup("{"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 423 | iface->close_brace_token.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 424 | iface->close_brace_token.data = cpp_strdup("}"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 425 | doc = (document_item_type*)iface; |
| 426 | } |
| 427 | else { |
| 428 | fprintf(stderr, "%s:%d: bad type in line: %s\n", |
| 429 | filename.c_str(), lineno, line); |
Elliott Hughes | 5cd0607 | 2013-10-29 15:25:52 -0700 | [diff] [blame] | 430 | fclose(f); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 431 | return 1; |
| 432 | } |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 433 | if (!gather_types(filename.c_str(), doc, types)) { |
| 434 | fprintf(stderr, "Failed to gather types for preprocessed aidl.\n"); |
| 435 | fclose(f); |
| 436 | return 1; |
| 437 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 438 | lineno++; |
| 439 | } |
| 440 | |
| 441 | if (!feof(f)) { |
| 442 | fprintf(stderr, "%s:%d: error reading file, line to long.\n", |
| 443 | filename.c_str(), lineno); |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | fclose(f); |
| 448 | return 0; |
| 449 | } |
| 450 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 451 | int check_and_assign_method_ids(const char * filename, |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame^] | 452 | const std::vector<std::unique_ptr<AidlMethod>>& items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 453 | // Check whether there are any methods with manually assigned id's and any that are not. |
| 454 | // Either all method id's must be manually assigned or all of them must not. |
| 455 | // Also, check for duplicates of user set id's and that the id's are within the proper bounds. |
| 456 | set<int> usedIds; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 457 | bool hasUnassignedIds = false; |
| 458 | bool hasAssignedIds = false; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame^] | 459 | for (const auto& item : items) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 460 | if (item->hasId) { |
| 461 | hasAssignedIds = true; |
| 462 | item->assigned_id = atoi(item->id.data); |
| 463 | // Ensure that the user set id is not duplicated. |
| 464 | if (usedIds.find(item->assigned_id) != usedIds.end()) { |
| 465 | // We found a duplicate id, so throw an error. |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 466 | fprintf(stderr, |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 467 | "%s:%d Found duplicate method id (%d) for method: %s\n", |
| 468 | filename, item->id.lineno, |
| 469 | item->assigned_id, item->name.data); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 470 | return 1; |
| 471 | } |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 472 | // Ensure that the user set id is within the appropriate limits |
| 473 | if (item->assigned_id < kMinUserSetMethodId || |
| 474 | item->assigned_id > kMaxUserSetMethodId) { |
| 475 | fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n", |
| 476 | filename, item->id.lineno, |
| 477 | item->assigned_id, item->name.data); |
| 478 | fprintf(stderr, " Value for id must be between %d and %d inclusive.\n", |
| 479 | kMinUserSetMethodId, kMaxUserSetMethodId); |
| 480 | return 1; |
| 481 | } |
| 482 | usedIds.insert(item->assigned_id); |
| 483 | } else { |
| 484 | hasUnassignedIds = true; |
| 485 | } |
| 486 | if (hasAssignedIds && hasUnassignedIds) { |
| 487 | fprintf(stderr, |
| 488 | "%s: You must either assign id's to all methods or to none of them.\n", |
| 489 | filename); |
| 490 | return 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 491 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // In the case that all methods have unassigned id's, set a unique id for them. |
| 495 | if (hasUnassignedIds) { |
| 496 | int newId = 0; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame^] | 497 | for (const auto& item : items) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 498 | item->assigned_id = newId++; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | // success |
| 503 | return 0; |
| 504 | } |
| 505 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 506 | int load_and_validate_aidl(const std::vector<std::string> preprocessed_files, |
| 507 | const std::vector<std::string> import_paths, |
| 508 | const std::string& input_file_name, |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 509 | TypeNamespace* types, |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 510 | interface_type** returned_interface, |
| 511 | import_info** returned_imports) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 512 | int err = 0; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 513 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 514 | set_import_paths(import_paths); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 515 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 516 | // import the preprocessed file |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 517 | for (const string& s : preprocessed_files) { |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 518 | err |= parse_preprocessed_file(s, types); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 519 | } |
| 520 | if (err != 0) { |
| 521 | return err; |
| 522 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 523 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 524 | // parse the input file |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 525 | Parser p{input_file_name}; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 526 | if (!p.OpenFileFromDisk() || !p.RunParser()) { |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 527 | return 1; |
| 528 | } |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 529 | document_item_type* parsed_doc = p.GetDocument(); |
| 530 | // We could in theory declare parcelables in the same file as the interface. |
| 531 | // In practice, those parcelables would have to have the same name as |
| 532 | // the interface, since this was originally written to support Java, with its |
| 533 | // packages and names that correspond to file system structure. |
| 534 | // Since we can't have two distinct classes with the same name and package, |
| 535 | // we can't actually declare parcelables in the same file. |
| 536 | if (parsed_doc == nullptr || |
| 537 | parsed_doc->item_type != INTERFACE_TYPE_BINDER || |
| 538 | parsed_doc->next != nullptr) { |
| 539 | cerr << "aidl expects exactly one interface per input file"; |
Christopher Wiley | a2f516d | 2015-09-24 10:12:31 -0700 | [diff] [blame] | 540 | return 1; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 541 | } |
| 542 | interface_type* interface = (interface_type*)parsed_doc; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 543 | err |= check_filename(input_file_name.c_str(), |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 544 | interface->package, &interface->name); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 545 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 546 | // parse the imports of the input file |
| 547 | for (import_info* import = p.GetImports(); import; import = import->next) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 548 | if (types->HasType(import->neededClass)) { |
| 549 | // There are places in the Android tree where an import doesn't resolve, |
| 550 | // but we'll pick the type up through the preprocessed types. |
| 551 | // This seems like an error, but legacy support demands we support it... |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 552 | continue; |
| 553 | } |
| 554 | import->filename = find_import_file(import->neededClass); |
| 555 | if (!import->filename) { |
| 556 | cerr << import->from << ":" << import->statement.lineno |
| 557 | << ": couldn't find import for class " |
| 558 | << import->neededClass << endl; |
| 559 | err |= 1; |
| 560 | continue; |
| 561 | } |
| 562 | Parser p{import->filename}; |
| 563 | if (!p.OpenFileFromDisk() || !p.RunParser() || p.GetDocument() == nullptr) { |
| 564 | cerr << "error while parsing import for class " |
| 565 | << import->neededClass << endl; |
| 566 | err |= 1; |
| 567 | continue; |
| 568 | } |
| 569 | import->doc = p.GetDocument(); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 570 | err |= check_filenames(import->filename, import->doc); |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 571 | } |
| 572 | if (err != 0) { |
| 573 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 574 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 575 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 576 | // gather the types that have been declared |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 577 | if (!gather_types(input_file_name.c_str(), parsed_doc, types)) { |
| 578 | err |= 1; |
| 579 | } |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 580 | for (import_info* import = p.GetImports(); import; import = import->next) { |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 581 | if (!gather_types(import->filename, import->doc, types)) { |
| 582 | err |= 1; |
| 583 | } |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 584 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 585 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 586 | // check the referenced types in parsed_doc to make sure we've imported them |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 587 | err |= check_types(input_file_name, interface, types); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 588 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 589 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 590 | // assign method ids and validate. |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 591 | err |= check_and_assign_method_ids(input_file_name.c_str(), |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame^] | 592 | *interface->methods); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 593 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 594 | // after this, there shouldn't be any more errors because of the |
| 595 | // input. |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 596 | if (err != 0) { |
| 597 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 598 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 599 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 600 | *returned_interface = interface; |
| 601 | *returned_imports = p.GetImports(); |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | } // namespace |
| 606 | |
| 607 | int compile_aidl_to_cpp(const CppOptions& options) { |
| 608 | interface_type* interface = nullptr; |
| 609 | import_info* imports = nullptr; |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 610 | unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace()); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 611 | int err = load_and_validate_aidl(std::vector<std::string>{}, |
| 612 | options.ImportPaths(), |
| 613 | options.InputFileName(), |
Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 614 | types.get(), |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 615 | &interface, |
| 616 | &imports); |
| 617 | if (err != 0) { |
| 618 | return err; |
| 619 | } |
| 620 | |
| 621 | // TODO(wiley) b/23600457 generate a dependency file if requested with -b |
| 622 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 623 | return (cpp::GenerateCpp(options, *types, *interface)) ? 0 : 1; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | int compile_aidl_to_java(const JavaOptions& options) { |
| 627 | interface_type* interface = nullptr; |
| 628 | import_info* imports = nullptr; |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 629 | unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace()); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 630 | int err = load_and_validate_aidl(options.preprocessed_files_, |
| 631 | options.import_paths_, |
| 632 | options.input_file_name_, |
Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 633 | types.get(), |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 634 | &interface, |
| 635 | &imports); |
| 636 | if (err != 0) { |
| 637 | return err; |
| 638 | } |
| 639 | document_item_type* parsed_doc = (document_item_type*)interface; |
| 640 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 641 | string output_file_name = options.output_file_name_; |
| 642 | // if needed, generate the output file name from the base folder |
| 643 | if (output_file_name.length() == 0 && |
| 644 | options.output_base_folder_.length() > 0) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 645 | output_file_name = generate_outputFileName(options, parsed_doc); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 646 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 647 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 648 | // if we were asked to, generate a make dependency file |
| 649 | // 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] | 650 | if (options.auto_dep_file_ || options.dep_file_name_ != "") { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 651 | // make sure the folders of the output file all exists |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 652 | check_outputFilePath(output_file_name); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 653 | generate_dep_file(options, parsed_doc, imports); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 654 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 655 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 656 | // make sure the folders of the output file all exists |
| 657 | check_outputFilePath(output_file_name); |
| 658 | |
| 659 | err = generate_java(output_file_name, options.input_file_name_.c_str(), |
Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 660 | interface, types.get()); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 661 | |
| 662 | return err; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 665 | int preprocess_aidl(const JavaOptions& options) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 666 | vector<string> lines; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 667 | |
| 668 | // read files |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 669 | int N = options.files_to_preprocess_.size(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 670 | for (int i=0; i<N; i++) { |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 671 | Parser p{options.files_to_preprocess_[i]}; |
Casey Dahlin | 9980181 | 2015-09-15 18:19:25 -0700 | [diff] [blame] | 672 | if (!p.OpenFileFromDisk()) |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 673 | return 1; |
Casey Dahlin | 9980181 | 2015-09-15 18:19:25 -0700 | [diff] [blame] | 674 | if (!p.RunParser()) |
| 675 | return 1; |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 676 | document_item_type* doc = p.GetDocument(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 677 | string line; |
| 678 | if (doc->item_type == USER_DATA_TYPE) { |
| 679 | user_data_type* parcelable = (user_data_type*)doc; |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 680 | if (parcelable->parcelable) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 681 | line = "parcelable "; |
| 682 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 683 | if (parcelable->package) { |
| 684 | line += parcelable->package; |
| 685 | line += '.'; |
| 686 | } |
| 687 | line += parcelable->name.data; |
| 688 | } else { |
| 689 | line = "interface "; |
| 690 | interface_type* iface = (interface_type*)doc; |
| 691 | if (iface->package) { |
| 692 | line += iface->package; |
| 693 | line += '.'; |
| 694 | } |
| 695 | line += iface->name.data; |
| 696 | } |
| 697 | line += ";\n"; |
| 698 | lines.push_back(line); |
| 699 | } |
| 700 | |
| 701 | // write preprocessed file |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 702 | int fd = open( options.output_file_name_.c_str(), |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 703 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 704 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 705 | _S_IREAD|_S_IWRITE); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 706 | #else |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 707 | S_IRUSR|S_IWUSR|S_IRGRP); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 708 | #endif |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 709 | if (fd == -1) { |
| 710 | fprintf(stderr, "aidl: could not open file for write: %s\n", |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 711 | options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 712 | return 1; |
| 713 | } |
| 714 | |
| 715 | N = lines.size(); |
| 716 | for (int i=0; i<N; i++) { |
| 717 | const string& s = lines[i]; |
| 718 | int len = s.length(); |
| 719 | if (len != write(fd, s.c_str(), len)) { |
| 720 | fprintf(stderr, "aidl: error writing to file %s\n", |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 721 | options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 722 | close(fd); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 723 | unlink(options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 724 | return 1; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | close(fd); |
| 729 | return 0; |
| 730 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 731 | |
| 732 | } // namespace android |
| 733 | } // namespace aidl |