diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 70eae62..f36be62 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,6 +25,12 @@ jobs: shell: pwsh run: ./common/verify-encoding.ps1 + - name: Cache artifacts + uses: actions/cache@v4 + with: + path: artifacts + key: linux.x86-64 + # Build: - name: Install shell: pwsh @@ -55,6 +61,12 @@ jobs: shell: pwsh run: ./common/verify-encoding.ps1 + - name: Cache artifacts + uses: actions/cache@v4 + with: + path: artifacts + key: macos.x86-64 + # Build: - name: Install shell: pwsh @@ -85,6 +97,12 @@ jobs: shell: pwsh run: ./common/verify-encoding.ps1 + - name: Cache artifacts + uses: actions/cache@v4 + with: + path: artifacts + key: windows.x86-64 + # Build: - name: Build shell: pwsh diff --git a/common/Test-UpToDate.ps1 b/common/Test-UpToDate.ps1 new file mode 100644 index 0000000..d9e9454 --- /dev/null +++ b/common/Test-UpToDate.ps1 @@ -0,0 +1,27 @@ +param ( + [string] $RepoDirectory = "$PSScriptRoot/../td", + [string] $ArtifactsDirectory = "$PSScriptRoot/../artifacts", + [switch] $GenerateCheckResult +) + +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest + +$resultFile = "$ArtifactsDirectory/artifact.json" +$inputs = [pscustomobject] @{ + $InputCommitHash = (git --git-dir=$RepoDirectory/.git rev-parse HEAD) +} + +if ($GenerateCheckResult) { + $inputs | ConvertTo-Json | Set-Content $resultFile + Write-Host "Result cache file generated: `"$resultFile`"." + return $true +} elseif (Test-Path $resultFile) { + $result = Get-Content $resultFile | ConvertFrom-Json + $upToDate = $result.InputCommitHash -eq $inputs.InputCommitHash + Write-Host "Cache found: cached commit hash is $($result.InputCommitHash), current commit hash is $($inputs.InputCommitHash). Up to date: $upToDate." + return $upToDate +} else { + Write-Host "Last result cache file not found: `"$resultFile`"." + return $false +} diff --git a/linux/build.ps1 b/linux/build.ps1 index a12755b..1d7fadd 100644 --- a/linux/build.ps1 +++ b/linux/build.ps1 @@ -1,54 +1,62 @@ param ( [string] $td = "$PSScriptRoot/../td", - [string] $InstallPrefix = "$PSScriptRoot/../build/install" + [string] $InstallPrefix = "$PSScriptRoot/../build/install", + [string] $CheckUpToDateScript = "$PSScriptRoot/../common/Test-UpToDate.ps1", + [switch] $SkipUpToDateCheck ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest -if (-not (Test-Path $td/build)) { - New-Item -Type Directory $td/build -} - -Push-Location $td/build -try { - $cmakeArguments = @( - '-DCMAKE_BUILD_TYPE=Release' - "-DCMAKE_INSTALL_PREFIX:PATH=$InstallPrefix" - '..' - ) - $cmakePrepareCrossCompilingArguments = @( - '--build' - '.' - '--target', 'prepare_cross_compiling' - ) - $cmakeBuildArguments = @( - '--build' - '.' - '--target', 'install' - ) - - cmake @cmakeArguments - if (!$?) { - throw 'Cannot execute cmake' +if ($SkipUpToDateCheck -or !$(& $CheckUpToDateScript)) { + if (-not (Test-Path $td/build)) { + New-Item -Type Directory $td/build } - cmake @cmakePrepareCrossCompilingArguments - if (!$?) { - throw 'Cannot execute cmake --build --target prepare_cross_compiling' - } + Push-Location $td/build + try { + $cmakeArguments = @( + '-DCMAKE_BUILD_TYPE=Release' + "-DCMAKE_INSTALL_PREFIX:PATH=$InstallPrefix" + '..' + ) + $cmakePrepareCrossCompilingArguments = @( + '--build' + '.' + '--target', 'prepare_cross_compiling' + ) + $cmakeBuildArguments = @( + '--build' + '.' + '--target', 'install' + ) - Set-Location .. - php SplitSource.php - if (!$?) { - throw 'Cannot execute php SplitSource.php' - } + cmake @cmakeArguments + if (!$?) { + throw 'Cannot execute cmake' + } + + cmake @cmakePrepareCrossCompilingArguments + if (!$?) { + throw 'Cannot execute cmake --build --target prepare_cross_compiling' + } + + Set-Location .. + php SplitSource.php + if (!$?) { + throw 'Cannot execute php SplitSource.php' + } + + Set-Location build + cmake @cmakeBuildArguments + if (!$?) { + throw 'Cannot execute cmake --build --target install' + } - Set-Location build - cmake @cmakeBuildArguments - if (!$?) { - throw 'Cannot execute cmake --build --target install' + & $CheckUpToDateScript -GenerateCheckResult + } finally { + Pop-Location } -} finally { - Pop-Location +} else { + Write-Host 'The build result is up to date.' } diff --git a/macos/build.ps1 b/macos/build.ps1 index 90c0396..edcdafc 100644 --- a/macos/build.ps1 +++ b/macos/build.ps1 @@ -1,35 +1,41 @@ param ( - [string] $td = "$PSScriptRoot/../td" + [string] $td = "$PSScriptRoot/../td", + [string] $CheckUpToDateScript = "$PSScriptRoot/../common/Test-UpToDate.ps1", + [switch] $SkipUpToDateCheck ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest -if (-not (Test-Path $td/build)) { - New-Item -Type Directory $td/build -} +if ($SkipUpToDateCheck -or !$(& $CheckUpToDateScript)) { + if (-not (Test-Path $td/build)) { + New-Item -Type Directory $td/build + } -Push-Location $td/build -try { - $cmakeArguments = @( - '-DCMAKE_BUILD_TYPE=Release' - '-DOPENSSL_ROOT_DIR=/usr/local/opt/openssl/' - '..' - ) - $cmakeBuildArguments = @( - '--build' - '.' - ) + Push-Location $td/build + try { + $cmakeArguments = @( + '-DCMAKE_BUILD_TYPE=Release' + '-DOPENSSL_ROOT_DIR=/usr/local/opt/openssl/' + '..' + ) + $cmakeBuildArguments = @( + '--build' + '.' + ) - cmake @cmakeArguments - if (!$?) { - throw 'Cannot execute cmake' - } + cmake @cmakeArguments + if (!$?) { + throw 'Cannot execute cmake' + } - cmake @cmakeBuildArguments - if (!$?) { - throw 'Cannot execute cmake --build' + cmake @cmakeBuildArguments + if (!$?) { + throw 'Cannot execute cmake --build' + } + } finally { + Pop-Location } -} finally { - Pop-Location +} else { + Write-Host 'The build result is up to date.' } diff --git a/windows/build.ps1 b/windows/build.ps1 index 5cd53ed..b8be2af 100644 --- a/windows/build.ps1 +++ b/windows/build.ps1 @@ -1,54 +1,60 @@ param ( [string] $td = "$PSScriptRoot/../td", [string] $Platform = 'x64-windows', - [Parameter(Mandatory = $true)] [string] $VcpkgToolchain + [Parameter(Mandatory = $true)] [string] $VcpkgToolchain, + [string] $CheckUpToDateScript = "$PSScriptRoot/../common/Test-UpToDate.ps1", + [switch] $SkipUpToDateCheck ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest -if (-not (Test-Path $td/build)) { - New-Item -Type Directory $td/build -} +if ($SkipUpToDateCheck -or !$(& $CheckUpToDateScript)) { + if (-not (Test-Path $td/build)) { + New-Item -Type Directory $td/build + } -Push-Location $td/build -try { - $vcpkgArguments = @( - 'install' - "gperf:$platform" - "openssl:$platform" - "zlib:$platform" - ) - $cmakeArguments = @( - "-DCMAKE_TOOLCHAIN_FILE=$VcpkgToolchain" - '-DCMAKE_DISABLE_FIND_PACKAGE_Readline=TRUE' # workaround for #76 - '..' - ) - $cmakeBuildArguments = @( - '--build' - '.' - '--config' - 'Release' - ) + Push-Location $td/build + try { + $vcpkgArguments = @( + 'install' + "gperf:$platform" + "openssl:$platform" + "zlib:$platform" + ) + $cmakeArguments = @( + "-DCMAKE_TOOLCHAIN_FILE=$VcpkgToolchain" + '-DCMAKE_DISABLE_FIND_PACKAGE_Readline=TRUE' # workaround for #76 + '..' + ) + $cmakeBuildArguments = @( + '--build' + '.' + '--config' + 'Release' + ) - if ($Platform -eq 'x64-windows') { - $cmakeArguments += @('-A', 'X64') - } + if ($Platform -eq 'x64-windows') { + $cmakeArguments += @('-A', 'X64') + } - vcpkg @vcpkgArguments - if (!$?) { - throw 'Cannot execute vcpkg' - } + vcpkg @vcpkgArguments + if (!$?) { + throw 'Cannot execute vcpkg' + } - cmake @cmakeArguments - if (!$?) { - throw 'Cannot execute cmake' - } + cmake @cmakeArguments + if (!$?) { + throw 'Cannot execute cmake' + } - cmake @cmakeBuildArguments - if (!$?) { - throw 'Cannot execute cmake --build' + cmake @cmakeBuildArguments + if (!$?) { + throw 'Cannot execute cmake --build' + } + } finally { + Pop-Location } -} finally { - Pop-Location +} else { + Write-Host 'The build result is up to date.' }