Skip to content

Commit a1af5b6

Browse files
committed
initial stab at mingw windows cross
1 parent c0e6a7e commit a1af5b6

File tree

4 files changed

+252
-5
lines changed

4 files changed

+252
-5
lines changed

.bazelrc

+12-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ build:linux_arm64 --cpu=aarch64
7070
build:linux_arm64 --compiler=clang
7171
build:linux_arm64 --copt=-march=armv8-a
7272

73-
#linux_arm64_debug
73+
# linux_arm64_debug
7474
build:linux_arm64_debug --config=linux_arm64
7575
build:linux_arm64_debug --config=debug
7676

@@ -89,4 +89,14 @@ build:osx_amd64_debug --config=osx_amd64
8989

9090
# osx_amd64 docker sandbox build config
9191
build:osx_amd64_docker --config=osx_amd64_debug
92-
build:osx_amd64_docker --config=docker-sandbox
92+
build:osx_amd64_docker --config=docker-sandbox
93+
94+
# windows
95+
build:windows_amd64 --config=cross
96+
build:windows_amd64 --platforms=@io_bazel_rules_go//go/toolchain:windows_amd64_cgo
97+
build:windows_amd64 --compiler=mingw-w64
98+
99+
# windows debug
100+
build:windows_amd64_debug --config=windows_amd64
101+
build:windows_amd64_debug --config=debug
102+

tools/cross-toolchain/BUILD

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package(default_visibility = ["//visibility:public"])
22

33
load(":cc_toolchain_config_osx.bzl", "osx_cc_toolchain_config")
44
load(":cc_toolchain_config_linux_arm64.bzl", "arm64_cc_toolchain_config")
5+
load(":cc_toolchain_config_windows.bzl", "windows_cc_toolchain_config")
56

67
cc_toolchain_suite(
78
name = "multiarch_toolchain",
@@ -11,6 +12,7 @@ cc_toolchain_suite(
1112
"aarch64|clang": ":cc-clang-arm64",
1213
"k8": "cc-clang-amd64",
1314
"aarch64": ":cc-clang-arm64",
15+
"k8|mingw-w64": ":cc-mingw-amd64",
1416
},
1517
)
1618

@@ -73,6 +75,25 @@ osx_cc_toolchain_config(
7375
target = "darwin_x86_64",
7476
)
7577

78+
windows_cc_toolchain_config(
79+
name = "local-windows",
80+
target = "x86_64-w64",
81+
)
82+
83+
cc_toolchain(
84+
name = "cc-mingw-amd64",
85+
all_files = ":empty",
86+
ar_files = ":empty",
87+
as_files = ":mingw_compiler_files",
88+
compiler_files = ":mingw_compiler_files",
89+
dwp_files = ":empty",
90+
linker_files = ":empty",
91+
objcopy_files = ":empty",
92+
strip_files = ":empty",
93+
supports_param_files = 0,
94+
toolchain_config = ":local-windows",
95+
)
96+
7697
cc_toolchain(
7798
name = "cc-clang-arm64",
7899
all_files = ":empty",
@@ -120,6 +141,7 @@ toolchain(
120141
":linux_arm64": ":cc-clang-arm64",
121142
":linux_amd64": ":cc-clang-amd64",
122143
":osx_amd64": ":cc-clang-osx",
144+
":windows_amd64": ":cc-mingw-amd64",
123145
}),
124146
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
125147
)

