Skip to content

Commit 6d90877

Browse files
authored
Merge pull request github#15536 from github/redsun82/bazel-cmake
Bazel/CMake: auto detect all `cc_binary`/`cc_test` targets
2 parents 051d63a + 0a137c7 commit 6d90877

File tree

7 files changed

+47
-10
lines changed

7 files changed

+47
-10
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
# local bazel options
4040
/local.bazelrc
4141

42+
# generated cmake directory
43+
/.bazel-cmake
44+
4245
# CLion project files
4346
/.clwb
4447

misc/bazel/cmake/cmake.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ def _cmake_aspect_impl(target, ctx):
7070

7171
is_macos = "darwin" in ctx.var["TARGET_CPU"]
7272

73-
is_binary = ctx.rule.kind == "cc_binary"
73+
is_binary = ctx.rule.kind in ("cc_binary", "cc_test")
7474
force_cxx_compilation = "force_cxx_compilation" in ctx.rule.attr.features
7575
attr = ctx.rule.attr
76-
srcs = attr.srcs + getattr(attr, "hdrs", []) + getattr(attr, "textual_hdrs", [])
76+
srcs = getattr(attr, "srcs", []) + getattr(attr, "hdrs", []) + getattr(attr, "textual_hdrs", [])
7777
srcs = [f for src in srcs for f in src.files.to_list()]
7878
inputs = [f for f in srcs if not f.is_source or f.path.startswith("external/")]
7979
by_kind = {}
@@ -92,10 +92,10 @@ def _cmake_aspect_impl(target, ctx):
9292
cxx_compilation = force_cxx_compilation or any([not src.endswith(".c") for src in srcs])
9393

9494
copts = ctx.fragments.cpp.copts + (ctx.fragments.cpp.cxxopts if cxx_compilation else ctx.fragments.cpp.conlyopts)
95-
copts += [ctx.expand_make_variables("copts", o, {}) for o in ctx.rule.attr.copts]
95+
copts += [ctx.expand_make_variables("copts", o, {}) for o in getattr(ctx.rule.attr, "copts", [])]
9696

9797
linkopts = ctx.fragments.cpp.linkopts
98-
linkopts += [ctx.expand_make_variables("linkopts", o, {}) for o in ctx.rule.attr.linkopts]
98+
linkopts += [ctx.expand_make_variables("linkopts", o, {}) for o in getattr(ctx.rule.attr, "linkopts", [])]
9999

100100
compilation_ctx = target[CcInfo].compilation_context
101101
system_includes = _get_includes(compilation_ctx.system_includes)

misc/bazel/cmake/setup.cmake

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,61 @@ if (NOT DEFINED BAZEL_BIN)
99
set(BAZEL_BIN "bazelisk")
1010
endif ()
1111

