EDDYMENS

Published a year ago

How To Compile Rust Code For Multiple Platforms

Table of contents

Introduction

In this tutorial, we will look at how to compile your Rust source code to binaries that run on the Windows, Linux, and macOS operating systems.

Note: This setup requires having a GitHub Account [↗] and assumes you already manage your project with GIT [↗].

We will be using Githubs Actions [↗] to build our source code into binaries for the Windows x86_64, Linux x86_64, and macOS architecture.

Setting up Github Action

  • First, you need to have your code in a GitHub repository [↗]
  • Next create a hidden GitHub directory i.e.: .github within the project source folder. Note: The . in front of github is what makes it hidden
  • Within the .github directory create a folder named workflows.
  • Now in the newly created workflows folder, add a YAML file with the name release.yml

Your directory structure should look similar to this:

01: . 02: ├── .github 03: │ └── workflows 04: │ └── release.yml 05: └── src

Copy and paste the following script into the release.yml YAML [↗] file.

Build Script:

01: # .github/workflows/release.yml 02: on: 03: release: 04: types: [created] 05: 06: jobs: 07: release: 08: name: release ${{ matrix.target }} 09: runs-on: ubuntu-latest 10: strategy: 11: fail-fast: false 12: matrix: 13: target: [x86_64-pc-windows-gnu, x86_64-unknown-linux-musl, x86_64-apple-darwin] 14: steps: 15: - uses: actions/checkout@master 16: - name: Compile and release 17: uses: rust-build/rust-build.action@v1.4.3 18: env: 19: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 20: with: 21: RUSTTARGET: ${{ matrix.target }}

Go ahead, commit and push the updated source code to GitHub.

Permissions

Pushing the source code over to GitHub will not trigger our build script, this is because it is set to run only when you create a release [↗].

When a release is created our script will then run, generating the binaries and adding them to the final release assets. From there you can download them.

We need to grant our script read and write access so it can save the binaries on release.

Follow the steps below to update the permissions for the script:

GitHub Repository permissions [→]

First, head over to the Settings section of your repository (As shown above).

GitHub Repository permissions [→]

From the left hand-nav, collapse the Actions menu and select the General option.

Make sure the permissions match the selection shown below:

Workflow permissions

GitHub Workflow permissions [→]

Actions permissions

GitHub Action permissions [→]

Build process

Now we can go ahead and create our release. Head back to the home page of your repository and click on the "Release" hyper-link as shown below:

GitHub release [→]

Click on the "Draft a new release" button above.

Drafting a new GitHub Release| GitHub [→]

Next, follow the steps shown in the image below to publish a release

  1. Choose or create a tag.
  2. If you don't have an existing tag, type out a name and click on the "Create new tag:.." button to create one.
  3. Describe your release.
  4. Click the "Publish release" button to publish your release.

GitHub Release process [→]

This will trigger a build process. This will take some time, once done your release page should now look like the image shown below with your zipped binaries.

Zipped Binaries [→]

Conclusion

This tutorial is based on a pre-packaged Action script [↗] from the GitHub marketplace.

Here is another article you might like 😊 GIT: List Files With Merge Conflicts