Skip to content

Commit

Permalink
Cross compilation for Linux, MacOS, and Windows (#18)
Browse files Browse the repository at this point in the history
* Added Windows specific modifications

of the DMD compiler imvocation to enable compilation on Windows.

* Added executableName() for Windows' ".exe" extension

as suggested by @linkrope and using it build().

* Get rid of OS specific file path separators

as suggested by @linkrope in #17 (comment).

* Enabled matrix build for Linux, MaxOS, and Windows

and the according tests.

* Renamed build.yml to ci.yml

as discussed on PR here: https://github.com/linkrope/gamma/pull/17/files/11c93a9229e028f778573de87b4f478dda96f508#r1437757527

* Refactored out reusable workflows

from ci.yml, to refernce them later there and in release.yml

* Added uploading of built executables

* Avoid CI workflow is running on release publications

and within twice, once for the tag pushed for release and second for the release event.
This was found in https://stackoverflow.com/questions/70743715/how-do-i-configure-a-github-actions-workflow-so-it-does-not-run-on-a-tag-push

* Improved matrix names in GH Actions GUI

* Refactored out upload workflow

into upload.yml to have the same abstractions levels for jobs in release.yml

* Attempt to optimize the built artifacts using a specific build type.

* Switched to single workflow in build.yml

for CI and release builds.

* Get rid of DMD compiler at all for build.

gamma now supports to use any $DC defined D compiler for target compiler compiling.
  • Loading branch information
kuniss committed Jun 18, 2024
1 parent 7be92f3 commit 45e52a9
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 54 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Build & Test Gamma

on:
workflow_call:
inputs:
compiler_version:
type: string
required: true
build_type:
type: string
required: false
default: "debug"

jobs:
build:
name: build & test Gamma
strategy:
matrix:
os: [Windows, Linux, MacOS]
include:
- os: Windows
runner: windows-latest
upload-path: ./gamma.exe
upload-name: gamma.windows-amd64.exe
- os: Linux
runner: ubuntu-latest
upload-path: ./gamma
upload-name: gamma.linux-amd64
- os: MacOS
runner: macOS-latest
upload-path: ./gamma
upload-name: gamma.macos-amd64

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

steps:
- name: Checkout source code
uses: actions/checkout@v4

- name: Install LDC compiler for building gamma
uses: dlang-community/setup-dlang@v1
with:
compiler: ${{ inputs.compiler_version }}

- name: Build & run tests
run: |
dub build --build=${{inputs.build_type}}
dub test --build=unittest --config=example
- name: Upload executables to workflow run page
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.upload-name }}
path: ${{ matrix.upload-path }}
retention-days: 1
28 changes: 28 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Check Gamma Project

on:
workflow_call:
inputs:
compiler_version:
type: string
required: true

jobs:
check:
name: Check Style
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v4

- name: Install D compiler
uses: dlang-community/setup-dlang@v1
with:
compiler: ${{ inputs.compiler_version }}

- run: |
dub fetch dscanner@0.14.0
- name: Check style
run: |
dub run dscanner -- --styleCheck src test
40 changes: 11 additions & 29 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
name: CI

on:
push:
branches:
- "*"
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: dlang-community/setup-dlang@v1
with:
compiler: dmd-2.103.1

- name: Build
run: |
dub build --compiler=$DC

- name: Test
run: |
dub test --compiler=$DC --config=example
build:
name: build & test
uses: ./.github/workflows/build.yml
with:
compiler_version: ldc-1.37.0

check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: dlang-community/setup-dlang@v1
with:
compiler: dmd-latest

- run: |
dub fetch dscanner@0.14.0
- name: Check Style
run: |
dub run dscanner -- --styleCheck src test
uses: ./.github/workflows/check.yml
with:
compiler_version: dmd-2.107.1
19 changes: 19 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Release

on:
release:
types: [published]

jobs:

build:
name: build & test
uses: ./.github/workflows/build.yml
with:
compiler_version: ldc-1.33.0
build_type: release-gamma

upload:
uses: ./.github/workflows/upload.yml
secrets: inherit
needs: build
37 changes: 37 additions & 0 deletions .github/workflows/upload.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Upload release binaries

on:
workflow_call:

jobs:
upload:
name: Upload release binary
strategy:
matrix:
os: [Windows, Linux, MacOS]
include:
- os: Windows
exe-name: gamma.windows-amd64.exe
rename: mv ./gamma.exe
- os: Linux
exe-name: gamma.linux-amd64
rename: mv ./gamma
- os: MacOS
exe-name: gamma.macos-amd64
rename: mv ./gamma

