Files
FemLite/tools/release_to_cclite.ps1
shenw 946d66a5a9
FemLite CI / build-test (push) Has been cancelled
Include IPC artifacts in CCLite release package
2026-07-05 21:11:30 +08:00

213 lines
8.9 KiB
PowerShell

param(
[string]$TargetRoot = "..\CCLite\FEMLite",
[string]$CCliteRoot = "..\CCLite",
[string]$GccRoot = "C:\gcc-16.1.0-64",
[string]$ReleaseBuildDir = "build-cclite-release",
[string]$DebugBuildDir = "build-cclite-debug",
[switch]$SkipBuild
)
$ErrorActionPreference = "Stop"
function Resolve-OrCreateDirectory {
param([string]$Path)
if (-not (Test-Path -LiteralPath $Path)) {
New-Item -ItemType Directory -Force -Path $Path | Out-Null
}
return (Resolve-Path -LiteralPath $Path).Path
}
function Assert-UnderDirectory {
param(
[string]$Child,
[string]$Parent
)
$childFull = [System.IO.Path]::GetFullPath($Child)
$parentFull = [System.IO.Path]::GetFullPath($Parent)
if (-not $parentFull.EndsWith([System.IO.Path]::DirectorySeparatorChar)) {
$parentFull += [System.IO.Path]::DirectorySeparatorChar
}
if (-not $childFull.StartsWith($parentFull, [System.StringComparison]::OrdinalIgnoreCase)) {
throw "Refusing to operate outside target root: $childFull"
}
}
function Reset-Directory {
param(
[string]$Path,
[string]$AllowedRoot
)
Assert-UnderDirectory -Child $Path -Parent $AllowedRoot
if (Test-Path -LiteralPath $Path) {
Remove-Item -LiteralPath $Path -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $Path | Out-Null
}
function Invoke-Checked {
param(
[string]$FilePath,
[string[]]$ArgumentList
)
& $FilePath @ArgumentList
if ($LASTEXITCODE -ne 0) {
throw "Command failed with exit code ${LASTEXITCODE}: $FilePath $($ArgumentList -join ' ')"
}
}
function Invoke-RobocopyChecked {
param(
[string]$Source,
[string]$Destination,
[string[]]$Arguments
)
& robocopy $Source $Destination @Arguments
if ($LASTEXITCODE -gt 7) {
throw "robocopy failed with exit code $LASTEXITCODE"
}
}
function Configure-And-Build {
param(
[string]$BuildDir,
[string]$BuildType,
[string]$GccRootPath
)
$make = Join-Path $GccRootPath "bin\make.exe"
$gcc = Join-Path $GccRootPath "bin\gcc.exe"
$gfortran = Join-Path $GccRootPath "bin\gfortran.exe"
foreach ($tool in @($make, $gcc, $gfortran)) {
if (-not (Test-Path -LiteralPath $tool)) {
throw "Required build tool was not found: $tool"
}
}
$configureArgs = @(
"-S", ".",
"-B", $BuildDir,
"-G", "MinGW Makefiles",
"-DCMAKE_BUILD_TYPE=$BuildType",
"-DCMAKE_MAKE_PROGRAM=$make",
"-DCMAKE_C_COMPILER=$gcc",
"-DCMAKE_Fortran_COMPILER=$gfortran"
)
Invoke-Checked -FilePath "cmake" -ArgumentList $configureArgs
Invoke-Checked -FilePath "cmake" -ArgumentList @("--build", $BuildDir, "--parallel")
}
function Copy-BinaryPackage {
param(
[string]$BuildDir,
[string]$Destination
)
$binDir = Join-Path $Destination "bin"
$includeDir = Join-Path $Destination "include"
$modDir = Join-Path $includeDir "mod"
$libraryDir = Join-Path $Destination "library"
$requiredFiles = @(
(Join-Path $BuildDir "femlite.exe"),
(Join-Path $BuildDir "libFemLite.a"),
(Join-Path $BuildDir "libfemlite_c.a"),
(Join-Path $BuildDir "libfemlite_ipc_c.a"),
(Join-Path $BuildDir "libfemlite_worker.a"),
(Join-Path $BuildDir "femlite_fortran_mangle.h")
)
foreach ($file in $requiredFiles) {
if (-not (Test-Path -LiteralPath $file)) {
throw "Required build artifact was not found: $file. Run without -SkipBuild or pass the matching build directory."
}
}
if (-not (Get-ChildItem -Path (Join-Path $BuildDir "mod\*.mod") -ErrorAction SilentlyContinue)) {
throw "Required Fortran module files were not found in: $(Join-Path $BuildDir 'mod')"
}
foreach ($dir in @($binDir, $includeDir, $modDir, $libraryDir)) {
New-Item -ItemType Directory -Force -Path $dir | Out-Null
}
Copy-Item -LiteralPath (Join-Path $BuildDir "femlite.exe") -Destination (Join-Path $binDir "femlite.exe") -Force
Copy-Item -LiteralPath (Join-Path $BuildDir "libFemLite.a") -Destination (Join-Path $libraryDir "libFemLite.a") -Force
Copy-Item -LiteralPath (Join-Path $BuildDir "libfemlite_c.a") -Destination (Join-Path $libraryDir "libfemlite_c.a") -Force
Copy-Item -LiteralPath (Join-Path $BuildDir "libfemlite_ipc_c.a") -Destination (Join-Path $libraryDir "libfemlite_ipc_c.a") -Force
Copy-Item -LiteralPath (Join-Path $BuildDir "libfemlite_worker.a") -Destination (Join-Path $libraryDir "libfemlite_worker.a") -Force
Copy-Item -LiteralPath "include\femlite_c_api.h" -Destination (Join-Path $includeDir "femlite_c_api.h") -Force
Copy-Item -LiteralPath "include\femlite_cclite_ipc.h" -Destination (Join-Path $includeDir "femlite_cclite_ipc.h") -Force
Copy-Item -LiteralPath (Join-Path $BuildDir "femlite_fortran_mangle.h") -Destination (Join-Path $includeDir "femlite_fortran_mangle.h") -Force
Copy-Item -Path (Join-Path $BuildDir "mod\*.mod") -Destination $modDir -Force
}
function Update-ExampleScripts {
param([string]$ExamplesDir)
$scripts = Get-ChildItem -LiteralPath $ExamplesDir -Recurse -File -Filter "run_case.bat"
$sharedRunner = Get-Item -LiteralPath (Join-Path $ExamplesDir "run_example_case.bat") -ErrorAction SilentlyContinue
if ($sharedRunner) {
$scripts += $sharedRunner
}
foreach ($script in $scripts) {
$text = [System.IO.File]::ReadAllText($script.FullName)
$text = [regex]::Replace($text, 'set "EXE=.*?"', 'set "EXE=%ROOT_DIR%\cpu_release\bin\femlite.exe"')
$text = [regex]::Replace($text, 'set "PYTHON=.*?"', 'set "PYTHON=%ROOT_DIR%\..\Python\.venv\Scripts\python.exe"')
$missingExeBlock = '(?s)if not exist "%EXE%" \(\s+echo femlite\.exe was not found\. Trying to build build-win-gcc-16\.\.\.\s+cmake --build "%ROOT_DIR%\\build-win-gcc-16" --config Release\s+if errorlevel 1 goto fail\s+\)'
$missingExeReplacement = @'
if not exist "%EXE%" (
echo femlite.exe was not found: "%EXE%"
goto fail
)
'@
$text = [regex]::Replace($text, $missingExeBlock, $missingExeReplacement.TrimEnd())
$text = $text -replace "`r?`n", "`r`n"
[System.IO.File]::WriteAllText($script.FullName, $text, [System.Text.UTF8Encoding]::new($false))
}
}
function Assert-NoExampleOutputs {
param([string]$ExamplesDir)
$generatedExtensions = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
foreach ($extension in @(".csv", ".vtk", ".vtu", ".pvd", ".log", ".restart", ".rst")) {
[void]$generatedExtensions.Add($extension)
}
$resultDirs = Get-ChildItem -LiteralPath $ExamplesDir -Recurse -Directory -Filter "results" -ErrorAction SilentlyContinue
$outputFiles = Get-ChildItem -LiteralPath $ExamplesDir -Recurse -File -ErrorAction SilentlyContinue |
Where-Object { $generatedExtensions.Contains($_.Extension) }
if ($resultDirs -or $outputFiles) {
$sample = @($resultDirs + $outputFiles) | Select-Object -First 5 -ExpandProperty FullName
throw "Published examples contain generated output files: $($sample -join '; ')"
}
}
$repoRoot = (Resolve-Path -LiteralPath ".").Path
$targetRootPath = Resolve-OrCreateDirectory -Path $TargetRoot
$ccliteRootPath = Resolve-OrCreateDirectory -Path $CCliteRoot
Assert-UnderDirectory -Child $targetRootPath -Parent $ccliteRootPath
if (-not $SkipBuild) {
Configure-And-Build -BuildDir $ReleaseBuildDir -BuildType "Release" -GccRootPath $GccRoot
Configure-And-Build -BuildDir $DebugBuildDir -BuildType "Debug" -GccRootPath $GccRoot
}
$releaseRoot = Join-Path $targetRootPath "cpu_release"
$debugRoot = Join-Path $targetRootPath "cpu_debug"
$docsRoot = Join-Path $targetRootPath "docs"
$examplesRoot = Join-Path $targetRootPath "examples"
Reset-Directory -Path $releaseRoot -AllowedRoot $targetRootPath
Reset-Directory -Path $debugRoot -AllowedRoot $targetRootPath
Reset-Directory -Path $docsRoot -AllowedRoot $targetRootPath
Reset-Directory -Path $examplesRoot -AllowedRoot $targetRootPath
Copy-BinaryPackage -BuildDir $ReleaseBuildDir -Destination $releaseRoot
Copy-BinaryPackage -BuildDir $DebugBuildDir -Destination $debugRoot
Copy-Item -LiteralPath "README.md" -Destination (Join-Path $targetRootPath "README.md") -Force
Invoke-RobocopyChecked -Source (Join-Path $repoRoot "docs") -Destination $docsRoot -Arguments @("/E", "/NFL", "/NDL", "/NJH", "/NJS", "/NP")
Invoke-RobocopyChecked -Source (Join-Path $repoRoot "examples") -Destination $examplesRoot -Arguments @("/E", "/XD", "results", "__pycache__", "/XF", "*.csv", "*.vtk", "*.vtu", "*.pvd", "*.log", "*.rst", "*.restart", "/NFL", "/NDL", "/NJH", "/NJS", "/NP")
Update-ExampleScripts -ExamplesDir $examplesRoot
Assert-NoExampleOutputs -ExamplesDir $examplesRoot
Write-Host "FemLite package published to $targetRootPath"
Write-Host "Release: $releaseRoot"
Write-Host "Debug: $debugRoot"