Skip to content

Commit

Permalink
Prepare static build for MacOs
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Feb 20, 2024
1 parent ad5264c commit 87a33ba
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 35 deletions.
38 changes: 29 additions & 9 deletions .github/actions/cache/action.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: 'Compute cache keys'
description: 'Export cache keys for the build process to env vars'

inputs:
os:
description: 'The OS to compute the cache key for'
required: true
default: 'linux'

runs:
using: "composite"
steps:
Expand All @@ -9,27 +15,41 @@ runs:
run: |
set -e
# Should be the same command as the one in .github/workflows/ci.yml
cache_dirname_main=$(tests/bin/compile-get-cache-key phar-location-is-not-used-in-cache-key --php-extensions=mbstring,phar,posix,tokenizer,pcntl)
cache_key_main=$(basename $cache_dirname_main)
echo cache_dirname_main=$cache_dirname_main >> $GITHUB_ENV
echo cache_key_main=$cache_key_main >> $GITHUB_ENV
# Should be the same command as the one in tools/static/castor.php
cache_dirname_linux_amd64=$(tests/bin/compile-get-cache-key phar-location-is-not-used-in-cache-key --os=linux --arch=x86_64 --php-extensions=mbstring,phar,posix,tokenizer,pcntl)
cache_key_linux_amd64=$(basename $cache_dirname_linux_amd64)
echo cache_dirname_linux_amd64=$cache_dirname_linux_amd64 >> $GITHUB_ENV
echo cache_key_linux_amd64=$cache_key_linux_amd64 >> $GITHUB_ENV
# Should be the same command as the one in tools/static/castor.php
cache_dirname_darwin_amd64=$(tests/bin/compile-get-cache-key phar-location-is-not-used-in-cache-key --os=macos --arch=x86_64 --php-extensions=mbstring,phar,posix,tokenizer,pcntl)
cache_key_darwin_amd64=$(basename $cache_dirname_darwin_amd64)
echo cache_dirname_darwin_amd64=$cache_dirname_darwin_amd64 >> $GITHUB_ENV
echo cache_key_darwin_amd64=$cache_key_darwin_amd64 >> $GITHUB_ENV
# And should be the same command as the one in CompileCommandTest
cache_dirname_test=$(tests/bin/compile-get-cache-key phar-location-is-not-used-in-cache-key --os linux --php-extensions mbstring,phar,posix,tokenizer)
cache_key_test=$(basename $cache_dirname_test)
echo cache_dirname_test=$cache_dirname_test >> $GITHUB_ENV
echo cache_key_test=$cache_key_test >> $GITHUB_ENV
- name: Restore PHP static building artifacts cache for main job
- name: Restore PHP static building artifacts cache for linux-amd64
uses: actions/cache@v4
with:
path: ${{ env.cache_dirname_linux_amd64 }}
key: php-static-${{ env.cache_key_linux_amd64 }}
if: ${{ inputs.os == 'linux' }}

- name: Restore PHP static building artifacts cache for darwin-amd64
uses: actions/cache@v4
with:
path: ${{ env.cache_dirname_main }}
key: php-static-${{ env.cache_key_main }}
if: matrix.castor.method == 'static'
path: ${{ env.cache_dirname_darwin_amd64 }}
key: php-static-${{ env.cache_key_darwin_amd64 }}
if: ${{ inputs.os == 'darwin' }}

- name: Restore PHP static building artifacts cache for test job
uses: actions/cache@v4
with:
path: ${{ env.cache_dirname_test }}
key: php-static-${{ env.cache_key_test }}
if: ${{ inputs.os == 'linux' }}
32 changes: 30 additions & 2 deletions .github/actions/static/action.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
name: 'Build static binaries'
description: 'Builds static binaries for all systems'

inputs:
os:
description: 'The operating system to build for'
required: true
default: 'linux'

runs:
using: "composite"
steps:
- name: Build Castor static binary for Linux
run: bin/castor castor:static:linux
shell: bash
if: ${{ inputs.os == 'linux' }}

- name: Ensure Linux binary is OK
run: ./castor.linux-amd64 --version
shell: bash
if: ${{ inputs.os == 'linux' }}

- name: Build Castor static binary for MacOS - amd64
run: bin/castor castor:static:darwin-amd64
shell: bash
if: ${{ inputs.os == 'darwin-amd64' }}

- name: Ensure MacOs - amd64 binary is OK
run: ./castor.darwin-amd64 --version
shell: bash
if: ${{ inputs.os == 'darwin-amd64' }}

- name: Build Castor static binary for MacOS - arm64
run: bin/castor castor:static:darwin-arm64
shell: bash
if: ${{ inputs.os == 'darwin-arm64' }}

- name: Ensure binary is OK
run: ./castor.linux.x86_64 --version
- name: Ensure MacOs - arm64 binary is OK
run: ./castor.darwin-arm64 --version
shell: bash
if: ${{ inputs.os == 'darwin-arm64' }}
61 changes: 47 additions & 14 deletions .github/workflows/artefacts.yml → .github/workflows/artifacts.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
name: Build phars and static binaries
name: Artifacts

on:
push:
branches: [ "main" ]
# REVERT ME !y
pull_request: ~
release:
types: [ "created" ]

permissions:
contents: write

jobs:
phar:
name: Create phar and upload them as an artifact
phars:
name: Create phars and upload
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -52,9 +54,9 @@ jobs:
path: tools/phar/build/castor.windows-amd64.phar
if-no-files-found: error

