1010from pathlib import Path
1111
1212
13+ def get_bazel_startup_options ():
14+ return shlex .split (os .environ .get ("BAZEL_STARTUP_OPTION_LIST" , "" ))
15+
16+
17+ def get_bazel_build_options ():
18+ return shlex .split (os .environ .get ("BAZEL_BUILD_OPTION_LIST" , "" )) + [
19+ "--config=compdb" ,
20+ "--remote_download_outputs=all" ,
21+ ]
22+
23+
24+ def get_output_base ():
25+ bazel_startup_options = get_bazel_startup_options ()
26+ bazel_options = get_bazel_build_options ()
27+ return subprocess .check_output (
28+ ["bazel" , * bazel_startup_options , "info" , * bazel_options , "output_base" ]).decode ().strip ()
29+
30+
1331# This method is equivalent to https://github.com/grailbio/bazel-compilation-database/blob/master/generate.py
1432def generate_compilation_database (args ):
1533 # We need to download all remote outputs for generated source code. This option lives here to override those
1634 # specified in bazelrc.
17- bazel_startup_options = shlex .split (os .environ .get ("BAZEL_STARTUP_OPTION_LIST" , "" ))
18- bazel_options = shlex .split (os .environ .get ("BAZEL_BUILD_OPTION_LIST" , "" )) + [
19- "--config=compdb" ,
20- "--remote_download_outputs=all" ,
21- ]
35+ bazel_startup_options = get_bazel_startup_options ()
36+ bazel_options = get_bazel_build_options ()
2237
2338 source_dir_targets = args .bazel_targets
2439 if args .exclude_contrib :
@@ -78,9 +93,14 @@ def is_compile_target(target, args):
7893 return True
7994
8095
81- def modify_compile_command (target , args ):
96+ def modify_compile_command (target , args , output_base ):
8297 cc , options = target ["command" ].split (" " , 1 )
8398
99+ # cc_wrapper.sh is a script of llvm_toolchain to wrap clang/clang++. Make sure to
100+ # use the one from output_base.
101+ if cc .endswith ("cc_wrapper.sh" ):
102+ cc = os .path .join (output_base , cc )
103+
84104 # Workaround for bazel added C++11 options, those doesn't affect build itself but
85105 # clang-tidy will misinterpret them.
86106 options = options .replace ("-std=c++0x " , "" )
@@ -91,10 +111,6 @@ def modify_compile_command(target, args):
91111 # old-style "-I".
92112 options = options .replace ("-iquote " , "-I " )
93113
94- if args .system_clang :
95- if cc .find ("clang" ):
96- cc = "clang++"
97-
98114 if is_header (target ["file" ]):
99115 options += " -Wno-pragma-once-outside-header -Wno-unused-const-variable"
100116 options += " -Wno-unused-function"
@@ -110,7 +126,13 @@ def modify_compile_command(target, args):
110126
111127
112128def fix_compilation_database (args , db ):
113- db = [modify_compile_command (target , args ) for target in db if is_compile_target (target , args )]
129+ output_base = get_output_base ()
130+
131+ db = [
132+ modify_compile_command (target , args , output_base )
133+ for target in db
134+ if is_compile_target (target , args )
135+ ]
114136
115137 with open ("compile_commands.json" , "w" ) as db_file :
116138 json .dump (db , db_file , indent = 2 )
@@ -124,12 +146,6 @@ def fix_compilation_database(args, db):
124146 parser .add_argument ('--vscode' , action = 'store_true' )
125147 parser .add_argument ('--include_all' , action = 'store_true' )
126148 parser .add_argument ('--exclude_contrib' , action = 'store_true' )
127- parser .add_argument (
128- '--system-clang' ,
129- action = 'store_true' ,
130- help =
131- 'Use `clang++` instead of the bazel wrapper for commands. This may help if `clangd` cannot find/run the tools.'
132- )
133149 parser .add_argument (
134150 'bazel_targets' ,
135151 nargs = '*' ,
0 commit comments