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