Skip to content

Commit 9cfef6e

Browse files
committed
Bazel/CMake: auto detect all cc_binary/cc_test targets
1 parent 643817e commit 9cfef6e

File tree

7 files changed

+39
-8
lines changed

7 files changed

+39
-8
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
@@ -68,10 +68,10 @@ def _cmake_aspect_impl(target, ctx):
6868

6969
is_macos = "darwin" in ctx.var["TARGET_CPU"]
7070

71-
is_binary = ctx.rule.kind == "cc_binary"
71+
is_binary = ctx.rule.kind in ("cc_binary", "cc_test")
7272
force_cxx_compilation = "force_cxx_compilation" in ctx.rule.attr.features
7373
attr = ctx.rule.attr
74-
srcs = attr.srcs + getattr(attr, "hdrs", []) + getattr(attr, "textual_hdrs", [])
74+
srcs = getattr(attr, "srcs", []) + getattr(attr, "hdrs", []) + getattr(attr, "textual_hdrs", [])
7575
srcs = [f for src in srcs for f in src.files.to_list()]
7676
inputs = [f for f in srcs if not f.is_source or f.path.startswith("external/")]
7777
by_kind = {}
@@ -90,10 +90,10 @@ def _cmake_aspect_impl(target, ctx):
9090
cxx_compilation = force_cxx_compilation or any([not src.endswith(".c") for src in srcs])
9191

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

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

9898
compilation_ctx = target[CcInfo].compilation_context
9999
system_includes = _get_includes(compilation_ctx.system_includes)

misc/bazel/cmake/setup.cmake

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ 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)
1317
execute_process(COMMAND ${BAZEL_BIN} ${ARGN}
1418
COMMAND_ERROR_IS_FATAL ANY
@@ -23,13 +27,38 @@ string(REPLACE "-" "_" BAZEL_EXEC_ROOT ${PROJECT_NAME})
2327
set(BAZEL_EXEC_ROOT ${BAZEL_OUTPUT_BASE}/execroot/${BAZEL_EXEC_ROOT})
2428

2529
macro(include_generated BAZEL_TARGET)
26-
bazel(build ${BAZEL_TARGET})
30+
bazel(build ${BAZEL_TARGET} --nocheck_visibility)
2731
string(REPLACE "@" "/external/" BAZEL_TARGET_PATH ${BAZEL_TARGET})
2832
string(REPLACE "//" "/" BAZEL_TARGET_PATH ${BAZEL_TARGET_PATH})
2933
string(REPLACE ":" "/" BAZEL_TARGET_PATH ${BAZEL_TARGET_PATH})
3034
include(${BAZEL_WORKSPACE}/bazel-bin${BAZEL_TARGET_PATH}.cmake)
3135
endmacro()
3236

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

swift/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ py_binary(
123123
deps = [":_create_extractor_pack"],
124124
)
125125

126+
# TODO this is unneeded here but still used in the internal repo. Remove once it's not
126127
generate_cmake(
127128
name = "cmake",
128129
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)