runs-on: ubuntu-latest
steps:
- name: Download binary from previous build job
uses: actions/download-artifact@v4
with:
name: ${{ matrix.exe-name }}

- name: Give binary a unique name
run: ${{ matrix.rename }} ${{ matrix.exe-name }}

- name: Upload release binary
uses: AButler/upload-release-assets@v3.0
with:
files: ${{ matrix.exe-name }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"importPaths": ["include", "src"],
"mainSourceFile": "src/gamma/main.d",
"stringImportPaths": ["fix/epsilon"],
"buildTypes": {
"release-gamma": {
"buildOptions": ["debugMode", "optimize", "inline", "debugInfo"]
}
},
"configurations": [
{
"name": "gamma",
Expand Down
11 changes: 10 additions & 1 deletion src/gamma/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ void build(string[] fileNames, string outputDirectory)
if (!outputDirectory.empty)
{
args ~= format!"-od=%s"(outputDirectory);
args ~= format!"-of=%s"(fileNames.front.stripExtension);
args ~= format!"-of=%s"(fileNames.front.stripExtension.executableName);
}

info!"%s"(args.join(' '));

auto pid = spawnProcess(args);
Expand All @@ -251,3 +252,11 @@ void build(string[] fileNames, string outputDirectory)
if (status)
exit(status);
}

private string executableName(const string name)
{
version(Windows)
return name ~ ".exe";
else
return name;
}
21 changes: 20 additions & 1 deletion test/helper.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ Result run(string fmt, A...)(lazy A args)
import std.process : executeShell;
import std.stdio : writeln;

const command = format!fmt(args);
auto command = format!fmt(args);

version(Windows)
{
import std.string : translate;
dchar[dchar] translation = ['/': '\\'];
command = translate(command, translation);

import std.regex : regex, replaceAll, replaceFirst;
command = replaceFirst(command, regex("echo\\s+\\|"), "echo. |");
command = replaceAll(command, regex("\\bcat\\b"), "type");
}

writeln(command);
return executeShell(command);
Expand Down Expand Up @@ -57,3 +68,11 @@ Result shouldFailWith(Result result, string pattern)
}
return result;
}

string asSingleLineDosInput(string multiLineInput)
{
import std.string : translate;

string[dchar] translation = ['\n' : " ", '|' : "^|", '<' : "^<", '>' : "^>"];
return translate(multiLineInput, translation);
}
43 changes: 35 additions & 8 deletions test/issues.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ unittest
A: 'a' A.
`.outdent;

run!"cat <<EOF | ./gamma --output-directory %s%sEOF"(directory, eag)
.shouldFailWith("error: start symbol S is unproductive");
version(Windows)
{
run!"mkdir %s & echo %s > %s\\input.eag && type %s\\input.eag | ./gamma --output-directory %s"
(directory, eag.asSingleLineDosInput, directory, directory, directory)
.shouldFailWith("error: start symbol S is unproductive");
}
else
{
run!"cat <<EOF | ./gamma --output-directory %s%sEOF"(directory, eag)
.shouldFailWith("error: start symbol S is unproductive");
}
}
}

Expand All @@ -31,9 +40,18 @@ unittest
S <+ : S>: A | 'b'.
A: 'a' A.
`.outdent;

run!"cat <<EOF | ./gamma --output-directory %s%sEOF"(directory, eag)
.shouldPassWith("warn: A is unproductive");

version(Windows)
{
run!"mkdir %s & echo %s > %s\\input.eag && type %s\\input.eag | ./gamma --output-directory %s"
(directory, eag.asSingleLineDosInput, directory, directory, directory)
.shouldPassWith("warn: A is unproductive");
}
else
{
run!"cat <<EOF | ./gamma --output-directory %s%sEOF"(directory, eag)
.shouldPassWith("warn: A is unproductive");
}
}
}

Expand All @@ -50,9 +68,18 @@ unittest
S<+ '1' N: N>:
'a' S<N> 'b'.
`.outdent;

run!"cat <<EOF | ./gamma --output-directory %s%sEOF"(directory, eag)
.shouldPassWith("S grammar is SLAG");

version(Windows)
{
run!"mkdir %s & echo %s > %s\\input.eag && type %s\\input.eag | ./gamma --output-directory %s"
(directory, eag.asSingleLineDosInput, directory, directory, directory)
.shouldPassWith("S grammar is SLAG");
}
else
{
run!"cat <<EOF | ./gamma --output-directory %s%sEOF"(directory, eag)
.shouldPassWith("S grammar is SLAG");
}
run!"cd %s && echo aa bbb | ./S"(directory)
.shouldFailWith("syntax error, end expected");
}
Expand Down
Loading

0 comments on commit 45e52a9

Please sign in to comment.