Skip to content

Commit f98de0c

Browse files
authored
enable building aarch64-linux targets from macOS (#890)
Add the ability to build for aarch64-unknown-linux-{musl,gnu} platforms on an arm64 macOS host using Docker. For example using: ./build-linux.py --target-triple aarch64-unknown-linux-musl --python cpython-3.14
1 parent 4e1ffb6 commit f98de0c

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

cpython-unix/build-main.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def main():
7979
"--no-docker",
8080
action="store_true",
8181
default=True if sys.platform == "darwin" else False,
82-
help="Disable building in Docker",
82+
help="Disable building in Docker on Linux hosts.",
8383
)
8484
parser.add_argument(
8585
"--serial",
@@ -129,9 +129,26 @@ def main():
129129

130130
musl = "musl" in target_triple
131131

132+
# Linux targets can be built on a macOS host using Docker.
133+
building_linux_from_macos = sys.platform == "darwin" and "linux" in target_triple
134+
if building_linux_from_macos:
135+
print("Note: Using Docker to build for Linux on macOS")
136+
args.no_docker = False
137+
132138
env = dict(os.environ)
133139

134-
env["PYBUILD_HOST_PLATFORM"] = host_platform
140+
# When building Linux targets from macOS using Docker, map to the equivalent
141+
# Linux host platform.
142+
effective_host_platform = host_platform
143+
if building_linux_from_macos:
144+
if host_platform == "macos_arm64":
145+
effective_host_platform = "linux_aarch64"
146+
else:
147+
raise Exception(f"Unhandled macOS platform: {host_platform}")
148+
print(
149+
f"Building Linux target from macOS using Docker ({effective_host_platform} toolchain)"
150+
)
151+
env["PYBUILD_HOST_PLATFORM"] = effective_host_platform
135152
env["PYBUILD_TARGET_TRIPLE"] = target_triple
136153
env["PYBUILD_BUILD_OPTIONS"] = args.options
137154
env["PYBUILD_PYTHON_SOURCE"] = python_source

cpython-unix/build.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def add_target_env(env, build_platform, target_triple, build_env, build_options)
100100
if build_platform.startswith("linux_"):
101101
machine = platform.machine()
102102

103-
if machine == "aarch64":
103+
# arm64 allows building for Linux on a macOS host using Docker
104+
if machine == "aarch64" or machine == "arm64":
104105
env["BUILD_TRIPLE"] = "aarch64-unknown-linux-gnu"
105106
env["TARGET_TRIPLE"] = target_triple
106107
elif machine == "x86_64":

cpython-unix/targets.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ aarch64-unknown-linux-gnu:
115115
host_platforms:
116116
- linux_x86_64
117117
- linux_aarch64
118+
- macos_arm64
118119
pythons_supported:
119120
- '3.10'
120121
- '3.11'
@@ -919,6 +920,7 @@ aarch64-unknown-linux-musl:
919920
host_platforms:
920921
- linux_x86_64
921922
- linux_aarch64
923+
- macos_arm64
922924
pythons_supported:
923925
- '3.10'
924926
- '3.11'

docs/building.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ As are various other targets::
4646
$ ./build-linux.py --target riscv64-unknown-linux-gnu
4747
$ ./build-linux.py --target s390x-unknown-linux-gnu
4848

49+
Additionally, an arm64 macOS host can be used to build Linux aarch64 targets
50+
using Docker::
51+
52+
$ ./build-linux.py --target aarch64-unknown-linux-gnu
53+
4954
macOS
5055
=====
5156

0 commit comments

Comments
 (0)