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 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 197 | if (!types->AddContainerType(m->GetType().GetName()) || |
| 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() |
| 204 | << "oneway method cannot return a value: " |
| 205 | << m->GetName() << endl; |
| 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()) { |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 211 | if (!types->AddContainerType(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() |
| 218 | << "oneway method cannot have out parameters: " |
| 219 | << m->GetName() << endl; |
| 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 | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 239 | void generate_dep_file(const JavaOptions& options, |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 240 | const std::vector<std::unique_ptr<AidlImport>>& imports, |
| 241 | const IoDelegate& io_delegate) { |
| 242 | string fileName; |
| 243 | if (options.auto_dep_file_) { |
| 244 | fileName = options.output_file_name_ + ".d"; |
| 245 | } else { |
| 246 | fileName = options.dep_file_name_; |
| 247 | } |
| 248 | CodeWriterPtr writer = io_delegate.GetCodeWriter(fileName); |
| 249 | if (!writer) { |
| 250 | cerr << "Could not open " << fileName << endl; |
| 251 | return; |
| 252 | } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 253 | |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 254 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 255 | writer->Write("%s: \\\n", options.output_file_name_.c_str()); |
| 256 | writer->Write(" %s %s\n", options.input_file_name_.c_str(), |
| 257 | imports.empty() ? "" : "\\"); |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 258 | |
| 259 | bool first = true; |
| 260 | for (const auto& import : imports) { |
| 261 | if (! first) { |
| 262 | writer->Write(" \\\n"); |
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 | first = false; |
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 | if (! import->GetFilename().empty()) { |
| 267 | writer->Write(" %s", import->GetFilename().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 268 | } |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 269 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 270 | |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 271 | writer->Write(first ? "\n" : "\n\n"); |
| 272 | |
| 273 | // Output "<input_aidl_file>: " so make won't fail if the input .aidl file |
| 274 | // has been deleted, moved or renamed in incremental build. |
| 275 | writer->Write("%s :\n", options.input_file_name_.c_str()); |
| 276 | |
| 277 | // Output "<imported_file>: " so make won't fail if the imported file has |
| 278 | // been deleted, moved or renamed in incremental build. |
| 279 | for (const auto& import : imports) { |
| 280 | if (! import->GetFilename().empty()) { |
| 281 | writer->Write("%s :\n", import->GetFilename().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 282 | } |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 283 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 286 | string generate_outputFileName(const JavaOptions& options, |
| 287 | const AidlInterface& interface) { |
| 288 | string name = interface.GetName(); |
| 289 | string package = interface.GetPackage(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 290 | string result; |
| 291 | |
| 292 | // create the path to the destination folder based on the |
| 293 | // interface package name |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 294 | result = options.output_base_folder_; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 295 | result += OS_PATH_SEPARATOR; |
| 296 | |
| 297 | string packageStr = package; |
| 298 | size_t len = packageStr.length(); |
| 299 | for (size_t i=0; i<len; i++) { |
| 300 | if (packageStr[i] == '.') { |
| 301 | packageStr[i] = OS_PATH_SEPARATOR; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | result += packageStr; |
| 306 | |
| 307 | // add the filename by replacing the .aidl extension to .java |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 308 | result += OS_PATH_SEPARATOR; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 309 | result.append(name, 0, name.find('.')); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 310 | result += ".java"; |
| 311 | |
| 312 | return result; |
| 313 | } |
| 314 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 315 | void check_outputFilePath(const string& path) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 316 | size_t len = path.length(); |
| 317 | for (size_t i=0; i<len ; i++) { |
| 318 | if (path[i] == OS_PATH_SEPARATOR) { |
| 319 | string p = path.substr(0, i); |
| 320 | if (access(path.data(), F_OK) != 0) { |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 321 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 322 | _mkdir(p.data()); |
| 323 | #else |
| 324 | mkdir(p.data(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); |
| 325 | #endif |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 332 | int parse_preprocessed_file(const string& filename, TypeNamespace* types) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 333 | FILE* f = fopen(filename.c_str(), "rb"); |
| 334 | if (f == NULL) { |
| 335 | fprintf(stderr, "aidl: can't open preprocessed file: %s\n", |
| 336 | filename.c_str()); |
| 337 | return 1; |
| 338 | } |
| 339 | |
| 340 | int lineno = 1; |
| 341 | char line[1024]; |
| 342 | char type[1024]; |
| 343 | char fullname[1024]; |
| 344 | while (fgets(line, sizeof(line), f)) { |
| 345 | // skip comments and empty lines |
| 346 | if (!line[0] || strncmp(line, "//", 2) == 0) { |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | sscanf(line, "%s %[^; \r\n\t];", type, fullname); |
| 351 | |
Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 352 | char* classname = strrchr(fullname, '.'); |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 353 | vector<string> package; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 354 | if (classname != NULL) { |
| 355 | *classname = '\0'; |
| 356 | classname++; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 357 | package = Split(fullname, "."); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 358 | } else { |
| 359 | classname = fullname; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | //printf("%s:%d:...%s...%s...%s...\n", filename.c_str(), lineno, |
| 363 | // type, packagename, classname); |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 364 | AidlDocumentItem* doc; |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 365 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 366 | if (0 == strcmp("parcelable", type)) { |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 367 | doc = new AidlParcelable(classname, lineno, package); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 368 | } |
| 369 | else if (0 == strcmp("interface", type)) { |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 370 | auto temp = new std::vector<std::unique_ptr<AidlMethod>>(); |
Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 371 | doc = new AidlInterface(classname, lineno, "", false, temp, |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 372 | package); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 373 | } |
| 374 | else { |
| 375 | fprintf(stderr, "%s:%d: bad type in line: %s\n", |
| 376 | filename.c_str(), lineno, line); |
Elliott Hughes | 5cd0607 | 2013-10-29 15:25:52 -0700 | [diff] [blame] | 377 | fclose(f); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 378 | return 1; |
| 379 | } |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 380 | if (!gather_types(filename.c_str(), doc, types)) { |
| 381 | fprintf(stderr, "Failed to gather types for preprocessed aidl.\n"); |
| 382 | fclose(f); |
| 383 | return 1; |
| 384 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 385 | lineno++; |
| 386 | } |
| 387 | |
| 388 | if (!feof(f)) { |
| 389 | fprintf(stderr, "%s:%d: error reading file, line to long.\n", |
| 390 | filename.c_str(), lineno); |
| 391 | return 1; |
| 392 | } |
| 393 | |
| 394 | fclose(f); |
| 395 | return 0; |
| 396 | } |
| 397 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 398 | int check_and_assign_method_ids(const char * filename, |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 399 | const std::vector<std::unique_ptr<AidlMethod>>& items) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 400 | // Check whether there are any methods with manually assigned id's and any that are not. |
| 401 | // Either all method id's must be manually assigned or all of them must not. |
| 402 | // Also, check for duplicates of user set id's and that the id's are within the proper bounds. |
| 403 | set<int> usedIds; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 404 | bool hasUnassignedIds = false; |
| 405 | bool hasAssignedIds = false; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 406 | for (const auto& item : items) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 407 | if (item->HasId()) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 408 | hasAssignedIds = true; |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 409 | // Ensure that the user set id is not duplicated. |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 410 | if (usedIds.find(item->GetId()) != usedIds.end()) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 411 | // We found a duplicate id, so throw an error. |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 412 | fprintf(stderr, |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 413 | "%s:%d Found duplicate method id (%d) for method: %s\n", |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 414 | filename, item->GetLine(), |
| 415 | item->GetId(), item->GetName().c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 416 | return 1; |
| 417 | } |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 418 | // Ensure that the user set id is within the appropriate limits |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 419 | if (item->GetId() < kMinUserSetMethodId || |
| 420 | item->GetId() > kMaxUserSetMethodId) { |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 421 | 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] | 422 | filename, item->GetLine(), |
| 423 | item->GetId(), item->GetName().c_str()); |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 424 | fprintf(stderr, " Value for id must be between %d and %d inclusive.\n", |
| 425 | kMinUserSetMethodId, kMaxUserSetMethodId); |
| 426 | return 1; |
| 427 | } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 428 | usedIds.insert(item->GetId()); |
Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 429 | } else { |
| 430 | hasUnassignedIds = true; |
| 431 | } |
| 432 | if (hasAssignedIds && hasUnassignedIds) { |
| 433 | fprintf(stderr, |
| 434 | "%s: You must either assign id's to all methods or to none of them.\n", |
| 435 | filename); |
| 436 | return 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 437 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // In the case that all methods have unassigned id's, set a unique id for them. |
| 441 | if (hasUnassignedIds) { |
| 442 | int newId = 0; |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 443 | for (const auto& item : items) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 444 | item->SetId(newId++); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | // success |
| 449 | return 0; |
| 450 | } |
| 451 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 452 | } // namespace |
| 453 | |
| 454 | namespace internals { |
| 455 | |
| 456 | int load_and_validate_aidl(const std::vector<std::string> preprocessed_files, |
| 457 | const std::vector<std::string> import_paths, |
| 458 | const std::string& input_file_name, |
| 459 | const IoDelegate& io_delegate, |
| 460 | TypeNamespace* types, |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 461 | std::unique_ptr<AidlInterface>* returned_interface, |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 462 | std::vector<std::unique_ptr<AidlImport>>* returned_imports) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 463 | int err = 0; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 464 | |
Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 465 | std::map<AidlImport*,std::unique_ptr<AidlDocumentItem>> docs; |
| 466 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 467 | // import the preprocessed file |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 468 | for (const string& s : preprocessed_files) { |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 469 | err |= parse_preprocessed_file(s, types); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 470 | } |
| 471 | if (err != 0) { |
| 472 | return err; |
| 473 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 474 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 475 | // parse the input file |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 476 | Parser p{io_delegate}; |
| 477 | if (!p.ParseFile(input_file_name)) { |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 478 | return 1; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 479 | } |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 480 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 481 | AidlDocumentItem* parsed_doc = p.GetDocument(); |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 482 | // We could in theory declare parcelables in the same file as the interface. |
| 483 | // In practice, those parcelables would have to have the same name as |
| 484 | // the interface, since this was originally written to support Java, with its |
| 485 | // packages and names that correspond to file system structure. |
| 486 | // Since we can't have two distinct classes with the same name and package, |
| 487 | // we can't actually declare parcelables in the same file. |
| 488 | if (parsed_doc == nullptr || |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 489 | parsed_doc->item_type != INTERFACE_TYPE_BINDER) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 490 | cerr << "aidl expects exactly one interface per input file"; |
Christopher Wiley | a2f516d | 2015-09-24 10:12:31 -0700 | [diff] [blame] | 491 | return 1; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 492 | } |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 493 | unique_ptr<AidlInterface> interface( |
| 494 | reinterpret_cast<AidlInterface*>(parsed_doc)); |
| 495 | |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 496 | if (!check_filename(input_file_name.c_str(), interface->GetPackage(), |
| 497 | interface->GetName(), interface->GetLine())) |
| 498 | err |= 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 499 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 500 | // parse the imports of the input file |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 501 | ImportResolver import_resolver{io_delegate, import_paths}; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 502 | for (auto& import : p.GetImports()) { |
| 503 | if (types->HasType(import->GetNeededClass())) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 504 | // There are places in the Android tree where an import doesn't resolve, |
| 505 | // but we'll pick the type up through the preprocessed types. |
| 506 | // This seems like an error, but legacy support demands we support it... |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 507 | continue; |
| 508 | } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 509 | string import_path = import_resolver.FindImportFile(import->GetNeededClass()); |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 510 | if (import_path.empty()) { |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 511 | cerr << import->GetFileFrom() << ":" << import->GetLine() |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 512 | << ": couldn't find import for class " |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 513 | << import->GetNeededClass() << endl; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 514 | err |= 1; |
| 515 | continue; |
| 516 | } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 517 | import->SetFilename(import_path); |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 518 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 519 | Parser p{io_delegate}; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 520 | if (!p.ParseFile(import->GetFilename())) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 521 | cerr << "error while parsing import for class " |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 522 | << import->GetNeededClass() << endl; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 523 | err |= 1; |
| 524 | continue; |
| 525 | } |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 526 | |
Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 527 | AidlDocumentItem* document = p.GetDocument(); |
| 528 | if (!check_filenames(import->GetFilename(), document)) |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 529 | err |= 1; |
Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 530 | docs[import.get()] = std::unique_ptr<AidlDocumentItem>(document); |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 531 | } |
| 532 | if (err != 0) { |
| 533 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 534 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 535 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 536 | // gather the types that have been declared |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 537 | if (!gather_types(input_file_name.c_str(), parsed_doc, types)) { |
| 538 | err |= 1; |
| 539 | } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 540 | for (const auto& import : p.GetImports()) { |
Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 541 | if (!gather_types(import->GetFilename(), docs[import.get()].get(), types)) { |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 542 | err |= 1; |
| 543 | } |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 544 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 545 | |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 546 | if (!types->IsValidPackage(interface->GetPackage())) { |
| 547 | LOG(ERROR) << "Invalid package declaration '" << interface->GetPackage() << "'"; |
| 548 | err += 1; |
| 549 | } |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 550 | // 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] | 551 | err |= check_types(input_file_name, interface.get(), types); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 552 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 553 | |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 554 | // assign method ids and validate. |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 555 | err |= check_and_assign_method_ids(input_file_name.c_str(), |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 556 | interface->GetMethods()); |
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 | // after this, there shouldn't be any more errors because of the |
| 559 | // input. |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 560 | if (err != 0) { |
| 561 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 562 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 563 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 564 | if (returned_interface) |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 565 | *returned_interface = std::move(interface); |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 566 | |
| 567 | if (returned_imports) |
| 568 | p.ReleaseImports(returned_imports); |
| 569 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 570 | return 0; |
| 571 | } |
| 572 | |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 573 | } // namespace internals |
| 574 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 575 | int compile_aidl_to_cpp(const CppOptions& options, |
| 576 | const IoDelegate& io_delegate) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 577 | unique_ptr<AidlInterface> interface; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 578 | std::vector<std::unique_ptr<AidlImport>> imports; |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 579 | unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace()); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 580 | int err = internals::load_and_validate_aidl( |
| 581 | std::vector<std::string>{}, // no preprocessed files |
| 582 | options.ImportPaths(), |
| 583 | options.InputFileName(), |
| 584 | io_delegate, |
| 585 | types.get(), |
| 586 | &interface, |
| 587 | &imports); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 588 | if (err != 0) { |
| 589 | return err; |
| 590 | } |
| 591 | |
| 592 | // TODO(wiley) b/23600457 generate a dependency file if requested with -b |
| 593 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 594 | return (cpp::GenerateCpp(options, *types, *interface, io_delegate)) ? 0 : 1; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 597 | int compile_aidl_to_java(const JavaOptions& options, |
| 598 | const IoDelegate& io_delegate) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 599 | unique_ptr<AidlInterface> interface; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 600 | std::vector<std::unique_ptr<AidlImport>> imports; |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 601 | unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace()); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 602 | int err = internals::load_and_validate_aidl( |
| 603 | options.preprocessed_files_, |
| 604 | options.import_paths_, |
| 605 | options.input_file_name_, |
| 606 | io_delegate, |
| 607 | types.get(), |
| 608 | &interface, |
| 609 | &imports); |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 610 | if (err != 0) { |
| 611 | return err; |
| 612 | } |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 613 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 614 | string output_file_name = options.output_file_name_; |
| 615 | // if needed, generate the output file name from the base folder |
| 616 | if (output_file_name.length() == 0 && |
| 617 | options.output_base_folder_.length() > 0) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 618 | output_file_name = generate_outputFileName(options, *interface); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 619 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 620 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 621 | // if we were asked to, generate a make dependency file |
| 622 | // unless it's a parcelable *and* it's supposed to fail on parcelable |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 623 | if (options.auto_dep_file_ || options.dep_file_name_ != "") { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 624 | // make sure the folders of the output file all exists |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 625 | check_outputFilePath(output_file_name); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 626 | generate_dep_file(options, imports, io_delegate); |
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 | |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 629 | // make sure the folders of the output file all exists |
| 630 | check_outputFilePath(output_file_name); |
| 631 | |
| 632 | err = generate_java(output_file_name, options.input_file_name_.c_str(), |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 633 | interface.get(), types.get(), io_delegate); |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 634 | |
| 635 | return err; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 638 | int preprocess_aidl(const JavaOptions& options, |
| 639 | const IoDelegate& io_delegate) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 640 | vector<string> lines; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 641 | |
| 642 | // read files |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 643 | int N = options.files_to_preprocess_.size(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 644 | for (int i=0; i<N; i++) { |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 645 | Parser p{io_delegate}; |
| 646 | if (!p.ParseFile(options.files_to_preprocess_[i])) |
| 647 | return 1; |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 648 | AidlDocumentItem* doc = p.GetDocument(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 649 | string line; |
| 650 | if (doc->item_type == USER_DATA_TYPE) { |
Casey Dahlin | 5ac9020 | 2015-10-09 15:16:43 -0700 | [diff] [blame] | 651 | AidlParcelable* parcelable = reinterpret_cast<AidlParcelable*>(doc); |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 652 | |
| 653 | line = "parcelable "; |
| 654 | |
| 655 | if (! parcelable->GetPackage().empty()) { |
| 656 | line += parcelable->GetPackage(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 657 | line += '.'; |
| 658 | } |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 659 | line += parcelable->GetName(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 660 | } else { |
| 661 | line = "interface "; |
Casey Dahlin | 5ac9020 | 2015-10-09 15:16:43 -0700 | [diff] [blame] | 662 | AidlInterface* iface = reinterpret_cast<AidlInterface*>(doc); |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 663 | if (!iface->GetPackage().empty()) { |
| 664 | line += iface->GetPackage(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 665 | line += '.'; |
| 666 | } |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 667 | line += iface->GetName(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 668 | } |
| 669 | line += ";\n"; |
| 670 | lines.push_back(line); |
| 671 | } |
| 672 | |
| 673 | // write preprocessed file |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 674 | int fd = open( options.output_file_name_.c_str(), |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 675 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 676 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 677 | _S_IREAD|_S_IWRITE); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 678 | #else |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 679 | S_IRUSR|S_IWUSR|S_IRGRP); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 680 | #endif |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 681 | if (fd == -1) { |
| 682 | fprintf(stderr, "aidl: could not open file for write: %s\n", |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 683 | options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 684 | return 1; |
| 685 | } |
| 686 | |
| 687 | N = lines.size(); |
| 688 | for (int i=0; i<N; i++) { |
| 689 | const string& s = lines[i]; |
| 690 | int len = s.length(); |
| 691 | if (len != write(fd, s.c_str(), len)) { |
| 692 | fprintf(stderr, "aidl: error writing to file %s\n", |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 693 | options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 694 | close(fd); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 695 | unlink(options.output_file_name_.c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 696 | return 1; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | close(fd); |
| 701 | return 0; |
| 702 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 703 | |
| 704 | } // namespace android |
| 705 | } // namespace aidl |