Skip to content

Commit

Permalink
[MACOS] add apple silicon support
Browse files Browse the repository at this point in the history
  • Loading branch information
hexdae committed Mar 27, 2024
1 parent 4a65ac7 commit 780065b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 53 deletions.
5 changes: 4 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""ARM cross compilation toolchains for bazel"""

module(
name = "arm_gnu_toolchain",
version = "1.0.0",
version = "0.0.1",
compatibility_level = 1,
)

Expand All @@ -17,6 +19,7 @@ use_repo(
arm_toolchain,
"arm_none_eabi",
"arm_none_eabi_darwin_x86_64",
"arm_none_eabi_darwin_arm64",
"arm_none_eabi_linux_aarch64",
"arm_none_eabi_linux_x86_64",
"arm_none_eabi_windows_x86_64",
Expand Down
25 changes: 19 additions & 6 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ GCC_ARM_NONE_EABI = {
"strip_prefix": "gcc-arm-none-eabi-9-2019-q4-major",
"url": "https://developer.arm.com/-/media/Files/downloads/gnu-rm/9-2019q4/gcc-arm-none-eabi-9-2019-q4-major-mac.tar.bz2?revision=c2c4fe0e-c0b6-4162-97e6-7707e12f2b6e&la=en&hash=EC9D4B5F5B050267B924F876B306D72CDF3BDDC0",
},
{
"name": "arm_none_eabi_darwin_arm64",
"sha256": "1249f860d4155d9c3ba8f30c19e7a88c5047923cea17e0d08e633f12408f01f0",
"strip_prefix": "gcc-arm-none-eabi-9-2019-q4-major",
"url": "https://developer.arm.com/-/media/Files/downloads/gnu-rm/9-2019q4/gcc-arm-none-eabi-9-2019-q4-major-mac.tar.bz2?revision=c2c4fe0e-c0b6-4162-97e6-7707e12f2b6e&la=en&hash=EC9D4B5F5B050267B924F876B306D72CDF3BDDC0",
},
{
"name": "arm_none_eabi_linux_x86_64",
"sha256": "bcd840f839d5bf49279638e9f67890b2ef3a7c9c7a9b25271e83ec4ff41d177a",
Expand All @@ -40,6 +46,12 @@ GCC_ARM_NONE_EABI = {
"strip_prefix": "arm-gnu-toolchain-13.2.Rel1-darwin-x86_64-arm-none-eabi",
"url": "https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-darwin-x86_64-arm-none-eabi.tar.xz?rev=a3d8c87bb0af4c40b7d7e0e291f6541b&hash=10927356ACA904E1A0122794E036E8DDE7D8435D",
},
{
"name": "arm_none_eabi_darwin_arm64",
"sha256": "39c44f8af42695b7b871df42e346c09fee670ea8dfc11f17083e296ea2b0d279",
"strip_prefix": "arm-gnu-toolchain-13.2.Rel1-darwin-arm64-arm-none-eabi",
"url": "https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-darwin-arm64-arm-none-eabi.tar.xz?rev=73e10891de3d41e29e95ac2878742b74&hash=6036196A3358CB5AD85FC01DFD0FEC02A",
},
{
"name": "arm_none_eabi_linux_x86_64",
"sha256": "6cd1bbc1d9ae57312bcd169ae283153a9572bd6a8e4eeae2fedfbc33b115fdbb",
Expand Down
33 changes: 22 additions & 11 deletions toolchain/config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@rules_cc//cc:cc_toolchain_config_lib.bzl", "action_config", "feature", "flag_group", "flag_set")

def _tool_path(bins, toolchain_prefix, tool_name):
"""Generate tool paths for GCC"""
for file in bins:
if file.basename.startswith("{}-{}".format(toolchain_prefix, tool_name)):
return file

return None

def _action_configs(ctx, action_names, tool_name, implies = []):
"""Generate action configs"""
return [
action_config(
action_name = action_name,
Expand All @@ -23,20 +24,30 @@ def _action_configs(ctx, action_names, tool_name, implies = []):
for action_name in action_names
]

def _impl(ctx):
action_configs = []

# Action -> binary mappings from Pigweed:
# https://github.com/google/pigweed/blob/aac7fab/pw_toolchain_bazel/cc_toolchain/private/cc_toolchain.bzl#L19

default_compiler_flags = [
def _default_compiler_flags(ctx):
"""Default compiler flags for GCC bazel toolchains"""
compiler_flags = [
"-fno-canonical-system-headers",
"-no-canonical-prefixes",
]

if not ctx.attr.include_std:
default_compiler_flags.append("-nostdinc")
compiler_flags.append("-nostdinc")
if ctx.attr.gcc_tool == "g++":
default_compiler_flags.append("-nostdinc++")
compiler_flags.append("-nostdinc++")

return compiler_flags

def _default_linker_flags(_ctx):
"""Default linker flags for GCC bazel toolchains"""
return [
"-no-canonical-prefixes",
]

def _impl(ctx):
default_compiler_flags = _default_compiler_flags(ctx)
default_linker_flags = _default_linker_flags(ctx)
action_configs = []

action_configs += _action_configs(
ctx,
Expand Down Expand Up @@ -118,7 +129,7 @@ def _impl(ctx):
flag_set(
actions = [ACTION_NAMES.cpp_link_executable],
flag_groups = [
flag_group(flags = ctx.attr.linkopts + ["-no-canonical-prefixes"]),
flag_group(flags = ctx.attr.linkopts + default_linker_flags),
],
),
],
Expand Down
84 changes: 49 additions & 35 deletions toolchain/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ target_constraints = {
"armv7e-mf": ["@platforms//os:none", "@platforms//cpu:armv7e-mf"],
"armv8-m": ["@platforms//os:none", "@platforms//cpu:armv8-m"],
},

"arm-none-linux-gnueabihf": {
"arm": ["@platforms//os:linux", "@platforms//cpu:arm"],
"armv7": ["@platforms//os:linux", "@platforms//cpu:armv7"],
Expand All @@ -40,12 +39,12 @@ target_constraints = {

hosts = {
"arm-none-eabi": {
"darwin_x86_64": ["@platforms//os:macos"], # Also runs on apple silicon
"darwin_x86_64": ["@platforms//os:macos", "@platforms//cpu:x86_64"],
"darwin_arm64": ["@platforms//os:macos", "@platforms//cpu:arm64"],
"linux_x86_64": ["@platforms//os:linux", "@platforms//cpu:x86_64"],
"linux_aarch64": ["@platforms//os:linux", "@platforms//cpu:arm64"],
"windows_x86_64": ["@platforms//os:windows", "@platforms//cpu:x86_64"],
},

"arm-none-linux-gnueabihf": {
# ARM has not provided an arm linux toolchain for darwin.
"linux_x86_64": ["@platforms//os:linux", "@platforms//cpu:x86_64"],
Expand All @@ -54,11 +53,24 @@ hosts = {
},
}

def _arm_gnu_toolchain(name, toolchain = "", toolchain_prefix = "",
gcc_tool = "gcc", abi_version = "",
target_compatible_with = [], copts = [],
linkopts = [], version = "", include_std = False):
def _arm_gnu_toolchain(
name,
toolchain = "",
toolchain_prefix = "",
gcc_tool = "gcc",
abi_version = "",
target_compatible_with = [],
copts = [],
linkopts = [],
version = "",
include_std = False):
for host, exec_compatible_with in hosts[toolchain_prefix].items():
fix_linkopts = []

# macOS on apple silicon rejects the relative path LTO plugin
if version == "13.2.1" and host == "darwin_arm64":
fix_linkopts.append("-fno-lto")

cc_arm_gnu_toolchain_config(
name = "config_{}_{}".format(host, name),
gcc_repo = "{}_{}".format(toolchain, host),
Expand All @@ -69,14 +81,10 @@ def _arm_gnu_toolchain(name, toolchain = "", toolchain_prefix = "",
toolchain_prefix = toolchain_prefix,
toolchain_identifier = "{}_{}_{}".format(toolchain, host, name),
toolchain_bins = "@{}_{}//:compiler_components".format(toolchain, host),
include_path = [
"@{}_{}//:include_path".format(toolchain, host),
],
library_path = [
"@{}_{}//:library_path".format(toolchain, host),
],
include_path = ["@{}_{}//:include_path".format(toolchain, host)],
library_path = ["@{}_{}//:library_path".format(toolchain, host)],
copts = copts,
linkopts = linkopts,
linkopts = linkopts + fix_linkopts,
include_std = include_std,
)

Expand All @@ -102,42 +110,48 @@ def _arm_gnu_toolchain(name, toolchain = "", toolchain_prefix = "",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

def arm_none_eabi_toolchain(name, version = "13.2.1", copts = [], **kwargs):
def arm_none_eabi_toolchain(name, version = "13.2.1", **kwargs):
"""
Create an arm-none-eabi toolchain with the given configuration.
Args:
name: The name of the toolchain.
gcc_tool: The gcc tool to use. Defaults to "gcc". [gcc, c++, cpp]
target_compatible_with: A list of constraint values to apply to the toolchain.
copts: A list of compiler options to apply to the toolchain.
linkopts: A list of linker options to apply to the toolchain.
version: The version of the gcc toolchain.
include_std: Whether to include the standard library in the include path.
**kwargs: same as arm_gnu_toolchain
"""
_arm_gnu_toolchain(name, toolchain = "arm_none_eabi",
toolchain_prefix = "arm-none-eabi", version = version,
abi_version = "eabi", copts = ["-nostdinc"] + copts,
**kwargs)
_arm_gnu_toolchain(
name,
toolchain = "arm_none_eabi",
toolchain_prefix = "arm-none-eabi",
version = version,
abi_version = "eabi",
**kwargs
)

def arm_none_linux_gnueabihf_toolchain(name, version = "13.2.1", linkopts = [],
**kwargs):
def arm_none_linux_gnueabihf_toolchain(
name,
version = "13.2.1",
linkopts = [],
**kwargs):
"""
Create an arm-none-linux-gnueabihf toolchain with the given configuration.
Args:
name: The name of the toolchain.
gcc_tool: The gcc tool to use. Defaults to "gcc". [gcc, c++, cpp]
target_compatible_with: A list of constraint values to apply to the toolchain.
copts: A list of compiler options to apply to the toolchain.
linkopts: A list of linker options to apply to the toolchain.
version: The version of the gcc toolchain.
linkopts: Additional linker options.
**kwargs: Additional keyword arguments.
"""
_arm_gnu_toolchain(name, toolchain = "arm_none_linux_gnueabihf",
toolchain_prefix = "arm-none-linux-gnueabihf",
version = version, abi_version = "gnueabihf",
linkopts = ["-lc", "-lstdc++"] + linkopts,
include_std = True, **kwargs)
_arm_gnu_toolchain(
name,
toolchain = "arm_none_linux_gnueabihf",
toolchain_prefix = "arm-none-linux-gnueabihf",
version = version,
abi_version = "gnueabihf",
linkopts = ["-lc", "-lstdc++"] + linkopts,
include_std = True,
**kwargs
)

def register_arm_gnu_toolchain(name):
for host in hosts:
Expand Down

0 comments on commit 780065b

Please sign in to comment.