From 9cfef6e42fea5baa0e165ad4bf8d5f805eaeeae0 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Wed, 7 Feb 2024 11:11:30 +0100 Subject: [PATCH] Bazel/CMake: auto detect all `cc_binary`/`cc_test` targets --- .gitignore | 3 ++ misc/bazel/cmake/cmake.bzl | 8 ++--- misc/bazel/cmake/setup.cmake | 31 ++++++++++++++++++- swift/BUILD.bazel | 1 + swift/CMakeLists.txt | 2 +- swift/extractor/BUILD.bazel | 1 - .../tests/assertion-diagnostics/BUILD.bazel | 1 - 7 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c81e23fc7f8..3d2e92e3ca9 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,9 @@ # local bazel options /local.bazelrc +# generated cmake directory +/.bazel-cmake + # CLion project files /.clwb diff --git a/misc/bazel/cmake/cmake.bzl b/misc/bazel/cmake/cmake.bzl index cba271dc517..3fa50995ce9 100644 --- a/misc/bazel/cmake/cmake.bzl +++ b/misc/bazel/cmake/cmake.bzl @@ -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) diff --git a/misc/bazel/cmake/setup.cmake b/misc/bazel/cmake/setup.cmake index 79314d76f85..0deee6c7d57 100644 --- a/misc/bazel/cmake/setup.cmake +++ b/misc/bazel/cmake/setup.cmake @@ -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 () diff --git a/swift/BUILD.bazel b/swift/BUILD.bazel index 57b828ef350..859798fec37 100644 --- a/swift/BUILD.bazel +++ b/swift/BUILD.bazel @@ -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 = [ diff --git a/swift/CMakeLists.txt b/swift/CMakeLists.txt index 2b44cc58993..46bcd32db32 100644 --- a/swift/CMakeLists.txt +++ b/swift/CMakeLists.txt @@ -17,4 +17,4 @@ project(codeql) include(../misc/bazel/cmake/setup.cmake) -include_generated(//swift:cmake) +generate_and_include(//swift/...) diff --git a/swift/extractor/BUILD.bazel b/swift/extractor/BUILD.bazel index 984c205d97b..f711b3b9d2b 100644 --- a/swift/extractor/BUILD.bazel +++ b/swift/extractor/BUILD.bazel @@ -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( diff --git a/swift/logging/tests/assertion-diagnostics/BUILD.bazel b/swift/logging/tests/assertion-diagnostics/BUILD.bazel index e51459e1e17..ea31eac779a 100644 --- a/swift/logging/tests/assertion-diagnostics/BUILD.bazel +++ b/swift/logging/tests/assertion-diagnostics/BUILD.bazel @@ -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",