Skip to content

Commit

Permalink
add mingw support [AP-2090] (#145)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sokhealy authored Dec 20, 2024
1 parent 8d9b80a commit c33a467
Show file tree
Hide file tree
Showing 19 changed files with 559 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cc/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ AARCH64_LINUX_LLVM = "https://github.com/llvm/llvm-project/releases/download/llv

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"

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"

AARCH64_LINUX_MUSL = "https://github.com/swift-nav/swift-toolchains/releases/download/musl-cross-11.2.0/aarch64-linux-musl-cross.tar.gz"

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

def llvm_mingw_toolchain():
http_archive(
name = "llvm_mingw_toolchain",
build_file = "@rules_swiftnav//cc/toolchains/llvm_x86_64_windows:toolchain.BUILD",
sha256 = "21458febf5d2c918df922dd0da60137a8787e5e6b427925a1977c882fc79b550",
strip_prefix = "llvm-mingw-20241203-ucrt-ubuntu-20.04-x86_64",
url = X86_64_LINUX_UCRT_LLVM_MINGW,
)

def register_llvm_mingw_toolchain():
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm_x86_64_windows:mingw_toolchain")

def gcc_arm_embedded_toolchain():
http_archive(
name = "x86_64_linux_gcc_arm_embedded_toolchain",
Expand Down
130 changes: 130 additions & 0 deletions cc/toolchains/llvm_x86_64_windows/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Copyright (C) 2024 Swift Navigation Inc.
# Contact: Swift Navigation <[email protected]>
#
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

load(":config.bzl", "config")

filegroup(name = "empty")

constraint_value(
name = "llvm_mingw_toolchain",
constraint_setting = "@rules_swiftnav//cc/constraints:toolchain",
visibility = ["//visibility:public"],
)

filegroup(
name = "wrappers",
srcs = glob([
"wrappers/**",
]),
visibility = ["//visibility:public"],
)

filegroup(
name = "ar_files",
srcs = [
":wrappers",
"@llvm_mingw_toolchain//:ar",
],
)

filegroup(
name = "as_files",
srcs = [
":wrappers",
"@llvm_mingw_toolchain//:as",
],
)

filegroup(
name = "compiler_files",
srcs = [
":wrappers",
"@llvm_mingw_toolchain//:bin",
"@llvm_mingw_toolchain//:include",
],
)

filegroup(
name = "dwp_files",
srcs = [
":wrappers",
"@llvm_mingw_toolchain//:dwp",
],
)

filegroup(
name = "linker_files",
srcs = [
":wrappers",
"@llvm_mingw_toolchain//:ar",
"@llvm_mingw_toolchain//:clang",
"@llvm_mingw_toolchain//:gcc",
"@llvm_mingw_toolchain//:ld",
"@llvm_mingw_toolchain//:lib",
],
)

filegroup(
name = "objcopy_files",
srcs = [
":wrappers",
"@llvm_mingw_toolchain//:objcopy",
],
)

filegroup(
name = "strip_files",
srcs = [
":wrappers",
"@llvm_mingw_toolchain//:strip",
],
)

filegroup(
name = "all_files",
srcs = [
":compiler_files",
":linker_files",
"@llvm_mingw_toolchain//:bin",
],
)

config(name = "config")

cc_toolchain(
name = "cc_toolchain",
all_files = ":all_files",
ar_files = ":ar_files",
as_files = ":as_files",
compiler_files = ":compiler_files",
dwp_files = ":empty",
linker_files = ":linker_files",
objcopy_files = ":objcopy_files",
strip_files = ":strip_files",
supports_param_files = 0,
toolchain_config = ":config",
toolchain_identifier = "llvm-mingw",
)

toolchain(
name = "mingw_toolchain",
exec_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
],
target_settings = None,
toolchain = ":cc_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
visibility = ["//visibility:public"],
)
151 changes: 151 additions & 0 deletions cc/toolchains/llvm_x86_64_windows/config.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set", "tool_path", "with_feature_set")
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")

def _impl(ctx):
SDK_PATH_PREFIX = "wrappers/x86_64-w64-mingw32uwp-{}"

tool_paths = [
tool_path(
name = "ar",
path = SDK_PATH_PREFIX.format("ar"),
),
tool_path(
name = "as",
path = SDK_PATH_PREFIX.format("as"),
),
tool_path(
name = "cpp",
path = SDK_PATH_PREFIX.format("cpp"),
),
tool_path(
name = "gcc",
path = SDK_PATH_PREFIX.format("gcc"),
),
tool_path(
name = "g++",
path = SDK_PATH_PREFIX.format("g++"),
),
tool_path(
name = "gcov",
path = SDK_PATH_PREFIX.format("gcov"),
),
tool_path(
name = "ld",
path = SDK_PATH_PREFIX.format("ld"),
),
tool_path(
name = "nm",
path = SDK_PATH_PREFIX.format("nm"),
),
tool_path(
name = "objcopy",
path = SDK_PATH_PREFIX.format("objcopy"),
),
tool_path(
name = "objdump",
path = SDK_PATH_PREFIX.format("objdump"),
),
tool_path(
name = "ranlib",
path = SDK_PATH_PREFIX.format("ranlib"),
),
tool_path(
name = "strip",
path = SDK_PATH_PREFIX.format("strip"),
),
]

all_compile_actions = [
ACTION_NAMES.assemble,
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_codegen,
ACTION_NAMES.cpp_module_compile,
ACTION_NAMES.clif_match,
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.lto_backend,
ACTION_NAMES.preprocess_assemble,
]

opt_feature = feature(name = "opt")

features = [
feature(
name = "default_compile_actions",
enabled = True,
flag_sets = [
flag_set(
actions = all_compile_actions,
flag_groups = ([
flag_group(
flags = [
"--sysroot=external/llvm_mingw_toolchain",
"-no-canonical-prefixes",
# Reproducibility
"-Wno-builtin-macro-redefined",
"-D__DATE__=\"redacted\"",
"-D__TIMESTAMP__=\"redacted\"",
"-D__TIME__=\"redacted\"",
],
),
]),
),
flag_set(
actions = all_compile_actions,
flag_groups = ([
flag_group(
flags = ["-O2", "-g"],
),
]),
with_features = [with_feature_set(features = ["opt"])],
),
],
),
feature(
name = "default_link_flags",
enabled = True,
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
],
flag_groups = ([
flag_group(
flags = [
"-lstdc++",
"-lm",
"-static",
],
),
]),
),
],
),
opt_feature,
]

