Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <cstdlib> |
| 16 | #include <fstream> |
| 17 | #include <iostream> |
| 18 | #include <regex> // NOLINT |
| 19 | #include <unordered_set> |
| 20 | |
| 21 | using std::ifstream; |
| 22 | using std::regex; |
| 23 | using std::string; |
| 24 | using std::unordered_set; |
Keith Smiley | 48dd159 | 2021-09-09 11:46:47 -0700 | [diff] [blame] | 25 | using std::vector; |
| 26 | |
| 27 | const regex libRegex = regex(".*\\.a$"); |
| 28 | const regex noArgFlags = |
| 29 | regex("-static|-s|-a|-c|-L|-T|-D|-no_warning_for_no_symbols"); |
| 30 | const regex singleArgFlags = regex("-arch_only|-syslibroot|-o"); |
Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 31 | |
| 32 | string getBasename(const string &path) { |
| 33 | // Assumes we're on an OS with "/" as the path separator |
| 34 | auto idx = path.find_last_of("/"); |
| 35 | if (idx == string::npos) { |
| 36 | return path; |
| 37 | } |
| 38 | return path.substr(idx + 1); |
| 39 | } |
| 40 | |
Keith Smiley | 48dd159 | 2021-09-09 11:46:47 -0700 | [diff] [blame] | 41 | vector<string> readFile(const string path) { |
| 42 | vector<string> lines; |
| 43 | ifstream file(path); |
| 44 | string line; |
| 45 | while (std::getline(file, line)) { |
| 46 | if (!line.empty()) { |
| 47 | lines.push_back(line); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return lines; |
| 52 | } |
| 53 | |
| 54 | unordered_set<string> parseArgs(vector<string> args) { |
Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 55 | unordered_set<string> basenames; |
Keith Smiley | 48dd159 | 2021-09-09 11:46:47 -0700 | [diff] [blame] | 56 | for (auto it = args.begin(); it != args.end(); ++it) { |
| 57 | const string arg = *it; |
Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 58 | if (arg == "-filelist") { |
Keith Smiley | 48dd159 | 2021-09-09 11:46:47 -0700 | [diff] [blame] | 59 | ++it; |
| 60 | ifstream list(*it); |
Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 61 | for (string line; getline(list, line);) { |
| 62 | const string basename = getBasename(line); |
| 63 | const auto pair = basenames.insert(basename); |
| 64 | if (!pair.second) { |
Keith Smiley | 48dd159 | 2021-09-09 11:46:47 -0700 | [diff] [blame] | 65 | exit(EXIT_FAILURE); |
Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | list.close(); |
Keith Smiley | 48dd159 | 2021-09-09 11:46:47 -0700 | [diff] [blame] | 69 | } else if (arg[0] == '@') { |
| 70 | string paramsFilePath(arg.substr(1)); |
| 71 | auto newBasenames = parseArgs(readFile(paramsFilePath)); |
| 72 | for (auto newBasename : newBasenames) { |
| 73 | const auto pair = basenames.insert(newBasename); |
| 74 | if (!pair.second) { |
| 75 | exit(EXIT_FAILURE); |
| 76 | } |
| 77 | } |
Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 78 | } else if (regex_match(arg, noArgFlags)) { |
| 79 | } else if (regex_match(arg, singleArgFlags)) { |
Keith Smiley | 48dd159 | 2021-09-09 11:46:47 -0700 | [diff] [blame] | 80 | ++it; |
Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 81 | } else if (arg[0] == '-') { |
Keith Smiley | 48dd159 | 2021-09-09 11:46:47 -0700 | [diff] [blame] | 82 | exit(EXIT_FAILURE); |
| 83 | // Unrecognized flag, let the wrapper deal with it, any flags added to |
| 84 | // libtool.sh should also be added here. |
Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 85 | } else if (regex_match(arg, libRegex)) { |
| 86 | // Archive inputs can remain untouched, as they come from other targets. |
| 87 | } else { |
| 88 | const string basename = getBasename(arg); |
| 89 | const auto pair = basenames.insert(basename); |
| 90 | if (!pair.second) { |
Keith Smiley | 48dd159 | 2021-09-09 11:46:47 -0700 | [diff] [blame] | 91 | exit(EXIT_FAILURE); |
Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
Keith Smiley | 48dd159 | 2021-09-09 11:46:47 -0700 | [diff] [blame] | 95 | |
| 96 | return basenames; |
| 97 | } |
| 98 | |
| 99 | // Returns 0 if there are no duplicate basenames in the object files (via |
| 100 | // -filelist, params files, and shell args), 1 otherwise |
| 101 | int main(int argc, const char *argv[]) { |
| 102 | vector<string> args; |
| 103 | // Set i to 1 to skip executable path |
| 104 | for (int i = 1; argv[i] != nullptr; i++) { |
| 105 | args.push_back(argv[i]); |
| 106 | } |
| 107 | parseArgs(args); |
Michael Eisel | d8723b4 | 2020-08-24 12:08:16 -0700 | [diff] [blame] | 108 | return EXIT_SUCCESS; |
| 109 | } |