@@ -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 "
@@ -143,24 +142,68 @@ 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
+ // Process the arguments, making sure that we don't have paths to param files
153
+ // within another param file (that is an arg composed of `@` + a path), which
154
+ // rustc doesn't support.
155
+ // (We process them in reverse so that we can append the param files to the
156
+ // front of `file_arguments` in the right order).
157
+ for (auto arg_it = arguments.rbegin (); arg_it != arguments.rend (); arg_it++) {
158
+ System::StrType& arg = *arg_it;
159
+ ReplaceTokens (arg, subst_mappings);
160
+
161
+ // Check whether this is a regular argument or a path to a param file.
162
+ if (arg.size () > 0 && arg.substr (0 , 1 ) == PW_SYS_STR (" @" )) {
163
+ System::StrType path = arg.substr (1 );
164
+
165
+ // Read the actual params off the param file so that we can 1) extract
166
+ // potential other param files inside and 2) substitute mappings.
167
+ System::StrVecType params;
168
+ if (!ReadFileToArray (path, params)) {
169
+ std::cerr << " Cannot read param file " << ToUtf8 (path) << " \n " ;
170
+ return -1 ;
171
+ }
172
+
173
+ for (auto par_it = params.rbegin (); par_it != params.rend (); par_it++) {
174
+ System::StrType& param = *par_it;
175
+ ReplaceTokens (param, subst_mappings);
176
+
177
+ // If we find a path to a param file, move it to the top-level file
178
+ // arguments instead (we cannot have nested param files).
179
+ if (param.size () > 0 && param.substr (0 , 1 ) == PW_SYS_STR (" @" )) {
180
+ file_arguments.insert (file_arguments.begin (), param);
181
+ params.erase (std::next (par_it).base ());
182
+ }
183
+ }
184
+
185
+ // Create a new param file so that we don't need to edit the original one
186
+ // that Bazel created.
187
+ System::StrType new_path = path + PW_SYS_STR (" .expanded" );
188
+ if (!WriteArrayToFile (new_path, params)) {
189
+ std::cerr << " Cannot write params to file " << ToUtf8 (new_path) << " \n " ;
190
+ return -1 ;
191
+ }
192
+
193
+ arguments.erase (std::next (arg_it).base ());
194
+ file_arguments.insert (file_arguments.begin (), PW_SYS_STR (" @" ) + new_path);
195
+ }
196
+ }
197
+
198
+ // after we consume all arguments we append the files arguments
199
+ for (const System::StrType& file_arg : file_arguments) {
200
+ arguments.push_back (file_arg);
201
+ }
202
+
157
203
// 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 (' }' );
204
+ if (stamp_mappings.size ()) {
162
205
for (System::StrType& env : environment_file_block) {
163
- ReplaceToken (env, token, stamp. second );
206
+ ReplaceTokens (env, stamp_mappings );
164
207
}
165
208
}
166
209
@@ -171,19 +214,9 @@ int PW_MAIN(int argc, const CharType* argv[], const CharType* envp[]) {
171
214
environment_file_block.begin (),
172
215
environment_file_block.end ());
173
216
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
- }
217
+ // Expand tokens in environment
218
+ for (System::StrType& env : environment_block) {
219
+ ReplaceTokens (env, subst_mappings);
187
220
}
188
221
189
222
// Have the last values added take precedence over the first.
0 commit comments