return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
features = features,
toolchain_identifier = "llvm-mingw",
host_system_name = "local",
target_system_name = "local",
target_cpu = "x86_64",
target_libc = "unknown",
compiler = "llvm",
abi_version = "unknown",
abi_libc_version = "unknown",
tool_paths = tool_paths,
)

config = rule(
implementation = _impl,
attrs = {
"c_opts": attr.string_list(),
"link_opts": attr.string_list(),
},
provides = [CcToolchainConfigInfo],
)
87 changes: 87 additions & 0 deletions cc/toolchains/llvm_x86_64_windows/toolchain.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "gcc",
srcs = [
"bin/x86_64-w64-mingw32uwp-c++",
"bin/x86_64-w64-mingw32uwp-g++",
"bin/x86_64-w64-mingw32uwp-gcc",
],
)

filegroup(
name = "clang",
srcs = [
"bin/clang-cpp",
"bin/x86_64-w64-mingw32uwp-clang",
"bin/x86_64-w64-mingw32uwp-clang++",
],
)

filegroup(
name = "ld",
srcs = [
"bin/x86_64-w64-mingw32uwp-ld",
],
)

filegroup(
name = "include",
srcs = glob([
"x86_64-w64-mingw32/include/**",
"lib/clang/*/include/**",
"generic-w64-mingw32/include/c++/v1/*",
]),
)

filegroup(
name = "bin",
srcs = glob([
"bin/**",
"x86_64-w64-mingw32/**",
]),
)

filegroup(
name = "lib",
srcs = glob([
"lib/**/lib*.a",
"lib/clang/*/lib/**/*.a",
"x86_64-w64-mingw32/lib/*.a",
]),
)

filegroup(
name = "ar",
srcs = ["bin/x86_64-w64-mingw32uwp-ar"],
)

filegroup(
name = "as",
srcs = ["bin/x86_64-w64-mingw32uwp-as"],
)

filegroup(
name = "nm",
srcs = ["bin/x86_64-w64-mingw32uwp-nm"],
)

filegroup(
name = "objcopy",
srcs = ["bin/x86_64-w64-mingw32uwp-objcopy"],
)

filegroup(
name = "objdump",
srcs = ["bin/x86_64-w64-mingw32uwp-objdump"],
)

filegroup(
name = "ranlib",
srcs = ["bin/x86_64-w64-mingw32uwp-ranlib"],
)

filegroup(
name = "strip",
srcs = ["bin/x86_64-w64-mingw32uwp-strip"],
)
Loading

0 comments on commit c33a467

Please sign in to comment.