12+
if (NOT DEFINED CODEQL_BAZEL_WORKSPACE)
13+
set(CODEQL_BAZEL_WORKSPACE "codeql")
14+
endif ()
15+
1216
macro(bazel)
13-
execute_process(COMMAND ${BAZEL_BIN} ${ARGN}
17+
execute_process(COMMAND ${BAZEL_BIN} ${BAZEL_STARTUP_OPTIONS} ${ARGN}
1418
COMMAND_ERROR_IS_FATAL ANY
1519
OUTPUT_STRIP_TRAILING_WHITESPACE
1620
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
1721
endmacro()
1822

19-
bazel(info workspace OUTPUT_VARIABLE BAZEL_WORKSPACE)
23+
macro(bazel_even_if_failing)
24+
execute_process(COMMAND ${BAZEL_BIN} ${BAZEL_STARTUP_OPTIONS} ${ARGN}
25+
OUTPUT_STRIP_TRAILING_WHITESPACE
26+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
27+
endmacro()
2028

29+
bazel(info workspace OUTPUT_VARIABLE BAZEL_WORKSPACE)
2130
bazel(info output_base OUTPUT_VARIABLE BAZEL_OUTPUT_BASE)
2231
set(BAZEL_EXEC_ROOT ${BAZEL_OUTPUT_BASE}/execroot/_main)
32+
set(BAZEL_BUILD_OPTIONS --nocheck_visibility --keep_going)
2333

2434
macro(include_generated BAZEL_TARGET)
25-
bazel(build ${BAZEL_TARGET})
35+
bazel(build ${BAZEL_TARGET} ${BAZEL_BUILD_OPTIONS})
2636
string(REPLACE "@" "/external/" BAZEL_TARGET_PATH ${BAZEL_TARGET})
2737
string(REPLACE "//" "/" BAZEL_TARGET_PATH ${BAZEL_TARGET_PATH})
2838
string(REPLACE ":" "/" BAZEL_TARGET_PATH ${BAZEL_TARGET_PATH})
2939
include(${BAZEL_WORKSPACE}/bazel-bin${BAZEL_TARGET_PATH}.cmake)
3040
endmacro()
3141

42+
macro(generate_and_include)
43+
file(REMOVE "${BAZEL_WORKSPACE}/.bazel-cmake/BUILD.bazel")
44+
# use aquery to only get targets compatible with the current platform
45+
bazel_even_if_failing(aquery "kind(\"cc_test|cc_binary\", ${ARGN})" ${BAZEL_BUILD_OPTIONS} --output=jsonproto OUTPUT_VARIABLE BAZEL_AQUERY_RESULT)
46+
string(JSON BAZEL_JSON_TARGETS GET "${BAZEL_AQUERY_RESULT}" targets)
47+
string(JSON LAST_IDX LENGTH "${BAZEL_JSON_TARGETS}")
48+
math(EXPR LAST_IDX "${LAST_IDX} - 1")
49+
foreach(IDX RANGE ${LAST_IDX})
50+
string(JSON CUR_BAZEL_TARGET GET "${BAZEL_JSON_TARGETS}" ${IDX} label)
51+
string(APPEND BAZEL_TARGETS " '${CUR_BAZEL_TARGET}',\n")
52+
endforeach ()
53+
file(WRITE "${BAZEL_WORKSPACE}/.bazel-cmake/BUILD.bazel" "\
54+
# this file was generated by cmake
55+
load('@${CODEQL_BAZEL_WORKSPACE}//misc/bazel/cmake:cmake.bzl', 'generate_cmake')\n\
56+
\n\
57+
generate_cmake(\n\
58+
name = 'cmake',\n\
59+
testonly = True,\n\
60+
targets = [\n\
61+
${BAZEL_TARGETS}\
62+
],\n\
63+
)\n")
64+
include_generated(//.bazel-cmake:cmake)
65+
endmacro()
66+
3267
if (CREATE_COMPILATION_DATABASE_LINK)
3368
file(CREATE_LINK ${PROJECT_BINARY_DIR}/compile_commands.json ${PROJECT_SOURCE_DIR}/compile_commands.json SYMBOLIC)
3469
endif ()

swift/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ py_binary(
115115
deps = [":_create_extractor_pack"],
116116
)
117117

118+
# TODO this is unneeded here but still used in the internal repo. Remove once it's not
118119
generate_cmake(
119120
name = "cmake",
120121
targets = [

swift/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ project(codeql)
1717

1818
include(../misc/bazel/cmake/setup.cmake)
1919

20-
include_generated(//swift:cmake)
20+
generate_and_include(//swift/...)

swift/extractor/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("//swift:rules.bzl", "swift_cc_binary")
2-
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
32
load("//misc/bazel:pkg_runfiles.bzl", "pkg_runfiles")
43

54
swift_cc_binary(

swift/logging/tests/assertion-diagnostics/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("//swift:rules.bzl", "swift_cc_binary")
2-
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
32

43
swift_cc_binary(
54
name = "assert-false",

0 commit comments

Comments
 (0)