Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ filegroup(
],
visibility = ["//:__subpackages__"],
)

filegroup(
name = "all_files_for_testing",
srcs = [
"BUILD",
"MODULE.bazel",
"//cc:all_files_for_testing",
],
)
56 changes: 56 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,59 @@ bazel_dep(name = "stardoc", version = "0.8.0", dev_dependency = True)
# Compatibility layer
compat = use_extension("//cc:extensions.bzl", "compatibility_proxy")
use_repo(compat, "cc_compatibility_proxy")

bazel_dep(
name = "rules_bazel_integration_test",
version = "0.37.1",
dev_dependency = True,
)

bazel_binaries = use_extension(
"@rules_bazel_integration_test//:extensions.bzl",
"bazel_binaries",
dev_dependency = True,
)

# A bazel version which is actually whatever is being used.
bazel_binaries.local(
name = "env",
path = "tests/integration/bazel_from_env.sh",
)

bazel_binaries.download(
version = "7.x",
)

bazel_binaries.download(
version = "8.x",
)

bazel_binaries.download(
version = "9.x",
)

bazel_binaries.download(
version = "last_green",
)

bazel_binaries.download(
version = "rolling",
)

use_repo(
bazel_binaries,
"bazel_binaries",
"build_bazel_bazel_7_x",
"build_bazel_bazel_8_x",
"build_bazel_bazel_9_x",
"build_bazel_bazel_last_green",
)

local_bazel = use_extension("//:extensions.bzl", "local_bazel_extension")
use_repo(local_bazel, "local_bazel")

remote_bazel_fetcher = use_extension("//:extensions.bzl", "remote_bazel_extension")
remote_bazel_fetcher.download(bazel_version = "bazel_7")
remote_bazel_fetcher.download(bazel_version = "bazel_8")
remote_bazel_fetcher.download(bazel_version = "bazel_9")
use_repo(remote_bazel_fetcher, "bazel_7", "bazel_8", "bazel_9")
5 changes: 4 additions & 1 deletion cc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ licenses(["notice"]) # Apache 2.0
filegroup(
name = "all_files_for_testing",
srcs = glob(["**"]) + [
"//cc/private/rules_impl:srcs",
"//cc/common:srcs",
"//cc/private:srcs",
"//cc/private/toolchain:srcs",
"//cc/proto:srcs",
"//cc/toolchains:srcs",
],
)

