# # Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0. # param ( [string]$SrcPath = ".", [string]$BuildOutputPath = ".\buildbase", [System.Boolean]$BuildEverything = $false, [string]$BuildConfiguration = "Release" ) <# .SYNOPSIS Builds kmstool_instance.exe for release and debug configurations .DESCRIPTION This script automates cloning dependencies, building them and producing kmstool_instance sample on Windows. .PARAMETER SrcPath Path to nitro-enclaves-sdk source .PARAMETER BuildOutputPath Path to be used for all build artifacts They are organized as follows: $BuildOutputPath\src - Used for cloning dependencies from git $BuildOutputPath\build - Used to stage temporary build files including visual studio projects generated by cmake. $BuildOutputPath\DepInstall - Used to install all the dependencies (headers, libs) $BuildOutputPath\KmsToolInstall\release\bin $BuildOutputPath\KmsToolInstall\debug\bin - Used to install kmstool_instance.exe .PARAMETER BuildEverything Builds all dependencies again when this is set to $true When this is omitted or set to $false, only kmstool_instance folder is compiled generating kmstool_instance.exe from its source. .PARAMETER BuildConfiguration Release configuration is built by default Use Debug to build debug configuration. .INPUTS None. You cannot pipe objects to this script. .EXAMPLE .\build.ps1 .EXAMPLE .\build.ps1 -SrcPath "c:\dev\aws-nitro-enclaves-sdk-c" -BuildOutputPath "c:\builds\aws-nitro-enclaves-sdk-c" -BuildEverything $true .EXAMPLE .\build.ps1 -BuildEverything $true -BuildConfiguration Debug #> Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' # This script depends on visual studio build tools and cmake. These can be installed easily with chocolatey. The instructions are as below: # Install chocolatey from https://docs.chocolatey.org/en-us/choco/setup # To install visual studio c++ tools, cmake and git use the below commands # choco install visualcpp-build-tools -y # choco install cmake -y # choco install git -y Function RunBuild() { <# .SYNOPSIS Builds all modules with given build config. .DESCRIPTION This function is useful to compile the dependencies and the KMStool itself in both release and debug configurations. Main purpose is to abstract the build steps while providing a way to configure all the paths. .PARAMETER Config Build configuration for the current build("Release" or "Debug"). .PARAMETER BuildParameters A custom object that contains all the parameters related to current build. This includes various path prefixes as well as the path of cmake tool itself. .PARAMETER ModuleList A collection of tuples containing(ModuleName, ModuleBranch, ModuleUrl) - Only ModuleName is used in this function. .EXAMPLE RunBuild -Config "Release" -BuildParameters $BuildParameters -ModuleList $ModuleList .EXAMPLE RunBuild -Config "Debug" -BuildParameters $BuildParameters -ModuleList $ModuleList #> param( [Parameter(Mandatory)] [string]$Config, $BuildParameters, [System.Collections.ArrayList]$ModuleList ) $Cmake = $BuildParameters.Cmake $depInstallPath = ("{0}`\{1}" -f $BuildParameters.DepInstallPrefix, $Config) $installPath = ("{0}`\{1}" -f $BuildParameters.InstallPrefix, $Config) if($BuildParameters.BuildEverything){ foreach($Module in $ModuleList){ Write-Output ("compiling: {0}" -f $Module.Item1) $srcPath = ("{0}`\{1}" -f $BuildParameters.ModuleSrcPrefix, $Module.Item1) $bldPath = ("{0}`\{1}`\{2}" -f $BuildParameters.BldPrefix, $Config, $Module.Item1) #Configure project - Note that visual studio does not need build type as it generates project files for all release configurations $cmakeArgs = ("-DCMAKE_PREFIX_PATH={0} -DCMAKE_INSTALL_PREFIX={0} -S {1} -B {2} -A x64" -f $depInstallPath, $srcPath, $bldPath) Invoke-Expression "& $Cmake $cmakeArgs" #Build $cmakeArgs = ("--build {0} --target install --config {1}" -f $bldPath, $Config) Invoke-Expression "& $Cmake $cmakeArgs" } } $srcPath = $BuildParameters.PrimarySrcPath $bldPath = ("{0}`\{1}`\KmsToolInstance" -f $BuildParameters.BldPrefix, $Config) $cmakeArgs = ("-DCMAKE_PREFIX_PATH={0} -DCMAKE_INSTALL_PREFIX={1} -S {2} -B {3} -A x64" -f $depInstallPath, $installPath, $srcPath, $bldPath) Invoke-Expression "& $Cmake $cmakeArgs" $cmakeArgs = ("--build {0} --target install --config {1}" -f $bldPath, $Config) Invoke-Expression "& $Cmake $cmakeArgs" } $ScratchFolder = ("{0}`\buildbase" -f (Get-Location).Path) if (Test-Path -Type Container $SrcPath){ $PrimarySrcPath = $(Resolve-Path -Path $SrcPath).Path; } else { Write-Output("Unable to access source path") Exit } if (Test-Path -Type Container $BuildOutputPath){ $ScratchFolder = $(Resolve-Path -Path $BuildOutputPath).Path; } else { Write-Output("Unable to access build output path - creating new folder") New-Item -Type Container $BuildOutputPath $ScratchFolder = $(Resolve-Path -Path $BuildOutputPath).Path; } $ModuleList = New-Object System.Collections.ArrayList # ModuleName, Branch, URL # Throw away the index returned as we are not using it. $index = $ModuleList.Add([Tuple]::Create('aws-c-common','v0.4.59','https://github.com/awslabs/aws-c-common.git')) $index = $ModuleList.Add([Tuple]::Create('aws-c-cal','v0.3.3','https://github.com/awslabs/aws-c-cal.git')) $index = $ModuleList.Add([Tuple]::Create('aws-c-io','v0.7.0','https://github.com/awslabs/aws-c-io.git')) $index = $ModuleList.Add([Tuple]::Create('aws-c-compression','v0.2.10','https://github.com/awslabs/aws-c-compression.git')) $index = $ModuleList.Add([Tuple]::Create('aws-c-http','v0.5.17','https://github.com/awslabs/aws-c-http.git')) $index = $ModuleList.Add([Tuple]::Create('aws-c-auth','v0.4.6','https://github.com/awslabs/aws-c-auth.git')) $index = $ModuleList.Add([Tuple]::Create('json-c','json-c-0.15-20200726','https://github.com/json-c/json-c.git')) $CmakePath = ""; try{ $CmakePath = $(Get-Command cmake).Source; } catch{ $defaultCmakePath = "C:\Program Files\Cmake\bin\cmake.exe"; # If cmake path was not resolved, try default install path from choclatey if (Test-Path -Type Leaf $defaultCmakePath) { Write-Output ("Cmake not found in path - using default install path") $CmakePath = $defaultCmakePath } else { Write-Output ("Cmake not found") Exit } } $BuildParameters = New-Object PSObject -Property @{ DepInstallPrefix = ("{0}`\DepInstall" -f $ScratchFolder); InstallPrefix = ("{0}`\KmstoolInstall" -f $ScratchFolder); ModuleSrcPrefix = ("{0}`\src" -f $ScratchFolder); BldPrefix = ("{0}`\build" -f $ScratchFolder); Cmake = ("'{0}'" -f $CmakePath); PrimarySrcPath = $PrimarySrcPath; BuildEverything = $BuildEverything; } Write-Output "Build Parameters $BuildParameters" # Create module source folder for cloning dependencies - git cannot create leading path. if(!(Test-Path -Type Container -Path $BuildParameters.ModuleSrcPrefix)){ New-Item $BuildParameters.ModuleSrcPrefix -ItemType Directory } $OriginalLocation = $(Get-Location).Path Set-Location -Path $BuildParameters.ModuleSrcPrefix foreach($Module in $ModuleList){ if(!(Test-Path -Type Container $Module.Item1)){ Write-Output ("Cloning:[{0}] - Branch:[{1}] from [{2}]" -f $Module.Item1, $Module.Item2, $Module.Item3) #clone if($Module.Item2){ git clone -b $Module.Item2 $Module.Item3 } else { git clone $Module.Item3 } } } Set-Location -Path $BuildParameters.PrimarySrcPath if ($BuildConfiguration -eq "Release") { RunBuild -Config "Release" -BuildParameters $BuildParameters -ModuleList $ModuleList } elseif ($BuildConfiguration -eq "Debug") { RunBuild -Config "Debug" -BuildParameters $BuildParameters -ModuleList $ModuleList } else { Write-Output("BuildConfiguration set to unsupported value: {0}" -f $BuildConfiguration) } Set-Location -Path $OriginalLocation