46
46
# When we notice a project using these flags,
47
47
# we should figure out how to handle them.
48
48
_DISALLOWED_CLANG_FLAGS = (
49
- "-fdebug-compilation-dir=" ,
50
49
"-fdebug-prefix-map=" ,
51
- "-ffile-compilation-dir=" ,
52
50
"-ffile-prefix-map=" ,
53
51
)
54
52
53
+ # Chromium GN builds use these flags with a period to make paths relative to
54
+ # the out directory. This is OK.
55
+ _ALLOWED_CLANG_FLAGS_ONLY_WITH_PERIOD = (
56
+ "-fdebug-compilation-dir=" ,
57
+ "-ffile-compilation-dir=" ,
58
+ )
59
+
55
60
SRC = Path (os .getenv ("SRC" , "/src" ))
56
61
# On OSS-Fuzz build infra, $OUT is not /out.
57
62
OUT = Path (os .getenv ("OUT" , "/out" ))
58
63
INDEXES_PATH = Path (os .getenv ("INDEXES_PATH" , "/indexes" ))
59
64
FUZZER_ENGINE = os .getenv ("LIB_FUZZING_ENGINE" , "/usr/lib/libFuzzingEngine.a" )
60
65
61
66
62
- def rewrite_argv0 (argv : Sequence [str ]) -> list [str ]:
67
+ def rewrite_argv0 (argv : Sequence [str ], clang_toolchain : str ) -> list [str ]:
63
68
"""Rewrite argv[0] to point to the real clang location."""
64
69
# We do this because we've set PATH to our wrapper.
65
- rewritten = [os .path .join ("/usr/local/ bin/ " , os .path .basename (argv [0 ]))]
70
+ rewritten = [os .path .join (clang_toolchain , " bin" , os .path .basename (argv [0 ]))]
66
71
rewritten .extend (argv [1 :])
67
72
return rewritten
68
73
69
74
70
- def execute (argv : Sequence [str ]) -> None :
71
- argv = rewrite_argv0 (argv )
75
+ def execute (argv : Sequence [str ], clang_toolchain : str ) -> None :
76
+ argv = rewrite_argv0 (argv , clang_toolchain )
72
77
print ("About to execute..." , argv )
73
78
os .execv (argv [0 ], tuple (argv ))
74
79
75
80
76
- def run (argv : Sequence [str ]) -> None :
77
- argv = rewrite_argv0 (argv )
81
+ def run (argv : Sequence [str ], clang_toolchain : str ) -> None :
82
+ argv = rewrite_argv0 (argv , clang_toolchain )
78
83
print ("About to run..." , argv )
79
84
ret = subprocess .run (argv , check = False )
80
85
if ret .returncode != 0 :
@@ -371,7 +376,17 @@ def check_fuzzing_engine_and_fix_argv(argv: MutableSequence[str]) -> bool:
371
376
372
377
def _has_disallowed_clang_flags (argv : Sequence [str ]) -> bool :
373
378
"""Checks if the command line arguments contain disallowed flags."""
374
- return any (arg .startswith (_DISALLOWED_CLANG_FLAGS ) for arg in argv )
379
+ if any (arg .startswith (_DISALLOWED_CLANG_FLAGS ) for arg in argv ):
380
+ return True
381
+
382
+ if any (
383
+ arg .startswith (_ALLOWED_CLANG_FLAGS_ONLY_WITH_PERIOD )
384
+ and not arg .endswith ("=." )
385
+ for arg in argv
386
+ ):
387
+ return True
388
+
389
+ return False
375
390
376
391
377
392
@dataclasses .dataclass (frozen = True )
@@ -400,7 +415,16 @@ def _filter_compile_commands(
400
415
unused_cc_paths = set ()
401
416
402
417
for compile_command in compile_commands :
403
- cc_path = Path (compile_command ["directory" ]) / compile_command ["file" ]
418
+ if (
419
+ "-ffile-compilation-dir=." in compile_command ["arguments" ]
420
+ or "-fdebug-compilation-dir=." in compile_command ["arguments" ]
421
+ ):
422
+ # Handle build systems that make their debug paths relative.
423
+ directory = Path ("." )
424
+ else :
425
+ directory = Path (compile_command ["directory" ])
426
+
427
+ cc_path = Path (directory / compile_command ["file" ])
404
428
if cc_path in cu_paths :
405
429
filtered_compile_commands .append (compile_command )
406
430
used_cu_paths .add (cc_path )
@@ -489,10 +513,27 @@ def main(argv: list[str]) -> None:
489
513
if _has_disallowed_clang_flags (argv ):
490
514
raise ValueError ("Disallowed clang flags found, aborting." )
491
515
516
+ # TODO: b/441872725 - Migrate more flags to be appended in the clang wrapper
517
+ # instead.
518
+ cdb_path = index_build .OUT / "cdb"
519
+ argv .extend (("-gen-cdb-fragment-path" , cdb_path .as_posix ()))
520
+ argv .extend ((
521
+ "-isystem" ,
522
+ (
523
+ f"{ compile_settings .clang_toolchain } /lib/clang/"
524
+ f"{ compile_settings .clang_version } "
525
+ ),
526
+ "-resource-dir" ,
527
+ (
528
+ f"{ compile_settings .clang_toolchain } /lib/clang/"
529
+ f"{ compile_settings .clang_version } "
530
+ ),
531
+ ))
532
+
492
533
if "-E" in argv :
493
534
# Preprocessor-only invocation.
494
535
modified_argv = remove_flag_and_value (argv , "-gen-cdb-fragment-path" )
495
- execute (modified_argv )
536
+ execute (modified_argv , compile_settings . clang_toolchain )
496
537
497
538
fuzzing_engine_in_argv = check_fuzzing_engine_and_fix_argv (argv )
498
539
indexer_targets : list [str ] = [
@@ -502,29 +543,24 @@ def main(argv: list[str]) -> None:
502
543
# If we are linking, collect the relevant flags and dependencies.
503
544
output_file = get_flag_value (argv , "-o" )
504
545
if not output_file :
505
- execute (argv ) # Missing output file
546
+ execute (argv , compile_settings . clang_toolchain ) # Missing output file
506
547
507
548
output_file = Path (output_file )
508
549
509
550
if output_file .name .endswith (".o" ):
510
- execute (argv ) # Not a real linker command
551
+ execute (argv , compile_settings . clang_toolchain ) # Not a real linker command
511
552
512
553
if indexer_targets :
513
554
if output_file .name not in indexer_targets :
514
555
# Not a relevant linker command
515
556
print (f"Not indexing as { output_file } is not in the allowlist" )
516
- execute (argv )
557
+ execute (argv , compile_settings . clang_toolchain )
517
558
elif not fuzzing_engine_in_argv :
518
559
# Not a fuzz target.
519
- execute (argv )
560
+ execute (argv , compile_settings . clang_toolchain )
520
561
521
562
print (f"Linking { argv } " )
522
563
523
- cdb_path = get_flag_value (argv , "-gen-cdb-fragment-path" )
524
- assert cdb_path , f"Missing Compile Directory Path: { argv } "
525
-
526
- cdb_path = Path (cdb_path )
527
-
528
564
# We can now run the linker and look at the output of some files.
529
565
dependency_file = (cdb_path / output_file .name ).with_suffix (".deps" )
530
566
why_extract_file = (cdb_path / output_file .name ).with_suffix (".why_extract" )
@@ -535,7 +571,7 @@ def main(argv: list[str]) -> None:
535
571
# We force lld, but it doesn't include this dir by default.
536
572
argv .append ("-L/usr/local/lib" )
537
573
argv .append ("-Qunused-arguments" )
538
- run (argv )
574
+ run (argv , compile_settings . clang_toolchain )
539
575
540
576
build_id = get_build_id (output_file )
541
577
assert build_id is not None
0 commit comments