|
14 | 14 |
|
15 | 15 | #include "tools/worker/compile_without_worker.h" |
16 | 16 |
|
17 | | -#include <iostream> |
| 17 | +#include <fstream> |
18 | 18 | #include <string> |
19 | 19 | #include <vector> |
20 | 20 |
|
| 21 | +#include "tools/common/file_system.h" |
| 22 | +#include "tools/common/string_utils.h" |
| 23 | +#include "tools/common/temp_file.h" |
21 | 24 | #include "tools/worker/swift_runner.h" |
22 | 25 |
|
| 26 | +namespace { |
| 27 | + |
| 28 | +static void ProcessPossibleResponseFile( |
| 29 | + const std::string &arg, std::function<void(const std::string &)> consumer) { |
| 30 | + auto path = arg.substr(1); |
| 31 | + std::ifstream original_file(path); |
| 32 | + // If we couldn't open it, maybe it's not a file; maybe it's just some other |
| 33 | + // argument that starts with "@". (Unlikely, but it's safer to check.) |
| 34 | + if (!original_file.good()) { |
| 35 | + consumer(arg); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + std::string arg_from_file; |
| 40 | + while (std::getline(original_file, arg_from_file)) { |
| 41 | + // Arguments in response files might be quoted/escaped, so we need to |
| 42 | + // unescape them ourselves. |
| 43 | + consumer(Unescape(arg_from_file)); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +static std::tuple<bool, std::vector<std::string>> |
| 48 | + ArgumentsIncludingParamsContent(const std::vector<std::string> &args) { |
| 49 | + std::vector<std::string> full_args; |
| 50 | + std::string prev_arg; |
| 51 | + std::string index_store_path; |
| 52 | + std::string global_index_store_path; |
| 53 | + bool process_args = false; |
| 54 | + |
| 55 | + auto consumer = [&](const std::string &arg) { |
| 56 | + if (arg == "-index-store-path") { |
| 57 | + // Peel off the `-index-store-path` argument, so we can rewrite it if |
| 58 | + // necessary later. |
| 59 | + } else if (prev_arg == "-index-store-path") { |
| 60 | + index_store_path = arg; |
| 61 | + } else if (arg == "-Xwrapped-swift=-global-index-store-path") { |
| 62 | + // Minimally we want to not pass this argument to the worker, since it |
| 63 | + // can't process it |
| 64 | + process_args = true; |
| 65 | + } else if (prev_arg == "-Xwrapped-swift=-global-index-store-path") { |
| 66 | + global_index_store_path = arg; |
| 67 | + } else { |
| 68 | + full_args.push_back(arg); |
| 69 | + } |
| 70 | + |
| 71 | + prev_arg = arg; |
| 72 | + }; |
| 73 | + |
| 74 | + for (auto arg : args) { |
| 75 | + if (arg[0] == '@') { |
| 76 | + ProcessPossibleResponseFile(arg, consumer); |
| 77 | + } else { |
| 78 | + consumer(arg); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (!global_index_store_path.empty() && FileExists(global_index_store_path + "/rules_swift_global_index_enabled")) { |
| 83 | + full_args.push_back("-index-store-path"); |
| 84 | + full_args.push_back(global_index_store_path); |
| 85 | + } else if (!index_store_path.empty()) { |
| 86 | + // Add back index store path |
| 87 | + full_args.push_back("-index-store-path"); |
| 88 | + full_args.push_back(index_store_path); |
| 89 | + } |
| 90 | + |
| 91 | + return std::make_tuple(process_args, full_args); |
| 92 | +} |
| 93 | + |
| 94 | +} // end namespace |
| 95 | + |
23 | 96 | int CompileWithoutWorker(const std::vector<std::string> &args) { |
24 | | - return SwiftRunner(args).Run(&std::cerr, /*stdout_to_stderr=*/false); |
| 97 | + std::vector<std::string> actual_args(args.begin() + 1, args.end()); |
| 98 | + auto arguments = ArgumentsIncludingParamsContent(actual_args); |
| 99 | + |
| 100 | + bool process_args = std::get<0>(arguments); |
| 101 | + if (!process_args) { |
| 102 | + return SwiftRunner(args).Run(&std::cerr, /*stdout_to_stderr=*/false); |
| 103 | + } |
| 104 | + |
| 105 | + // We have to rewrite the arguments. This means that if we just try to pass |
| 106 | + // the new arguments verbatim to swiftc, we might end up with a command line |
| 107 | + // that's too long. Rather than try to figure out these limits (which is very |
| 108 | + // OS-specific and easy to get wrong), we unconditionally write the processed |
| 109 | + // arguments out to a params file. |
| 110 | + auto params_file = TempFile::Create("swiftc_params.XXXXXX"); |
| 111 | + std::ofstream params_file_stream(params_file->GetPath()); |
| 112 | + |
| 113 | + // Write out all processed arguments |
| 114 | + for (auto arg : std::get<1>(arguments)) { |
| 115 | + params_file_stream << arg << '\n'; |
| 116 | + } |
| 117 | + |
| 118 | + std::vector<std::string> new_args(args.begin(), args.begin() + 1); |
| 119 | + new_args.push_back("@" + params_file->GetPath()); |
| 120 | + params_file_stream.close(); |
| 121 | + |
| 122 | + return SwiftRunner(new_args).Run(&std::cerr, /*stdout_to_stderr=*/false); |
25 | 123 | } |
0 commit comments