mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Bazel/CMake: auto detect all cc_binary/cc_test targets
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -39,6 +39,9 @@
|
|||||||
# local bazel options
|
# local bazel options
|
||||||
/local.bazelrc
|
/local.bazelrc
|
||||||
|
|
||||||
|
# generated cmake directory
|
||||||
|
/.bazel-cmake
|
||||||
|
|
||||||
# CLion project files
|
# CLion project files
|
||||||
/.clwb
|
/.clwb
|
||||||
|
|
||||||
|
|||||||
@@ -68,10 +68,10 @@ def _cmake_aspect_impl(target, ctx):
|
|||||||
|
|
||||||
is_macos = "darwin" in ctx.var["TARGET_CPU"]
|
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
|
force_cxx_compilation = "force_cxx_compilation" in ctx.rule.attr.features
|
||||||
attr = ctx.rule.attr
|
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()]
|
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/")]
|
inputs = [f for f in srcs if not f.is_source or f.path.startswith("external/")]
|
||||||
by_kind = {}
|
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])
|
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.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.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
|
compilation_ctx = target[CcInfo].compilation_context
|
||||||
system_includes = _get_includes(compilation_ctx.system_includes)
|
system_includes = _get_includes(compilation_ctx.system_includes)
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ if (NOT DEFINED BAZEL_BIN)
|
|||||||
set(BAZEL_BIN "bazelisk")
|
set(BAZEL_BIN "bazelisk")
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
|
if (NOT DEFINED CODEQL_BAZEL_WORKSPACE)
|
||||||
|
set(CODEQL_BAZEL_WORKSPACE "codeql")
|
||||||
|
endif ()
|
||||||
|
|
||||||
macro(bazel)
|
macro(bazel)
|
||||||
execute_process(COMMAND ${BAZEL_BIN} ${ARGN}
|
execute_process(COMMAND ${BAZEL_BIN} ${ARGN}
|
||||||
COMMAND_ERROR_IS_FATAL ANY
|
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})
|
set(BAZEL_EXEC_ROOT ${BAZEL_OUTPUT_BASE}/execroot/${BAZEL_EXEC_ROOT})
|
||||||
|
|
||||||
macro(include_generated BAZEL_TARGET)
|
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 "@" "/external/" BAZEL_TARGET_PATH ${BAZEL_TARGET})
|
||||||
string(REPLACE "//" "/" BAZEL_TARGET_PATH ${BAZEL_TARGET_PATH})
|
string(REPLACE "//" "/" BAZEL_TARGET_PATH ${BAZEL_TARGET_PATH})
|
||||||
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)
|
include(${BAZEL_WORKSPACE}/bazel-bin${BAZEL_TARGET_PATH}.cmake)
|
||||||
endmacro()
|
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)
|
if (CREATE_COMPILATION_DATABASE_LINK)
|
||||||
file(CREATE_LINK ${PROJECT_BINARY_DIR}/compile_commands.json ${PROJECT_SOURCE_DIR}/compile_commands.json SYMBOLIC)
|
file(CREATE_LINK ${PROJECT_BINARY_DIR}/compile_commands.json ${PROJECT_SOURCE_DIR}/compile_commands.json SYMBOLIC)
|
||||||
endif ()
|
endif ()
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ py_binary(
|
|||||||
deps = [":_create_extractor_pack"],
|
deps = [":_create_extractor_pack"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# TODO this is unneeded here but still used in the internal repo. Remove once it's not
|
||||||
generate_cmake(
|
generate_cmake(
|
||||||
name = "cmake",
|
name = "cmake",
|
||||||
targets = [
|
targets = [
|
||||||
|
|||||||
@@ -17,4 +17,4 @@ project(codeql)
|
|||||||
|
|
||||||
include(../misc/bazel/cmake/setup.cmake)
|
include(../misc/bazel/cmake/setup.cmake)
|
||||||
|
|
||||||
include_generated(//swift:cmake)
|
generate_and_include(//swift/...)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
load("//swift:rules.bzl", "swift_cc_binary")
|
load("//swift:rules.bzl", "swift_cc_binary")
|
||||||
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
|
|
||||||
load("//misc/bazel:pkg_runfiles.bzl", "pkg_runfiles")
|
load("//misc/bazel:pkg_runfiles.bzl", "pkg_runfiles")
|
||||||
|
|
||||||
swift_cc_binary(
|
swift_cc_binary(
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
load("//swift:rules.bzl", "swift_cc_binary")
|
load("//swift:rules.bzl", "swift_cc_binary")
|
||||||
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
|
|
||||||
|
|
||||||
swift_cc_binary(
|
swift_cc_binary(
|
||||||
name = "assert-false",
|
name = "assert-false",
|
||||||
|
|||||||
Reference in New Issue
Block a user