init_build.ps1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #-------------------------------------------------------------------------------------------------------
  2. # Copyright (C) Microsoft. All rights reserved.
  3. # Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. #-------------------------------------------------------------------------------------------------------
  5. # Init Build Script
  6. #
  7. # Run this as the very first step in the build to configure the environment.
  8. # This is distinct from the Pre-Build script as there may be more non-script steps that need to be
  9. # taken before setting up and running the build.
  10. # For example, this script creates a cmd script which should be run to initialize environment variables
  11. # before running the Pre-Build script.
  12. param (
  13. [ValidateSet("x86", "x64", "arm", "")]
  14. [string]$arch = "",
  15. [ValidateSet("debug", "release", "test", "codecoverage", "")]
  16. [string]$flavor = "",
  17. [ValidateSet("default", "codecoverage", "pogo")]
  18. [string]$subtype = "default",
  19. [string]$buildtype,
  20. [string]$envConfigScript = "ComputedEnvironment.cmd",
  21. [string[]]$supportedPogoBuildTypes = @("x64_release", "x86_release"),
  22. [string]$verMajor = "",
  23. [string]$verMinor = "",
  24. [string]$verPatch = "",
  25. [string]$verSecurity = "",
  26. [string]$dropRoot,
  27. [switch]$cleanBinDir,
  28. [string]$oauth
  29. )
  30. . $PSScriptRoot\pre_post_util.ps1
  31. #
  32. # Process build type parameters
  33. #
  34. # Define values for variables based on parameters and environment variables
  35. # with default values in case the environment variables are not defined.
  36. $BuildType = UseValueOrDefault $buildtype $Env:BuildType
  37. $BuildPlatform = UseValueOrDefault $arch $Env:BuildPlatform
  38. $BuildConfiguration = UseValueOrDefault $flavor $Env:BuildConfiguration
  39. $BuildSubtype = UseValueOrDefault $subtype $Env:BuildSubtype
  40. # If $BuildType is specified, extract BuildPlatform and BuildConfiguration
  41. # Otherwise, if $BuildPlatform and $BuildConfiguration are specified, construct $BuildType
  42. # $BuildSubtype will remain as "default" if not already specified, or become e.g. "pogo", "codecoverage"
  43. if ($BuildType) {
  44. $buildTypeSegments = $BuildType.split("_")
  45. $BuildPlatform = $buildTypeSegments[0]
  46. $BuildConfiguration = $buildTypeSegments[1]
  47. if ($buildTypeSegments[2]) {
  48. # overwrite with new value if it exists, otherwise keep as "default"
  49. $BuildSubtype = $buildTypeSegments[2]
  50. }
  51. if ($BuildConfiguration -eq "codecoverage") {
  52. $BuildConfiguration = "test" # codecoverage builds are actually "test" configuration
  53. $BuildSubtype = "codecoverage" # keep information about codecoverage in the subtype
  54. }
  55. if (-not ($BuildSubtype -in @("default", "pogo", "codecoverage"))) {
  56. Write-Error "Unsupported BuildSubtype: $BuildSubtype"
  57. }
  58. } elseif ($BuildPlatform -and $BuildConfiguration) {
  59. $BuildType = "${BuildPlatform}_${BuildConfiguration}"
  60. } else {
  61. Write-Error (@"
  62. Not enough information about BuildType:
  63. BuildType={0}
  64. BuildPlatform={1}
  65. BuildConfiguration={2}
  66. BuildSubtype={3}
  67. "@ -f $BuildType, $BuildPlatform, $BuildConfiguration, $BuildSubtype)
  68. exit 1
  69. }
  70. $gitExe = GetGitPath
  71. $CommitHash = UseValueOrDefault $Env:BUILD_SOURCEVERSION $(Invoke-Expression "${gitExe} rev-parse HEAD")
  72. $branchFullName = UseValueOrDefault $Env:BUILD_SOURCEBRANCH $(Invoke-Expression "${gitExe} rev-parse --symbolic-full-name HEAD")
  73. $SourcesDirectory = UseValueOrDefault $Env:BUILD_SOURCESDIRECTORY $(GetRepoRoot)
  74. $BinariesDirectory = UseValueOrDefault (Join-Path $SourcesDirectory "Build\VcBuild")
  75. $ObjectDirectory = Join-Path $BinariesDirectory "obj\${BuildPlatform}_${BuildConfiguration}"
  76. $DropRoot = UseValueOrDefault $dropRoot $Env:DROP_ROOT (Join-Path $(GetRepoRoot) "_DROP")
  77. $BuildName = ConstructBuildName -arch $BuildPlatform -flavor $BuildConfiguration -subtype $BuildSubtype
  78. $BranchName = $branchFullName.split('/',3)[2]
  79. $BranchPath = $BranchName.replace('/','\')
  80. if (-not $CommitHash) {
  81. $CommitHash = Invoke-Expression "${gitExe} rev-parse HEAD"
  82. }
  83. $CommitShortHash = $(Invoke-Expression "${gitExe} rev-parse --short $CommitHash")
  84. $Username = (Invoke-Expression "${gitExe} log $CommitHash -1 --pretty=%ae").split('@')[0]
  85. $CommitDateTime = [DateTime]$(Invoke-Expression "${gitExe} log $CommitHash -1 --pretty=%aD")
  86. $CommitTime = Get-Date $CommitDateTime -Format yyMMdd.HHmm
  87. #
  88. # Get Build Info
  89. #
  90. $buildPushDate = $null
  91. $buildPushIdString = $null
  92. if (-not $oauth)
  93. {
  94. $buildPushIdPart1 = 65535
  95. $buildPushIdPart2 = 65535
  96. $buildPushIdString = "65535.65535"
  97. $buildPushDate = [DateTime]$CommitDateTime
  98. }
  99. else
  100. {
  101. $info = GetBuildInfo $oauth $CommitHash
  102. $_, $buildPushIdPart1, $buildPushIdPart2, $buildPushIdString = GetBuildPushId $info
  103. $buildPushDate = [DateTime]$info.push.date
  104. }
  105. $PushDate = Get-Date $buildPushDate -Format yyMMdd.HHmm
  106. $VersionMajor = UseValueOrDefault $verMajor $Env:VERSION_MAJOR (GetVersionField "CHAKRA_CORE_MAJOR_VERSION") "0"
  107. $VersionMinor = UseValueOrDefault $verMinor $Env:VERSION_MINOR (GetVersionField "CHAKRA_CORE_MINOR_VERSION") "0"
  108. $VersionPatch = UseValueOrDefault $verPatch $Env:VERSION_PATCH (GetVersionField "CHAKRA_CORE_PATCH_VERSION") "0"
  109. $VersionSecurity = UseValueOrDefault $verSecurity $Env:VERSION_QFE (GetVersionField "CHAKRA_CORE_VERSION_RELEASE_QFE") "0"
  110. $VersionString = "${VersionMajor}.${VersionMinor}.${VersionPatch}" # Only use MAJOR.MINOR.PATCH to align with SemVer
  111. $buildVersionString = "{0}-{1}" -f $buildPushIdPart1.ToString("00000"), $buildPushIdPart2.ToString("00000")
  112. $PreviewVersionString = "${VersionString}-preview-${buildVersionString}"
  113. $ShortBranch = "commit"
  114. if ($BranchName -eq "master") {
  115. $ShortBranch = "master"
  116. } elseif ($BranchName.StartsWith("release")) {
  117. $ShortBranch = $BranchName.replace("release/","")
  118. }
  119. # unless it is a build branch, subdivide the output directory by month
  120. if ($BranchPath.StartsWith("build")) {
  121. $YearAndMonth = ""
  122. } else {
  123. $YearAndMonth = (Get-Date $buildPushDate -Format yyMM) + "\"
  124. }
  125. $BuildIdentifier = "${buildPushIdString}_${PushDate}_${Username}_${CommitHash}"
  126. $ComputedDropPathSegment = "${BranchPath}\${YearAndMonth}${BuildIdentifier}"
  127. $ObjectDirectory = "${BinariesDirectory}\obj\${BuildPlatform}_${BuildConfiguration}"
  128. # Create a sentinel file for each build flavor to track whether the build is complete.
  129. # * ${arch}_${flavor}.incomplete # will be deleted when the build of this flavor completes
  130. $buildIncompleteFileContentsString = @"
  131. {0} is incomplete.
  132. This could mean that the build is in progress, or that it was unable to run to completion.
  133. The contents of this directory should not be relied on until the build completes.
  134. "@
  135. $DropPath = Join-Path $DropRoot $ComputedDropPathSegment
  136. New-Item -ItemType Directory -Force -Path $DropPath
  137. New-Item -ItemType Directory -Force -Path (Join-Path $SourcesDirectory "test\logs")
  138. New-Item -ItemType Directory -Force -Path (Join-Path $BinariesDirectory "buildlogs")
  139. New-Item -ItemType Directory -Force -Path (Join-Path $BinariesDirectory "logs")
  140. $FlavorBuildIncompleteFile = Join-Path $DropPath "${BuildType}.incomplete"
  141. if (-not (Test-Path $FlavorBuildIncompleteFile)) {
  142. ($buildIncompleteFileContentsString -f "Build of ${BuildType}") `
  143. | Out-File $FlavorBuildIncompleteFile -Encoding utf8
  144. }
  145. $PogoConfig = $supportedPogoBuildTypes -contains "${BuildPlatform}_${BuildConfiguration}"
  146. # Write the $envConfigScript
  147. @"
  148. set BranchName=${BranchName}
  149. set ShortBranch=${ShortBranch}
  150. set BranchPath=${BranchPath}
  151. set YearAndMonth=${YearAndMonth}
  152. set BuildIdentifier=${BuildIdentifier}
  153. set VersionMajor=${VersionMajor}
  154. set VersionMinor=${VersionMinor}
  155. set VersionPatch=${VersionPatch}
  156. set VersionSecurity=${VersionSecurity}
  157. set BuildPushIdString=${buildPushIdString}
  158. set VersionString=${VersionString}
  159. set PreviewVersionString=${PreviewVersionString}
  160. set PushDate=${PushDate}
  161. set CommitTime=${CommitTime}
  162. set Username=${Username}
  163. set CommitHash=${CommitHash}
  164. set CommitShortHash=${CommitShortHash}
  165. set ComputedDropPathSegment=${ComputedDropPathSegment}
  166. set BinariesDirectory=${BinariesDirectory}
  167. set DropPath=${DropPath}
  168. set BuildType=${BuildType}
  169. set BuildPlatform=${BuildPlatform}
  170. set BuildConfiguration=${BuildConfiguration}
  171. set BuildSubtype=${BuildSubtype}
  172. set BuildName=${BuildName}
  173. set FlavorBuildIncompleteFile=${FlavorBuildIncompleteFile}
  174. set PogoConfig=${PogoConfig}
  175. "@ `
  176. | Out-File $envConfigScript -Encoding ASCII
  177. # Use the VSTS environment vars to construct a backwards-compatible VSO build environment
  178. # for the sake of reusing the pre-build and post-build scripts as they are.
  179. @"
  180. set TF_BUILD_SOURCEGETVERSION=LG:${branchFullName}:${CommitHash}
  181. set TF_BUILD_DROPLOCATION=${BinariesDirectory}
  182. set TF_BUILD_SOURCESDIRECTORY=${SourcesDirectory}
  183. set TF_BUILD_BUILDDIRECTORY=${ObjectDirectory}
  184. set TF_BUILD_BINARIESDIRECTORY=${BinariesDirectory}
  185. REM The following variables are only used for logging build metadata.
  186. set TF_BUILD_BUILDDEFINITIONNAME=${Env:BUILD_DEFINITIONNAME}
  187. set TF_BUILD_BUILDNUMBER=${Env:BUILD_BUILDNUMBER}
  188. set TF_BUILD_BUILDURI=${Env:BUILD_BUILDURI}
  189. "@ `
  190. | Out-File $envConfigScript -Encoding ASCII -Append
  191. # Print contents of $envConfigScript as a sanity check
  192. Write-Output ""
  193. Get-Content $envConfigScript | Write-Output
  194. Write-Output ""
  195. # Export VSO variables that can be consumed by other VSO tasks where the task
  196. # definition in VSO itself needs to know the value of the variable.
  197. # If the task definition itself doesn't need to know the value of the variables,
  198. # the variables that are added to the environment via the script generated above
  199. # will be interpolated when the tasks run the associated command line with the
  200. # given parameters.
  201. #
  202. # For example, for a Publish Artifacts task, VSO itself needs to know
  203. # the value of DropPath in order to construct links to the artifacts correctly.
  204. # Thus, we export a variable called VSO_DropPath (VSO_ prefix by convention)
  205. # that the VSO build definition, not just the command environment, will know about.
  206. #
  207. # Uses command syntax documented here:
  208. # https://github.com/Microsoft/vso-agent-tasks/blob/master/docs/authoring/commands.md
  209. # Lines written to stdout that match this pattern are interpreted with this command syntax.
  210. Write-Output "Setting VSO variable VSO_DropPath = ${DropPath}"
  211. Write-Output "##vso[task.setvariable variable=VSO_DropPath;]${DropPath}"
  212. Write-Output "Setting VSO variable VSO_VersionString = ${VersionString}"
  213. Write-Output "##vso[task.setvariable variable=VSO_VersionString;]${VersionString}"
  214. #
  215. # Optionally ($cleanBinDir): clean up files that might have been left behind from a previous build.
  216. #
  217. if ($BinariesDirectory -and (Test-Path "$BinariesDirectory") -and $cleanBinDir)
  218. {
  219. Remove-Item -Verbose "${BinariesDirectory}\*" -Recurse
  220. }