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 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 35 | #include <android-base/strings.h> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 36 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 37 | #include "aidl_language.h" |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 38 | #include "generate_cpp.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 39 | #include "generate_java.h" |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 40 | #include "import_resolver.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 41 | #include "logging.h" |
| 42 | #include "options.h" |
| 43 | #include "os.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 | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 52 | using android::base::Join; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 53 | using android::base::Split; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 54 | using std::cerr; |
| 55 | using std::endl; |
Christopher Wiley | 9f4c7ae | 2015-08-24 14:07:32 -0700 | [diff] [blame] | 56 | using std::map; |
| 57 | using std::set; |
| 58 | using std::string; |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 59 | using std::unique_ptr; |
Christopher Wiley | 9f4c7ae | 2015-08-24 14:07:32 -0700 | [diff] [blame] | 60 | using std::vector; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 61 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 62 | namespace android { |
| 63 | namespace aidl { |
| 64 | namespace { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 65 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 66 | // The following are gotten as the offset from the allowable id's between |
| 67 | // android.os.IBinder.FIRST_CALL_TRANSACTION=1 and |
| 68 | // android.os.IBinder.LAST_CALL_TRANSACTION=16777215 |
| 69 | const int kMinUserSetMethodId = 0; |
| 70 | const int kMaxUserSetMethodId = 16777214; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 71 | |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 72 | bool check_filename(const std::string& filename, |
| 73 | const std::string& package, |
| 74 | const std::string& name, |
| 75 | unsigned line) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 76 | const char* p; |
| 77 | string expected; |
| 78 | string fn; |
| 79 | size_t len; |
| 80 | char cwd[MAXPATHLEN]; |
| 81 | bool valid = false; |
| 82 | |
Elliott Hughes | ce310da | 2015-07-29 08:44:17 -0700 | [diff] [blame] | 83 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 84 | if (isalpha(filename[0]) && filename[1] == ':' |
| 85 | && filename[2] == OS_PATH_SEPARATOR) { |
| 86 | #else |
| 87 | if (filename[0] == OS_PATH_SEPARATOR) { |
| 88 | #endif |
| 89 | fn = filename; |
| 90 | } else { |
| 91 | fn = getcwd(cwd, sizeof(cwd)); |
| 92 | len = fn.length(); |
| 93 | if (fn[len-1] != OS_PATH_SEPARATOR) { |
| 94 | fn += OS_PATH_SEPARATOR; |
| 95 | } |
| 96 | fn += filename; |
| 97 | } |
| 98 | |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 99 | if (!package.empty()) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 100 | expected = package; |
| 101 | expected += '.'; |
| 102 | } |
| 103 | |
| 104 | len = expected.length(); |
| 105 | for (size_t i=0; i<len; i++) { |
| 106 | if (expected[i] == '.') { |
| 107 | expected[i] = OS_PATH_SEPARATOR; |
| 108 | } |
| 109 | } |
| 110 | |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 111 | expected.append(name, 0, name.find('.')); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 112 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 113 | expected += ".aidl"; |
| 114 | |
| 115 | len = fn.length(); |
| 116 | valid = (len >= expected.length()); |
| 117 | |
| 118 | if (valid) { |
| 119 | p = fn.c_str() + (len - expected.length()); |
| 120 | |
Elliott Hughes | ce310da | 2015-07-29 08:44:17 -0700 | [diff] [blame] | 121 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 122 | if (OS_PATH_SEPARATOR != '/') { |
| 123 | // Input filename under cygwin most likely has / separators |
| 124 | // whereas the expected string uses \\ separators. Adjust |
| 125 | // them accordingly. |
| 126 | for (char *c = const_cast<char *>(p); *c; ++c) { |
| 127 | if (*c == '/') *c = OS_PATH_SEPARATOR; |
| 128 | } |
| 129 | } |
| 130 | #endif |
| 131 | |
Yabin Cui | 482eefb | 2014-11-10 15:01:43 -0800 | [diff] [blame] | 132 | // aidl assumes case-insensitivity on Mac Os and Windows. |
| 133 | #if defined(__linux__) |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 134 | valid = (expected == p); |
| 135 | #else |
| 136 | valid = !strcasecmp(expected.c_str(), p); |
| 137 | #endif |
| 138 | } |
| 139 | |
| 140 | if (!valid) { |
| 141 | fprintf(stderr, "%s:%d interface %s should be declared in a file" |
| 142 | " called %s.\n", |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 143 | filename.c_str(), line, name.c_str(), expected.c_str()); |
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 | return valid; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 149 | bool check_filenames(const std::string& filename, const AidlDocument* doc) { |
| 150 | if (!doc) |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 151 | return true; |
| 152 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 153 | const AidlInterface* interface = doc->GetInterface(); |
| 154 | |
| 155 | if (interface) { |
| 156 | return check_filename(filename, interface->GetPackage(), |
| 157 | interface->GetName(), interface->GetLine()); |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | bool success = true; |
| 161 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 162 | for (const auto& item : doc->GetParcelables()) { |
| 163 | success &= check_filename(filename, item->GetPackage(), item->GetName(), |
| 164 | item->GetLine()); |
| 165 | } |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 166 | |
| 167 | return success; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 170 | bool gather_types(const std::string& filename, |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 171 | const AidlDocument* doc, |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 172 | TypeNamespace* types) { |
| 173 | bool success = true; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 174 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 175 | const AidlInterface* interface = doc->GetInterface(); |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 176 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 177 | if (interface) |
| 178 | return types->AddBinderType(*interface, filename); |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 179 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 180 | for (const auto& item : doc->GetParcelables()) { |
| 181 | success &= types->AddParcelableType(*item, filename); |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 182 | } |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 183 | |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 184 | return success; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 187 | int check_types(const string& filename, |
Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 188 | const AidlInterface* c, |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 189 | TypeNamespace* types) { |
| 190 | int err = 0; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 191 | |
| 192 | // Has to be a pointer due to deleting copy constructor. No idea why. |
| 193 | map<string, const AidlMethod*> method_names; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 194 | for (const auto& m : c->GetMethods()) { |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 195 | bool oneway = m->IsOneway() || c->IsOneway(); |
| 196 | |
Christopher Wiley | 934a82d | 2016-01-27 13:02:24 -0800 | [diff] [blame^] | 197 | if (!types->MaybeAddContainerType(m->GetType())) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 198 | err = 1; // return type is invalid |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 201 | const ValidatableType* return_type = |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 202 | types->GetReturnType(m->GetType(), filename); |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 203 | |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 204 | if (!return_type) { |
| 205 | err = 1; |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | m->GetMutableType()->SetLanguageType(return_type); |
| 209 | |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 210 | if (oneway && m->GetType().GetName() != "void") { |
| 211 | cerr << filename << ":" << m->GetLine() |
Christopher Wiley | 45db9ee | 2015-10-26 18:53:53 -0700 | [diff] [blame] | 212 | << " oneway method '" << m->GetName() << "' cannot return a value" |
| 213 | << endl; |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 214 | err = 1; |
| 215 | } |
| 216 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 217 | int index = 1; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 218 | for (const auto& arg : m->GetArguments()) { |
Christopher Wiley | 934a82d | 2016-01-27 13:02:24 -0800 | [diff] [blame^] | 219 | if (!types->MaybeAddContainerType(arg->GetType())) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 220 | err = 1; |
| 221 | } |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 222 | |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 223 | const ValidatableType* arg_type = |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 224 | types->GetArgType(*arg, index, filename); |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 225 | |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 226 | if (!arg_type) { |
| 227 | err = 1; |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | arg->GetMutableType()->SetLanguageType(arg_type); |
| 231 | |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 232 | if (oneway && arg->IsOut()) { |
| 233 | cerr << filename << ":" << m->GetLine() |
Christopher Wiley | 45db9ee | 2015-10-26 18:53:53 -0700 | [diff] [blame] | 234 | << " oneway method '" << m->GetName() |
| 235 | << "' cannot have out parameters" << endl; |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 236 | err = 1; |
| 237 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 240 | auto it = method_names.find(m->GetName()); |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 241 | // prevent duplicate methods |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 242 | if (it == method_names.end()) { |
| 243 | method_names[m->GetName()] = m.get(); |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 244 | } else { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 245 | cerr << filename << ":" << m->GetLine() |
| 246 | << " attempt to redefine method " << m->GetName() << "," << endl |
| 247 | << filename << ":" << it->second->GetLine() |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 248 | << " previously defined here." << endl; |
| 249 | err = 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 250 | } |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 251 | } |
| 252 | return err; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 255 | void write_common_dep_file(const string& output_file, |
| 256 | const vector<string>& aidl_sources, |
| 257 | CodeWriter* writer) { |
| 258 | // Encode that the output file depends on aidl input files. |
| 259 | writer->Write("%s : \\\n", output_file.c_str()); |
| 260 | writer->Write(" %s", Join(aidl_sources, " \\\n ").c_str()); |
| 261 | writer->Write("\n\n"); |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 262 | |
| 263 | // Output "<input_aidl_file>: " so make won't fail if the input .aidl file |
| 264 | // has been deleted, moved or renamed in incremental build. |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 265 | for (const auto& src : aidl_sources) { |
| 266 | writer->Write("%s :\n", src.c_str()); |
| 267 | } |
| 268 | } |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 269 | |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 270 | bool write_java_dep_file(const JavaOptions& options, |
| 271 | const vector<unique_ptr<AidlImport>>& imports, |
| 272 | const IoDelegate& io_delegate) { |
| 273 | string dep_file_name = options.DependencyFilePath(); |
| 274 | if (dep_file_name.empty()) { |
| 275 | return true; // nothing to do |
| 276 | } |
| 277 | CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name); |
| 278 | if (!writer) { |
| 279 | LOG(ERROR) << "Could not open dependency file: " << dep_file_name; |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | vector<string> source_aidl = {options.input_file_name_}; |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 284 | for (const auto& import : imports) { |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 285 | if (!import->GetFilename().empty()) { |
| 286 | source_aidl.push_back(import->GetFilename()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 287 | } |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 288 | } |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 289 | |
| 290 | write_common_dep_file(options.output_file_name_, source_aidl, writer.get()); |
| 291 | |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | bool write_cpp_dep_file(const CppOptions& options, |
| 296 | const AidlInterface& interface, |
| 297 | const vector<unique_ptr<AidlImport>>& imports, |
| 298 | const IoDelegate& io_delegate) { |
| 299 | using ::android::aidl::cpp::HeaderFile; |
| 300 | using ::android::aidl::cpp::ClassNames; |
| 301 | |
| 302 | string dep_file_name = options.DependencyFilePath(); |
| 303 | if (dep_file_name.empty()) { |
| 304 | return true; // nothing to do |
| 305 | } |
| 306 | CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name); |
| 307 | if (!writer) { |
| 308 | LOG(ERROR) << "Could not open dependency file: " << dep_file_name; |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | vector<string> source_aidl = {options.InputFileName()}; |
| 313 | for (const auto& import : imports) { |
| 314 | if (!import->GetFilename().empty()) { |
| 315 | source_aidl.push_back(import->GetFilename()); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | vector<string> headers; |
| 320 | for (ClassNames c : {ClassNames::CLIENT, |
| 321 | ClassNames::SERVER, |
| 322 | ClassNames::INTERFACE}) { |
| 323 | headers.push_back(options.OutputHeaderDir() + '/' + |
| 324 | HeaderFile(interface, c, false /* use_os_sep */)); |
| 325 | } |
| 326 | |
| 327 | write_common_dep_file(options.OutputCppFilePath(), source_aidl, writer.get()); |
| 328 | writer->Write("\n"); |
| 329 | |
| 330 | // Generated headers also depend on the source aidl files. |
| 331 | writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(), |
| 332 | Join(source_aidl, " \\\n ").c_str()); |
| 333 | |
| 334 | return true; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 337 | string generate_outputFileName(const JavaOptions& options, |
| 338 | const AidlInterface& interface) { |
| 339 | string name = interface.GetName(); |
| 340 | string package = interface.GetPackage(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 341 | string result; |
| 342 | |
| 343 | // create the path to the destination folder based on the |
| 344 | // interface package name |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 345 | result = options.output_base_folder_; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 346 | result += OS_PATH_SEPARATOR; |
| 347 | |
| 348 | string packageStr = package; |
| 349 | size_t len = packageStr.length(); |
| 350 | for (size_t i=0; i<len; i++) { |
| 351 | if (packageStr[i] == '.') { |
| 352 | packageStr[i] = OS_PATH_SEPARATOR; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | result += packageStr; |
| 357 | |
| 358 | // add the filename by replacing the .aidl extension to .java |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 359 | result += OS_PATH_SEPARATOR; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 360 | result.append(name, 0, name.find('.')); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 361 | result += ".java"; |
| 362 | |
| 363 | return result; |
| 364 | } |
| 365 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 366 | int check_and_assign_method_ids(const char * filename, |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 367 | const std::vector<std::unique_ptr<AidlMethod>>& items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 368 | // Check whether there are any methods with manually assigned id's and any that are not. |
| 369 | // Either all method id's must be manually assigned or all of them must not. |
| 370 | // Also, check for duplicates of user set id's and that the id's are within the proper bounds. |
| 371 | set<int> usedIds; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 372 | bool hasUnassignedIds = false; |
| 373 | bool hasAssignedIds = false; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 374 | for (const auto& item : items) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 375 | if (item->HasId()) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 376 | hasAssignedIds = true; |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 377 | // Ensure that the user set id is not duplicated. |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 378 | if (usedIds.find(item->GetId()) != usedIds.end()) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 379 | // We found a duplicate id, so throw an error. |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 380 | fprintf(stderr, |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 381 | "%s:%d Found duplicate method id (%d) for method: %s\n", |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 382 | filename, item->GetLine(), |
| 383 | item->GetId(), item->GetName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 384 | return 1; |
| 385 | } |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 386 | // Ensure that the user set id is within the appropriate limits |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 387 | if (item->GetId() < kMinUserSetMethodId || |
| 388 | item->GetId() > kMaxUserSetMethodId) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 389 | 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] | 390 | filename, item->GetLine(), |
| 391 | item->GetId(), item->GetName().c_str()); |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 392 | fprintf(stderr, " Value for id must be between %d and %d inclusive.\n", |
| 393 | kMinUserSetMethodId, kMaxUserSetMethodId); |
| 394 | return 1; |
| 395 | } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 396 | usedIds.insert(item->GetId()); |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 397 | } else { |
| 398 | hasUnassignedIds = true; |
| 399 | } |
| 400 | if (hasAssignedIds && hasUnassignedIds) { |
| 401 | fprintf(stderr, |
| 402 | "%s: You must either assign id's to all methods or to none of them.\n", |
| 403 | filename); |
| 404 | return 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 405 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // In the case that all methods have unassigned id's, set a unique id for them. |
| 409 | if (hasUnassignedIds) { |
| 410 | int newId = 0; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 411 | for (const auto& item : items) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 412 | item->SetId(newId++); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | // success |
| 417 | return 0; |
| 418 | } |
| 419 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 420 | // TODO: Remove this in favor of using the YACC parser b/25479378 |
| 421 | bool ParsePreprocessedLine(const string& line, string* decl, |
| 422 | vector<string>* package, string* class_name) { |
| 423 | // erase all trailing whitespace and semicolons |
| 424 | const size_t end = line.find_last_not_of(" ;\t"); |
| 425 | if (end == string::npos) { |
| 426 | return false; |
| 427 | } |
| 428 | if (line.rfind(';', end) != string::npos) { |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | decl->clear(); |
| 433 | string type; |
| 434 | vector<string> pieces = Split(line.substr(0, end + 1), " \t"); |
| 435 | for (const string& piece : pieces) { |
| 436 | if (piece.empty()) { |
| 437 | continue; |
| 438 | } |
| 439 | if (decl->empty()) { |
| 440 | *decl = std::move(piece); |
| 441 | } else if (type.empty()) { |
| 442 | type = std::move(piece); |
| 443 | } else { |
| 444 | return false; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // Note that this logic is absolutely wrong. Given a parcelable |
| 449 | // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that |
| 450 | // the class is just Bar. However, this was the way it was done in the past. |
| 451 | // |
| 452 | // See b/17415692 |
| 453 | size_t dot_pos = type.rfind('.'); |
| 454 | if (dot_pos != string::npos) { |
| 455 | *class_name = type.substr(dot_pos + 1); |
| 456 | *package = Split(type.substr(0, dot_pos), "."); |
| 457 | } else { |
| 458 | *class_name = type; |
| 459 | package->clear(); |
| 460 | } |
| 461 | |
| 462 | return true; |
| 463 | } |
| 464 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 465 | } // namespace |
| 466 | |
| 467 | namespace internals { |
| 468 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 469 | bool parse_preprocessed_file(const IoDelegate& io_delegate, |
| 470 | const string& filename, TypeNamespace* types) { |
| 471 | bool success = true; |
| 472 | unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename); |
| 473 | if (!line_reader) { |
| 474 | LOG(ERROR) << "cannot open preprocessed file: " << filename; |
| 475 | success = false; |
| 476 | return success; |
| 477 | } |
| 478 | |
| 479 | string line; |
| 480 | unsigned lineno = 1; |
| 481 | for ( ; line_reader->ReadLine(&line); ++lineno) { |
| 482 | if (line.empty() || line.compare(0, 2, "//") == 0) { |
| 483 | // skip comments and empty lines |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | string decl; |
| 488 | vector<string> package; |
| 489 | string class_name; |
| 490 | if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) { |
| 491 | success = false; |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | if (decl == "parcelable") { |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 496 | AidlParcelable doc(new AidlQualifiedName(class_name, ""), |
| 497 | lineno, package); |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 498 | types->AddParcelableType(doc, filename); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 499 | } else if (decl == "interface") { |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 500 | auto temp = new std::vector<std::unique_ptr<AidlMember>>(); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 501 | AidlInterface doc(class_name, lineno, "", false, temp, package); |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 502 | types->AddBinderType(doc, filename); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 503 | } else { |
| 504 | success = false; |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | if (!success) { |
| 509 | LOG(ERROR) << filename << ':' << lineno |
| 510 | << " malformed preprocessed file line: '" << line << "'"; |
| 511 | } |
| 512 | |
| 513 | return success; |
| 514 | } |
| 515 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 516 | AidlError load_and_validate_aidl( |
| 517 | const std::vector<std::string> preprocessed_files, |
| 518 | const std::vector<std::string> import_paths, |
| 519 | const std::string& input_file_name, |
| 520 | const IoDelegate& io_delegate, |
| 521 | TypeNamespace* types, |
| 522 | std::unique_ptr<AidlInterface>* returned_interface, |
| 523 | std::vector<std::unique_ptr<AidlImport>>* returned_imports) { |
| 524 | AidlError err = AidlError::OK; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 525 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 526 | std::map<AidlImport*,std::unique_ptr<AidlDocument>> docs; |
Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 527 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 528 | // import the preprocessed file |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 529 | for (const string& s : preprocessed_files) { |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 530 | if (!parse_preprocessed_file(io_delegate, s, types)) { |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 531 | err = AidlError::BAD_PRE_PROCESSED_FILE; |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 532 | } |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 533 | } |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 534 | if (err != AidlError::OK) { |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 535 | return err; |
| 536 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 537 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 538 | // parse the input file |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 539 | Parser p{io_delegate}; |
| 540 | if (!p.ParseFile(input_file_name)) { |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 541 | return AidlError::PARSE_ERROR; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 542 | } |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 543 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 544 | AidlDocument* parsed_doc = p.GetDocument(); |
| 545 | |
| 546 | unique_ptr<AidlInterface> interface(parsed_doc->ReleaseInterface()); |
| 547 | |
| 548 | if (!interface) { |
Christopher Wiley | e60d99c | 2015-11-06 13:14:24 -0800 | [diff] [blame] | 549 | LOG(ERROR) << "refusing to generate code from aidl file defining " |
| 550 | "parcelable"; |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 551 | return AidlError::FOUND_PARCELABLE; |
| 552 | } |
| 553 | |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 554 | if (!check_filename(input_file_name.c_str(), interface->GetPackage(), |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 555 | interface->GetName(), interface->GetLine()) || |
| 556 | !types->IsValidPackage(interface->GetPackage())) { |
| 557 | LOG(ERROR) << "Invalid package declaration '" << interface->GetPackage() |
| 558 | << "'"; |
| 559 | return AidlError::BAD_PACKAGE; |
| 560 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 561 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 562 | // parse the imports of the input file |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 563 | ImportResolver import_resolver{io_delegate, import_paths}; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 564 | for (auto& import : p.GetImports()) { |
Christopher Wiley | 934a82d | 2016-01-27 13:02:24 -0800 | [diff] [blame^] | 565 | if (types->HasImportType(*import)) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 566 | // There are places in the Android tree where an import doesn't resolve, |
| 567 | // but we'll pick the type up through the preprocessed types. |
| 568 | // This seems like an error, but legacy support demands we support it... |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 569 | continue; |
| 570 | } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 571 | string import_path = import_resolver.FindImportFile(import->GetNeededClass()); |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 572 | if (import_path.empty()) { |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 573 | cerr << import->GetFileFrom() << ":" << import->GetLine() |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 574 | << ": couldn't find import for class " |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 575 | << import->GetNeededClass() << endl; |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 576 | err = AidlError::BAD_IMPORT; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 577 | continue; |
| 578 | } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 579 | import->SetFilename(import_path); |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 580 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 581 | Parser p{io_delegate}; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 582 | if (!p.ParseFile(import->GetFilename())) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 583 | cerr << "error while parsing import for class " |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 584 | << import->GetNeededClass() << endl; |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 585 | err = AidlError::BAD_IMPORT; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 586 | continue; |
| 587 | } |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 588 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 589 | std::unique_ptr<AidlDocument> document(p.ReleaseDocument()); |
| 590 | if (!check_filenames(import->GetFilename(), document.get())) |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 591 | err = AidlError::BAD_IMPORT; |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 592 | docs[import.get()] = std::move(document); |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 593 | } |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 594 | if (err != AidlError::OK) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 595 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 596 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 597 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 598 | // gather the types that have been declared |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 599 | if (!types->AddBinderType(*interface.get(), input_file_name)) { |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 600 | err = AidlError::BAD_TYPE; |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 601 | } |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 602 | |
Christopher Wiley | 934a82d | 2016-01-27 13:02:24 -0800 | [diff] [blame^] | 603 | interface->SetLanguageType(types->GetInterfaceType(*interface)); |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 604 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 605 | for (const auto& import : p.GetImports()) { |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 606 | // If we skipped an unresolved import above (see comment there) we'll have |
| 607 | // an empty bucket here. |
| 608 | const auto import_itr = docs.find(import.get()); |
| 609 | if (import_itr == docs.cend()) { |
| 610 | continue; |
| 611 | } |
| 612 | |
| 613 | if (!gather_types(import->GetFilename(), import_itr->second.get(), types)) { |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 614 | err = AidlError::BAD_TYPE; |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 615 | } |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 616 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 617 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 618 | // check the referenced types in parsed_doc to make sure we've imported them |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 619 | if (check_types(input_file_name, interface.get(), types) != 0) { |
| 620 | err = AidlError::BAD_TYPE; |
| 621 | } |
| 622 | if (err != AidlError::OK) { |
| 623 | return err; |
| 624 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 625 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 626 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 627 | // assign method ids and validate. |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 628 | if (check_and_assign_method_ids(input_file_name.c_str(), |
| 629 | interface->GetMethods()) != 0) { |
| 630 | return AidlError::BAD_METHOD_ID; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 631 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 632 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 633 | if (returned_interface) |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 634 | *returned_interface = std::move(interface); |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 635 | |
| 636 | if (returned_imports) |
| 637 | p.ReleaseImports(returned_imports); |
| 638 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 639 | return AidlError::OK; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 642 | } // namespace internals |
| 643 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 644 | int compile_aidl_to_cpp(const CppOptions& options, |
| 645 | const IoDelegate& io_delegate) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 646 | unique_ptr<AidlInterface> interface; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 647 | std::vector<std::unique_ptr<AidlImport>> imports; |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 648 | unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace()); |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 649 | types->Init(); |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 650 | AidlError err = internals::load_and_validate_aidl( |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 651 | std::vector<std::string>{}, // no preprocessed files |
| 652 | options.ImportPaths(), |
| 653 | options.InputFileName(), |
| 654 | io_delegate, |
| 655 | types.get(), |
| 656 | &interface, |
| 657 | &imports); |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 658 | if (err != AidlError::OK) { |
| 659 | return 1; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 662 | if (!write_cpp_dep_file(options, *interface, imports, io_delegate)) { |
| 663 | return 1; |
Christopher Wiley | 19059cb | 2015-11-05 16:11:56 -0800 | [diff] [blame] | 664 | } |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 665 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 666 | return (cpp::GenerateCpp(options, *types, *interface, io_delegate)) ? 0 : 1; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 669 | int compile_aidl_to_java(const JavaOptions& options, |
| 670 | const IoDelegate& io_delegate) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 671 | unique_ptr<AidlInterface> interface; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 672 | std::vector<std::unique_ptr<AidlImport>> imports; |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 673 | unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace()); |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 674 | types->Init(); |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 675 | AidlError aidl_err = internals::load_and_validate_aidl( |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 676 | options.preprocessed_files_, |
| 677 | options.import_paths_, |
| 678 | options.input_file_name_, |
| 679 | io_delegate, |
| 680 | types.get(), |
| 681 | &interface, |
| 682 | &imports); |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 683 | if (aidl_err == AidlError::FOUND_PARCELABLE && !options.fail_on_parcelable_) { |
| 684 | // We aborted code generation because this file contains parcelables. |
| 685 | // However, we were not told to complain if we find parcelables. |
| 686 | // Just exit quietly. |
| 687 | return 0; |
| 688 | } |
| 689 | if (aidl_err != AidlError::OK) { |
| 690 | return 1; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 691 | } |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 692 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 693 | string output_file_name = options.output_file_name_; |
| 694 | // if needed, generate the output file name from the base folder |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 695 | if (output_file_name.empty() && !options.output_base_folder_.empty()) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 696 | output_file_name = generate_outputFileName(options, *interface); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 697 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 698 | |
Casey Dahlin | 6453351 | 2015-10-23 17:11:21 -0700 | [diff] [blame] | 699 | // make sure the folders of the output file all exists |
| 700 | if (!io_delegate.CreatePathForFile(output_file_name)) { |
| 701 | return 1; |
| 702 | } |
| 703 | |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 704 | if (!write_java_dep_file(options, imports, io_delegate)) { |
| 705 | return 1; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 706 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 707 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 708 | return generate_java(output_file_name, options.input_file_name_.c_str(), |
| 709 | interface.get(), types.get(), io_delegate); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 710 | } |
| 711 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 712 | bool preprocess_aidl(const JavaOptions& options, |
| 713 | const IoDelegate& io_delegate) { |
| 714 | unique_ptr<CodeWriter> writer = |
| 715 | io_delegate.GetCodeWriter(options.output_file_name_); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 716 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 717 | for (const auto& file : options.files_to_preprocess_) { |
| 718 | Parser p{io_delegate}; |
| 719 | if (!p.ParseFile(file)) |
| 720 | return false; |
| 721 | AidlDocument* doc = p.GetDocument(); |
| 722 | string line; |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 723 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 724 | const AidlInterface* interface = doc->GetInterface(); |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 725 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 726 | if (interface != nullptr && |
| 727 | !writer->Write("interface %s;\n", |
| 728 | interface->GetCanonicalName().c_str())) { |
| 729 | return false; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 732 | for (const auto& parcelable : doc->GetParcelables()) { |
| 733 | if (!writer->Write("parcelable %s;\n", |
| 734 | parcelable->GetCanonicalName().c_str())) { |
| 735 | return false; |
| 736 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 737 | } |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 738 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 739 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 740 | return writer->Close(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 741 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 742 | |
| 743 | } // namespace android |
| 744 | } // namespace aidl |