PowerShell/scripts/build-repo.ps1

152 lines
5.1 KiB
PowerShell
Raw Normal View History

2024-10-01 13:37:53 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2022-12-22 12:41:07 +01:00
Builds a repository
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2023-10-16 10:31:43 +02:00
This PowerShell script builds a Git repository by supporting build systems such as: autogen, cmake, configure, Gradle, Imakefile, Makefile, and Meson.
2023-10-15 11:49:46 +02:00
.PARAMETER path
2024-03-18 12:44:42 +01:00
Specifies the path to the Git repository (default is current working directory)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2023-10-15 11:49:46 +02:00
PS> ./build-repo.ps1 C:\Repos\ninja
2024-03-18 12:44:42 +01:00
Building 📂ninja using CMakeLists.txt into 📂ninja/_Build_Results...
2023-10-15 11:49:46 +02:00
...
2024-09-24 21:28:31 +02:00
Built 📂ninja repository in 47 sec.
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-04-14 17:50:35 +02:00
#>
2023-10-15 11:49:46 +02:00
param([string]$path = "$PWD")
2021-04-14 17:50:35 +02:00
2023-10-15 11:49:46 +02:00
function BuildInDir([string]$path) {
$dirName = (Get-Item "$path").Name
if (Test-Path "$path/CMakeLists.txt" -pathType leaf) {
2024-03-18 12:44:42 +01:00
"⏳ (1/4) Building 📂$dirName by using CMake into 📂$dirName/_Build_Results..."
if (-not(Test-Path "$path/_Build_Results/" -pathType container)) {
& mkdir "$path/_Build_Results/"
2021-04-14 19:23:34 +02:00
}
2024-03-18 12:44:42 +01:00
Set-Location "$path/_Build_Results/"
2021-05-10 16:21:38 +02:00
2023-10-23 18:01:56 +02:00
"⏳ (2/4) Executing 'cmake' to generate the Makefile..."
2021-04-14 19:01:47 +02:00
& cmake ..
2024-05-02 13:09:54 +02:00
if ($lastExitCode -ne "0") { throw "Executing 'cmake ..' exited with error code $lastExitCode" }
2021-04-14 19:01:47 +02:00
2023-10-23 18:01:56 +02:00
"⏳ (3/4) Executing 'make -j4' to compile and link..."
2021-04-14 19:01:47 +02:00
& make -j4
2024-05-02 13:09:54 +02:00
if ($lastExitCode -ne "0") { throw "Executing 'make -j4' exited with error code $lastExitCode" }
2021-04-14 20:02:04 +02:00
2024-01-02 19:46:10 +01:00
"⏳ (4/4) Executing 'ctest -V' to perform tests (optional)..."
& ctest -V
2024-05-02 13:09:54 +02:00
if ($lastExitCode -ne "0") { throw "Executing 'ctest -V' exited with error code $lastExitCode" }
2021-05-22 15:51:36 +02:00
2024-05-02 13:09:54 +02:00
} elseif (Test-Path "$path/autogen.sh" -pathType leaf) {
"⏳ Building 📂$dirName by using 'autogen.sh'..."
2023-10-15 11:49:46 +02:00
Set-Location "$path/"
2021-04-14 20:02:04 +02:00
2024-05-02 13:09:54 +02:00
& ./autogen.sh --force
if ($lastExitCode -ne "0") { throw "Executing './autogen.sh --force' exited with error code $lastExitCode" }
2021-04-14 20:02:04 +02:00
& ./configure
2024-05-02 13:09:54 +02:00
if ($lastExitCode -ne "0") { throw "Executing './configure' exited with error code $lastExitCode" }
2021-04-14 20:02:04 +02:00
& make -j4
2024-05-02 13:09:54 +02:00
if ($lastExitCode -ne "0") { throw "Executing 'make -j4' exited with error code $lastExitCode" }
2021-04-14 20:02:04 +02:00
2021-07-02 17:09:52 +02:00
2024-05-02 13:09:54 +02:00
} elseif (Test-Path "$path/configure" -pathType leaf) {
"⏳ Building 📂$dirName by using 'configure'..."
2023-10-15 11:49:46 +02:00
Set-Location "$path/"
2021-04-14 20:02:04 +02:00
2024-05-02 13:09:54 +02:00
& ./configure
#if ($lastExitCode -ne "0") { throw "Executing './configure' exited with error code $lastExitCode" }
2021-04-14 20:02:04 +02:00
& make -j4
if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }
2024-05-02 13:09:54 +02:00
& make test
if ($lastExitCode -ne "0") { throw "Executing 'make test' has failed" }
2023-10-15 11:49:46 +02:00
} elseif (Test-Path "$path/build.gradle" -pathType leaf) {
2023-10-16 10:31:43 +02:00
"⏳ Building 📂$dirName by using Gradle..."
2023-10-15 11:49:46 +02:00
Set-Location "$path"
2021-05-10 16:21:38 +02:00
& gradle build
2024-05-02 13:09:54 +02:00
if ($lastExitCode -ne "0") { throw "Executing 'gradle build' exited with error code $lastExitCode" }
2021-05-10 16:21:38 +02:00
& gradle test
2024-05-02 13:09:54 +02:00
if ($lastExitCode -ne "0") { throw "Executing 'gradle test' exited with error code $lastExitCode" }
2021-05-10 16:21:38 +02:00
2023-10-16 10:31:43 +02:00
} elseif (Test-Path "$path/meson.build" -pathType leaf) {
"⏳ Building 📂$dirName by using Meson..."
Set-Location "$path"
& meson . build --prefix=/usr/local
2024-05-02 13:09:54 +02:00
if ($lastExitCode -ne "0") { throw "Executing 'meson . build' exited with error code $lastExitCode" }
2023-10-16 10:31:43 +02:00
2023-10-15 11:49:46 +02:00
} elseif (Test-Path "$path/Imakefile" -pathType leaf) {
2023-10-16 10:31:43 +02:00
"⏳ Building 📂$dirName by using Imakefile..."
2023-10-15 11:49:46 +02:00
Set-Location "$path/"
2021-04-15 08:25:20 +02:00
& xmkmf
if ($lastExitCode -ne "0") { throw "Executing 'xmkmf' has failed" }
& make -j4
if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }
2023-10-15 11:49:46 +02:00
} elseif (Test-Path "$path/Makefile" -pathType leaf) {
2023-10-16 10:31:43 +02:00
"⏳ Building 📂$dirName by using Makefile..."
2023-10-15 11:49:46 +02:00
Set-Location "$path"
2023-04-27 12:51:58 +02:00
& make -j4
if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }
2023-10-15 11:49:46 +02:00
} elseif (Test-Path "$path/makefile" -pathType leaf) {
2023-10-16 10:31:43 +02:00
"⏳ Building 📂$dirName by using makefile..."
2023-10-15 11:49:46 +02:00
Set-Location "$path"
2021-04-14 19:01:47 +02:00
& make -j4
if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }
2021-04-14 20:02:04 +02:00
2023-10-15 11:49:46 +02:00
} elseif (Test-Path "$path/compile.sh" -pathType leaf) {
2023-10-16 10:31:43 +02:00
"⏳ Building 📂$dirName by using 'compile.sh'..."
2023-10-15 11:49:46 +02:00
Set-Location "$path/"
2021-05-23 11:59:46 +02:00
& ./compile.sh
2024-05-02 13:09:54 +02:00
if ($lastExitCode -ne "0") { throw "Executing './compile.sh' exited with error code $lastExitCode" }
2021-05-23 11:59:46 +02:00
& make -j4
if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }
2023-10-15 11:49:46 +02:00
} elseif (Test-Path "$path/attower/src/build/DevBuild/build.bat" -pathType leaf) {
2023-10-16 10:31:43 +02:00
"⏳ Building 📂$dirName by using build.bat ..."
2023-10-15 11:49:46 +02:00
Set-Location "$path/attower/src/build/DevBuild/"
2021-04-14 19:23:34 +02:00
2021-04-15 08:25:20 +02:00
& ./build.bat build-all-release
2024-05-02 13:09:54 +02:00
if ($lastExitCode -ne "0") { throw "Executing 'build.bat build-all-release' exited with error code $lastExitCode" }
2021-04-14 19:23:34 +02:00
2023-10-15 11:49:46 +02:00
} elseif (Test-Path "$path/$dirName" -pathType container) {
"⏳ No make rule found, trying subfolder 📂$($dirName)..."
BuildInDir "$path/$dirName"
2021-04-14 17:50:35 +02:00
} else {
2023-10-15 11:49:46 +02:00
Write-Warning "Sorry, no make rule applies to: 📂$dirName"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-04-14 17:50:35 +02:00
}
2021-05-07 15:37:04 +02:00
}
try {
2023-10-15 11:49:46 +02:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2021-05-07 15:37:04 +02:00
2023-10-15 11:49:46 +02:00
if (-not(Test-Path "$path" -pathType container)) { throw "Can't access directory: $path" }
2021-05-07 15:37:04 +02:00
2023-10-15 11:49:46 +02:00
$previousPath = Get-Location
BuildInDir "$path"
Set-Location "$previousPath"
2021-04-15 08:41:37 +02:00
2023-10-15 11:49:46 +02:00
$repoDirName = (Get-Item "$path").Name
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-09-24 21:28:31 +02:00
"✅ Built 📂$repoDirName repository in $elapsed sec."
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-04-14 17:50:35 +02:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-04-14 17:50:35 +02:00
exit 1
2023-04-27 12:51:58 +02:00
}