Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Commit

Permalink
Avoid legacy C++ toolchain and configuration APIs (#15)
Browse files Browse the repository at this point in the history
* Update expected_file.json with bazel-bin iquotes

The compile_commands.json now includes
-iquote bazel-out/k8-fastbuild/bin
and
-iquote bazel-out/k8-fastbuild/bin/external/bazel_tools

This is likely due to difference between bazel versions.

* Avoid legacy C++ toolchain and configuration APIs

According to the Bazel 0.19.0 release notes, Bazel 0.20 introduces
incompatible changes:
https://docs.bazel.build/versions/master/skylark/backward-compatibility.html#disable-legacy-c-configuration-api
and
https://docs.bazel.build/versions/master/skylark/backward-compatibility.html#disable-legacy-c-toolchain-api

This change uses the examples from the docs to use different APIs and
builtins so that the compilation_database_aspect works with
--incompatible_disable_legacy_cpp_toolchain_skylark_api
and
--incompatible_disable_legacy_flags_cc_toolchain_api

This commit also changes the order of the flags in expected_file.json to
match the new order of output.

Test: Ran generate.sh on my bazel project before and after the change,
and all the compile_commands.json files contained the same flags (though
in a different order). Note that I'm not on a mac so one code path was
not explicitly tested.
  • Loading branch information
sgreenstein authored and siddharthab committed Nov 2, 2018
1 parent c4cf16f commit 69a8783
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
62 changes: 52 additions & 10 deletions aspects.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ An alternative approach is the one used by the kythe project using
https://github.com/google/kythe/blob/master/tools/cpp/generate_compilation_database.sh
"""

load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load(
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
"CPP_COMPILE_ACTION_NAME",
"CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME",
"C_COMPILE_ACTION_NAME",
)

CompilationAspect = provider()

_cpp_extensions = ["cc", "cpp", "cxx"]
Expand Down Expand Up @@ -62,18 +70,33 @@ def _compilation_database_aspect_impl(target, ctx):

compilation_db = []

cpp_fragment = ctx.fragments.cpp
compiler = str(cpp_fragment.compiler_executable)
compile_flags = (cpp_fragment.compiler_options(ctx.features)
+ cpp_fragment.c_options
+ target.cc.compile_flags
+ (ctx.rule.attr.copts if "copts" in dir(ctx.rule.attr) else [])
+ cpp_fragment.unfiltered_compiler_options(ctx.features))
cc_toolchain = find_cpp_toolchain(ctx)
feature_configuration = cc_common.configure_features(
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
compile_variables = cc_common.create_compile_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
user_compile_flags = ctx.fragments.cpp.copts,
)
compiler_options = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = C_COMPILE_ACTION_NAME,
variables = compile_variables,
)
compiler = str(
cc_common.get_tool_for_action(
feature_configuration = feature_configuration,
action_name = C_COMPILE_ACTION_NAME,
),
)

# system built-in directories (helpful for macOS).
if cpp_fragment.libc == "macosx":
if cc_toolchain.libc == "macosx":
compile_flags += ["-isystem " + str(d)
for d in cpp_fragment.built_in_include_directories]
for d in cc_toolchain.built_in_include_directories]

srcs = _sources(target, ctx)
if not srcs:
Expand All @@ -84,9 +107,23 @@ def _compilation_database_aspect_impl(target, ctx):
# This is useful for compiling .h headers as C++ code.
force_cpp_mode_option = ""
if _is_cpp_target(srcs):
compile_flags += cpp_fragment.cxx_options(ctx.features)
compile_variables = cc_common.create_compile_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
user_compile_flags = ctx.fragments.cpp.cxxopts +
ctx.fragments.cpp.copts,
add_legacy_cxx_options = True,
)
compiler_options = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = CPP_COMPILE_ACTION_NAME,
variables = compile_variables,
)
force_cpp_mode_option = " -x c++"

compile_flags = (compiler_options +
target.cc.compile_flags +
(ctx.rule.attr.copts if "copts" in dir(ctx.rule.attr) else []))
compile_command = compiler + " " + " ".join(compile_flags) + force_cpp_mode_option

for src in srcs:
Expand Down Expand Up @@ -120,6 +157,11 @@ compilation_database_aspect = aspect(
fragments = ["cpp"],
required_aspect_providers = [CompilationAspect],
implementation = _compilation_database_aspect_impl,
attrs = {
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
},
)


Expand Down
6 changes: 3 additions & 3 deletions tests/expected_file.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
{"directory":"bazel-tests","command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -iquote . -iquote bazel-out/k8-fastbuild/genfiles -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/genfiles/external/bazel_tools -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -std=c++0x -x c++ -c a.cc","file":"a.cc"},
{"directory":"bazel-tests","command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -iquote . -iquote bazel-out/k8-fastbuild/genfiles -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/genfiles/external/bazel_tools -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -std=c++0x -x c++ -c a.h","file":"a.h"},
{"directory":"bazel-tests","command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -iquote . -iquote bazel-out/k8-fastbuild/genfiles -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/genfiles/external/bazel_tools -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -std=c++0x -x c++ -c b.cc","file":"b.cc"}
{"directory":"bazel-tests","command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -iquote . -iquote bazel-out/k8-fastbuild/genfiles -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/genfiles/external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -x c++ -c a.cc","file":"a.cc"},
{"directory":"bazel-tests","command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -iquote . -iquote bazel-out/k8-fastbuild/genfiles -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/genfiles/external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -x c++ -c a.h","file":"a.h"},
{"directory":"bazel-tests","command":"/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -iquote . -iquote bazel-out/k8-fastbuild/genfiles -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/genfiles/external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -x c++ -c b.cc","file":"b.cc"}
]

0 comments on commit 69a8783

Please sign in to comment.