Skip to content

Commit c33a467

Browse files
authored
add mingw support [AP-2090] (#145)
Add MingW Cross Compiler Toolchain Testing: The current consumer of this toolchain is Starling-Core. This toolchain is limited to an x86_64 Linux host to build an x86_64 Windows target. With this toolchain, we are generating a binary and a static library. The artifacts were generated using CI/CD and were tested on Linux using Wine and on a Windows 11 system. The static library that was generated was also tested on Windows 11 to generate the binary and static data was passed, and the results were compared. The same steps above were also done on an x86_64 Ubuntu 24.04 developer machine.
1 parent 8d9b80a commit c33a467

19 files changed

+559
-0
lines changed

cc/repositories.bzl

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ AARCH64_LINUX_LLVM = "https://github.com/llvm/llvm-project/releases/download/llv
2020

2121
X86_64_LINUX_LLVM = "https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang%2Bllvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz"
2222

23+
X86_64_LINUX_UCRT_LLVM_MINGW = "https://github.com/mstorsjo/llvm-mingw/releases/download/20241203/llvm-mingw-20241203-ucrt-ubuntu-20.04-x86_64.tar.xz"
24+
2325
AARCH64_LINUX_MUSL = "https://github.com/swift-nav/swift-toolchains/releases/download/musl-cross-11.2.0/aarch64-linux-musl-cross.tar.gz"
2426

2527
ARM_LINUX_MUSLEABIHF = "https://github.com/swift-nav/swift-toolchains/releases/download/musl-cross-11.2.0/arm-linux-musleabihf-cross.tar.gz"
@@ -158,6 +160,18 @@ def x86_64_linux_musl_toolchain():
158160
def register_x86_64_linux_musl_toolchain():
159161
native.register_toolchains("@rules_swiftnav//cc/toolchains/musl/x86_64:toolchain")
160162

163+
def llvm_mingw_toolchain():
164+
http_archive(
165+
name = "llvm_mingw_toolchain",
166+
build_file = "@rules_swiftnav//cc/toolchains/llvm_x86_64_windows:toolchain.BUILD",
167+
sha256 = "21458febf5d2c918df922dd0da60137a8787e5e6b427925a1977c882fc79b550",
168+
strip_prefix = "llvm-mingw-20241203-ucrt-ubuntu-20.04-x86_64",
169+
url = X86_64_LINUX_UCRT_LLVM_MINGW,
170+
)
171+
172+
def register_llvm_mingw_toolchain():
173+
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm_x86_64_windows:mingw_toolchain")
174+
161175
def gcc_arm_embedded_toolchain():
162176
http_archive(
163177
name = "x86_64_linux_gcc_arm_embedded_toolchain",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Copyright (C) 2024 Swift Navigation Inc.
2+
# Contact: Swift Navigation <[email protected]>
3+
#
4+
# This source is subject to the license found in the file 'LICENSE' which must
5+
# be be distributed together with this source. All other rights reserved.
6+
#
7+
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
8+
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
9+
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
10+
11+
load(":config.bzl", "config")
12+
13+
filegroup(name = "empty")
14+
15+
constraint_value(
16+
name = "llvm_mingw_toolchain",
17+
constraint_setting = "@rules_swiftnav//cc/constraints:toolchain",
18+
visibility = ["//visibility:public"],
19+
)
20+
21+
filegroup(
22+
name = "wrappers",
23+
srcs = glob([
24+
"wrappers/**",
25+
]),
26+
visibility = ["//visibility:public"],
27+
)
28+
29+
filegroup(
30+
name = "ar_files",
31+
srcs = [
32+
":wrappers",
33+
"@llvm_mingw_toolchain//:ar",
34+
],
35+
)
36+
37+
filegroup(
38+
name = "as_files",
39+
srcs = [
40+
":wrappers",
41+
"@llvm_mingw_toolchain//:as",
42+
],
43+
)
44+
45+
filegroup(
46+
name = "compiler_files",
47+
srcs = [
48+
":wrappers",
49+
"@llvm_mingw_toolchain//:bin",
50+
"@llvm_mingw_toolchain//:include",
51+
],
52+
)
53+
54+
filegroup(
55+
name = "dwp_files",
56+
srcs = [
57+
":wrappers",
58+
"@llvm_mingw_toolchain//:dwp",
59+
],
60+
)
61+
62+
filegroup(
63+
name = "linker_files",
64+
srcs = [
65+
":wrappers",
66+
"@llvm_mingw_toolchain//:ar",
67+
"@llvm_mingw_toolchain//:clang",
68+
"@llvm_mingw_toolchain//:gcc",
69+
"@llvm_mingw_toolchain//:ld",
70+
"@llvm_mingw_toolchain//:lib",
71+
],
72+
)
73+
74+
filegroup(
75+
name = "objcopy_files",
76+
srcs = [
77+
":wrappers",
78+
"@llvm_mingw_toolchain//:objcopy",
79+
],
80+
)
81+
82+
filegroup(
83+
name = "strip_files",
84+
srcs = [
85+
":wrappers",
86+
"@llvm_mingw_toolchain//:strip",
87+
],
88+
)
89+
90+
filegroup(
91+
name = "all_files",
92+
srcs = [
93+
":compiler_files",
94+
":linker_files",
95+
"@llvm_mingw_toolchain//:bin",
96+
],
97+
)
98+
99+
config(name = "config")
100+
101+
cc_toolchain(
102+
name = "cc_toolchain",
103+
all_files = ":all_files",
104+
ar_files = ":ar_files",
105+
as_files = ":as_files",
106+
compiler_files = ":compiler_files",
107+
dwp_files = ":empty",
108+
linker_files = ":linker_files",
109+
objcopy_files = ":objcopy_files",
110+
strip_files = ":strip_files",
111+
supports_param_files = 0,
112+
toolchain_config = ":config",
113+
toolchain_identifier = "llvm-mingw",
114+
)
115+
116+
toolchain(
117+
name = "mingw_toolchain",
118+
exec_compatible_with = [
119+
"@platforms//cpu:x86_64",
120+
"@platforms//os:linux",
121+
],
122+
target_compatible_with = [
123+
"@platforms//cpu:x86_64",
124+
"@platforms//os:windows",
125+
],
126+
target_settings = None,
127+
toolchain = ":cc_toolchain",
128+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
129+
visibility = ["//visibility:public"],
130+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set", "tool_path", "with_feature_set")
2+
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
3+
4+
def _impl(ctx):
5+
SDK_PATH_PREFIX = "wrappers/x86_64-w64-mingw32uwp-{}"
6+
7+
tool_paths = [
8+
tool_path(
9+
name = "ar",
10+
path = SDK_PATH_PREFIX.format("ar"),
11+
),
12+
tool_path(
13+
name = "as",
14+
path = SDK_PATH_PREFIX.format("as"),
15+
),
16+
tool_path(
17+
name = "cpp",
18+
path = SDK_PATH_PREFIX.format("cpp"),
19+
),
20+
tool_path(
21+
name = "gcc",
22+
path = SDK_PATH_PREFIX.format("gcc"),
23+
),
24+
tool_path(
25+
name = "g++",
26+
path = SDK_PATH_PREFIX.format("g++"),
27+
),
28+
tool_path(
29+
name = "gcov",
30+
path = SDK_PATH_PREFIX.format("gcov"),
31+
),
32+
tool_path(
33+
name = "ld",
34+
path = SDK_PATH_PREFIX.format("ld"),
35+
),
36+
tool_path(
37+
name = "nm",
38+
path = SDK_PATH_PREFIX.format("nm"),
39+
),
40+
tool_path(
41+
name = "objcopy",
42+
path = SDK_PATH_PREFIX.format("objcopy"),
43+
),
44+
tool_path(
45+
name = "objdump",
46+
path = SDK_PATH_PREFIX.format("objdump"),
47+
),
48+
tool_path(
49+
name = "ranlib",
50+
path = SDK_PATH_PREFIX.format("ranlib"),
51+
),
52+
tool_path(
53+
name = "strip",
54+
path = SDK_PATH_PREFIX.format("strip"),
55+
),
56+
]
57+
58+
all_compile_actions = [
59+
ACTION_NAMES.assemble,
60+
ACTION_NAMES.c_compile,
61+
ACTION_NAMES.cpp_compile,
62+
ACTION_NAMES.cpp_header_parsing,
63+
ACTION_NAMES.cpp_module_codegen,
64+
ACTION_NAMES.cpp_module_compile,
65+
ACTION_NAMES.clif_match,
66+
ACTION_NAMES.linkstamp_compile,
67+
ACTION_NAMES.lto_backend,
68+
ACTION_NAMES.preprocess_assemble,
69+
]
70+
71+
opt_feature = feature(name = "opt")
72+
73+
features = [
74+
feature(
75+
name = "default_compile_actions",
76+
enabled = True,
77+
flag_sets = [
78+
flag_set(
79+
actions = all_compile_actions,
80+
flag_groups = ([
81+
flag_group(
82+
flags = [
83+
"--sysroot=external/llvm_mingw_toolchain",
84+
"-no-canonical-prefixes",
85+
# Reproducibility
86+
"-Wno-builtin-macro-redefined",
87+
"-D__DATE__=\"redacted\"",
88+
"-D__TIMESTAMP__=\"redacted\"",
89+
"-D__TIME__=\"redacted\"",
90+
],
91+
),
92+
]),
93+
),
94+
flag_set(
95+
actions = all_compile_actions,
96+
flag_groups = ([
97+
flag_group(
98+
flags = ["-O2", "-g"],
99+
),
100+
]),
101+
with_features = [with_feature_set(features = ["opt"])],
102+
),
103+
],
104+
),
105+
feature(
106+
name = "default_link_flags",
107+
enabled = True,
108+
flag_sets = [
109+
flag_set(
110+
actions = [
111+
ACTION_NAMES.cpp_link_executable,
112+
ACTION_NAMES.cpp_link_dynamic_library,
113+
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
114+
],
115+
flag_groups = ([
116+
flag_group(
117+
flags = [
118+
"-lstdc++",
119+
"-lm",
120+
"-static",
121+
],
122+
),
123+
]),
124+
),
125+
],
126+
),
127+
opt_feature,
128+
]
129+
130+
return cc_common.create_cc_toolchain_config_info(
131+
ctx = ctx,
132+
features = features,
133+
toolchain_identifier = "llvm-mingw",
134+
host_system_name = "local",
135+
target_system_name = "local",
136+
target_cpu = "x86_64",
137+
target_libc = "unknown",
138+
compiler = "llvm",
139+
abi_version = "unknown",
140+
abi_libc_version = "unknown",
141+
tool_paths = tool_paths,
142+
)
143+
144+
config = rule(
145+
implementation = _impl,
146+
attrs = {
147+
"c_opts": attr.string_list(),
148+
"link_opts": attr.string_list(),
149+
},
150+
provides = [CcToolchainConfigInfo],
151+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
filegroup(
4+
name = "gcc",
5+
srcs = [
6+
"bin/x86_64-w64-mingw32uwp-c++",
7+
"bin/x86_64-w64-mingw32uwp-g++",
8+
"bin/x86_64-w64-mingw32uwp-gcc",
9+
],
10+
)
11+
12+
filegroup(
13+
name = "clang",
14+
srcs = [
15+
"bin/clang-cpp",
16+
"bin/x86_64-w64-mingw32uwp-clang",
17+
"bin/x86_64-w64-mingw32uwp-clang++",
18+
],
19+
)
20+
21+
filegroup(
22+
name = "ld",
23+
srcs = [
24+
"bin/x86_64-w64-mingw32uwp-ld",
25+
],
26+
)
27+
28+
filegroup(
29+
name = "include",
30+
srcs = glob([
31+
"x86_64-w64-mingw32/include/**",
32+
"lib/clang/*/include/**",
33+
"generic-w64-mingw32/include/c++/v1/*",
34+
]),
35+
)
36+
37+
filegroup(
38+
name = "bin",
39+
srcs = glob([
40+
"bin/**",
41+
"x86_64-w64-mingw32/**",
42+
]),
43+
)
44+
45+
filegroup(
46+
name = "lib",
47+
srcs = glob([
48+
"lib/**/lib*.a",
49+
"lib/clang/*/lib/**/*.a",
50+
"x86_64-w64-mingw32/lib/*.a",
51+
]),
52+
)
53+
54+
filegroup(
55+
name = "ar",
56+
srcs = ["bin/x86_64-w64-mingw32uwp-ar"],
57+
)
58+
59+
filegroup(
60+
name = "as",
61+
srcs = ["bin/x86_64-w64-mingw32uwp-as"],
62+
)
63+
64+
filegroup(
65+
name = "nm",
66+
srcs = ["bin/x86_64-w64-mingw32uwp-nm"],
67+
)
68+
69+
filegroup(
70+
name = "objcopy",
71+
srcs = ["bin/x86_64-w64-mingw32uwp-objcopy"],
72+
)
73+
74+
filegroup(
75+
name = "objdump",
76+
srcs = ["bin/x86_64-w64-mingw32uwp-objdump"],
77+
)
78+
79+
filegroup(
80+
name = "ranlib",
81+
srcs = ["bin/x86_64-w64-mingw32uwp-ranlib"],
82+
)
83+
84+
filegroup(
85+
name = "strip",
86+
srcs = ["bin/x86_64-w64-mingw32uwp-strip"],
87+
)

0 commit comments

Comments
 (0)