Bazel/CMake: auto detect all cc_binary/cc_test targets

This commit is contained in:
Paolo Tranquilli
2024-02-07 11:11:30 +01:00
parent 643817e74e
commit 9cfef6e42f
7 changed files with 39 additions and 8 deletions

3
.gitignore vendored
View File

@@ -39,6 +39,9 @@
# local bazel options
/local.bazelrc
# generated cmake directory
/.bazel-cmake
# CLion project files
/.clwb

View File

@@ -68,10 +68,10 @@ def _cmake_aspect_impl(target, ctx):
is_macos = "darwin" in ctx.var["TARGET_CPU"]
is_binary = ctx.rule.kind == "cc_binary"
is_binary = ctx.rule.kind in ("cc_binary", "cc_test")
force_cxx_compilation = "force_cxx_compilation" in ctx.rule.attr.features
attr = ctx.rule.attr
srcs = attr.srcs + getattr(attr, "hdrs", []) + getattr(attr, "textual_hdrs", [])
srcs = getattr(attr, "srcs", []) + getattr(attr, "hdrs", []) + getattr(attr, "textual_hdrs", [])
srcs = [f for src in srcs for f in src.files.to_list()]
inputs = [f for f in srcs if not f.is_source or f.path.startswith("external/")]
by_kind = {}
@@ -90,10 +90,10 @@ def _cmake_aspect_impl(target, ctx):
cxx_compilation = force_cxx_compilation or any([not src.endswith(".c") for src in srcs])
copts = ctx.fragments.cpp.copts + (ctx.fragments.cpp.cxxopts if cxx_compilation else ctx.fragments.cpp.conlyopts)
copts += [ctx.expand_make_variables("copts", o, {}) for o in ctx.rule.attr.copts]
copts += [ctx.expand_make_variables("copts", o, {}) for o in getattr(ctx.rule.attr, "copts", [])]
linkopts = ctx.fragments.cpp.linkopts
linkopts += [ctx.expand_make_variables("linkopts", o, {}) for o in ctx.rule.attr.linkopts]
linkopts += [ctx.expand_make_variables("linkopts", o, {}) for o in getattr(ctx.rule.attr, "linkopts", [])]
compilation_ctx = target[CcInfo].compilation_context
system_includes = _get_includes(compilation_ctx.system_includes)

View File

@@ -9,6 +9,10 @@ if (NOT DEFINED BAZEL_BIN)
set(BAZEL_BIN "bazelisk")
endif ()
if (NOT DEFINED CODEQL_BAZEL_WORKSPACE)
set(CODEQL_BAZEL_WORKSPACE "codeql")
endif ()
macro(bazel)
execute_process(COMMAND ${BAZEL_BIN} ${ARGN}
COMMAND_ERROR_IS_FATAL ANY
@@ -23,13 +27,38 @@ string(REPLACE "-" "_" BAZEL_EXEC_ROOT ${PROJECT_NAME})
set(BAZEL_EXEC_ROOT ${BAZEL_OUTPUT_BASE}/execroot/${BAZEL_EXEC_ROOT})
macro(include_generated BAZEL_TARGET)
bazel(build ${BAZEL_TARGET})
bazel(build ${BAZEL_TARGET} --nocheck_visibility)
string(REPLACE "@" "/external/" BAZEL_TARGET_PATH ${BAZEL_TARGET})
string(REPLACE "//" "/" BAZEL_TARGET_PATH ${BAZEL_TARGET_PATH})
string(REPLACE ":" "/" BAZEL_TARGET_PATH ${BAZEL_TARGET_PATH})
include(${BAZEL_WORKSPACE}/bazel-bin${BAZEL_TARGET_PATH}.cmake)
endmacro()
macro(generate_and_include)
file(REMOVE "${BAZEL_WORKSPACE}/.bazel-cmake/BUILD.bazel")
# use aquery to only get targets compatible with the current platform
bazel(aquery "kind(\"cc_test|cc_binary\", ${ARGN})" --nocheck_visibility --output=jsonproto OUTPUT_VARIABLE BAZEL_AQUERY_RESULT)
string(JSON BAZEL_JSON_TARGETS GET "${BAZEL_AQUERY_RESULT}" targets)
string(JSON LAST_IDX LENGTH "${BAZEL_JSON_TARGETS}")
math(EXPR LAST_IDX "${LAST_IDX} - 1")
foreach(IDX RANGE ${LAST_IDX})
string(JSON CUR_BAZEL_TARGET GET "${BAZEL_JSON_TARGETS}" ${IDX} label)
string(APPEND BAZEL_TARGETS " '${CUR_BAZEL_TARGET}',\n")
endforeach ()
file(WRITE "${BAZEL_WORKSPACE}/.bazel-cmake/BUILD.bazel" "\
# this file was generated by cmake
load('@${CODEQL_BAZEL_WORKSPACE}//misc/bazel/cmake:cmake.bzl', 'generate_cmake')\n\
generate_cmake(\n\
name = 'cmake',\n\
testonly = True,\n\
targets = [\n\
${BAZEL_TARGETS}\
],\n\
)\n")
include_generated(//.bazel-cmake:cmake)
endmacro()
if (CREATE_COMPILATION_DATABASE_LINK)
file(CREATE_LINK ${PROJECT_BINARY_DIR}/compile_commands.json ${PROJECT_SOURCE_DIR}/compile_commands.json SYMBOLIC)
endif ()

View File

@@ -123,6 +123,7 @@ py_binary(
deps = [":_create_extractor_pack"],
)
# TODO this is unneeded here but still used in the internal repo. Remove once it's not
generate_cmake(
name = "cmake",
targets = [

View File

@@ -17,4 +17,4 @@ project(codeql)
include(../misc/bazel/cmake/setup.cmake)
include_generated(//swift:cmake)
generate_and_include(//swift/...)

View File

@@ -1,5 +1,4 @@
load("//swift:rules.bzl", "swift_cc_binary")
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
load("//misc/bazel:pkg_runfiles.bzl", "pkg_runfiles")
swift_cc_binary(

View File

@@ -1,5 +1,4 @@
load("//swift:rules.bzl", "swift_cc_binary")
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
swift_cc_binary(
name = "assert-false",