Compiling Dart to Native Executables for Windows, Linux and macOS using GitHub Actions

Dart v2.6 includes dart2native - a new tool for compiling Dart scripts to native executables for Windows, Linux and macOS. Currently there’s no cross-compilation support (see dart-lang/sdk#28617) so each binary needs to be compiled from the correct OS.

Cloud services like GitHub Actions make it very easy to run code on different OSes so I created a sample repo that compiles a simple Dart script to native executables for each platform and upload them as artifacts for easy download:

You can try this out with your own scripts by forking the repo and modifying the script or copying the relevant parts out of the GitHub workflow.

The GitHub Workflow

The workflow is relatively straight forward. It sets up a matrix to run on all platforms and sets an output-name variable for the executable.

    runs-on: ${{ matrix.os }}

        os: [ubuntu-latest, windows-latest, macOS-latest]
          - os: ubuntu-latest
            output-name: hello-linux
          - os: macOS-latest
            output-name: hello-mac
          - os: windows-latest
            output-name: hello-windows.exe

Dart is downloaded using a simple GitHub action I created:

- uses: DanTup/gh-actions/setup-dart@master
  # Defaults to stable channel, but can be overriden. 
  # with:
  #   channel: dev

Next, dart2native is invoked with the platform-specific output name:

- run: dart2native bin/main.dart -v -o build/${{ matrix.output-name }}

And finally, the executable is uploaded as a GitHub artifact:

- uses: actions/upload-artifact@v1
    name: native-executables
    path: build

After each commit, GitHub will run this action on each platform:

GitHub Actions run

The executables will be attached to the Action results in the artifacts list:

GitHub Actions artifacts

Compiled executables in ZIP file