Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross compilation for Linux, MacOS, and Windows #18

Merged
merged 13 commits into from
Jun 18, 2024
Merged
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 [email protected]

- 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 [email protected]

- 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/[email protected]
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