Skip to content

Commit

Permalink
Support building with Clang-CL on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
phst committed Mar 4, 2025
1 parent 3032a43 commit edd6c7b
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ build --//private:treat_warnings_as_errors

build --compilation_mode=dbg --host_compilation_mode=dbg

build:clang-cl --compiler=clang-cl --host_compiler=clang-cl
build:clang-cl --extra_toolchains='@local_config_cc//:cc-toolchain-x64_windows-clang-cl'
build:clang-cl --extra_execution_platforms='//elisp/private:windows_clang_cl'

import %workspace%/c-std.bazelrc
6 changes: 6 additions & 0 deletions .github/actions/set-up/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ runs:
# --incompatible_enforce_starlark_utf8 was added in Bazel 8.1.
# FIXME: Add it to the project’s .bazelrc once we drop support for
# Bazel 8.0 and below.
# See https://bazel.build/configure/windows#clang for how to configure
# Bazel to use Clang on Windows.
bazelrc: |
common --announce_rc
common --show_progress_rate_limit=10
Expand All @@ -73,6 +75,10 @@ runs:
inputs.bazel-version == 'latest' &&
'common --incompatible_enforce_starlark_utf8=error'
}}
${{
runner.os == 'Windows' && inputs.cc == 'clang' &&
'build --config=clang-cl'
}}
# Use disk cache to speed up runs.
disk-cache: ${{inputs.bazel-version}}-${{inputs.cc}}
repository-cache: true
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/bazel-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ jobs:
cc: msvc
- os: windows-latest
cc: gcc
- os: windows-latest
cc: clang
# Exclude non-default compilers for now. We should add them later.
- os: macos-latest
cc: gcc
Expand Down
20 changes: 20 additions & 0 deletions elisp/private/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ selects.config_setting_group(
visibility = ["//visibility:private"],
)

selects.config_setting_group(
name = "msvc_or_clang_cl",
match_any = [
"@rules_cc//cc/compiler:msvc-cl",
"@rules_cc//cc/compiler:clang-cl",
],
visibility = ["//visibility:private"],
)

# keep
bzl_library(
name = "module_config_info",
Expand Down Expand Up @@ -242,6 +251,17 @@ bzl_library(
visibility = ["//elisp:__pkg__"],
)

# https://bazel.build/configure/windows#clang
platform(
name = "windows_clang_cl",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
"@rules_cc//cc/private/toolchain:clang-cl",
],
visibility = ["//visibility:private"],
)

bzl_library(
name = "cc_config",
srcs = ["cc_config.bzl"],
Expand Down
16 changes: 13 additions & 3 deletions elisp/private/cc_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ FEATURES = select({

# Shared C++ compilation options.
COPTS = select({
Label("@rules_cc//cc/compiler:msvc-cl"): [
Label(":msvc_or_clang_cl"): [
"/W4",
"/utf-8",
"/permissive-",
Expand All @@ -48,10 +48,20 @@ COPTS = select({
"--system-header-prefix=upb/",
],
Label("//conditions:default"): [],
}) + select({
Label("@rules_cc//cc/compiler:clang-cl"): [
# Allow Clang-CL to include windows.h without error.
"-Wno-error=ignored-attributes",
"-Wno-error=ignored-pragma-intrinsic",
"-Wno-error=macro-redefined",
"-Wno-error=pragma-pack",
"-Wno-error=unknown-pragmas",
],
Label("//conditions:default"): [],
})

CXXOPTS = select({
Label("@rules_cc//cc/compiler:msvc-cl"): [],
Label(":msvc_or_clang_cl"): [],
Label("@rules_cc//cc/compiler:gcc"): [
# GCC appears to treat some moves as redundant that are in fact
# necessary.
Expand All @@ -61,7 +71,7 @@ CXXOPTS = select({
})

CONLYOPTS = select({
Label("@rules_cc//cc/compiler:msvc-cl"): [],
Label(":msvc_or_clang_cl"): [],
Label(":gcc_or_clang"): ["-Wvla"],
})

Expand Down
2 changes: 1 addition & 1 deletion elisp/private/cc_launcher_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ LAUNCHER_FEATURES = FEATURES
LAUNCHER_COPTS = COPTS + CXXOPTS
LAUNCHER_DEFINES = DEFINES
LAUNCHER_LINKOPTS = LINKOPTS + select({
Label("@rules_cc//cc/compiler:msvc-cl"): ["/SUBSYSTEM:CONSOLE"],
Label(":msvc_or_clang_cl"): ["/SUBSYSTEM:CONSOLE"],
Label(":gcc_or_clang"): [],
})

Expand Down
2 changes: 1 addition & 1 deletion elisp/proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ elisp_cc_module(
name = "module",
srcs = ["module.c"],
copts = COPTS + CONLYOPTS + select({
"@rules_cc//cc/compiler:msvc-cl": ["/wd4100"],
"//elisp/private:msvc_or_clang_cl": ["/wd4100"],
"//elisp/private:gcc_or_clang": ["-fvisibility=hidden"],
}),
features = FEATURES + ["-default_link_libs"],
Expand Down
4 changes: 4 additions & 0 deletions examples/ext/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

build:clang-cl --compiler=clang-cl --host_compiler=clang-cl
build:clang-cl --extra_toolchains='@local_config_cc//:cc-toolchain-x64_windows-clang-cl'
build:clang-cl --extra_execution_platforms='@phst_rules_elisp//elisp/private:windows_clang_cl'

import %workspace%/../../c-std.bazelrc
6 changes: 6 additions & 0 deletions examples/ext/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ elisp.http_archive(
],
)
use_repo(elisp, "dash")

# Needed for building with Clang-CL on Windows.
bazel_dep(name = "rules_cc", version = "0.1.1", dev_dependency = True)

cc_configure = use_extension("@rules_cc//cc:extensions.bzl", "cc_configure_extension", dev_dependency = True)
use_repo(cc_configure, "local_config_cc")

0 comments on commit edd6c7b

Please sign in to comment.