forked from aws/aws-nitro-enclaves-sdk-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
205 lines (173 loc) · 8.09 KB
/
build.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#
# 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