Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -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 | */ |
| 16 | |
| 17 | #include "import_resolver.h" |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 18 | #include "aidl_language.h" |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 19 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 20 | #include <android-base/file.h> |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 21 | #include <android-base/strings.h> |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
| 24 | #ifdef _WIN32 |
| 25 | #include <io.h> |
| 26 | #endif |
| 27 | |
| 28 | #include "os.h" |
| 29 | |
| 30 | using std::string; |
| 31 | using std::vector; |
| 32 | |
| 33 | namespace android { |
| 34 | namespace aidl { |
| 35 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 36 | ImportResolver::ImportResolver(const IoDelegate& io_delegate, const string& input_file_name, |
| 37 | const set<string>& import_paths, const vector<string>& input_files) |
| 38 | : io_delegate_(io_delegate), input_file_name_(input_file_name), input_files_(input_files) { |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 39 | for (string path : import_paths) { |
| 40 | if (path.empty()) { |
| 41 | path = "."; |
| 42 | } |
| 43 | if (path[path.size() - 1] != OS_PATH_SEPARATOR) { |
| 44 | path += OS_PATH_SEPARATOR; |
| 45 | } |
| 46 | import_paths_.push_back(std::move(path)); |
| 47 | } |
| 48 | } |
| 49 | |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 50 | string ImportResolver::FindImportFile(const string& canonical_name) const { |
| 51 | // Convert the canonical name to a relative file path. |
| 52 | string relative_path = canonical_name; |
| 53 | for (char& c : relative_path) { |
| 54 | if (c == '.') { |
| 55 | c = OS_PATH_SEPARATOR; |
| 56 | } |
| 57 | } |
| 58 | relative_path += ".aidl"; |
| 59 | |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 60 | // Look for that relative path at each of our import roots. |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 61 | vector<string> found_paths; |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 62 | for (string path : import_paths_) { |
| 63 | path = path + relative_path; |
| 64 | if (io_delegate_.FileIsReadable(path)) { |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 65 | found_paths.emplace_back(path); |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 69 | int num_found = found_paths.size(); |
| 70 | if (num_found == 0) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 71 | // If not found from the import paths, try to find from the input files |
| 72 | relative_path.insert(0, 1, OS_PATH_SEPARATOR); |
| 73 | for (string input_file : input_files_) { |
| 74 | if (android::base::EndsWith(input_file, relative_path)) { |
| 75 | return input_file; |
| 76 | } |
| 77 | } |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 78 | return ""; |
| 79 | } else if (num_found == 1) { |
| 80 | return found_paths.front(); |
| 81 | } else { |
| 82 | AIDL_ERROR(input_file_name_) << "Duplicate files found for " << canonical_name |
| 83 | << " from:" << std::endl |
| 84 | << android::base::Join(found_paths, "\n"); |
| 85 | return ""; |
| 86 | } |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace android |
| 90 | } // namespace aidl |