DEV Community

Michael
Michael

Posted on

Rust & Cross-compiling from Linux to Mac on GitHub Actions

Introduction

So you have a binary that you wish to distribute but you are only able to compile for Windows and Linux at the moment. You have spent hours if not days googling for the appropriate way to cross-compile from Linux to Mac... Well, I am here to tell you I have been there and now provide an easy solution!

The Sauce

Basic Usage

# Setup Rust, use the x86_64-apple-darwin target but the rest can be customized.
- uses: ATiltedTree/setup-rust@v1
  with:
    targets: x86_64-apple-darwin
    rust-version: nightly # Works both with 'stable' and 'nightly'

# Use the v1 of this action
- uses: mbround18/setup-osxcross@v1
  # This builds executables & sets env variables for rust to consume.
  with:
    osx-version: "12.3"

# Checkout your code
- name: Clone your Code
  uses: actions/checkout@v3

# Build your code for apple-darwin based release
- name: Build Your Code
  run: cargo build --release --target x86_64-apple-darwin
Enter fullscreen mode Exit fullscreen mode

Explanation of its Functionality

  1. It creates a folder called osxcross in the github actions working directory. (to avoid interference with any files you might be operating with)
  2. Clones the osxcross repository
  3. It downloads the SDK matching the version you specified from @joseluisq/macosx-sdks
  4. Checks cache
  5. Builds if cache wasn't hit
  6. Sets environment variables for build target x86_64-apple-darwin

Shoutouts & Thanks yous

Top comments (0)