@@ -38,8 +38,6 @@ int PW_MAIN(int argc, const CharType* argv[], const CharType* envp[]) {
38
38
39
39
System::EnvironmentBlock environment_file_block;
40
40
41
- using Subst = std::pair<System::StrType, System::StrType>;
42
-
43
41
System::StrType exec_path;
44
42
std::vector<Subst> subst_mappings;
45
43
std::vector<Subst> stamp_mappings;
@@ -79,7 +77,8 @@ int PW_MAIN(int argc, const CharType* argv[], const CharType* envp[]) {
79
77
if (value == PW_SYS_STR (" ${pwd}" )) {
80
78
value = System::GetWorkingDirectory ();
81
79
}
82
- subst_mappings.push_back ({std::move (key), std::move (value)});
80
+ subst_mappings.push_back ({PW_SYS_STR (" ${" ) + key + PW_SYS_STR (" }" ),
81
+ std::move (value)});
83
82
} else if (arg == PW_SYS_STR (" --volatile-status-file" )) {
84
83
if (!volatile_status_file.empty ()) {
85
84
std::cerr << " process wrapper error: \" --volatile-status-file\" can "
@@ -90,11 +89,11 @@ int PW_MAIN(int argc, const CharType* argv[], const CharType* envp[]) {
90
89
return -1 ;
91
90
}
92
91
} else if (arg == PW_SYS_STR (" --env-file" )) {
93
- if (!ReadFileToArray (argv[i], environment_file_block)) {
92
+ if (!ReadFileToVec (argv[i], environment_file_block)) {
94
93
return -1 ;
95
94
}
96
95
} else if (arg == PW_SYS_STR (" --arg-file" )) {
97
- if (!ReadFileToArray (argv[i], file_arguments)) {
96
+ if (!ReadFileToVec (argv[i], file_arguments)) {
98
97
return -1 ;
99
98
}
100
99
} else if (arg == PW_SYS_STR (" --touch-file" )) {
@@ -143,24 +142,77 @@ int PW_MAIN(int argc, const CharType* argv[], const CharType* envp[]) {
143
142
for (++i; i < argc; ++i) {
144
143
arguments.push_back (argv[i]);
145
144
}
146
- // after we consume all arguments we append the files arguments
147
- for (const System::StrType& file_arg : file_arguments) {
148
- arguments.push_back (file_arg);
149
- }
150
145
} else {
151
146
std::cerr << " process wrapper error: unknow argument \" " << ToUtf8 (arg)
152
147
<< " \" ." << ' \n ' ;
153
148
return -1 ;
154
149
}
155
150
}
156
151
152
+ // Expand tokens into the file arguments (since some paths might embed
153
+ // `${pwd}` that we need to resolve from the subsitution mappings).
154
+ for (System::StrType& file_arg : file_arguments) {
155
+ ReplaceTokens (file_arg, subst_mappings);
156
+ }
157
+
158
+ // Process the arguments, making sure that we don't have paths to param files
159
+ // within another param file (that is an arg composed of `@` + a path), which
160
+ // rustc doesn't support.
161
+ System::StrVecType param_file_arguments;
162
+ for (auto arg_it = arguments.begin (); arg_it != arguments.end (); arg_it++) {
163
+ System::StrType& arg = *arg_it;
164
+ ReplaceTokens (arg, subst_mappings);
165
+
166
+ // Check whether this is a regular argument or a path to a param file.
167
+ if (arg.size () > 0 && arg.substr (0 , 1 ) == PW_SYS_STR (" @" )) {
168
+ System::StrType path = arg.substr (1 );
169
+
170
+ // Read the actual params off the param file so that we can 1) extract
171
+ // potential other param files inside and 2) substitute mappings.
172
+ System::StrVecType params;
173
+ if (!ReadFileToVec (path, params)) {
174
+ std::cerr << " Cannot read param file " << ToUtf8 (path) << " \n " ;
175
+ return -1 ;
176
+ }
177
+
178
+ for (auto par_it = params.begin (); par_it != params.end (); par_it++) {
179
+ System::StrType& param = *par_it;
180
+ ReplaceTokens (param, subst_mappings);
181
+
182
+ // If we find a path to a param file, move it to the top-level file
183
+ // arguments instead (we cannot have nested param files).
184
+ if (param.size () > 0 && param.substr (0 , 1 ) == PW_SYS_STR (" @" )) {
185
+ param_file_arguments.push_back (param);
186
+ params.erase (par_it--);
187
+ }
188
+ }
189
+
190
+ // Create a new param file so that we don't need to edit the original one
191
+ // that Bazel created.
192
+ System::StrType new_path = path + PW_SYS_STR (" .expanded" );
193
+ if (!WriteVecToFile (new_path, params)) {
194
+ std::cerr << " Cannot write params to file " << ToUtf8 (new_path) << " \n " ;
195
+ return -1 ;
196
+ }
197
+
198
+ param_file_arguments.push_back (PW_SYS_STR (" @" ) + new_path);
199
+ arguments.erase (arg_it--);
200
+ }
201
+ }
202
+
203
+ // After we consume all arguments we append the param files arguments and the
204
+ // remaining of the files arguments
205
+ for (const System::StrType& file_arg : param_file_arguments) {
206
+ arguments.push_back (file_arg);
207
+ }
208
+ for (const System::StrType& file_arg : file_arguments) {
209
+ arguments.push_back (file_arg);
210
+ }
211
+
157
212
// Stamp any format string in an environment variable block
158
- for (const Subst& stamp : stamp_mappings) {
159
- System::StrType token = PW_SYS_STR (" {" );
160
- token += stamp.first ;
161
- token.push_back (' }' );
213
+ if (stamp_mappings.size ()) {
162
214
for (System::StrType& env : environment_file_block) {
163
- ReplaceToken (env, token, stamp. second );
215
+ ReplaceTokens (env, stamp_mappings );
164
216
}
165
217
}
166
218
@@ -171,19 +223,9 @@ int PW_MAIN(int argc, const CharType* argv[], const CharType* envp[]) {
171
223
environment_file_block.begin (),
172
224
environment_file_block.end ());
173
225
174
- if (subst_mappings.size ()) {
175
- for (const Subst& subst : subst_mappings) {
176
- System::StrType token = PW_SYS_STR (" ${" );
177
- token += subst.first ;
178
- token.push_back (' }' );
179
- for (System::StrType& arg : arguments) {
180
- ReplaceToken (arg, token, subst.second );
181
- }
182
-
183
- for (System::StrType& env : environment_block) {
184
- ReplaceToken (env, token, subst.second );
185
- }
186
- }
226
+ // Expand tokens in environment
227
+ for (System::StrType& env : environment_block) {
228
+ ReplaceTokens (env, subst_mappings);
187
229
}
188
230
189
231
// Have the last values added take precedence over the first.
0 commit comments