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