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 | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 207 | |
| 208 | // Has to be a pointer due to deleting copy constructor. No idea why. |
| 209 | map<string, const AidlMethod*> method_names; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 210 | for (const auto& m : *c->methods) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 211 | if (!types->AddContainerType(m->GetType().GetName()) || |
| 212 | !types->IsValidReturnType(m->GetType(), filename)) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 213 | err = 1; // return type is invalid |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | int index = 1; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 217 | for (const auto& arg : m->GetArguments()) { |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 218 | if (!types->AddContainerType(arg->GetType().GetName()) || |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 219 | !types->IsValidArg(*arg, index, filename)) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 220 | err = 1; |
| 221 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 224 | auto it = method_names.find(m->GetName()); |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 225 | // prevent duplicate methods |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 226 | if (it == method_names.end()) { |
| 227 | method_names[m->GetName()] = m.get(); |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 228 | } else { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 229 | cerr << filename << ":" << m->GetLine() |
| 230 | << " attempt to redefine method " << m->GetName() << "," << endl |
| 231 | << filename << ":" << it->second->GetLine() |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 232 | << " previously defined here." << endl; |
| 233 | err = 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 234 | } |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 235 | } |
| 236 | return err; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 239 | void generate_dep_file(const JavaOptions& options, |
| 240 | const document_item_type* items, |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 241 | import_info* import_head) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 242 | /* we open the file in binary mode to ensure that the same output is |
| 243 | * generated on all platforms !! |
| 244 | */ |
| 245 | FILE* to = NULL; |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 246 | if (options.auto_dep_file_) { |
| 247 | string fileName = options.output_file_name_ + ".d"; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 248 | to = fopen(fileName.c_str(), "wb"); |
| 249 | } else { |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 250 | to = fopen(options.dep_file_name_.c_str(), "wb"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | if (to == NULL) { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | const char* slash = "\\"; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 258 | import_info* import = import_head; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 259 | if (import == NULL) { |
| 260 | slash = ""; |
| 261 | } |
| 262 | |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 263 | if (items->item_type == INTERFACE_TYPE_BINDER) { |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 264 | fprintf(to, "%s: \\\n", options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 265 | } else { |
| 266 | // parcelable: there's no output file. |
| 267 | fprintf(to, " : \\\n"); |
| 268 | } |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 269 | fprintf(to, " %s %s\n", options.input_file_name_.c_str(), slash); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 270 | |
| 271 | while (import) { |
| 272 | if (import->next == NULL) { |
| 273 | slash = ""; |
| 274 | } |
| 275 | if (import->filename) { |
| 276 | fprintf(to, " %s %s\n", import->filename, slash); |
| 277 | } |
| 278 | import = import->next; |
| 279 | } |
| 280 | |
| 281 | fprintf(to, "\n"); |
| 282 | |
Ying Wang | 0e4861a | 2015-07-22 17:42:35 -0700 | [diff] [blame] | 283 | // Output "<input_aidl_file>: " so make won't fail if the input .aidl file |
| 284 | // has been deleted, moved or renamed in incremental build. |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 285 | fprintf(to, "%s :\n", options.input_file_name_.c_str()); |
Ying Wang | 0e4861a | 2015-07-22 17:42:35 -0700 | [diff] [blame] | 286 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 287 | // Output "<imported_file>: " so make won't fail if the imported file has |
| 288 | // been deleted, moved or renamed in incremental build. |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 289 | import = import_head; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 290 | while (import) { |
| 291 | if (import->filename) { |
| 292 | fprintf(to, "%s :\n", import->filename); |
| 293 | } |
| 294 | import = import->next; |
| 295 | } |
| 296 | |
| 297 | fclose(to); |
| 298 | } |
| 299 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 300 | string generate_outputFileName2(const JavaOptions& options, |
| 301 | const buffer_type& name, |
| 302 | const char* package) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 303 | string result; |
| 304 | |
| 305 | // create the path to the destination folder based on the |
| 306 | // interface package name |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 307 | result = options.output_base_folder_; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 308 | result += OS_PATH_SEPARATOR; |
| 309 | |
| 310 | string packageStr = package; |
| 311 | size_t len = packageStr.length(); |
| 312 | for (size_t i=0; i<len; i++) { |
| 313 | if (packageStr[i] == '.') { |
| 314 | packageStr[i] = OS_PATH_SEPARATOR; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | result += packageStr; |
| 319 | |
| 320 | // add the filename by replacing the .aidl extension to .java |
| 321 | const char* p = strchr(name.data, '.'); |
| 322 | len = p ? p-name.data : strlen(name.data); |
| 323 | |
| 324 | result += OS_PATH_SEPARATOR; |
| 325 | result.append(name.data, len); |
| 326 | result += ".java"; |
| 327 | |
| 328 | return result; |
| 329 | } |
| 330 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 331 | string generate_outputFileName(const JavaOptions& options, |
| 332 | const document_item_type* items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 333 | // items has already been checked to have only one interface. |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 334 | if (items->item_type == INTERFACE_TYPE_BINDER) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 335 | interface_type* type = (interface_type*)items; |
| 336 | |
| 337 | return generate_outputFileName2(options, type->name, type->package); |
| 338 | } else if (items->item_type == USER_DATA_TYPE) { |
| 339 | user_data_type* type = (user_data_type*)items; |
| 340 | return generate_outputFileName2(options, type->name, type->package); |
| 341 | } |
| 342 | |
| 343 | // I don't think we can come here, but safer than returning NULL. |
| 344 | string result; |
| 345 | return result; |
| 346 | } |
| 347 | |
| 348 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 349 | void check_outputFilePath(const string& path) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 350 | size_t len = path.length(); |
| 351 | for (size_t i=0; i<len ; i++) { |
| 352 | if (path[i] == OS_PATH_SEPARATOR) { |
| 353 | string p = path.substr(0, i); |
| 354 | if (access(path.data(), F_OK) != 0) { |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 355 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 356 | _mkdir(p.data()); |
| 357 | #else |
| 358 | mkdir(p.data(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); |
| 359 | #endif |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 366 | int parse_preprocessed_file(const string& filename, TypeNamespace* types) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 367 | FILE* f = fopen(filename.c_str(), "rb"); |
| 368 | if (f == NULL) { |
| 369 | fprintf(stderr, "aidl: can't open preprocessed file: %s\n", |
| 370 | filename.c_str()); |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | int lineno = 1; |
| 375 | char line[1024]; |
| 376 | char type[1024]; |
| 377 | char fullname[1024]; |
| 378 | while (fgets(line, sizeof(line), f)) { |
| 379 | // skip comments and empty lines |
| 380 | if (!line[0] || strncmp(line, "//", 2) == 0) { |
| 381 | continue; |
| 382 | } |
| 383 | |
| 384 | sscanf(line, "%s %[^; \r\n\t];", type, fullname); |
| 385 | |
| 386 | char* packagename; |
| 387 | char* classname = rfind(fullname, '.'); |
| 388 | if (classname != NULL) { |
| 389 | *classname = '\0'; |
| 390 | classname++; |
| 391 | packagename = fullname; |
| 392 | } else { |
| 393 | classname = fullname; |
| 394 | packagename = NULL; |
| 395 | } |
| 396 | |
| 397 | //printf("%s:%d:...%s...%s...%s...\n", filename.c_str(), lineno, |
| 398 | // type, packagename, classname); |
| 399 | document_item_type* doc; |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 400 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 401 | if (0 == strcmp("parcelable", type)) { |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 402 | user_data_type* parcl = new user_data_type(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 403 | memset(parcl, 0, sizeof(user_data_type)); |
| 404 | parcl->document_item.item_type = USER_DATA_TYPE; |
| 405 | parcl->keyword_token.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 406 | parcl->keyword_token.data = cpp_strdup(type); |
| 407 | parcl->package = packagename ? cpp_strdup(packagename) : NULL; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 408 | parcl->name.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 409 | parcl->name.data = cpp_strdup(classname); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 410 | parcl->semicolon_token.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 411 | parcl->semicolon_token.data = cpp_strdup(";"); |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 412 | parcl->parcelable = true; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 413 | doc = (document_item_type*)parcl; |
| 414 | } |
| 415 | else if (0 == strcmp("interface", type)) { |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 416 | interface_type* iface = new interface_type(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 417 | memset(iface, 0, sizeof(interface_type)); |
| 418 | iface->document_item.item_type = INTERFACE_TYPE_BINDER; |
| 419 | iface->interface_token.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 420 | iface->interface_token.data = cpp_strdup(type); |
| 421 | iface->package = packagename ? cpp_strdup(packagename) : NULL; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 422 | iface->name.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 423 | iface->name.data = cpp_strdup(classname); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 424 | iface->open_brace_token.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 425 | iface->open_brace_token.data = cpp_strdup("{"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 426 | iface->close_brace_token.lineno = lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 427 | iface->close_brace_token.data = cpp_strdup("}"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 428 | doc = (document_item_type*)iface; |
| 429 | } |
| 430 | else { |
| 431 | fprintf(stderr, "%s:%d: bad type in line: %s\n", |
| 432 | filename.c_str(), lineno, line); |
Elliott Hughes | 5cd0607 | 2013-10-29 15:25:52 -0700 | [diff] [blame] | 433 | fclose(f); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 434 | return 1; |
| 435 | } |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 436 | if (!gather_types(filename.c_str(), doc, types)) { |
| 437 | fprintf(stderr, "Failed to gather types for preprocessed aidl.\n"); |
| 438 | fclose(f); |
| 439 | return 1; |
| 440 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 441 | lineno++; |
| 442 | } |
| 443 | |
| 444 | if (!feof(f)) { |
| 445 | fprintf(stderr, "%s:%d: error reading file, line to long.\n", |
| 446 | filename.c_str(), lineno); |
| 447 | return 1; |
| 448 | } |
| 449 | |
| 450 | fclose(f); |
| 451 | return 0; |
| 452 | } |
| 453 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 454 | int check_and_assign_method_ids(const char * filename, |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 455 | const std::vector<std::unique_ptr<AidlMethod>>& items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 456 | // Check whether there are any methods with manually assigned id's and any that are not. |
| 457 | // Either all method id's must be manually assigned or all of them must not. |
| 458 | // Also, check for duplicates of user set id's and that the id's are within the proper bounds. |
| 459 | set<int> usedIds; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 460 | bool hasUnassignedIds = false; |
| 461 | bool hasAssignedIds = false; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 462 | for (const auto& item : items) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 463 | if (item->HasId()) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 464 | hasAssignedIds = true; |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 465 | // Ensure that the user set id is not duplicated. |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 466 | if (usedIds.find(item->GetId()) != usedIds.end()) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 467 | // We found a duplicate id, so throw an error. |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 468 | fprintf(stderr, |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 469 | "%s:%d Found duplicate method id (%d) for method: %s\n", |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 470 | filename, item->GetLine(), |
| 471 | item->GetId(), item->GetName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 472 | return 1; |
| 473 | } |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 474 | // Ensure that the user set id is within the appropriate limits |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 475 | if (item->GetId() < kMinUserSetMethodId || |
| 476 | item->GetId() > kMaxUserSetMethodId) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 477 | fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n", |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 478 | filename, item->GetLine(), |
| 479 | item->GetId(), item->GetName().c_str()); |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 480 | fprintf(stderr, " Value for id must be between %d and %d inclusive.\n", |
| 481 | kMinUserSetMethodId, kMaxUserSetMethodId); |
| 482 | return 1; |
| 483 | } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 484 | usedIds.insert(item->GetId()); |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 485 | } else { |
| 486 | hasUnassignedIds = true; |
| 487 | } |
| 488 | if (hasAssignedIds && hasUnassignedIds) { |
| 489 | fprintf(stderr, |
| 490 | "%s: You must either assign id's to all methods or to none of them.\n", |
| 491 | filename); |
| 492 | return 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 493 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // In the case that all methods have unassigned id's, set a unique id for them. |
| 497 | if (hasUnassignedIds) { |
| 498 | int newId = 0; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 499 | for (const auto& item : items) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 500 | item->SetId(newId++); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
| 504 | // success |
| 505 | return 0; |
| 506 | } |
| 507 | |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 508 | int load_and_validate_aidl_internal(const std::vector<std::string> preprocessed_files, |
| 509 | const std::vector<std::string> import_paths, |
| 510 | const std::string& input_file_name, |
| 511 | TypeNamespace* types, |
| 512 | interface_type** returned_interface, |
| 513 | import_info** returned_imports, |
| 514 | bool use_data, |
| 515 | std::string data) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 516 | int err = 0; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 517 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 518 | set_import_paths(import_paths); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 519 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 520 | // import the preprocessed file |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 521 | for (const string& s : preprocessed_files) { |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 522 | err |= parse_preprocessed_file(s, types); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 523 | } |
| 524 | if (err != 0) { |
| 525 | return err; |
| 526 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 527 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 528 | // parse the input file |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 529 | Parser p{input_file_name}; |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 530 | if (use_data) |
| 531 | p.SetFileContents(data); |
| 532 | else if (!p.OpenFileFromDisk()) |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 533 | return 1; |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 534 | |
| 535 | if (!p.RunParser()) |
| 536 | return 1; |
| 537 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 538 | document_item_type* parsed_doc = p.GetDocument(); |
| 539 | // We could in theory declare parcelables in the same file as the interface. |
| 540 | // In practice, those parcelables would have to have the same name as |
| 541 | // the interface, since this was originally written to support Java, with its |
| 542 | // packages and names that correspond to file system structure. |
| 543 | // Since we can't have two distinct classes with the same name and package, |
| 544 | // we can't actually declare parcelables in the same file. |
| 545 | if (parsed_doc == nullptr || |
| 546 | parsed_doc->item_type != INTERFACE_TYPE_BINDER || |
| 547 | parsed_doc->next != nullptr) { |
| 548 | cerr << "aidl expects exactly one interface per input file"; |
Christopher Wiley | a2f516d | 2015-09-24 10:12:31 -0700 | [diff] [blame] | 549 | return 1; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 550 | } |
| 551 | interface_type* interface = (interface_type*)parsed_doc; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 552 | err |= check_filename(input_file_name.c_str(), |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 553 | interface->package, &interface->name); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 554 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 555 | // parse the imports of the input file |
| 556 | for (import_info* import = p.GetImports(); import; import = import->next) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 557 | if (types->HasType(import->neededClass)) { |
| 558 | // There are places in the Android tree where an import doesn't resolve, |
| 559 | // but we'll pick the type up through the preprocessed types. |
| 560 | // This seems like an error, but legacy support demands we support it... |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 561 | continue; |
| 562 | } |
| 563 | import->filename = find_import_file(import->neededClass); |
| 564 | if (!import->filename) { |
| 565 | cerr << import->from << ":" << import->statement.lineno |
| 566 | << ": couldn't find import for class " |
| 567 | << import->neededClass << endl; |
| 568 | err |= 1; |
| 569 | continue; |
| 570 | } |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 571 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 572 | Parser p{import->filename}; |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 573 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 574 | if (!p.OpenFileFromDisk() || !p.RunParser() || p.GetDocument() == nullptr) { |
| 575 | cerr << "error while parsing import for class " |
| 576 | << import->neededClass << endl; |
| 577 | err |= 1; |
| 578 | continue; |
| 579 | } |
| 580 | import->doc = p.GetDocument(); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 581 | err |= check_filenames(import->filename, import->doc); |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 582 | } |
| 583 | if (err != 0) { |
| 584 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 585 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 586 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 587 | // gather the types that have been declared |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 588 | if (!gather_types(input_file_name.c_str(), parsed_doc, types)) { |
| 589 | err |= 1; |
| 590 | } |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 591 | for (import_info* import = p.GetImports(); import; import = import->next) { |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 592 | if (!gather_types(import->filename, import->doc, types)) { |
| 593 | err |= 1; |
| 594 | } |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 595 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 596 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 597 | // 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] | 598 | err |= check_types(input_file_name, interface, types); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 599 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 600 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 601 | // assign method ids and validate. |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 602 | err |= check_and_assign_method_ids(input_file_name.c_str(), |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 603 | *interface->methods); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 604 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 605 | // after this, there shouldn't be any more errors because of the |
| 606 | // input. |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 607 | if (err != 0) { |
| 608 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 609 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 610 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 611 | *returned_interface = interface; |
| 612 | *returned_imports = p.GetImports(); |
| 613 | return 0; |
| 614 | } |
| 615 | |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 616 | int load_and_validate_aidl(const std::vector<std::string> preprocessed_files, |
| 617 | const std::vector<std::string> import_paths, |
| 618 | const std::string& input_file_name, |
| 619 | TypeNamespace* types, |
| 620 | interface_type** returned_interface, |
| 621 | import_info** returned_imports) { |
| 622 | return load_and_validate_aidl_internal(preprocessed_files, |
| 623 | import_paths, |
| 624 | input_file_name, |
| 625 | types, |
| 626 | returned_interface, |
| 627 | returned_imports, |
| 628 | false, |
| 629 | ""); |
| 630 | } |
| 631 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 632 | } // namespace |
| 633 | |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 634 | namespace internals { |
| 635 | |
| 636 | int load_aidl_for_test(const std::string& input_file_name, |
| 637 | const std::string& data, |
| 638 | TypeNamespace* types, |
| 639 | interface_type** returned_interface) { |
| 640 | import_info *throwaway; |
| 641 | int ret = load_and_validate_aidl_internal({}, {}, input_file_name, types, |
| 642 | returned_interface, &throwaway, |
| 643 | true, data); |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | } // namespace internals |
| 648 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 649 | int compile_aidl_to_cpp(const CppOptions& options) { |
| 650 | interface_type* interface = nullptr; |
| 651 | import_info* imports = nullptr; |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 652 | unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace()); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 653 | int err = load_and_validate_aidl(std::vector<std::string>{}, |
| 654 | options.ImportPaths(), |
| 655 | options.InputFileName(), |
Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 656 | types.get(), |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 657 | &interface, |
| 658 | &imports); |
| 659 | if (err != 0) { |
| 660 | return err; |
| 661 | } |
| 662 | |
| 663 | // TODO(wiley) b/23600457 generate a dependency file if requested with -b |
| 664 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 665 | return (cpp::GenerateCpp(options, *types, *interface)) ? 0 : 1; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | int compile_aidl_to_java(const JavaOptions& options) { |
| 669 | interface_type* interface = nullptr; |
| 670 | import_info* imports = nullptr; |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 671 | unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace()); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 672 | int err = load_and_validate_aidl(options.preprocessed_files_, |
| 673 | options.import_paths_, |
| 674 | options.input_file_name_, |
Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 675 | types.get(), |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 676 | &interface, |
| 677 | &imports); |
| 678 | if (err != 0) { |
| 679 | return err; |
| 680 | } |
| 681 | document_item_type* parsed_doc = (document_item_type*)interface; |
| 682 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 683 | string output_file_name = options.output_file_name_; |
| 684 | // if needed, generate the output file name from the base folder |
| 685 | if (output_file_name.length() == 0 && |
| 686 | options.output_base_folder_.length() > 0) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 687 | output_file_name = generate_outputFileName(options, parsed_doc); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 688 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 689 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 690 | // if we were asked to, generate a make dependency file |
| 691 | // 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] | 692 | if (options.auto_dep_file_ || options.dep_file_name_ != "") { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 693 | // make sure the folders of the output file all exists |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 694 | check_outputFilePath(output_file_name); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 695 | generate_dep_file(options, parsed_doc, imports); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 696 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 697 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 698 | // make sure the folders of the output file all exists |
| 699 | check_outputFilePath(output_file_name); |
| 700 | |
| 701 | err = generate_java(output_file_name, options.input_file_name_.c_str(), |
Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 702 | interface, types.get()); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 703 | |
| 704 | return err; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 705 | } |
| 706 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 707 | int preprocess_aidl(const JavaOptions& options) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 708 | vector<string> lines; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 709 | |
| 710 | // read files |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 711 | int N = options.files_to_preprocess_.size(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 712 | for (int i=0; i<N; i++) { |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 713 | Parser p{options.files_to_preprocess_[i]}; |
Casey Dahlin | 9980181 | 2015-09-15 18:19:25 -0700 | [diff] [blame] | 714 | if (!p.OpenFileFromDisk()) |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 715 | return 1; |
Casey Dahlin | 9980181 | 2015-09-15 18:19:25 -0700 | [diff] [blame] | 716 | if (!p.RunParser()) |
| 717 | return 1; |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 718 | document_item_type* doc = p.GetDocument(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 719 | string line; |
| 720 | if (doc->item_type == USER_DATA_TYPE) { |
| 721 | user_data_type* parcelable = (user_data_type*)doc; |
Casey Dahlin | 88868fc | 2015-09-01 13:21:26 -0700 | [diff] [blame] | 722 | if (parcelable->parcelable) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 723 | line = "parcelable "; |
| 724 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 725 | if (parcelable->package) { |
| 726 | line += parcelable->package; |
| 727 | line += '.'; |
| 728 | } |
| 729 | line += parcelable->name.data; |
| 730 | } else { |
| 731 | line = "interface "; |
| 732 | interface_type* iface = (interface_type*)doc; |
| 733 | if (iface->package) { |
| 734 | line += iface->package; |
| 735 | line += '.'; |
| 736 | } |
| 737 | line += iface->name.data; |
| 738 | } |
| 739 | line += ";\n"; |
| 740 | lines.push_back(line); |
| 741 | } |
| 742 | |
| 743 | // write preprocessed file |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 744 | int fd = open( options.output_file_name_.c_str(), |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 745 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 746 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 747 | _S_IREAD|_S_IWRITE); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 748 | #else |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 749 | S_IRUSR|S_IWUSR|S_IRGRP); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 750 | #endif |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 751 | if (fd == -1) { |
| 752 | fprintf(stderr, "aidl: could not open file for write: %s\n", |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 753 | options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 754 | return 1; |
| 755 | } |
| 756 | |
| 757 | N = lines.size(); |
| 758 | for (int i=0; i<N; i++) { |
| 759 | const string& s = lines[i]; |
| 760 | int len = s.length(); |
| 761 | if (len != write(fd, s.c_str(), len)) { |
| 762 | fprintf(stderr, "aidl: error writing to file %s\n", |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 763 | options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 764 | close(fd); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 765 | unlink(options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 766 | return 1; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | close(fd); |
| 771 | return 0; |
| 772 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 773 | |
| 774 | } // namespace android |
| 775 | } // namespace aidl |