Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 16 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 17 | #include "aidl.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 18 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 20 | #include <iostream> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 21 | #include <map> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 25 | #include <sys/param.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <unistd.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 29 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 30 | #include <io.h> |
Andrew Hsieh | 15ce994 | 2014-05-07 20:14:30 +0800 | [diff] [blame] | 31 | #include <direct.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #endif |
| 34 | |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 35 | #include <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 | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 148 | bool check_filenames(const std::string& filename, const AidlDocumentItem* items) { |
| 149 | if (! items) |
| 150 | return true; |
| 151 | |
| 152 | if (items->item_type == INTERFACE_TYPE_BINDER) { |
| 153 | const AidlInterface* c = reinterpret_cast<const AidlInterface*>(items); |
| 154 | return check_filename(filename, c->GetPackage(), c->GetName(), c->GetLine()); |
| 155 | } |
| 156 | |
| 157 | bool success = true; |
| 158 | |
| 159 | for (const AidlParcelable* p = reinterpret_cast<const AidlParcelable*>(items); |
| 160 | p; p = p->next) |
| 161 | success &= check_filename(filename, p->GetPackage(), p->GetName(), |
| 162 | p->GetLine()); |
| 163 | |
| 164 | return success; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 167 | bool gather_types(const std::string& filename, |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 168 | const AidlDocumentItem* all_items, |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 169 | TypeNamespace* types) { |
| 170 | bool success = true; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 171 | |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 172 | if (! all_items) |
| 173 | return true; |
| 174 | |
| 175 | if (all_items->item_type == INTERFACE_TYPE_BINDER) |
| 176 | return types->AddBinderType(reinterpret_cast<const AidlInterface *>(all_items), filename); |
| 177 | |
| 178 | for (const AidlParcelable* item = |
| 179 | reinterpret_cast<const AidlParcelable *>(all_items); |
| 180 | item; item = item->next) { |
| 181 | success &= types->AddParcelableType(item, filename); |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 182 | } |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 183 | |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 184 | return success; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 187 | int check_types(const string& filename, |
Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 188 | const AidlInterface* c, |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 189 | TypeNamespace* types) { |
| 190 | int err = 0; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 191 | |
| 192 | // Has to be a pointer due to deleting copy constructor. No idea why. |
| 193 | map<string, const AidlMethod*> method_names; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 194 | for (const auto& m : c->GetMethods()) { |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 195 | bool oneway = m->IsOneway() || c->IsOneway(); |
| 196 | |
Christopher Wiley | cd8e897 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 197 | if (!types->MaybeAddContainerType(m->GetType().GetName()) || |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 198 | !types->IsValidReturnType(m->GetType(), filename)) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 199 | err = 1; // return type is invalid |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 202 | if (oneway && m->GetType().GetName() != "void") { |
| 203 | cerr << filename << ":" << m->GetLine() |
Christopher Wiley | 45db9ee | 2015-10-26 18:53:53 -0700 | [diff] [blame] | 204 | << " oneway method '" << m->GetName() << "' cannot return a value" |
| 205 | << endl; |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 206 | err = 1; |
| 207 | } |
| 208 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 209 | int index = 1; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 210 | for (const auto& arg : m->GetArguments()) { |
Christopher Wiley | cd8e897 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 211 | if (!types->MaybeAddContainerType(arg->GetType().GetName()) || |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 212 | !types->IsValidArg(*arg, index, filename)) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 213 | err = 1; |
| 214 | } |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 215 | |
| 216 | if (oneway && arg->IsOut()) { |
| 217 | cerr << filename << ":" << m->GetLine() |
Christopher Wiley | 45db9ee | 2015-10-26 18:53:53 -0700 | [diff] [blame] | 218 | << " oneway method '" << m->GetName() |
| 219 | << "' cannot have out parameters" << endl; |
Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 220 | err = 1; |
| 221 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 224 | auto it = method_names.find(m->GetName()); |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 225 | // prevent duplicate methods |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 226 | if (it == method_names.end()) { |
| 227 | method_names[m->GetName()] = m.get(); |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 228 | } else { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 229 | cerr << filename << ":" << m->GetLine() |
| 230 | << " attempt to redefine method " << m->GetName() << "," << endl |
| 231 | << filename << ":" << it->second->GetLine() |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 232 | << " previously defined here." << endl; |
| 233 | err = 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 234 | } |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 235 | } |
| 236 | return err; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Christopher Wiley | ef4132c | 2015-11-05 15:47:40 -0800 | [diff] [blame^] | 239 | void generate_dep_file(const std::string& dep_file_name, |
| 240 | const std::string& input_file_name, |
| 241 | const std::string& output_file_name, |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 242 | const std::vector<std::unique_ptr<AidlImport>>& imports, |
| 243 | const IoDelegate& io_delegate) { |
Christopher Wiley | ef4132c | 2015-11-05 15:47:40 -0800 | [diff] [blame^] | 244 | CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name); |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 245 | if (!writer) { |
Christopher Wiley | ef4132c | 2015-11-05 15:47:40 -0800 | [diff] [blame^] | 246 | cerr << "Could not open " << dep_file_name << endl; |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 247 | return; |
| 248 | } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 249 | |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 250 | |
Christopher Wiley | ef4132c | 2015-11-05 15:47:40 -0800 | [diff] [blame^] | 251 | writer->Write("%s: \\\n", output_file_name.c_str()); |
| 252 | writer->Write(" %s %s\n", input_file_name.c_str(), |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 253 | imports.empty() ? "" : "\\"); |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 254 | |
| 255 | bool first = true; |
| 256 | for (const auto& import : imports) { |
| 257 | if (! first) { |
| 258 | writer->Write(" \\\n"); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 259 | } |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 260 | first = false; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 261 | |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 262 | if (! import->GetFilename().empty()) { |
| 263 | writer->Write(" %s", import->GetFilename().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 264 | } |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 265 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 266 | |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 267 | writer->Write(first ? "\n" : "\n\n"); |
| 268 | |
| 269 | // Output "<input_aidl_file>: " so make won't fail if the input .aidl file |
| 270 | // has been deleted, moved or renamed in incremental build. |
Christopher Wiley | ef4132c | 2015-11-05 15:47:40 -0800 | [diff] [blame^] | 271 | writer->Write("%s :\n", input_file_name.c_str()); |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 272 | |
| 273 | // Output "<imported_file>: " so make won't fail if the imported file has |
| 274 | // been deleted, moved or renamed in incremental build. |
| 275 | for (const auto& import : imports) { |
| 276 | if (! import->GetFilename().empty()) { |
| 277 | writer->Write("%s :\n", import->GetFilename().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 278 | } |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 279 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 282 | string generate_outputFileName(const JavaOptions& options, |
| 283 | const AidlInterface& interface) { |
| 284 | string name = interface.GetName(); |
| 285 | string package = interface.GetPackage(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 286 | string result; |
| 287 | |
| 288 | // create the path to the destination folder based on the |
| 289 | // interface package name |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 290 | result = options.output_base_folder_; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 291 | result += OS_PATH_SEPARATOR; |
| 292 | |
| 293 | string packageStr = package; |
| 294 | size_t len = packageStr.length(); |
| 295 | for (size_t i=0; i<len; i++) { |
| 296 | if (packageStr[i] == '.') { |
| 297 | packageStr[i] = OS_PATH_SEPARATOR; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | result += packageStr; |
| 302 | |
| 303 | // add the filename by replacing the .aidl extension to .java |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 304 | result += OS_PATH_SEPARATOR; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 305 | result.append(name, 0, name.find('.')); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 306 | result += ".java"; |
| 307 | |
| 308 | return result; |
| 309 | } |
| 310 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 311 | int check_and_assign_method_ids(const char * filename, |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 312 | const std::vector<std::unique_ptr<AidlMethod>>& items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 313 | // Check whether there are any methods with manually assigned id's and any that are not. |
| 314 | // Either all method id's must be manually assigned or all of them must not. |
| 315 | // Also, check for duplicates of user set id's and that the id's are within the proper bounds. |
| 316 | set<int> usedIds; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 317 | bool hasUnassignedIds = false; |
| 318 | bool hasAssignedIds = false; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 319 | for (const auto& item : items) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 320 | if (item->HasId()) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 321 | hasAssignedIds = true; |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 322 | // Ensure that the user set id is not duplicated. |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 323 | if (usedIds.find(item->GetId()) != usedIds.end()) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 324 | // We found a duplicate id, so throw an error. |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 325 | fprintf(stderr, |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 326 | "%s:%d Found duplicate method id (%d) for method: %s\n", |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 327 | filename, item->GetLine(), |
| 328 | item->GetId(), item->GetName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 329 | return 1; |
| 330 | } |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 331 | // Ensure that the user set id is within the appropriate limits |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 332 | if (item->GetId() < kMinUserSetMethodId || |
| 333 | item->GetId() > kMaxUserSetMethodId) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 334 | 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] | 335 | filename, item->GetLine(), |
| 336 | item->GetId(), item->GetName().c_str()); |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 337 | fprintf(stderr, " Value for id must be between %d and %d inclusive.\n", |
| 338 | kMinUserSetMethodId, kMaxUserSetMethodId); |
| 339 | return 1; |
| 340 | } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 341 | usedIds.insert(item->GetId()); |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 342 | } else { |
| 343 | hasUnassignedIds = true; |
| 344 | } |
| 345 | if (hasAssignedIds && hasUnassignedIds) { |
| 346 | fprintf(stderr, |
| 347 | "%s: You must either assign id's to all methods or to none of them.\n", |
| 348 | filename); |
| 349 | return 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 350 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // In the case that all methods have unassigned id's, set a unique id for them. |
| 354 | if (hasUnassignedIds) { |
| 355 | int newId = 0; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 356 | for (const auto& item : items) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 357 | item->SetId(newId++); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | // success |
| 362 | return 0; |
| 363 | } |
| 364 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 365 | // TODO: Remove this in favor of using the YACC parser b/25479378 |
| 366 | bool ParsePreprocessedLine(const string& line, string* decl, |
| 367 | vector<string>* package, string* class_name) { |
| 368 | // erase all trailing whitespace and semicolons |
| 369 | const size_t end = line.find_last_not_of(" ;\t"); |
| 370 | if (end == string::npos) { |
| 371 | return false; |
| 372 | } |
| 373 | if (line.rfind(';', end) != string::npos) { |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | decl->clear(); |
| 378 | string type; |
| 379 | vector<string> pieces = Split(line.substr(0, end + 1), " \t"); |
| 380 | for (const string& piece : pieces) { |
| 381 | if (piece.empty()) { |
| 382 | continue; |
| 383 | } |
| 384 | if (decl->empty()) { |
| 385 | *decl = std::move(piece); |
| 386 | } else if (type.empty()) { |
| 387 | type = std::move(piece); |
| 388 | } else { |
| 389 | return false; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // Note that this logic is absolutely wrong. Given a parcelable |
| 394 | // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that |
| 395 | // the class is just Bar. However, this was the way it was done in the past. |
| 396 | // |
| 397 | // See b/17415692 |
| 398 | size_t dot_pos = type.rfind('.'); |
| 399 | if (dot_pos != string::npos) { |
| 400 | *class_name = type.substr(dot_pos + 1); |
| 401 | *package = Split(type.substr(0, dot_pos), "."); |
| 402 | } else { |
| 403 | *class_name = type; |
| 404 | package->clear(); |
| 405 | } |
| 406 | |
| 407 | return true; |
| 408 | } |
| 409 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 410 | } // namespace |
| 411 | |
| 412 | namespace internals { |
| 413 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 414 | bool parse_preprocessed_file(const IoDelegate& io_delegate, |
| 415 | const string& filename, TypeNamespace* types) { |
| 416 | bool success = true; |
| 417 | unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename); |
| 418 | if (!line_reader) { |
| 419 | LOG(ERROR) << "cannot open preprocessed file: " << filename; |
| 420 | success = false; |
| 421 | return success; |
| 422 | } |
| 423 | |
| 424 | string line; |
| 425 | unsigned lineno = 1; |
| 426 | for ( ; line_reader->ReadLine(&line); ++lineno) { |
| 427 | if (line.empty() || line.compare(0, 2, "//") == 0) { |
| 428 | // skip comments and empty lines |
| 429 | continue; |
| 430 | } |
| 431 | |
| 432 | string decl; |
| 433 | vector<string> package; |
| 434 | string class_name; |
| 435 | if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) { |
| 436 | success = false; |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | if (decl == "parcelable") { |
| 441 | AidlParcelable doc(class_name, lineno, package); |
| 442 | types->AddParcelableType(&doc, filename); |
| 443 | } else if (decl == "interface") { |
| 444 | auto temp = new std::vector<std::unique_ptr<AidlMethod>>(); |
| 445 | AidlInterface doc(class_name, lineno, "", false, temp, package); |
| 446 | types->AddBinderType(&doc, filename); |
| 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 | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 460 | int load_and_validate_aidl(const std::vector<std::string> preprocessed_files, |
| 461 | const std::vector<std::string> import_paths, |
| 462 | const std::string& input_file_name, |
| 463 | const IoDelegate& io_delegate, |
| 464 | TypeNamespace* types, |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 465 | std::unique_ptr<AidlInterface>* returned_interface, |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 466 | std::vector<std::unique_ptr<AidlImport>>* returned_imports) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 467 | int err = 0; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 468 | |
Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 469 | std::map<AidlImport*,std::unique_ptr<AidlDocumentItem>> docs; |
| 470 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 471 | // import the preprocessed file |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 472 | for (const string& s : preprocessed_files) { |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 473 | if (!parse_preprocessed_file(io_delegate, s, types)) { |
| 474 | err |= 1; |
| 475 | } |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 476 | } |
| 477 | if (err != 0) { |
| 478 | return err; |
| 479 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 480 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 481 | // parse the input file |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 482 | Parser p{io_delegate}; |
| 483 | if (!p.ParseFile(input_file_name)) { |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 484 | return 1; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 485 | } |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 486 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 487 | AidlDocumentItem* parsed_doc = p.GetDocument(); |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 488 | // We could in theory declare parcelables in the same file as the interface. |
| 489 | // In practice, those parcelables would have to have the same name as |
| 490 | // the interface, since this was originally written to support Java, with its |
| 491 | // packages and names that correspond to file system structure. |
| 492 | // Since we can't have two distinct classes with the same name and package, |
| 493 | // we can't actually declare parcelables in the same file. |
| 494 | if (parsed_doc == nullptr || |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 495 | parsed_doc->item_type != INTERFACE_TYPE_BINDER) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 496 | cerr << "aidl expects exactly one interface per input file"; |
Christopher Wiley | a2f516d | 2015-09-24 10:12:31 -0700 | [diff] [blame] | 497 | return 1; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 498 | } |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 499 | unique_ptr<AidlInterface> interface( |
| 500 | reinterpret_cast<AidlInterface*>(parsed_doc)); |
| 501 | |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 502 | if (!check_filename(input_file_name.c_str(), interface->GetPackage(), |
| 503 | interface->GetName(), interface->GetLine())) |
| 504 | err |= 1; |
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 | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 520 | err |= 1; |
| 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 | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 529 | err |= 1; |
| 530 | continue; |
| 531 | } |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 532 | |
Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 533 | AidlDocumentItem* document = p.GetDocument(); |
| 534 | if (!check_filenames(import->GetFilename(), document)) |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 535 | err |= 1; |
Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 536 | docs[import.get()] = std::unique_ptr<AidlDocumentItem>(document); |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 537 | } |
| 538 | if (err != 0) { |
| 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 |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 543 | if (!gather_types(input_file_name.c_str(), parsed_doc, types)) { |
| 544 | err |= 1; |
| 545 | } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 546 | for (const auto& import : p.GetImports()) { |
Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 547 | if (!gather_types(import->GetFilename(), docs[import.get()].get(), types)) { |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 548 | err |= 1; |
| 549 | } |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 550 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 551 | |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 552 | if (!types->IsValidPackage(interface->GetPackage())) { |
| 553 | LOG(ERROR) << "Invalid package declaration '" << interface->GetPackage() << "'"; |
| 554 | err += 1; |
| 555 | } |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 556 | // check the referenced types in parsed_doc to make sure we've imported them |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 557 | err |= check_types(input_file_name, interface.get(), types); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [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 | // assign method ids and validate. |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 561 | err |= check_and_assign_method_ids(input_file_name.c_str(), |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 562 | interface->GetMethods()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 563 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 564 | // after this, there shouldn't be any more errors because of the |
| 565 | // input. |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 566 | if (err != 0) { |
| 567 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 568 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 569 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 570 | if (returned_interface) |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 571 | *returned_interface = std::move(interface); |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 572 | |
| 573 | if (returned_imports) |
| 574 | p.ReleaseImports(returned_imports); |
| 575 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 579 | } // namespace internals |
| 580 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 581 | int compile_aidl_to_cpp(const CppOptions& options, |
| 582 | const IoDelegate& io_delegate) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 583 | unique_ptr<AidlInterface> interface; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 584 | std::vector<std::unique_ptr<AidlImport>> imports; |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 585 | unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace()); |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 586 | types->Init(); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 587 | int err = internals::load_and_validate_aidl( |
| 588 | std::vector<std::string>{}, // no preprocessed files |
| 589 | options.ImportPaths(), |
| 590 | options.InputFileName(), |
| 591 | io_delegate, |
| 592 | types.get(), |
| 593 | &interface, |
| 594 | &imports); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 595 | if (err != 0) { |
| 596 | return err; |
| 597 | } |
| 598 | |
| 599 | // TODO(wiley) b/23600457 generate a dependency file if requested with -b |
| 600 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 601 | return (cpp::GenerateCpp(options, *types, *interface, io_delegate)) ? 0 : 1; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 604 | int compile_aidl_to_java(const JavaOptions& 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 | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 608 | unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace()); |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 609 | types->Init(); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 610 | int err = internals::load_and_validate_aidl( |
| 611 | options.preprocessed_files_, |
| 612 | options.import_paths_, |
| 613 | options.input_file_name_, |
| 614 | io_delegate, |
| 615 | types.get(), |
| 616 | &interface, |
| 617 | &imports); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 618 | if (err != 0) { |
| 619 | return err; |
| 620 | } |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 621 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 622 | string output_file_name = options.output_file_name_; |
| 623 | // if needed, generate the output file name from the base folder |
| 624 | if (output_file_name.length() == 0 && |
| 625 | options.output_base_folder_.length() > 0) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 626 | output_file_name = generate_outputFileName(options, *interface); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 627 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 628 | |
Casey Dahlin | 6453351 | 2015-10-23 17:11:21 -0700 | [diff] [blame] | 629 | // make sure the folders of the output file all exists |
| 630 | if (!io_delegate.CreatePathForFile(output_file_name)) { |
| 631 | return 1; |
| 632 | } |
| 633 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 634 | // if we were asked to, generate a make dependency file |
Christopher Wiley | ef4132c | 2015-11-05 15:47:40 -0800 | [diff] [blame^] | 635 | string dep_file_name = options.DependencyFilePath(); |
| 636 | if (!dep_file_name.empty()) { |
| 637 | generate_dep_file(dep_file_name, options.input_file_name_, |
| 638 | options.output_file_name_, imports, io_delegate); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 639 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 640 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 641 | err = generate_java(output_file_name, options.input_file_name_.c_str(), |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 642 | interface.get(), types.get(), io_delegate); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 643 | |
| 644 | return err; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 647 | int preprocess_aidl(const JavaOptions& options, |
| 648 | const IoDelegate& io_delegate) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 649 | vector<string> lines; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 650 | |
| 651 | // read files |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 652 | int N = options.files_to_preprocess_.size(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 653 | for (int i=0; i<N; i++) { |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 654 | Parser p{io_delegate}; |
| 655 | if (!p.ParseFile(options.files_to_preprocess_[i])) |
| 656 | return 1; |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 657 | AidlDocumentItem* doc = p.GetDocument(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 658 | string line; |
| 659 | if (doc->item_type == USER_DATA_TYPE) { |
Casey Dahlin | 5ac9020 | 2015-10-09 15:16:43 -0700 | [diff] [blame] | 660 | AidlParcelable* parcelable = reinterpret_cast<AidlParcelable*>(doc); |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 661 | |
| 662 | line = "parcelable "; |
| 663 | |
| 664 | if (! parcelable->GetPackage().empty()) { |
| 665 | line += parcelable->GetPackage(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 666 | line += '.'; |
| 667 | } |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 668 | line += parcelable->GetName(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 669 | } else { |
| 670 | line = "interface "; |
Casey Dahlin | 5ac9020 | 2015-10-09 15:16:43 -0700 | [diff] [blame] | 671 | AidlInterface* iface = reinterpret_cast<AidlInterface*>(doc); |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 672 | if (!iface->GetPackage().empty()) { |
| 673 | line += iface->GetPackage(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 674 | line += '.'; |
| 675 | } |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 676 | line += iface->GetName(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 677 | } |
| 678 | line += ";\n"; |
| 679 | lines.push_back(line); |
| 680 | } |
| 681 | |
| 682 | // write preprocessed file |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 683 | int fd = open( options.output_file_name_.c_str(), |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 684 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 685 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 686 | _S_IREAD|_S_IWRITE); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 687 | #else |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 688 | S_IRUSR|S_IWUSR|S_IRGRP); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 689 | #endif |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 690 | if (fd == -1) { |
| 691 | fprintf(stderr, "aidl: could not open file for write: %s\n", |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 692 | options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 693 | return 1; |
| 694 | } |
| 695 | |
| 696 | N = lines.size(); |
| 697 | for (int i=0; i<N; i++) { |
| 698 | const string& s = lines[i]; |
| 699 | int len = s.length(); |
| 700 | if (len != write(fd, s.c_str(), len)) { |
| 701 | fprintf(stderr, "aidl: error writing to file %s\n", |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 702 | options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 703 | close(fd); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 704 | unlink(options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 705 | return 1; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | close(fd); |
| 710 | return 0; |
| 711 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 712 | |
| 713 | } // namespace android |
| 714 | } // namespace aidl |