{"id":13603121,"url":"https://github.com/shepherdjerred/macos-cross-compiler","last_synced_at":"2025-05-16T11:05:02.499Z","repository":{"id":103525600,"uuid":"605323155","full_name":"shepherdjerred/macos-cross-compiler","owner":"shepherdjerred","description":"Compile binaries for macOS on Linux","archived":false,"fork":false,"pushed_at":"2024-12-07T19:14:16.000Z","size":833,"stargazers_count":375,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-09T11:09:05.251Z","etag":null,"topics":["c","cpp","cross-compiler","docker","earthly","fortran","linux","macos","rust"],"latest_commit_sha":null,"homepage":"","language":"Earthly","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shepherdjerred.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-22T23:11:11.000Z","updated_at":"2025-04-05T19:15:55.000Z","dependencies_parsed_at":"2024-12-24T00:11:00.471Z","dependency_job_id":"7e15d709-bd9e-4160-af66-a60b51800205","html_url":"https://github.com/shepherdjerred/macos-cross-compiler","commit_stats":{"total_commits":113,"total_committers":2,"mean_commits":56.5,"dds":0.03539823008849563,"last_synced_commit":"b36c1aeaac3ff31004185d6079bddb320379cdb9"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shepherdjerred%2Fmacos-cross-compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shepherdjerred%2Fmacos-cross-compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shepherdjerred%2Fmacos-cross-compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shepherdjerred%2Fmacos-cross-compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shepherdjerred","download_url":"https://codeload.github.com/shepherdjerred/macos-cross-compiler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254518384,"owners_count":22084374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cpp","cross-compiler","docker","earthly","fortran","linux","macos","rust"],"created_at":"2024-08-01T18:01:52.206Z","updated_at":"2025-05-16T11:04:58.468Z","avatar_url":"https://github.com/shepherdjerred.png","language":"Earthly","funding_links":[],"categories":["Earthly","c","Fundamentals"],"sub_categories":["Utility"],"readme":"# macOS Cross Compiler\n\n\u003e [!CAUTION]\n\u003e If you're using this for a production use-case you should fork this project and build the image yourself\n\nThis project allows you to cross-compile code on Linux that will be executed on macOS. This can be very useful for CI environments where you want to build for macOS, but you don't want to go through the trouble (and cost) of setting up a macOS environment.\n\nIt supports:\n\n* ✅ C\n* ✅ C++\n* ✅ Fortran\n* ✅ Rust (through Zig)\n\nSupport for Objective C and Objective C++ may work out-of-the-box, but this is not tested.\n\n\u003e [!NOTE]\n\u003e This project is focused on supporting newer versions of macOS and C, C++, Fortran, and Rust. Versions older than macOS 13 (Ventura) are not well tested, though they _should_ work fine.\n\nmacOS system libraries and headers are provided with the Docker image. This should be suitable for compiling standalone macOS programs and possibly native macOS applications.\n\nThe cross-compilers are available as a Docker image. This is easiest way to distribute the project since there are so many host dependencies. If you are interested in using this without Docker, you should take a look at [osxcross](https://github.com/tpoechtrager/osxcross) which forms the base of this project.\n\nThe Docker image is available at [ghcr.io/shepherdjerred/macos-cross-compiler](https://github.com/shepherdjerred/macos-cross-compiler/pkgs/container/macos-cross-compiler).\n\n## Quick Start\n\nInstall the requirements below, then follow the instructions in the usage section.\n\n### Host Requirements\n\n* Docker\n\n### Usage\n\n\u003e [!IMPORTANT]\n\u003e The Docker image is quite large. It includes several compilers and the macOS SDK.\n\n```bash\n# Start a Docker container using the Docker image.\n# Replace `$PWD/samples` with the path to the source you want to compile.\n$ docker run --platform=linux/amd64 \\\n  -v $PWD/samples:/workspace \\\n  --rm \\\n  -it \\\n  ghcr.io/shepherdjerred/macos-cross-compiler:latest \\\n  /bin/bash\n\n# Now that you're inside of the Docker container, you can run the compilers.\n\n# Compile using gcc\n## targeting darwin arm64\n$ aarch64-apple-darwin24-gcc hello.c -o hello\n$ aarch64-apple-darwin24-g++ hello.cpp -o hello\n## targeting darwin x86_64\n$ x86_64-apple-darwin24-gcc hello.c -o hello\n$ x86_64-apple-darwin24-g++ hello.cpp -o hello\n\n# Compile using clang\n## for darwin arm64\n$ aarch64-apple-darwin24-clang --target=aarch64-apple-darwin24 hello.c -o hello\n$ aarch64-apple-darwin24-clang++ --target=aarch64-apple-darwin24 hello.cpp -o hello\n## for darwin x86_64\n$ x86_64-apple-darwin24-clang --target=x86_64-apple-darwin24 hello.c -o hello\n$ x86_64-apple-darwin24-clang++ --target=x86_64-apple-darwin24 hello.cpp -o hello\n\n# Compile using gfortran\n## for darwin arm64\n$ aarch64-apple-darwin24-gfortran hello.f90 -o hello\n## for darwin x86_64\n$ x86_64-apple-darwin24-gfortran hello.f90 -o hello\n\n# Compile using Zig\n## C targeting darwin arm64 (change aarch64 -\u003e x86_64 to target amd64)\n$ zig cc \\\n    -target aarch64-macos \\\n    --sysroot=/sdk \\\n    -I/sdk/usr/include \\\n    -L/sdk/usr/lib \\\n    -F/sdk/System/Library/Frameworks \\\n    -framework CoreFoundation \\\n    -o hello hello.c\n\n## Rust targeting darwin arm64 (change aarch64 -\u003e x86_64 to target amd64)\n$ export CC=zig-cc-aarch64-macos\n$ cd rust \u0026\u0026 cargo build --target aarch64-apple-darwin\n```\n\n### Rust\n\nSupport for Rust requires a bit of project configuration.\n\n```toml\n# .cargo/config.toml\n[build]\n[target.aarch64-apple-darwin]\nlinker = \"zig-cc-aarch64-macos\"\n\n[target.x86_64-apple-darwin]\nlinker = \"zig-cc-x86_64-macos\"\n```\n\nOnce configured, you can run `cargo` after setting the `CC` variable:\n\n```bash\nexport CC=\"zig-cc-x86_64-macos\"\ncargo build --target x86_64-apple-darwin\n\nexport CC=\"zig-cc-aarch64-macos\"\ncargo build --target aarch64-apple-darwin\n```\n\n### C, C++ and Fortran Compiler Executables\n\nThe table below shows the name of the executable for each architecture/compiler pair.\n\n\u003e [!NOTE]\n\u003e The target kernel version is `darwin24`. You'll need to build a new Docker image if you want to support a different kernel version.\n\n|          | aarch64                         |\n|----------|---------------------------------|\n| **clang**    | aarch64-apple-darwin24-clang    |\n| **clang++**  | aarch64-apple-darwin24-clang++  |\n| **gcc**      | aarch64-apple-darwin24-gcc      |\n| **g++**      | aarch64-apple-darwin24-g++      |\n| **gfortran** | aarch64-apple-darwin24-gfortran |\n\nThe relevant compilers are located at `/osxcross/bin` and `/gcc/bin`. Both these directories are already on the `PATH` in the Docker container.\n\n### cctools\n\nThis project compiles [cctools](https://github.com/tpoechtrager/cctools-port), which is Apple's version of [binutils](https://www.gnu.org/software/binutils/). These programs are low-level utilities that are used by compilers, such as the archiver `ar`, the loader `ld`, and the assembler `as`.\n\nYou probably don't need to run these programs directly, but if you do they are located at `/cctools/bin`, and they are also on the `PATH`.\n\nComplete tool list:\n\n* ObjectDump\n* ar\n* as\n* bitcode_strip\n* check_dylib\n* checksyms\n* cmpdylib\n* codesign_allocate\n* ctf_insert\n* dyldinfo\n* install_name_tool\n* ld\n* libtool\n* lipo\n* machocheck\n* makerelocs\n* mtoc\n* mtor\n* nm\n* nmedit\n* otool\n* pagestuff\n* ranlib\n* redo_prebinding\n* seg_addr_table\n* seg_hack\n* segedit\n* size\n* strings\n* strip\n* unwinddump\n* vtool\n\n## Code Signing, Notarizing, and Universal Binaries\n\n[Code signing](https://developer.apple.com/documentation/security/code_signing_services) (but _not_ [notarizing](https://developer.apple.com/documentation/security/notarizing_macos_software_before_distribution/)) should be possible with this project, but it is untested. Building [universal binaries](https://developer.apple.com/documentation/apple-silicon/building-a-universal-macos-binary) should also be possible, but again, this is not tested.\n\nThe [rcodesign](https://gregoryszorc.com/docs/apple-codesign/stable/) has been recommended as a way to sign and notarize binaries for macOS.\n\n## Target Compatibility\n\nThis project can build for macOS on both x86_64 and aarch64 archtictures, regardless of the host architecture.\n\n|              | Linux x86_64 | Linux arm64 |\n|--------------|--------------|-------------|\n| **macOS x86_64** | ✅            | ✅           |\n| **macOS aarch64**  | ✅            | ✅           |\n\n\u003e [!NOTE]\n\u003e aarch64 is Apple's internal name for arm64. They're used interchangably, but aarch64 is more correct when referring to macOS on arm64.\n\nThis project supports the following languages:\n\n* C (up to C 23)\n* C++ (up to C++ 23)\n* Fortran (up to Fortran 2023)\n* Rust (any version)\n\nThis project supports the following versions of macOS:\n\n* ✅ macOS 11 Big Sur\n* ✅ macOS 12 Monterey\n* ✅ macOS 13 Ventura\n* ✅ macOS 14 Sonoma\n* ✅ macOS 15 Seqouia\n\nSupport for macOS 15 Seqouia has not been extensively tested. The Docker image uses the 15.0 SDK by default.\n\n\u003e [!IMPORTANT]\n\u003e This project is tested on modern verisons of macOS, Clang, and GCC. It has not been tested with older versions of these softwares. If you need compatabiltiy with older versions, check out the [osxcross project](https://github.com/tpoechtrager/osxcross).\n\n## Xcode SDK\n\nThis Docker image bundles the Xcode SDK from [joseluisq/macosx-sdks](https://github.com/joseluisq/macosx-sdks/).  Please familiarize yourself with the [SDK's terms of service](https://www.apple.com/legal/sla/docs/xcode.pdf).\n\n## Technical Details\n\nThis repository is essentially a wrapper around the following projects:\n\n* \u003chttps://github.com/tpoechtrager/apple-libtapi\u003e\n* \u003chttps://github.com/tpoechtrager/cctools-port\u003e\n* \u003chttps://github.com/tpoechtrager/xar\u003e\n* \u003chttps://github.com/tpoechtrager/apple-libdispatch\u003e\n* \u003chttps://github.com/iains/gcc-darwin-arm64\u003e\n\nThese resources were helpful when working on this project:\n\n* \u003chttps://www.lurklurk.org/linkers/linkers.html\u003e\n* \u003chttp://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html\u003e\n* \u003chttps://gist.github.com/loderunner/b6846dd82967ac048439\u003e\n* \u003chttp://clarkkromenaker.com/post/library-dynamic-loading-mac/\u003e\n* \u003chttps://github.com/qyang-nj/llios\u003e\n\nThe Zig and Rust portion were informed by these resources:\n\n* \u003chttps://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html\u003e\n* \u003chttps://actually.fyi/posts/zig-makes-rust-cross-compilation-just-work/\u003e\n\n## Development\n\nThe Docker images for this repository are built with [Earthly](https://earthly.dev).\n\n\u003e [!TIP]\n\u003e Building GCC is compute and memory intense. You can adjust the number of parallel tasks by passing a `--cores=\u003cnumber\u003e` argument to any command, e.g. `earthly +image --cores=4`.\n\u003e In my experience you'll need a lot of memory if you want to use all of your cores.\n\u003e I've had success by setting this value to about half as many cores as my CPU. You should lower the value if your machine stalls.\n\n```bash\n# Create a Docker image tagged as `shepherdjerred/macos-cross-compiler`\n# The first run will take ~20 minutes on an M1 MacBook.\n# Subsequent runs are faster.\nearthly +image\n\n# Verify that the compilers work correctly\nearthly +test\n\n# If you're on macOS, try actually running the binaries\nearthly +validate\n```\n\n## Inspiration\n\nThis project would not have been possible without the [osxcross](https://github.com/tpoechtrager/osxcross) project.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshepherdjerred%2Fmacos-cross-compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshepherdjerred%2Fmacos-cross-compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshepherdjerred%2Fmacos-cross-compiler/lists"}