警告
如果您將其用於生產用例
該項目允許您在MACOS上執行的Linux上交叉編譯代碼。這對於要為MACOS構建的CI環境可能非常有用,但是您不想經歷設置MacOS環境的麻煩(和成本)。
它支持:
對目標C和目標C ++的支持可能會在開箱即用的外部工作,但這未進行測試。
筆記
該項目的重點是支持MacOS和C,C ++,Fortran和Rust的新版本。比Macos 13(文圖拉)年齡大的版本測試不佳,儘管它們應該正常。
MacOS系統庫和標頭配有Docker圖像。這應該適合編譯獨立的MACOS程序和可能的本機MacOS應用程序。
跨編譯器可作為Docker圖像使用。這是分發項目的最簡單方法,因為有很多主機依賴性。如果您有興趣在沒有Docker的情況下使用它,則應查看構成該項目基礎的OSXCROSS。
Docker圖像可在ghcr.io/shepherdjerred/macos-cross-compiler上找到。
安裝下面的要求,然後按照“使用”部分中的說明進行操作。
重要的
Docker圖像很大。它包括多個編譯器和MacOS SDK。
# Start a Docker container using the Docker image.
# Replace `$PWD/samples` with the path to the source you want to compile.
$ docker run --platform=linux/amd64
-v $PWD /samples:/workspace
--rm
-it
ghcr.io/shepherdjerred/macos-cross-compiler:latest
/bin/bash
# Now that you're inside of the Docker container, you can run the compilers.
# Compile using gcc
# # targeting darwin arm64
$ aarch64-apple-darwin24-gcc hello.c -o hello
$ aarch64-apple-darwin24-g++ hello.cpp -o hello
# # targeting darwin x86_64
$ x86_64-apple-darwin24-gcc hello.c -o hello
$ x86_64-apple-darwin24-g++ hello.cpp -o hello
# Compile using clang
# # for darwin arm64
$ aarch64-apple-darwin24-clang --target=aarch64-apple-darwin24 hello.c -o hello
$ aarch64-apple-darwin24-clang++ --target=aarch64-apple-darwin24 hello.cpp -o hello
# # for darwin x86_64
$ x86_64-apple-darwin24-clang --target=x86_64-apple-darwin24 hello.c -o hello
$ x86_64-apple-darwin24-clang++ --target=x86_64-apple-darwin24 hello.cpp -o hello
# Compile using gfortran
# # for darwin arm64
$ aarch64-apple-darwin24-gfortran hello.f90 -o hello
# # for darwin x86_64
$ x86_64-apple-darwin24-gfortran hello.f90 -o hello
# Compile using Zig
# # C targeting darwin arm64 (change aarch64 -> x86_64 to target amd64)
$ zig cc
-target aarch64-macos
--sysroot=/sdk
-I/sdk/usr/include
-L/sdk/usr/lib
-F/sdk/System/Library/Frameworks
-framework CoreFoundation
-o hello hello.c
# # Rust targeting darwin arm64 (change aarch64 -> x86_64 to target amd64)
$ export CC=zig-cc-aarch64-macos
$ cd rust && cargo build --target aarch64-apple-darwin對RUST的支持需要一些項目配置。
# .cargo/config.toml
[ build ]
[ target . aarch64-apple-darwin ]
linker = " zig-cc-aarch64-macos "
[ target . x86_64-apple-darwin ]
linker = " zig-cc-x86_64-macos "配置後,您可以在設置CC變量之後運行cargo :
export CC= " zig-cc-x86_64-macos "
cargo build --target x86_64-apple-darwin
export CC= " zig-cc-aarch64-macos "
cargo build --target aarch64-apple-darwin下表顯示了每個體系結構/編譯器對可執行文件的名稱。
筆記
目標內核版本是darwin24 。如果要支持其他內核版本,則需要構建新的Docker映像。
| aarch64 | |
|---|---|
| 鐺 | AARCH64-APPLE-DARWIN24-CLANG |
| clang ++ | Aarch64-Apple-darwin24-clang ++ |
| 海灣合作委員會 | AARCH64-APPLE-DARWIN24-GCC |
| G ++ | AARCH64-APPLE-DARWIN24-G ++ |
| Gfortran | AARCH64-APPLE-DARWIN24-GFORTRAN |
相關編譯器位於/osxcross/bin和/gcc/bin 。這兩個目錄已經在Docker容器中的PATH上。
該項目編譯了Cctools,這是Apple版本的Binutils。這些程序是編譯器,例如Archiver ar ,Loader ld和Assemberer as編譯器使用的低級實用程序。
您可能不需要直接運行這些程序,但是如果這樣做,它們位於/cctools/bin ,它們也在PATH上。
完整的工具列表:
該項目應該可以使用代碼簽名(但不是公證),但未經測試。建造通用二進製文件也應該是可能的,但是再次進行了測試。
建議將rcodesign作為MacOS簽名和公證二進制的一種方式。
該項目可以在X86_64和AARCH64構圖上為MacOS構建,而不論主機架構如何。
| Linux X86_64 | Linux ARM64 | |
|---|---|---|
| MacOS X86_64 | ✅ | ✅ |
| MacOS Aarch64 | ✅ | ✅ |
筆記
Aarch64是Apple的ARM64的內部名稱。它們可以互換使用,但是在指代ARM64上的MacOS時,Aarch64更正確。
該項目支持以下語言:
該項目支持MacOS的以下版本:
對MacOS 15 Seqouia的支持尚未進行廣泛測試。 Docker映像默認使用15.0 SDK。
重要的
該項目對MacOS,Clang和GCC的現代Verison進行了測試。尚未對這些軟件的較舊版本進行測試。如果您需要與較舊版本的兼容,請查看OSXCROSS項目。
此Docker圖像將Joseluisq/MacOSX-SDK的Xcode SDK捆綁在一起。請熟悉SDK的服務條款。
該存儲庫本質上是圍繞以下項目的包裝器:
這些資源在從事該項目時很有幫助:
這些資源告知了曲折的部分:
該存儲庫的Docker圖像是用塵世的構建的。
提示
建造海灣合作委員會是計算和內存的強度。您可以通過將一個--cores=<number>參數傳遞給任何命令,例如earthly +image --cores=4來調整併行任務的數量。根據我的經驗,如果您想使用所有內核,則需要大量的內存。我將這個價值設置為大約一半的核心,我取得了成功。如果您的機器失速,則應降低該值。
# Create a Docker image tagged as `shepherdjerred/macos-cross-compiler`
# The first run will take ~20 minutes on an M1 MacBook.
# Subsequent runs are faster.
earthly +image
# Verify that the compilers work correctly
earthly +test
# If you're on macOS, try actually running the binaries
earthly +validate沒有OSXCROSS項目,該項目將是不可能的。