Skip to content

Commit 24e8242

Browse files
keithcopybara-github
authored andcommitted
Fix aggressive params file assumption
Most programs that accept params files use the `@file` syntax. For Apple platform builds `@` can be the start of non-params file arguments as well, such as `-rpath @executable_path/Frameworks`. There is a small list of options where this is the case, so this new behavior no longer assumes params files if args start with `@`, they also have to not start with one of the 3 keywords used with this (from `man dyld` on macOS). This should always hold since params files generated by bazel should always start with `bazel-out`, if someone renames the symlinks to one of the keywords, they're on their own. Previously the workaround was to always make sure to pass the `-Wl,-rpath,@executable_path` form of these arguments, but this makes users not have to worry about this. In a few other places we check this by checking if the file exists, which is likely more accurate, but feels excessive and potentially dangerous in this context. Related: bazelbuild#13148 Fixes: bazelbuild#14316 Closes bazelbuild#14650. PiperOrigin-RevId: 430195929
1 parent c046f96 commit 24e8242

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/LinkCommandLine.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -270,36 +270,43 @@ CommandLine paramCmdLine() {
270270
paramFile, linkTargetType, forcedToolPath, featureConfiguration, actionName, variables);
271271
}
272272

273-
public static void extractArgumentsForStaticLinkParamFile(
273+
private static void extractArgumentsForStaticLinkParamFile(
274274
List<String> args, List<String> commandlineArgs, List<String> paramFileArgs) {
275275
commandlineArgs.add(args.get(0)); // ar command, must not be moved!
276276
int argsSize = args.size();
277277
for (int i = 1; i < argsSize; i++) {
278278
String arg = args.get(i);
279-
if (arg.startsWith("@")) {
279+
if (isLikelyParamFile(arg)) {
280280
commandlineArgs.add(arg); // params file, keep it in the command line
281281
} else {
282282
paramFileArgs.add(arg); // the rest goes to the params file
283283
}
284284
}
285285
}
286286

287-
public static void extractArgumentsForDynamicLinkParamFile(
287+
private static void extractArgumentsForDynamicLinkParamFile(
288288
List<String> args, List<String> commandlineArgs, List<String> paramFileArgs) {
289289
// Note, that it is not important that all linker arguments are extracted so that
290290
// they can be moved into a parameter file, but the vast majority should.
291291
commandlineArgs.add(args.get(0)); // gcc command, must not be moved!
292292
int argsSize = args.size();
293293
for (int i = 1; i < argsSize; i++) {
294294
String arg = args.get(i);
295-
if (arg.startsWith("@")) {
295+
if (isLikelyParamFile(arg)) {
296296
commandlineArgs.add(arg); // params file, keep it in the command line
297297
} else {
298298
paramFileArgs.add(arg); // the rest goes to the params file
299299
}
300300
}
301301
}
302302

303+
private static boolean isLikelyParamFile(String arg) {
304+
return arg.startsWith("@")
305+
&& !arg.startsWith("@rpath")
306+
&& !arg.startsWith("@loader_path")
307+
&& !arg.startsWith("@executable_path");
308+
}
309+
303310
/**
304311
* Returns a raw link command for the given link invocation, including both command and arguments
305312
* (argv). The version that uses the expander is preferred, but that one can't be used during

0 commit comments

Comments
 (0)