• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
18 #include "aidl_language.h"
19 #include "logging.h"
20 
21 #include <algorithm>
22 
23 #include <android-base/file.h>
24 #include <android-base/strings.h>
25 #include <unistd.h>
26 
27 #ifdef _WIN32
28 #include <io.h>
29 #endif
30 
31 #include "os.h"
32 
33 using std::string;
34 using std::vector;
35 
36 namespace android {
37 namespace aidl {
38 
ImportResolver(const IoDelegate & io_delegate,const string & input_file_name,const set<string> & import_paths,const vector<string> & input_files)39 ImportResolver::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) {
42   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 
FindImportFile(const string & canonical_name) const53 string 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 
63   // Look for that relative path at each of our import roots.
64   vector<string> found_paths;
65   for (string path : import_paths_) {
66     path = path + relative_path;
67     if (io_delegate_.FileIsReadable(path)) {
68       found_paths.emplace_back(path);
69     }
70   }
71   // 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());
75 
76   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 << " from:\n"
83                                  << android::base::Join(found_paths, "\n");
84     return "";
85   }
86 }
87 
88 }  // namespace aidl
89 }  // namespace android
90