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