Skip to content

Commit 1809c77

Browse files
Copilotphlax
andauthored
bazel: Add GCC compat and CI (#3476)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: phlax <454682+phlax@users.noreply.github.com>
1 parent 6d0734b commit 1809c77

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

.github/workflows/bazel.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,39 @@ jobs:
8787
- workspace
8888
- bzlmod
8989

90+
test-gcc:
91+
needs: request
92+
if: >-
93+
always()
94+
&& fromJSON(needs.request.outputs.run || 'false')
95+
uses: ./.github/workflows/_bazel.yml
96+
with:
97+
action: build
98+
bazel-args: >-
99+
--config=ci
100+
--config=gcc
101+
${{ matrix.mode == 'bzlmod'
102+
&& '--enable_bzlmod'
103+
|| '' }}
104+
bazel-mode: ${{ matrix.mode }}-gcc
105+
bazel-path: bazel
106+
targets: >-
107+
//compile/...
108+
//coverage/...
109+
//dependency/...
110+
//format/...
111+
//repository/...
112+
//sha/...
113+
//tarball/...
114+
//toolchains/...
115+
//:dependency_versions
116+
strategy:
117+
fail-fast: false
118+
matrix:
119+
mode:
120+
- workspace
121+
- bzlmod
122+
90123
status:
91124
runs-on: ubuntu-24.04
92125
if: >-
@@ -97,6 +130,7 @@ jobs:
97130
- request
98131
- test
99132
- build
133+
- test-gcc
100134
steps:
101135
- run: |
102136
if [[ "${{ contains(needs.*.result, 'failure') }}" == "true" || "${{ contains(needs.*.result, 'cancelled') }}" == "true" ]]; then

bazel/.bazelrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ common:ci --noshow_loading_progress
2020
common:ci --test_output=errors
2121

2222

23+
#############################################################################
24+
# gcc - Use system GCC toolchain (similar to BCR testing)
25+
#############################################################################
26+
27+
# Allow detection of system C++ toolchain for GCC testing
28+
common:gcc --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=0
29+
30+
# Enable GCC-compatible flags in compile targets
31+
common:gcc --//compile:use_gcc_toolchain=true
32+
33+
2334
#############################################################################
2435
# imports
2536
#############################################################################

bazel/compile/BUILD

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
12
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
23
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
34
load("@rules_pkg//pkg:pkg.bzl", "pkg_tar")
@@ -7,20 +8,56 @@ package(default_visibility = ["//visibility:public"])
78

89
exports_files(["sanitizer_libs.bzl", "extensions.bzl"])
910

10-
SANITIZER_ENV = {
11+
# Build setting to control which toolchain to use
12+
# This can be set with --//compile:use_gcc_toolchain=true
13+
bool_flag(
14+
name = "use_gcc_toolchain",
15+
build_setting_default = False,
16+
)
17+
18+
config_setting(
19+
name = "gcc_build",
20+
flag_values = {":use_gcc_toolchain": "true"},
21+
)
22+
23+
# LLVM/Clang specific environment variables
24+
LLVM_SANITIZER_ENV = {
1125
"CXXFLAGS": "-nostdinc++ -nostdlib++",
1226
"LDFLAGS": "-nostdlib++",
1327
}
1428

15-
BASE_CMAKE_CACHE = {
16-
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
17-
"CMAKE_INSTALL_PREFIX": ".",
18-
"CMAKE_POSITION_INDEPENDENT_CODE": "ON",
19-
"LLVM_ENABLE_RUNTIMES": "libcxxabi;libcxx;libunwind",
29+
# GCC compatible environment (nostdlib++ is Clang-specific)
30+
GCC_SANITIZER_ENV = {
31+
"CXXFLAGS": "-nostdinc++",
32+
"LDFLAGS": "",
33+
}
34+
35+
SANITIZER_ENV = select({
36+
":gcc_build": GCC_SANITIZER_ENV,
37+
"//conditions:default": LLVM_SANITIZER_ENV,
38+
})
39+
40+
# Linker flags that differ between LLVM (lld) and GCC (default linker)
41+
LLVM_LINKER_FLAGS = {
2042
"LLVM_USE_LINKER": "lld",
2143
"CMAKE_SHARED_LINKER_FLAGS": "-shared -Wl,-S -fuse-ld=lld",
2244
"CMAKE_MODULE_LINKER_FLAGS": "-shared -Wl,-S -fuse-ld=lld",
2345
"CMAKE_EXE_LINKER_FLAGS": "-Wl,-S -fuse-ld=lld",
46+
}
47+
48+
GCC_LINKER_FLAGS = {
49+
# Don't specify LLVM_USE_LINKER for GCC, let CMake/compiler choose
50+
"CMAKE_SHARED_LINKER_FLAGS": "-shared -Wl,-S",
51+
"CMAKE_MODULE_LINKER_FLAGS": "-shared -Wl,-S",
52+
"CMAKE_EXE_LINKER_FLAGS": "-Wl,-S",
53+
}
54+
55+
# Base CMake configuration without linker-specific flags
56+
BASE_CMAKE_CACHE_COMMON = {
57+
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
58+
"CMAKE_INSTALL_PREFIX": ".",
59+
"CMAKE_POSITION_INDEPENDENT_CODE": "ON",
60+
"LLVM_ENABLE_RUNTIMES": "libcxxabi;libcxx;libunwind",
2461
"BUILD_SHARED_LIBS": "OFF",
2562
"LIBCXX_ENABLE_SHARED": "OFF",
2663
"LIBCXX_ENABLE_STATIC": "ON",
@@ -30,6 +67,12 @@ BASE_CMAKE_CACHE = {
3067
"LIBUNWIND_ENABLE_STATIC": "ON",
3168
}
3269

70+
# Merge with linker-specific flags based on toolchain
71+
BASE_CMAKE_CACHE = BASE_CMAKE_CACHE_COMMON | select({
72+
":gcc_build": GCC_LINKER_FLAGS,
73+
"//conditions:default": LLVM_LINKER_FLAGS,
74+
})
75+
3376
cmake(
3477
name = "libcxx_msan",
3578
cache_entries = BASE_CMAKE_CACHE | {

0 commit comments

Comments
 (0)