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