You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
assertsource_file.endswith(_get_files.source_extensions), f"Source file candidate, {source_file}, seems to be wrong.\nSelected from {compile_action.arguments}.\nPlease file an issue with this information!"
565
582
566
583
# Warn gently about missing files
567
-
ifnotos.path.isfile(source_file):
568
-
ifnot_get_files.has_logged_missing_file_error: # Just log once; subsequent messages wouldn't add anything.
569
-
_get_files.has_logged_missing_file_error=True
570
-
log_warning(f""">>> A source file you compile doesn't (yet) exist: {source_file}
571
-
It's probably a generated file, and you haven't yet run a build to generate it.
572
-
That's OK; your code doesn't even have to compile for this tool to work.
573
-
If you can, though, you might want to run a build of your code.
574
-
That way everything is generated, browsable and indexed for autocomplete.
575
-
However, if you have *already* built your code, and generated the missing file...
576
-
Please make sure you're supplying this tool with the same flags you use to build.
577
-
You can either use a refresh_compile_commands rule or the special -- syntax. Please see the README.
578
-
[Supplying flags normally won't work. That just causes this tool to be built with those flags.]
579
-
Continuing gracefully...""")
584
+
ifnot_file_exists(source_file):
580
585
return {source_file}, set()
581
586
582
587
# Note: We need to apply commands to headers and sources.
# Unless xcode-select has been invoked (like for a beta) we'd expect, e.g., '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang' or '/Library/Developer/CommandLineTools/usr/bin/clang'.
"""De-Bazel the command into something clangd can parse.
688
728
689
729
This function has fixes specific to Apple platforms, but you should call it on all platforms. It'll determine whether the fixes should be applied or not.
# Bazel wraps the compiler as `external/local_config_cc/wrapped_clang` and exports that wrapped compiler in the proto. However, we need a clang call that clangd can introspect. (See notes in "how clangd uses compile_commands.json" in ImplementationReadme.md for more.)
696
736
# Removing the wrapper is also important because Bazel's Xcode (but not CommandLineTools) wrapper crashes if you don't specify particular environment variables (replaced below). We'd need the wrapper to be invokable by clangd's --query-driver if we didn't remove the wrapper.
697
-
compile_args[0] =_get_apple_active_clang()
737
+
# Bazel wrapps the swiftc as `external/build_bazel_rules_swift/tools/worker/worker swiftc ` and worker has been removed in apple_swift_patch
738
+
739
+
ifcompile_args[0][-6:] =="swiftc":
740
+
compile_args[0] =_get_apple_active_swiftc()
741
+
else:
742
+
compile_args[0] =_get_apple_active_clang()
698
743
699
744
# We have to manually substitute out Bazel's macros so clang can parse the command
700
745
# Code this mirrors is in https://github.com/bazelbuild/bazel/blob/master/tools/osx/crosstool/wrapped_clang.cc
assertcompile_action.mnemonicincompile_action.mnemonic, f"Expecting mnemonic is one of (Objc|Cpp|Swift)Compile. Found mnemonic {compile_action.mnemonic}, target {compile_action}"
max_workers=min(32, (os.cpu_count() or1) +4) # Backport. Default in MIN_PY=3.8. See "using very large resources implicitly on many-core machines" in https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
Try adding them as flags in your refresh_compile_commands rather than targets.
844
923
In a moment, Bazel will likely fail to parse.""")
845
924
925
+
support_mnemonics= ["Objc", "Cpp"]
926
+
if {enable_swift}:
927
+
support_mnemonics+= ["Swift"]
928
+
mnemonics_string='|'.join(support_mnemonics)
929
+
846
930
# First, query Bazel's C-family compile actions for that configured target
847
931
aquery_args= [
848
932
'bazel',
849
933
'aquery',
850
934
# Aquery docs if you need em: https://docs.bazel.build/versions/master/aquery.html
851
935
# Aquery output proto reference: https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/analysis_v2.proto
852
936
# One bummer, not described in the docs, is that aquery filters over *all* actions for a given target, rather than just those that would be run by a build to produce a given output. This mostly isn't a problem, but can sometimes surface extra, unnecessary, misconfigured actions. Chris has emailed the authors to discuss and filed an issue so anyone reading this could track it: https://github.com/bazelbuild/bazel/issues/14156.
# We switched to jsonproto instead of proto because of https://github.com/bazelbuild/bazel/issues/13404. We could change back when fixed--reverting most of the commit that added this line and tweaking the build file to depend on the target in that issue. That said, it's kinda nice to be free of the dependency, unless (OPTIMNOTE) jsonproto becomes a performance bottleneck compated to binary protos.
855
939
'--output=jsonproto',
856
940
# We'll disable artifact output for efficiency, since it's large and we don't use them. Small win timewise, but dramatically less json output from aquery.
Copy file name to clipboardExpand all lines: refresh_compile_commands.bzl
+4-1Lines changed: 4 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -63,6 +63,7 @@ def refresh_compile_commands(
63
63
targets=None,
64
64
exclude_headers=None,
65
65
exclude_external_sources=False,
66
+
enable_swift=False,
66
67
**kwargs): # For the other common attributes. Tags, compatible_with, etc. https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes.
67
68
# Convert the various, acceptable target shorthands into the dictionary format
68
69
# In Python, `type(x) == y` is an antipattern, but [Starlark doesn't support inheritance](https://bazel.build/rules/language), so `isinstance` doesn't exist, and this is the correct way to switch on type.
" {windows_default_include_paths}": "\n".join([" %r,"%pathforpathinfind_cpp_toolchain(ctx).built_in_include_directories]), # find_cpp_toolchain is from https://docs.bazel.build/versions/main/integrating-with-rules-cc.html
"_cc_toolchain": attr.label(default="@bazel_tools//tools/cpp:current_cc_toolchain"), # For Windows INCLUDE. If this were eliminated, for example by the resolution of https://github.com/clangd/clangd/issues/123, we'd be able to just use a macro and skylib's expand_template rule: https://github.com/bazelbuild/bazel-skylib/pull/330
0 commit comments