Expand Down
4 changes: 3 additions & 1 deletion cc/private/toolchain/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ filegroup(

filegroup(
name = "srcs",
srcs = glob(["**"]),
srcs = glob(["**"]) + [
"//cc/private/toolchain/test:srcs",
],
)

filegroup(
Expand Down
6 changes: 6 additions & 0 deletions cc/private/toolchain/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ compat_toolchain(
toolchain_type = "@bazel_tools//tools/cpp:test_runner_toolchain_type",
use_target_platform_constraints = True,
)

filegroup(
name = "srcs",
srcs = glob(["**"]),
visibility = ["//cc/private/toolchain:__pkg__"],
)
6 changes: 6 additions & 0 deletions cc/proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ package(default_visibility = ["//visibility:public"])

# Toolchain type provided by proto_lang_toolchain rule and used by cc_proto_library
toolchain_type(name = "toolchain_type")

filegroup(
name = "srcs",
srcs = glob(["**"]),
visibility = ["//cc:__pkg__"],
)
87 changes: 87 additions & 0 deletions extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
load("//:local_bazel.bzl", "local_bazel_import")

SHORT_VERSION_MAPPING = {
"bazel_7": "7.7.1",
"bazel_8": "8.7.0",
"bazel_9": "9.1.0",
}

SHA_MATRIX = {
"7.7.1": {
"linux_x86_64": "115a1b62be95f29e5821d4dddffba1b058905a48019b499919c285e7f708d5e2",
"linux_arm64": "71df04ec724f1b577f1f47ec9a6b81d13f39683f6c3215cacf45fdaf40b2c5c1",
"darwin_x86_64": "8582aea5ee2d8d0448bbda10fd7034734db1a21cbe4ea351a10012b969aa5d31",
"darwin_arm64": "fe8a1ee9064e94afae075c0dd4efb453db9c1373b9df12fecbff8479d408eb08",
"windows_x86_64": "6d9fb21e806cf4f4e61bfa2bc865df4900ffdc1e9ea90ca1016ba70367ef0de4",
},
"8.7.0": {
"linux_x86_64": "d7606e679b78067c811096fb3d6cf135225b528835ca396e3a4dddf957859544",
"linux_arm64": "bfe9558bd8a2ecfe4841ec46c0dbccb4b469fe22d81f2f859de0de222b3e7ce3",
"darwin_x86_64": "76f3eb05782098e9f9ddd8247ec969b085195a3ae2978c81721a2235052ccf26",
"darwin_arm64": "575f20fb23955e02f73519befd180df635b4ed0960c60f0e70fcc8d74014a713",
"windows_x86_64": "29f1796f57379933340afa135f02703ffa21dd30135754bea695f8fd15103420",
},
"9.1.0": {
"linux_x86_64": "a667454f3f4f8878df8199136b82c199f6ada8477b337fae3b1ef854f01e4e2f",
"linux_arm64": "ba933bfc943e4c44f0743a5823aa2312a34b39628532add5dd037e08d8ec27a4",
"darwin_x86_64": "666c6c79eda285cada5f5c39c891c6dd7ee0971b20bff365ea87a4b897271433",
"darwin_arm64": "084a1784fa8f0dcae77fb4e88faa15048d8149a36c947ce198508bffb060e1bb",
"windows_x86_64": "b457dccd36a9bb9be01326cc1d069a201bb50b4b94562a652afb6f43c5148d42",
},
}

def _remote_bazel_extension_impl(module_ctx):
os_name = module_ctx.os.name.lower()
os_key = "linux"
exe_extension = ""
if "windows" in os_name:
os_key = "windows"
exe_extension = ".exe"
elif "mac" in os_name or "darwin" in os_name:
os_key = "darwin"

cpu_arch = module_ctx.os.arch.lower()
if "x86_64" in cpu_arch or "amd64" in cpu_arch:
arch_key = "x86_64"
elif "arm64" in cpu_arch:
arch_key = "arm64"
else:
fail("Unsupported architecture '{}'.".format(cpu_arch))

platform_key = "{}_{}".format(os_key, arch_key)

requested_versions = {}
for mod in module_ctx.modules:
for tag in mod.tags.download:
requested_versions[tag.bazel_version] = True

for bazel_version in requested_versions:
if bazel_version not in SHORT_VERSION_MAPPING:
fail("Unrecognised version '%s'" % bazel_version)
version = SHORT_VERSION_MAPPING[bazel_version]
filename = "bazel-%s-%s-%s%s" % (version, os_key, arch_key, exe_extension)
url = "https://github.com/bazelbuild/bazel/releases/download/%s/%s" % (version, filename)

http_file(
name = bazel_version,
downloaded_file_path = "bazel" + exe_extension,
executable = True,
url = url,
sha256 = SHA_MATRIX[version][platform_key],
)


def _local_bazel_extension_impl(module_ctx):
local_bazel_import(name = "local_bazel")

download_tag = tag_class(attrs = {"bazel_version": attr.string(mandatory = True)})

remote_bazel_extension = module_extension(
implementation = _remote_bazel_extension_impl,
tag_classes = {"download": download_tag},
)

local_bazel_extension = module_extension(
implementation = _local_bazel_extension_impl,
)
44 changes: 44 additions & 0 deletions local_bazel.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
def _local_bazel_import_impl(repository_ctx):
if "windows" in repository_ctx.os.name.lower():
bazel_real = "bazel-real.exe"
bazel_name = "bazel.exe"
else:
bazel_real = "bazel-real"
bazel_name = "bazel"

# Prioritise bazel-real if it exists since it's much more likely to be an actual executable.
bazel_path = repository_ctx.which(bazel_real)
if bazel_path == None:
bazel_path = repository_ctx.which(bazel_name)
if bazel_path == None:
fail("Neither '%s' or '%s' not found on PATH." % (bazel_real, bazel_name))

repository_ctx.symlink(bazel_path, bazel_real)
repository_ctx.file(
"BUILD",
executable = False,
content = """
load({sh_binary_bzl}, "sh_binary")

sh_binary(
name = "{bazel_bin}",
srcs = ["{bazel_binary}"],
visibility = ["//visibility:public"],
)

alias(
name = "bazel_bin",
actual = "{bazel_bin}",
visibility = ["//visibility:public"],
)
""".format(
sh_binary_bzl = repr(str(Label("@rules_shell//shell:sh_binary.bzl"))),
bazel_bin = bazel_name,
bazel_binary = bazel_real,
),
)

local_bazel_import = repository_rule(
implementation = _local_bazel_import_impl,
environ = ["PATH"],
)
62 changes: 62 additions & 0 deletions tests/integration/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@rules_shell//shell:sh_library.bzl", "sh_library")
load("//tests/integration:rules_cc_integration_test.bzl", "gen_workspace", "rules_cc_integration_test")

BAZEL_VERSIONS = [
"7",
"8",
"9",
"latest",
]

string_flag(
name = "bazel_version",
build_setting_default = "latest",
values = BAZEL_VERSIONS,
)

[
config_setting(
name = "bazel_" + VERSION,
flag_values = {
":bazel_version": VERSION,
},
)
for VERSION in BAZEL_VERSIONS
]

sh_library(
name = "test_utils",
srcs = ["test_utils.sh"],
)

sh_library(
name = "platform_utils",
srcs = ["platform_utils.sh"],
)

gen_workspace(
"simple_compile_workspace",
workspace_files = glob(["simple_compile/**"]),
)

rules_cc_integration_test(
name = "simple_compile_test",
test_script = "simple_compile_test.sh",
workspace = "simple_compile_workspace",
workspace_name = "simple_compile",
)

rules_cc_integration_test(
name = "stripped_compile_test",
test_script = "stripped_compile_test.sh",
workspace = "simple_compile_workspace",
workspace_name = "simple_compile",
)

rules_cc_integration_test(
name = "no_recompile_on_shutdown_test",
test_script = "no_recompile_on_shutdown_test.sh",
workspace = "simple_compile_workspace",
workspace_name = "simple_compile",
)
3 changes: 3 additions & 0 deletions tests/integration/bazel_from_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

bazel "$@"
10 changes: 10 additions & 0 deletions tests/integration/no_recompile_on_shutdown_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set -euo pipefail
[ -z "$TEST_SRCDIR" ] && { echo "TEST_SRCDIR not set!" >&2; exit 1; }

source "$(rlocation rules_cc/tests/integration/test_utils.sh)"

bazel build -s //:hello >& "$TEST_log" || fail "Build failed"
expect_log "Compiling hello.cc"
bazel shutdown
bazel build -s //:hello >& "$TEST_log" || fail "Build failed"
expect_not_log "Compiling hello.cc"
32 changes: 32 additions & 0 deletions tests/integration/platform_utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
case "$(uname -s | tr [:upper:] [:lower:])" in
linux*)
declare -r PLATFORM=linux
;;
darwin*)
declare -r PLATFORM=darwin
;;
msys*|mingw*|cygwin*)
declare -r PLATFORM=windows
;;
*)
declare -r PLATFORM=unknown
;;
esac

function is_linux() {
[[ "$PLATFORM" == "linux" ]]
}

function is_darwin() {
[[ "$PLATFORM" == "darwin" ]]
}

function is_windows() {
[[ "$PLATFORM" == "windows" ]]
}

if is_windows; then
declare -r EXE_EXT=".exe"
else
declare -r EXE_EXT=""
fi
Loading