blob: edde65bd020abf6bc8df962f735c4a0e0626b423 [file] [log] [blame]
Christopher Wiley72877ac2015-10-06 14:41:42 -07001/*
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 Park8c380532018-08-30 14:55:26 +090018#include "aidl_language.h"
Jiyong Park2a7c92b2020-07-22 19:12:36 +090019#include "logging.h"
Christopher Wiley72877ac2015-10-06 14:41:42 -070020
Jiyong Parkc6816252019-07-08 08:12:28 +090021#include <algorithm>
22
Jiyong Parke59c3682018-09-11 23:10:25 +090023#include <android-base/file.h>
Jiyong Park02da7422018-07-16 16:00:26 +090024#include <android-base/strings.h>
Christopher Wiley72877ac2015-10-06 14:41:42 -070025#include <unistd.h>
26
27#ifdef _WIN32
28#include <io.h>
29#endif
30
31#include "os.h"
32
33using std::string;
34using std::vector;
35
36namespace android {
37namespace aidl {
38
Jiyong Park8c380532018-08-30 14:55:26 +090039ImportResolver::ImportResolver(const IoDelegate& io_delegate, const string& input_file_name,
40 const set<string>& import_paths, const vector<string>& input_files)
41 : io_delegate_(io_delegate), input_file_name_(input_file_name), input_files_(input_files) {
Christopher Wiley72877ac2015-10-06 14:41:42 -070042 for (string path : import_paths) {
43 if (path.empty()) {
44 path = ".";
45 }
46 if (path[path.size() - 1] != OS_PATH_SEPARATOR) {
47 path += OS_PATH_SEPARATOR;
48 }
49 import_paths_.push_back(std::move(path));
50 }
51}
52
Christopher Wiley72877ac2015-10-06 14:41:42 -070053string ImportResolver::FindImportFile(const string& canonical_name) const {
54 // Convert the canonical name to a relative file path.
55 string relative_path = canonical_name;
56 for (char& c : relative_path) {
57 if (c == '.') {
58 c = OS_PATH_SEPARATOR;
59 }
60 }
61 relative_path += ".aidl";
62
Christopher Wiley72877ac2015-10-06 14:41:42 -070063 // Look for that relative path at each of our import roots.
Jiyong Park8c380532018-08-30 14:55:26 +090064 vector<string> found_paths;
Christopher Wiley72877ac2015-10-06 14:41:42 -070065 for (string path : import_paths_) {
66 path = path + relative_path;
67 if (io_delegate_.FileIsReadable(path)) {
Jiyong Park8c380532018-08-30 14:55:26 +090068 found_paths.emplace_back(path);
Christopher Wiley72877ac2015-10-06 14:41:42 -070069 }
70 }
Jiyong Parkc6816252019-07-08 08:12:28 +090071 // remove duplicates
72 std::sort(found_paths.begin(), found_paths.end());
73 auto last = std::unique(found_paths.begin(), found_paths.end());
74 found_paths.erase(last, found_paths.end());
Christopher Wiley72877ac2015-10-06 14:41:42 -070075
Jiyong Park8c380532018-08-30 14:55:26 +090076 int num_found = found_paths.size();
77 if (num_found == 0) {
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 Wiley72877ac2015-10-06 14:41:42 -070087}
88
Christopher Wiley72877ac2015-10-06 14:41:42 -070089} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -070090} // namespace android