poco/buildwin.ps1

553 lines
15 KiB
PowerShell
Raw Normal View History

2014-02-01 09:21:01 +01:00
#
# POCO build script
2018-05-28 12:19:50 +02:00
#
2014-02-01 09:21:01 +01:00
# Usage:
# ------
2014-02-01 09:27:31 +01:00
# buildwin.ps1 [-poco_base dir]
# [-vs 160| 170]
2014-02-01 09:21:01 +01:00
# [-action build | rebuild | clean]
# [-linkmode shared | static_mt | static_md | all]
# [-config release | debug | both]
# [-platform Win32 | x64 | ARM64 | WEC2013]
2014-02-01 09:21:01 +01:00
# [-samples]
# [-tests]
# [-omit "Lib1X,LibY,LibZ,..."]
# [-components "Lib1X,LibY,LibZ,..."]
2018-01-29 16:22:45 +01:00
# [-tool msbuild | devenv]
# [-useenv env | noenv]
# [-verbosity minimal | quiet | normal | detailed | diagnostic]
2014-02-01 09:21:01 +01:00
# [-openssl_base dir]
# [-mysql_base dir]
[CmdletBinding()]
Param
(
2022-05-25 01:35:58 +02:00
[Parameter()]
[string] $poco_base = $([System.Environment]::GetEnvironmentVariable('POCO_BASE')),
2014-02-01 09:21:01 +01:00
2022-05-25 01:35:58 +02:00
[Parameter()]
[ValidateSet(160, 170)]
[int] $vs = 170,
2018-05-28 12:19:50 +02:00
2022-05-25 01:35:58 +02:00
[Parameter()]
[ValidateSet('build', 'rebuild', 'clean')]
[string] $action = 'build',
2014-02-01 09:21:01 +01:00
2022-05-25 01:35:58 +02:00
[Parameter()]
[ValidateSet('shared', 'static_mt', 'static_md', 'all')]
[string] $linkmode = 'shared',
2014-02-01 09:21:01 +01:00
2022-05-25 01:35:58 +02:00
[Parameter()]
[ValidateSet('release', 'debug', 'both')]
[string] $config = 'release',
2014-02-01 09:21:01 +01:00
2022-05-25 01:35:58 +02:00
[Parameter()]
[ValidateSet('Win32', 'x64', 'ARM64', 'WEC2013')]
2022-05-25 01:35:58 +02:00
[string] $platform = 'x64',
2018-01-29 16:22:45 +01:00
2022-05-25 01:35:58 +02:00
[switch] $tests = $false,
[switch] $samples = $false,
[string] $omit,
[string] $components,
2018-01-29 16:22:45 +01:00
2022-05-25 01:35:58 +02:00
[Parameter()]
[ValidateSet('msbuild', 'devenv')]
[string] $tool = 'msbuild',
2014-02-01 09:21:01 +01:00
2022-05-25 01:35:58 +02:00
[Parameter()]
[ValidateSet('env', 'noenv')]
[string] $useenv = 'env',
2022-05-25 01:35:58 +02:00
[Parameter()]
[ValidateSet('quiet', 'minimal', 'normal', 'detailed', 'diagnostic')]
2022-05-25 01:35:58 +02:00
[string] $verbosity = 'minimal',
2022-05-25 01:35:58 +02:00
[Parameter()]
[string] $openssl_base,
2014-02-01 09:21:01 +01:00
2022-05-25 01:35:58 +02:00
[Parameter()]
[string] $mysql_base,
2014-02-01 09:21:01 +01:00
2022-05-25 01:35:58 +02:00
[switch] $help
2014-02-01 09:21:01 +01:00
)
function Add-VSCOMNTOOLS([int] $vsver)
{
if ($vsver -ge 160)
{
$vssetup= $([Environment]::GetFolderPath("MyDocuments"))
$vssetup= Join-Path $vssetup "WindowsPowerShell"
$vssetup= Join-Path $vssetup "Modules"
$vssetup= Join-Path $vssetup "VSSetup"
if (-not (Test-Path $vssetup))
{
Install-Module VSSetup -Scope CurrentUser -Force
}
if ($vsver -eq 160)
{
$range='[16.0,17.0)'
}
2022-05-25 01:35:58 +02:00
if ($vsver -eq 170)
{
$range='[17.0,18.0)'
}
2023-11-07 17:38:48 +01:00
if ($platform -eq 'ARM64')
{
$global:installationPath = Get-VSSetupInstance | `
2023-11-07 17:38:48 +01:00
Select-VSSetupInstance -Version $range -product * -Latest -Require Microsoft.VisualStudio.Component.VC.Tools.ARM64 | `
Select-Object InstallationPath
}
2023-11-07 17:38:48 +01:00
else
{
$global:installationPath = Get-VSSetupInstance | `
2023-11-07 17:38:48 +01:00
Select-VSSetupInstance -Version $range -product * -Latest -Require Microsoft.VisualStudio.Component.VC.Tools.x86.x64 | `
Select-Object InstallationPath
}
$vscomntools = $installationPath.psobject.properties.Value;
if ($vsver -eq 160)
{
set-item -force -path "ENV:VS160COMNTOOLS" -value "$vscomntools\Common7\Tools\"
Write-Host "`n----------------------------------------" -ForegroundColor Yellow
Write-Host "VS160COMNTOOLS=$env:VS160COMNTOOLS" -ForegroundColor Yellow
Write-Host "----------------------------------------" -ForegroundColor Yellow
Write-Host ""
}
2022-05-25 01:35:58 +02:00
if ($vsver -eq 170)
{
set-item -force -path "ENV:VS170COMNTOOLS" -value "$vscomntools\Common7\Tools\"
Write-Host "`n----------------------------------------" -ForegroundColor Yellow
Write-Host "VS170COMNTOOLS=$env:VS170COMNTOOLS" -ForegroundColor Yellow
Write-Host "----------------------------------------" -ForegroundColor Yellow
Write-Host ""
2022-05-25 01:35:58 +02:00
}
}
}
2014-02-01 09:23:48 +01:00
function Add-Env-Var([string] $lib, [string] $var)
{
$envvar = if (Test-Path "Env:$var") { Get-Content "Env:$var" } Else { "" }
$libvar = Get-Content "Env:${lib}_$var"
if (-not $envvar.Contains($libvar))
2022-05-25 01:35:58 +02:00
{
$envvar = $envvar + ";$libvar"
Set-Content "Env:${var}" $envvar
2022-05-25 01:35:58 +02:00
}
}
2014-02-01 09:21:01 +01:00
function Set-Environment
{
2022-05-25 01:35:58 +02:00
if ($poco_base -eq '') { $script:poco_base = Get-Location }
2014-02-01 09:21:01 +01:00
Add-VSCOMNTOOLS $vs
2022-05-25 01:35:58 +02:00
if (-Not $Env:PATH.Contains("$Env:POCO_BASE\bin64;$Env:POCO_BASE\bin;"))
{ $Env:PATH = "$Env:POCO_BASE\bin64;$Env:POCO_BASE\bin;$Env:PATH" }
if ($openssl_base -eq '')
{
$script:openssl_base = "$poco_base\openssl"
2022-05-25 01:35:58 +02:00
}
$Env:OPENSSL_DIR = "$openssl_base"
2022-05-25 01:35:58 +02:00
$Env:OPENSSL_INCLUDE = "$Env:OPENSSL_DIR\include"
$Env:OPENSSL_LIB = "$Env:OPENSSL_DIR\lib;$Env:OPENSSL_DIR\lib\VC"
2022-05-25 01:35:58 +02:00
Add-Env-Var "OPENSSL" "INCLUDE"
Add-Env-Var "OPENSSL" "LIB"
if ($mysql_base -ne '')
{
$Env:MYSQL_DIR = "$mysql_base"
2022-05-25 01:35:58 +02:00
$Env:MYSQL_INCLUDE = "$Env:MYSQL_DIR\include"
$Env:MYSQL_LIB = "$Env:MYSQL_DIR\lib"
2022-05-25 01:35:58 +02:00
Add-Env-Var "MYSQL" "INCLUDE"
Add-Env-Var "MYSQL" "LIB"
}
$vsct = "VS$($vs)COMNTOOLS"
$vsdir = ''
$vsdir = (Get-Item Env:$vsct).Value
$Command = ''
$CommandArg = ''
if ($platform -eq 'x64') { $CommandArg = "amd64" }
2023-11-07 17:38:48 +01:00
elseif ($platform -eq 'ARM64') { $CommandArg = "ARM64" }
else { $CommandArg = "x86" }
2022-05-25 01:35:58 +02:00
if ($vs -eq 160)
{
$Command = Resolve-Path "$($vsdir)\..\..\VC\Auxiliary\Build\vcvarsall.bat"
$script:msbuild_exe = Resolve-Path "$($vsdir)\..\..\MSBuild\Current\Bin\MSBuild.exe"
}
else
{
if ($vs -eq 170)
{
$Command = Resolve-Path "$($vsdir)\..\..\VC\Auxiliary\Build\vcvarsall.bat"
$script:msbuild_exe = Resolve-Path "$($vsdir)\..\..\MSBuild\Current\Bin\MSBuild.exe"
}
else
{
$Command = Resolve-Path "$($vsdir)\..\..\VC\vcvarsall.bat"
$script:msbuild_exe = "MSBuild.exe"
}
}
2022-05-25 01:35:58 +02:00
$tempFile = [IO.Path]::GetTempFileName()
cmd /c " `"$Command`" $CommandArg && set > `"$tempFile`" "
Get-Content $tempFile | Foreach-Object {
if($_ -match "^(.*?)=(.*)$")
{
Set-Content "Env:$($matches[1])" $matches[2]
}
}
Remove-Item $tempFile
2014-02-01 09:21:01 +01:00
}
function Process-Input
{
2022-05-25 01:35:58 +02:00
if ($help -eq $true)
{
Write-Host 'Usage:'
Write-Host '------'
Write-Host 'buildwin.ps1 [-poco_base <dir>]'
Write-Host ' [-vs 160 | 170]'
Write-Host ' [-action build | rebuild | clean]'
Write-Host ' [-linkmode shared | static_mt | static_md | all]'
Write-Host ' [-config release | debug | both]'
Write-Host ' [-platform Win32 | x64 | WEC2013 | ARM64]'
Write-Host ' [-samples]'
Write-Host ' [-tests]'
Write-Host ' [-omit "Lib1X,LibY,LibZ,..."]'
Write-Host ' [-components "Lib1X,LibY,LibZ,..."]'
Write-Host ' [-tool msbuild | devenv]'
Write-Host ' [-useenv env | noenv]'
Write-Host ' [-verbosity minimal | quiet | normal | detailed | diagnostic'
Write-Host ' [-openssl_base <dir>]'
Write-Host ' [-mysql_base <dir>]'
2022-05-25 01:35:58 +02:00
Exit
}
else
{
if($components -ne '' -and $omit -ne '') {
Write-Host "-components and -omit cannot be used simultaneously, exiting..."
Exit 1
}
2022-05-25 01:35:58 +02:00
Set-Environment
Write-Host ""
Write-Host "--------------------"
Write-Host "PS Version: " $PSVersionTable.PSVersion
Write-Host "--------------------"
Write-Host ""
Write-Host "--------------------"
2022-05-25 01:35:58 +02:00
Write-Host "Build configuration:"
Write-Host "--------------------"
Write-Host "Poco Base: $poco_base"
Write-Host "Version: $vs"
Write-Host "Action: $action"
Write-Host "Link Mode: $linkmode"
2022-05-25 01:35:58 +02:00
Write-Host "Configuration: $config"
Write-Host "Platform: $platform"
Write-Host "Tests: $tests"
Write-Host "Samples: $samples"
Write-Host "Build Tool: $tool"
2022-05-25 01:35:58 +02:00
if ($omit -ne '')
{
Write-Host "Omit: $omit"
2022-05-25 01:35:58 +02:00
}
if ($components -ne '')
{
Write-Host "Components: $components"
}
2022-05-25 01:35:58 +02:00
if ($openssl_base -ne '')
{
Write-Host "OpenSSL: $openssl_base"
2022-05-25 01:35:58 +02:00
}
if ($mysql_base -ne '')
{
Write-Host "MySQL: $mysql_base"
2022-05-25 01:35:58 +02:00
}
Write-Host "----------------------------------------"
Write-Host ""
2022-05-25 01:35:58 +02:00
# NB: this won't work in PowerShell ISE
#Write-Host "Press Ctrl-C to exit or any other key to continue ..."
#$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp")
}
2014-02-01 09:21:01 +01:00
}
function ExecuteMSBuild([string] $vsProject, [string] $projectConfig)
2018-01-29 16:22:45 +01:00
{
2022-05-25 01:35:58 +02:00
if (!(Test-Path -Path $vsProject -PathType leaf)) {
Write-Host "Project $vsProject not found, skipping."
return
}
$cmd = "&`"$script:msbuild_exe`" $vsProject /nologo /m /t:$action /p:Configuration=$projectConfig /p:BuildProjectReferences=false /p:Platform=$platform /p:useenv=$($useenv -eq 'env') /v:$verbosity"
2022-05-25 01:35:58 +02:00
Write-Host $cmd
Invoke-Expression $cmd
if ($LastExitCode -ne 0) { Exit $LastExitCode }
2018-01-29 16:22:45 +01:00
}
function RunMSBuild([string] $vsProject, [switch] $skipStatic)
2014-02-01 09:21:01 +01:00
{
2022-05-25 01:35:58 +02:00
if ($linkmode -contains "static" -and $skipStatic) { Return }
if ($linkmode.Contains("static") -and $vsProject.Contains("TestLibrary"))
{
Write-Host "Skipping static build of DLL-only $vsProject"
return
}
2022-05-25 01:35:58 +02:00
if ($linkmode -eq 'all')
{
$linkModeArr = @('shared')
if (-not $skipStatic)
{
$linkModeArr += 'static_mt', 'static_md'
}
foreach ($mode in $linkModeArr)
{
if ($config -eq 'both')
{
$configArr = 'release', 'debug'
foreach ($cfg in $configArr)
{
ExecuteMSBuild $vsProject "$($cfg)_$($mode)"
2022-05-25 01:35:58 +02:00
}
}
else #config
{
ExecuteMSBuild $vsProject "$($config)_$($mode)"
2022-05-25 01:35:58 +02:00
}
}
}
else #linkmode
{
if ($config -eq 'both')
{
$configArr = 'release', 'debug'
foreach ($cfg in $configArr)
{
ExecuteMSBuild $vsProject "$($cfg)_$($linkmode)"
2022-05-25 01:35:58 +02:00
}
}
else #config
{
ExecuteMSBuild $vsProject "$($config)_$($linkmode)"
2022-05-25 01:35:58 +02:00
}
}
2014-02-01 09:21:01 +01:00
}
function ExecuteDevenv([string] $projectConfig, [string] $vsProject)
2018-01-29 16:22:45 +01:00
{
2022-05-25 01:35:58 +02:00
$cmd = "devenv /useenv /$action $projectConfig $vsProject"
Write-Host $cmd
Invoke-Expression $cmd
2018-01-29 16:22:45 +01:00
}
function BuildDevenv([string] $vsProject, [switch] $skipStatic)
2014-02-01 09:21:01 +01:00
{
2022-05-25 01:35:58 +02:00
if ($linkmode -contains "static" -and $skipStatic) { Return }
if ($linkmode -eq 'all')
{
$linkModeArr = @('shared')
if (-not $skipStatic)
{
$linkModeArr += 'static_mt', 'static_md'
}
foreach ($mode in $linkModeArr)
{
if ($config -eq 'both')
{
$configArr = 'release', 'debug'
foreach ($cfg in $configArr)
{
ExecuteDevenv "$($cfg)_$($mode)" $vsProject
2022-05-25 01:35:58 +02:00
}
}
else #config
{
ExecuteDevenv "$($config)_$($mode)" $vsProject
2022-05-25 01:35:58 +02:00
}
}
}
else #linkmode
{
if ($config -eq 'both')
{
$configArr = 'release', 'debug'
foreach ($cfg in $configArr)
{
ExecuteDevenv "$($cfg)_$($linkmode)" $vsProject
2022-05-25 01:35:58 +02:00
}
}
else #config
{
ExecuteDevenv "$($config)_$($linkmode)" $vsProject
2022-05-25 01:35:58 +02:00
}
}
2014-02-01 09:21:01 +01:00
}
function BuildSamples
2014-02-01 09:21:01 +01:00
{
2022-05-25 01:35:58 +02:00
process {
$sampleName = $_.BaseName.split("_")[0]
$sampleProjName = "$($poco_base)\$($componentDir)\samples\$($sampleName)\$($_)"
if ($tool -eq 'devenv') { BuildDevenv $sampleProjName }
elseif ($tool -eq 'msbuild') { RunMSBuild $sampleProjName }
2022-05-25 01:35:58 +02:00
else{ Write-Host "Tool not supported: $tool" }
}
2014-02-01 09:21:01 +01:00
}
function BuildExecute([string] $tool, [string] $vsProject, [switch] $skipStatic)
2018-05-31 21:21:50 +02:00
{
2022-05-25 01:35:58 +02:00
if (!(Test-Path -Path $vsProject)) # not found
{
Write-Host "+------------------------------------------------------------------"
Write-Host "| VS project $vsProject not found, skipping."
Write-Host "+------------------------------------------------------------------"
Return
}
if ($tool -eq 'devenv') { BuildDevenv $vsProject -skipStatic:$skipStatic }
elseif ($tool -eq 'msbuild') { RunMSBuild $vsProject -skipStatic:$skipStatic }
2022-05-25 01:35:58 +02:00
else
{
Write-Host "Build tool $tool not supported. Exiting."
Exit -1
}
2018-05-31 21:21:50 +02:00
}
function BuildComponents([string] $extension, [string] $type)
{
2022-05-25 01:35:58 +02:00
Get-Content "$poco_base\components" | Foreach-Object {
$component = $_
$componentDir = $_.Replace("/", "\")
$componentArr = $_.split('/')
$componentName = $componentArr[$componentArr.Length - 1]
$suffix = "_vs$vs"
$omitArray = @()
$omit.Split(',') | ForEach-Object {
2022-05-25 01:35:58 +02:00
$omitArray += $_.Trim()
}
$componentsArray = @()
$components.Split(',') | ForEach-Object {
$componentsArray += $_.Trim()
}
if ($omitArray -NotContains $component -and (($componentsArray -Contains $component) -or ($components -eq '')))
2022-05-25 01:35:58 +02:00
{
$vsProject = "$poco_base\$componentDir\$componentName$($suffix).$($extension)"
2022-05-25 01:35:58 +02:00
if (!(Test-Path -Path $vsProject)) # when VS project name is not same as directory name
{
$vsProject = "$poco_base\$componentDir$($suffix).$($extension)"
2022-05-25 01:35:58 +02:00
if (!(Test-Path -Path $vsProject)) # not found
{
Write-Host "+------------------------------------------------------------------"
Write-Host "| VS project $vsProject not found, skipping."
Write-Host "+------------------------------------------------------------------"
Return # since Foreach-Object is a function, this is actually loop "continue"
}
}
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
Write-Host "| Building $vsProject"
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
if ($type -eq "lib")
{
BuildExecute $tool $vsProject
2022-05-25 01:35:58 +02:00
}
ElseIf ($tests -and ($type -eq "test"))
{
$vsTestProject = "$poco_base\$componentDir\testsuite\TestSuite$($suffix).$($extension)"
2022-05-25 01:35:58 +02:00
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
Write-Host "| Building $vsTestProject"
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
BuildExecute $tool $vsTestProject
2022-05-25 01:35:58 +02:00
if ($component -eq "Foundation") # special case for Foundation, which needs test app and dll
{
$vsTestProject = "$poco_base\$componentDir\testsuite\TestApp$($suffix).$($extension)"
2022-05-25 01:35:58 +02:00
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
Write-Host "| Building $vsTestProject"
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
BuildExecute $tool $vsTestProject
2022-05-25 01:35:58 +02:00
$vsTestProject = "$poco_base\$componentDir\testsuite\TestLibrary$($suffix).$($extension)"
2022-05-25 01:35:58 +02:00
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
Write-Host "| Building $vsTestProject"
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
BuildExecute $tool $vsTestProject -skipStatic
2022-05-25 01:35:58 +02:00
}
elseif ($component -eq "Data") # special case for Data, which needs DataTest lib
{
2024-02-19 09:58:05 +01:00
$vsTestProject = "$poco_base\$componentDir\DataTest\DataTest$($suffix).$($extension)"
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
Write-Host "| Building $vsTestProject"
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
BuildExecute $tool $vsTestProject
}
2022-05-25 01:35:58 +02:00
}
ElseIf ($samples -and ($type -eq "sample"))
{
if ($platform -eq 'x64')
{
Get-Childitem "$poco_base\$($componentDir)" -Recurse |`
Where-Object {$_.Extension -Match $extension -And $_.DirectoryName -Like "*samples*" -And $_.BaseName -Like "*$($suffix)" } `
| BuildSamples "$_"
2022-05-25 01:35:58 +02:00
}
else
{
Get-Childitem "$poco_base\$($componentDir)" -Recurse |`
Where-Object {$_.Extension -Match $extension -And $_.DirectoryName -Like "*samples*" -And $_.BaseName -Like "*$($suffix)" -And $_.BaseName -NotLike "*_x64_*" } `
| BuildSamples "$_"
2022-05-25 01:35:58 +02:00
}
}
}
else
{
Write-Host "-------------------------------"
Write-Host "# Skipping $componentDir"
Write-Host "-------------------------------"
}
}
2014-02-01 09:21:01 +01:00
}
2018-05-28 12:19:50 +02:00
function Build
{
2022-05-25 01:35:58 +02:00
Process-Input
2018-05-28 12:19:50 +02:00
$extension = 'vcxproj'
BuildComponents $extension "lib"
BuildComponents $extension "test"
BuildComponents $extension "sample"
2018-05-28 12:19:50 +02:00
}
2014-02-01 09:21:01 +01:00
Build