blob: 10cc4e47b0336dd2070127acc0d83fb1a27f7dd9 [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"
Christopher Wiley72877ac2015-10-06 14:41:42 -070019
Jiyong Parkc6816252019-07-08 08:12:28 +090020#include <algorithm>
21
Jiyong Parke59c3682018-09-11 23:10:25 +090022#include <android-base/file.h>
Jiyong Park02da7422018-07-16 16:00:26 +090023#include <android-base/strings.h>
Christopher Wiley72877ac2015-10-06 14:41:42 -070024#include <unistd.h>
25
26#ifdef _WIN32
27#include <io.h>
28#endif
29
30#include "os.h"
31
32using std::string;
33using std::vector;
34
35namespace android {
36namespace aidl {
37
Jiyong Park8c380532018-08-30 14:55:26 +090038ImportResolver::ImportResolver(const IoDelegate& io_delegate, const string& input_file_name,
39 const set<string>& import_paths, const vector<string>& input_files)
40 : io_delegate_(io_delegate), input_file_name_(input_file_name), input_files_(input_files) {
Christopher Wiley72877ac2015-10-06 14:41:42 -070041 for (string path : import_paths) {
42 if (path.empty()) {
43 path = ".";
44 }
45 if (path[path.size() - 1] != OS_PATH_SEPARATOR) {
46 path += OS_PATH_SEPARATOR;
47 }
48 import_paths_.push_back(std::move(path));
49 }
50}
51
Christopher Wiley72877ac2015-10-06 14:41:42 -070052string ImportResolver::FindImportFile(const string& canonical_name) const {
53 // Convert the canonical name to a relative file path.
54 string relative_path = canonical_name;
55 for (char& c : relative_path) {
56 if (c == '.') {
57 c = OS_PATH_SEPARATOR;
58 }
59 }
60 relative_path += ".aidl";
61
Christopher Wiley72877ac2015-10-06 14:41:42 -070062 // Look for that relative path at each of our import roots.
Jiyong Park8c380532018-08-30 14:55:26 +090063 vector<string> found_paths;
Christopher Wiley72877ac2015-10-06 14:41:42 -070064 for (string path : import_paths_) {
65 path = path + relative_path;
66 if (io_delegate_.FileIsReadable(path)) {
Jiyong Park8c380532018-08-30 14:55:26 +090067 found_paths.emplace_back(path);
Christopher Wiley72877ac2015-10-06 14:41:42 -070068 }
69 }
Jiyong Parkc6816252019-07-08 08:12:28 +090070 // remove duplicates
71 std::sort(found_paths.begin(), found_paths.end());
72 auto last = std::unique(found_paths.begin(), found_paths.end());
73 found_paths.erase(last, found_paths.end());
Christopher Wiley72877ac2015-10-06 14:41:42 -070074
Jiyong Park8c380532018-08-30 14:55:26 +090075 int num_found = found_paths.size();
76 if (num_found == 0) {
77 return "";
78 } else if (num_found == 1) {
79 return found_paths.front();
80 } else {
81 AIDL_ERROR(input_file_name_) << "Duplicate files found for " << canonical_name
82 << " from:" << std::endl
83 << android::base::Join(found_paths, "\n");
84 return "";
85 }
Christopher Wiley72877ac2015-10-06 14:41:42 -070086}
87
Christopher Wiley72877ac2015-10-06 14:41:42 -070088} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -070089} // namespace android