5
5
import argparse
6
6
import glob
7
7
import json
8
+ import logging
8
9
import os
9
10
import shlex
10
11
import subprocess
11
12
from pathlib import Path
12
13
13
14
14
- def runBazelBuildForCompilationDatabase (bazel_options , bazel_targets ):
15
- query = 'attr(include_prefix, ".+", kind(cc_library, deps({})))' .format (
16
- ' union ' .join (bazel_targets ))
17
- build_targets = subprocess .check_output (["bazel" , "query" , query ]).decode ().splitlines ()
18
- subprocess .check_call (["bazel" , "build" ] + bazel_options + build_targets )
19
-
20
-
21
15
# This method is equivalent to https://github.com/grailbio/bazel-compilation-database/blob/master/generate.sh
22
16
def generateCompilationDatabase (args ):
23
- # We need to download all remote outputs for generated source code, we don't care about built
24
- # binaries so just always strip and use dynamic link to minimize download size .
17
+ # We need to download all remote outputs for generated source code. This option lives here to override those
18
+ # specified in bazelrc .
25
19
bazel_options = shlex .split (os .environ .get ("BAZEL_BUILD_OPTIONS" , "" )) + [
26
- "-c" , "fastbuild" , "--build_tag_filters=-manual " ,
27
- "--experimental_remote_download_outputs =all" , "--strip=always"
20
+ "--config=compdb " ,
21
+ "--remote_download_outputs =all" ,
28
22
]
29
- if args .run_bazel_build :
30
- runBazelBuildForCompilationDatabase (bazel_options , args .bazel_targets )
31
23
32
24
subprocess .check_call (["bazel" , "build" ] + bazel_options + [
33
25
"--aspects=@bazel_compdb//:aspects.bzl%compilation_database_aspect" ,
34
- "--output_groups=compdb_files"
26
+ "--output_groups=compdb_files,header_files "
35
27
] + args .bazel_targets )
36
28
37
29
execroot = subprocess .check_output (["bazel" , "info" , "execution_root" ] +
@@ -83,6 +75,9 @@ def modifyCompileCommand(target, args):
83
75
if isHeader (target ["file" ]):
84
76
options += " -Wno-pragma-once-outside-header -Wno-unused-const-variable"
85
77
options += " -Wno-unused-function"
78
+ if not target ["file" ].startswith ("external/" ):
79
+ # *.h file is treated as C header by default while our headers files are all C++17.
80
+ options = "-x c++ -std=c++17 -fexceptions " + options
86
81
87
82
target ["command" ] = " " .join ([cc , options ])
88
83
return target
@@ -97,14 +92,11 @@ def fixCompilationDatabase(args, db):
97
92
98
93
if __name__ == "__main__" :
99
94
parser = argparse .ArgumentParser (description = 'Generate JSON compilation database' )
100
- parser .add_argument ('--run_bazel_build' , action = 'store_true' )
101
95
parser .add_argument ('--include_external' , action = 'store_true' )
102
96
parser .add_argument ('--include_genfiles' , action = 'store_true' )
103
97
parser .add_argument ('--include_headers' , action = 'store_true' )
104
98
parser .add_argument ('--vscode' , action = 'store_true' )
105
99
# @@@
106
- parser .add_argument ('bazel_targets' ,
107
- nargs = '*' ,
108
- default = ["//..." ])
100
+ parser .add_argument ('bazel_targets' , nargs = '*' , default = ["//..." ])
109
101
args = parser .parse_args ()
110
102
fixCompilationDatabase (args , generateCompilationDatabase (args ))
0 commit comments