tools/cross-toolchain/Dockerfile

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ ENV DOCKER_CLI_EXPERIMENTAL=enabled
66
RUN apt-get update && \
77
apt-get install -y \
88
curl xz-utils \
9-
gcc g++ \
9+
gcc g++ mingw-w64 \
1010
gcc-aarch64-linux-gnu g++-aarch64-linux-gnu \
1111
cmake libssl-dev libxml2-dev vim apt-transport-https \
1212
zip unzip libtinfo5 patch zlib1g-dev autoconf libtool \
13-
pkg-config make docker.io gnupg2 && \
14-
curl https://bazel.build/bazel-release.pub.gpg | apt-key add - && \
13+
pkg-config make docker.io gnupg2
14+
15+
# install bazel
16+
RUN curl https://bazel.build/bazel-release.pub.gpg | apt-key add - && \
1517
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list && \
1618
apt update && apt install -y bazel && \
1719
apt-get clean && \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
load(
2+
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
3+
"action_config",
4+
"feature",
5+
"feature_set",
6+
"flag_group",
7+
"flag_set",
8+
"make_variable",
9+
"tool",
10+
"tool_path",
11+
"with_feature_set",
12+
"artifact_name_pattern",
13+
"env_set",
14+
"env_entry",
15+
)
16+
17+
load(
18+
"@bazel_tools//tools/cpp:cc_toolchain_config.bzl",
19+
ALL_COMPILE_ACTIONS = "all_compile_actions",
20+
ALL_CPP_COMPILE_ACTIONS = "all_cpp_compile_actions",
21+
ALL_LINK_ACTIONS = "all_link_actions",
22+
)
23+
24+
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
25+
26+
def _impl(ctx):
27+
toolchain_identifier = "msys_x64_mingw"
28+
host_system_name = "local"
29+
target_system_name = "local"
30+
target_cpu = "x64_windows"
31+
target_libc = "mingw"
32+
compiler = "mingw-gcc"
33+
abi_version = "local"
34+
abi_libc_version = "local"
35+
cc_target_os = None
36+
builtin_sysroot = None
37+
action_configs = []
38+
39+
install = "/usr/x86_64-w64-mingw32/"
40+
bin_prefix = "/usr/bin/x86_64-w64-mingw32-"
41+
42+
43+
targets_windows_feature = feature(
44+
name = "targets_windows",
45+
implies = ["copy_dynamic_libraries_to_binary"],
46+
enabled = True,
47+
)
48+
49+
copy_dynamic_libraries_to_binary_feature = feature(name = "copy_dynamic_libraries_to_binary")
50+
51+
gcc_env_feature = feature(
52+
name = "gcc_env",
53+
enabled = True,
54+
env_sets = [
55+
env_set(
56+
actions = [
57+
ACTION_NAMES.c_compile,
58+
ACTION_NAMES.cpp_compile,
59+
ACTION_NAMES.cpp_module_compile,
60+
ACTION_NAMES.cpp_module_codegen,
61+
ACTION_NAMES.cpp_header_parsing,
62+
ACTION_NAMES.assemble,
63+
ACTION_NAMES.preprocess_assemble,
64+
ACTION_NAMES.cpp_link_executable,
65+
ACTION_NAMES.cpp_link_dynamic_library,
66+
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
67+
ACTION_NAMES.cpp_link_static_library,
68+
],
69+
env_entries = [
70+
env_entry(key = "PATH", value = "NOT_USED"),
71+
],
72+
),
73+
],
74+
)
75+
76+
msys_mingw_flags = [
77+
"-B " + install + "bin",
78+
"-nostdinc",
79+
"-U_FORTIFY_SOURCE",
80+
"-fstack-protector",
81+
"-fno-omit-frame-pointer",
82+
"-fcolor-diagnostics",
83+
"-Wall",
84+
"-Wthread-safety",
85+
"-Wself-assign",
86+
"-x c++",
87+
"-lstdc++",
88+
"-lpthread"
89+
]
90+
91+
msys_mingw_link_flags = [
92+
"-l:libstdc++.a",
93+
"-L" + install + "lib",
94+
"-L/usr/lib/gcc/x86_64-w64-mingw32/8.3-w32",
95+
"-v",
96+
"-lm",
97+
"-no-canonical-prefixes",
98+
]
99+
100+
default_compile_flags_feature = feature(
101+
name = "default_compile_flags",
102+
enabled = True,
103+
flag_sets = [
104+
flag_set(
105+
actions = [
106+
ACTION_NAMES.assemble,
107+
ACTION_NAMES.preprocess_assemble,
108+
ACTION_NAMES.linkstamp_compile,
109+
ACTION_NAMES.c_compile,
110+
ACTION_NAMES.cpp_compile,
111+
ACTION_NAMES.cpp_header_parsing,
112+
ACTION_NAMES.cpp_module_compile,
113+
ACTION_NAMES.cpp_module_codegen,
114+
ACTION_NAMES.lto_backend,
115+
ACTION_NAMES.clif_match,
116+
],
117+
),
118+
flag_set(
119+
actions = [
120+
ACTION_NAMES.linkstamp_compile,
121+
ACTION_NAMES.cpp_compile,
122+
ACTION_NAMES.cpp_header_parsing,
123+
ACTION_NAMES.cpp_module_compile,
124+
ACTION_NAMES.cpp_module_codegen,
125+
ACTION_NAMES.lto_backend,
126+
ACTION_NAMES.clif_match,
127+
],
128+
flag_groups = ([flag_group(flags = msys_mingw_flags)] if msys_mingw_flags else []),
129+
),
130+
],
131+
)
132+
133+
compiler_param_file_feature = feature(
134+
name = "compiler_param_file",
135+
)
136+
137+
default_link_flags_feature = feature(
138+
name = "default_link_flags",
139+
enabled = True,
140+
flag_sets = [
141+
flag_set(
142+
actions = ALL_LINK_ACTIONS,
143+
flag_groups = ([flag_group(flags = msys_mingw_link_flags)] if msys_mingw_link_flags else []),
144+
),
145+
],
146+
)
147+
148+
supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True)
149+
150+
features = [
151+
targets_windows_feature,
152+
copy_dynamic_libraries_to_binary_feature,
153+
gcc_env_feature,
154+
default_compile_flags_feature,
155+
compiler_param_file_feature,
156+
default_link_flags_feature,
157+
supports_dynamic_linker_feature,
158+
]
159+
160+
cxx_builtin_include_directories = [
161+
install +"include"
162+
]
163+
164+
artifact_name_patterns = [
165+
artifact_name_pattern(
166+
category_name = "executable",
167+
prefix = "",
168+
extension = ".exe",
169+
),
170+
]
171+
172+
make_variables = []
173+
tool_paths = [
174+
tool_path(name = "ld", path = bin_prefix + "ld"),
175+
tool_path(name = "cpp", path = bin_prefix + "cpp"),
176+
tool_path(name = "gcov", path = "/usr/bin/gcov"),
177+
tool_path(name = "nm", path = bin_prefix + "nm"),
178+
tool_path(name = "objcopy", path = bin_prefix + "objcopy"),
179+
tool_path(name = "objdump", path = bin_prefix + "objdump"),
180+
tool_path(name = "strip", path = bin_prefix + "strip"),
181+
tool_path(name = "gcc", path = bin_prefix + "gcc"),
182+
tool_path(name = "ar", path = bin_prefix + "ar"),
183+
]
184+
185+
return cc_common.create_cc_toolchain_config_info(
186+
ctx = ctx,
187+
features = features,
188+
action_configs = action_configs,
189+
artifact_name_patterns = artifact_name_patterns,
190+
cxx_builtin_include_directories = cxx_builtin_include_directories,
191+
toolchain_identifier = toolchain_identifier,
192+
host_system_name = host_system_name,
193+
target_system_name = target_system_name,
194+
target_cpu = target_cpu,
195+
target_libc = target_libc,
196+
compiler = compiler,
197+
abi_version = abi_version,
198+
abi_libc_version = abi_libc_version,
199+
tool_paths = tool_paths,
200+
make_variables = make_variables,
201+
builtin_sysroot = builtin_sysroot,
202+
cc_target_os = cc_target_os,
203+
)
204+
205+
windows_cc_toolchain_config = rule(
206+
implementation = _impl,
207+
attrs = {
208+
"target": attr.string(mandatory = True),
209+
"stdlib": attr.string(),
210+
},
211+
provides = [CcToolchainConfigInfo],
212+
)
213+

0 commit comments

Comments
 (0)