pre_build.ps1 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. # Pre-Build script
  6. #
  7. # This script is fairly simple. It checks if it's running
  8. # in a VSO. If it is, it uses the VSO environment variables
  9. # to figure out the commit that triggered the build, and if
  10. # such a commit exists, it saves it's description to the build
  11. # output to make it easy to inspect builds.
  12. #
  13. # Require Environment:
  14. # $Env:TF_BUILD_SOURCEGETVERSION
  15. # $Env:TF_BUILD_DROPLOCATION
  16. #
  17. # Inferable Environment (if not specified, inferred by pre_post_util.ps1):
  18. # $Env:TF_BUILD_SOURCESDIRECTORY (a.k.a. $srcpath)
  19. # $Env:TF_BUILD_BUILDDIRECTORY (a.k.a. $objpath)
  20. # $Env:TF_BUILD_BINARIESDIRECTORY (a.k.a. $binpath)
  21. #
  22. # Optional information (metadata only):
  23. # $Env:TF_BUILD_BUILDDEFINITIONNAME
  24. # $Env:TF_BUILD_BUILDNUMBER
  25. # $Env:TF_BUILD_BUILDURI
  26. param (
  27. [Parameter(Mandatory=$True)]
  28. [ValidateSet("x86", "x64", "arm", "arm64")]
  29. [string]$arch,
  30. [Parameter(Mandatory=$True)]
  31. [ValidateSet("debug", "release", "test", "codecoverage")]
  32. [string]$flavor,
  33. [ValidateSet("default", "codecoverage", "pogo")]
  34. [string]$subtype = "default",
  35. [string]$srcpath = "",
  36. [string]$binpath = "",
  37. [string]$objpath = "",
  38. [string]$logFile = "",
  39. [string]$corePath = "core",
  40. [string]$oauth
  41. )
  42. . $PSScriptRoot\pre_post_util.ps1
  43. $srcpath, $buildRoot, $objpath, $binpath = `
  44. ComputePaths `
  45. -arch $arch -flavor $flavor -subtype $subtype -OuterScriptRoot $PSScriptRoot `
  46. -srcpath $srcpath -buildRoot $buildRoot -objpath $objpath -binpath $binpath
  47. WriteCommonArguments
  48. $buildName = ConstructBuildName $arch $flavor $subtype
  49. if (($logFile -eq "") -and (Test-Path $buildRoot)) {
  50. if (-not(Test-Path -Path "${buildRoot}\logs")) {
  51. $dummy = New-Item -Path "${buildRoot}\logs" -ItemType Directory -Force
  52. }
  53. $logFile = "${buildRoot}\logs\pre_build.${buildName}.log"
  54. if (Test-Path -Path $logFile) {
  55. Remove-Item $logFile -Force
  56. }
  57. }
  58. #
  59. # Create packages.config files
  60. #
  61. $packagesConfigFileText = @"
  62. <?xml version="1.0" encoding="utf-8"?>
  63. <packages>
  64. <package id="MicroBuild.Core" version="0.2.0" targetFramework="native" developmentDependency="true" />
  65. </packages>
  66. "@
  67. $packagesFiles = Get-ChildItem -Path $Env:TF_BUILD_SOURCESDIRECTORY *.vcxproj -Recurse `
  68. | ForEach-Object { Join-Path $_.DirectoryName "packages.config" }
  69. foreach ($file in $packagesFiles) {
  70. if (-not (Test-Path $file)) {
  71. Write-Output $packagesConfigFileText | Out-File $file -Encoding utf8
  72. }
  73. }
  74. #
  75. # Create build metadata
  76. #
  77. if (Test-Path Env:\TF_BUILD_SOURCEGETVERSION)
  78. {
  79. $commitHash = ($Env:TF_BUILD_SOURCEGETVERSION).split(':')[2]
  80. $gitExe = GetGitPath
  81. $CoreHash = ""
  82. if (Test-Path $corePath) {
  83. $CoreHash = Invoke-Expression "$gitExe rev-parse ${commitHash}:core"
  84. if (-not $?) {
  85. $CoreHash = ""
  86. }
  87. }
  88. $outputDir = $Env:TF_BUILD_DROPLOCATION
  89. if (-not(Test-Path -Path $outputDir)) {
  90. $dummy = New-Item -Path $outputDir -ItemType Directory -Force
  91. }
  92. Push-Location $srcpath
  93. $info = GetBuildInfo $oauth $commitHash
  94. $BuildDate = ([DateTime]$info.push.date).toString("yyMMdd-HHmm")
  95. $buildPushId, $buildPushIdPart1, $buildPushIdPart2, $buildPushIdString = GetBuildPushId $info
  96. # commit message
  97. $command = "$gitExe log -1 --name-status -m --first-parent -p $commitHash"
  98. $CommitMessageLines = Invoke-Expression $command
  99. $CommitMessage = $CommitMessageLines -join "`r`n"
  100. $changeTextFile = Join-Path -Path $outputDir -ChildPath "change.txt"
  101. $changeTextFileContent = @"
  102. TF_BUILD_BUILDDEFINITIONNAME = $Env:TF_BUILD_BUILDDEFINITIONNAME
  103. TF_BUILD_BUILDNUMBER = $Env:TF_BUILD_BUILDNUMBER
  104. TF_BUILD_SOURCEGETVERSION = $Env:TF_BUILD_SOURCEGETVERSION
  105. TF_BUILD_BUILDURI = $Env:TF_BUILD_BUILDURI
  106. PushId = $buildPushId $buildPushIdString
  107. PushDate = $buildDate
  108. $CommitMessage
  109. "@
  110. Write-Output "-----"
  111. Write-Output $changeTextFile
  112. Write-Output $changeTextFileContent
  113. Write-Output $changeTextFileContent | Out-File $changeTextFile -Encoding utf8 -Append
  114. Pop-Location
  115. # commit hash
  116. $buildCommit = ($Env:TF_BUILD_SOURCEGETVERSION).SubString(14)
  117. $CommitHash = $buildCommit.Split(":")[1]
  118. $outputJsonFile = Join-Path -Path $outputDir -ChildPath "change.json"
  119. $changeJson = New-Object System.Object
  120. $changeJson | Add-Member -type NoteProperty -name BuildDefinitionName -value $Env:TF_BUILD_BUILDDEFINITIONNAME
  121. $changeJson | Add-Member -type NoteProperty -name BuildNumber -value $Env:TF_BUILD_BUILDNUMBER
  122. $changeJson | Add-Member -type NoteProperty -name BuildUri -value $Env:TF_BUILD_BUILDURI
  123. $changeJson | Add-Member -type NoteProperty -name BuildDate -value $BuildDate
  124. $changeJson | Add-Member -type NoteProperty -name Branch -value $Env:BranchName
  125. $changeJson | Add-Member -type NoteProperty -name CommitHash -value $CommitHash
  126. $changeJson | Add-Member -type NoteProperty -name CoreHash -value $CoreHash
  127. $changeJson | Add-Member -type NoteProperty -name PushId -value $BuildPushId
  128. $changeJson | Add-Member -type NoteProperty -name PushIdPart1 -value $BuildPushIdPart1
  129. $changeJson | Add-Member -type NoteProperty -name PushIdPart2 -value $BuildPushIdPart2
  130. $changeJson | Add-Member -type NoteProperty -name PushIdString -value $BuildPushIdString
  131. $changeJson | Add-Member -type NoteProperty -name Username -value $Env:Username
  132. $changeJson | Add-Member -type NoteProperty -name CommitMessage -value $CommitMessageLines
  133. ## Look for a git note that requests a cirro run.
  134. ## We assume that, when this script is called in the context of a full build, the working directory
  135. ## is the root of the local full checkout.
  136. ## It's a bit of a layering violation to have this logic, which only applies to full builds, in the
  137. ## core pre_build script, but there's not an obviously better place to put it. There is a pre_build
  138. ## script in the full repo, but it doesn't seem to be executed as part of a VSO build, and it's not
  139. ## clear if anyone else uses it.
  140. ##
  141. ## Since we have to handle the case where a full build doesn't have a ChakraHub note anyway, just
  142. ## add the logic to the core script.
  143. ## Without the OAuth token, the git fetch command hangs, causing the entire build to time out. Per
  144. ## https://docs.microsoft.com/en-us/vsts/build-release/concepts/definitions/release/variables?view=vsts&tabs=shell#using-default-variables,
  145. ## we expect to find the auth token in System_AccessToken instead of System.AccessToken.
  146. if (${Env:System_AccessToken})
  147. {
  148. git -c http.extraheader="AUTHORIZATION: bearer ${Env:System_AccessToken}" fetch origin refs/notes/ChakraHub:refs/notes/ChakraHub
  149. Write-Output "looking for notes"
  150. ## One might be tempted to redirect the output of the "git notes" command on the next line to $null. Don't do that;
  151. ## it causes the check to fail even when git notes are present. However, we do have to redirect stderr, because
  152. ## git prints something to stderr if it doesn't find a note, and that would cause VSTS to think that the build failed.
  153. if (git notes --ref=ChakraHub list HEAD 2>&1)
  154. {
  155. Write-Output "found git notes; adding to change.json"
  156. $testConfig = (git notes --ref=ChakraHub show HEAD) -join "`n" | ConvertFrom-Json
  157. $changeJson | Add-Member -type NoteProperty -name TestConfig -value $testConfig
  158. }
  159. else
  160. {
  161. Write-Output "no git notes found"
  162. }
  163. }
  164. else
  165. {
  166. Write-Output "No OAuth token found; skipping check for git note that requests a cirro run"
  167. }
  168. Write-Output "-----"
  169. Write-Output $outputJsonFile
  170. $changeJson | ConvertTo-Json -depth 100 | Write-Output
  171. $changeJson | ConvertTo-Json -depth 100 | Out-File $outputJsonFile -Encoding utf8
  172. $buildInfoOutputDir = $objpath
  173. if (-not(Test-Path -Path $buildInfoOutputDir)) {
  174. $dummy = New-Item -Path $buildInfoOutputDir -ItemType Directory -Force
  175. }
  176. # generate build version prop file
  177. $propsFile = Join-Path -Path $buildInfoOutputDir -ChildPath "Chakra.Generated.BuildInfo.props"
  178. $propsFileTemplate = @"
  179. <?xml version="1.0" encoding="utf-16"?>
  180. <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  181. <PropertyGroup>
  182. <OutBaseDir>{0}</OutBaseDir>
  183. <IntBaseDir>{1}</IntBaseDir>
  184. <ChakraVersionBuildNumber>{2}</ChakraVersionBuildNumber>
  185. <ChakraVersionBuildQFENumber>{3}</ChakraVersionBuildQFENumber>
  186. <ChakraVersionBuildCommit>{4}</ChakraVersionBuildCommit>
  187. <ChakraVersionBuildDate>{5}</ChakraVersionBuildDate>
  188. </PropertyGroup>
  189. </Project>
  190. "@
  191. $propsFileContent = $propsFileTemplate -f $buildRoot, $objpath, $buildPushIdPart1, $buildPushIdPart2, $buildCommit, $buildDate
  192. Write-Output "-----"
  193. Write-Output $propsFile
  194. Write-Output $propsFileContent
  195. Write-Output $propsFileContent | Out-File $propsFile
  196. }
  197. #
  198. # Clean up code analysis summary files in case they get left behind
  199. #
  200. if (($objpath -ne "") -and (Test-Path $objpath)) {
  201. Get-ChildItem $objpath -Include vc.nativecodeanalysis.all.xml -Recurse | Remove-Item -Verbose
  202. }