static:
needs: phar
name: Create static binaries and upload them as an artifact
static-linux:
needs: phars
name: Create Linux static binary and upload
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -63,6 +65,8 @@ jobs:
- uses: ./.github/actions/install

- uses: ./.github/actions/cache
with:
os: 'linux'

- name: retrieve phar artifacts
uses: actions/download-artifact@v4
Expand All @@ -71,18 +75,51 @@ jobs:
merge-multiple: true

- uses: ./.github/actions/static
with:
os: 'linux'

- name: Upload the Linux static binary
uses: actions/upload-artifact@v4
with:
name: 'castor.linux-amd64'
path: ./castor.linux.x86_64
path: ./castor.linux-amd64
if-no-files-found: error

static-darwin-amd64:
needs: phars
name: Create MacOs amd64 static binary and upload
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: ./.github/actions/install

- uses: ./.github/actions/cache
with:
os: 'darwin'

- name: retrieve phar artifacts
uses: actions/download-artifact@v4
with:
path: tools/phar/build
merge-multiple: true

- uses: ./.github/actions/static
with:
os: 'darwin-amd64'

- name: Upload the Linux static binary
uses: actions/upload-artifact@v4
with:
name: 'castor.darwin-amd64'
path: ./castor.darwin-amd64
if-no-files-found: error

release:
name: Upload phars and static binaries to the release
name: Upload artefacts to the release
if: github.event_name == 'release'
needs: [phar, static]
needs: [phars, static-linux, static-darwin-amd64]
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -96,11 +133,7 @@ jobs:

- name: Upload files
run: |
# Weird, in the artefacts, the binary is ./build/castor.linux-amd64, but
# when we download it, it's ./castor.linux.x86_64 (the old name)
if [ -f ./build/castor.linux.x86_64 ]; then
mv ./build/castor.linux.x86_64 ./build/castor.linux-amd64
fi
gh release upload ${{ github.ref_name }} ./build/castor.darwin-amd64
gh release upload ${{ github.ref_name }} ./build/castor.darwin-amd64.phar
gh release upload ${{ github.ref_name }} ./build/castor.darwin-arm64.phar
gh release upload ${{ github.ref_name }} ./build/castor.linux-amd64
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
if: matrix.castor.method == 'phar' || matrix.castor.method == 'static'

- name: Compile Custom Built PHP along Castor phar for Linux
# Should be the same command as the one in .github/actions/cache/action.yaml
# Should be the same command as the one in tools/static/castor.php
run: bin/castor compile tools/phar/build/castor.linux-amd64.phar --php-extensions=mbstring,phar,posix,tokenizer,pcntl --binary-path=${{ github.workspace }}/${{ matrix.castor.bin }}
if: matrix.castor.method == 'static'

Expand Down
3 changes: 2 additions & 1 deletion bin/generate-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
'castor:qa:cs',
'castor:qa:phpstan',
'castor:repack',
'castor:static:build',
'castor:static:darwin-amd64',
'castor:static:darwin-arm64',
'castor:static:linux',
'castor:watcher:build',
'castor:watcher:darwin',
Expand Down
5 changes: 3 additions & 2 deletions tests/Examples/Generated/ListTest.php.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ castor:phar:linux Build phar for
castor:phar:windows Build phar for Windows system
castor:qa:cs Fix CS
castor:qa:phpstan Run PHPStan
castor:static:build Build phar for all systems
castor:static:linux Build phar for Linux system
castor:static:darwin-amd64 Build static binary for MacOS (amd64) system
castor:static:darwin-arm64 Build static binary for MacOS (arm64) system
castor:static:linux Build static binary for Linux system
castor:watcher:build Build watcher for all systems
castor:watcher:darwin Build watcher for MacOS system
castor:watcher:linux Build watcher for Linux system
Expand Down
17 changes: 11 additions & 6 deletions tools/static/castor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

use Castor\Attribute\AsTask;

use function Castor\parallel;
use function Castor\run;

#[AsTask(description: 'Build phar for Linux system')]
#[AsTask(description: 'Build static binary for Linux system')]
function linux()
{
run('bin/castor compile ./tools/phar/build/castor.linux-amd64.phar --os=linux --arch=x86_64', timeout: 0);
run('bin/castor compile tools/phar/build/castor.linux-amd64.phar --os=linux --arch=x86_64 --binary-path=castor.linux-amd64 --php-extensions=mbstring,phar,posix,tokenizer,pcntl', timeout: 0);
}

#[AsTask(description: 'Build phar for all systems')]
function build()
#[AsTask(description: 'Build static binary for MacOS (amd64) system')]
function darwinAmd64()
{
parallel(linux(...));
run('bin/castor compile tools/phar/build/castor.darwin-amd64.phar --os=macos --arch=x86_64 --binary-path=castor.darwin-amd64 --php-extensions=mbstring,phar,posix,tokenizer,pcntl', timeout: 0);
}

#[AsTask(description: 'Build static binary for MacOS (arm64) system')]
function darwinArm64()
{
run('bin/castor compile tools/phar/build/castor.darwin-arm64.phar --os=macos --arch=aarch64 --binary-path=castor.darwin-arm64 --php-extensions=mbstring,phar,posix,tokenizer,pcntl', timeout: 0);
}

0 comments on commit 87a33ba

Please sign in to comment.