run_build.ps1 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. # Use this script to run a build for the given BuildType (arch, flavor, subtype)
  6. param (
  7. [ValidateSet("x86", "x64", "arm")]
  8. [Parameter(Mandatory=$True)]
  9. [string]$arch,
  10. # We do not use ValidateSet here because this $flavor param is used to name the BuildConfuration
  11. # from the solution file. MsBuild will determine whether it is valid.
  12. [Parameter(Mandatory=$True)]
  13. [string]$flavor,
  14. [ValidateSet("default", "codecoverage", "pogo")]
  15. [string]$subtype = "default",
  16. [Parameter(Mandatory=$True)]
  17. [string]$solutionFile,
  18. [switch]$clean,
  19. [string]$buildRoot = "", # will be inferred if not provided
  20. [string]$buildlogsSubdir = "buildlogs",
  21. # assume NuGet is on the path, otherwise the caller must specify an explicit path
  22. [string]$nugetExe = "NuGet.exe",
  23. [string]$logFile = "",
  24. #
  25. # Skip flags
  26. #
  27. [switch]$skipPogo, # or set $Env:SKIP_POGO before invoking build
  28. #
  29. # POGO training parameters
  30. #
  31. [string[]]$scenarios = @(),
  32. [string]$binpath, # will be inferred if not provided
  33. [string]$binaryName = "ch.exe"
  34. )
  35. . $PSScriptRoot\pre_post_util.ps1
  36. . $PSScriptRoot\locate_msbuild.ps1
  37. # TODO (doilij) update all scripts that use pre_post_util.ps1 and rely on ComputeProjectPaths
  38. $_, $buildRoot, $_, $binpath = `
  39. ComputePaths `
  40. -arch $arch -flavor $flavor -subtype $subtype -OuterScriptRoot $PSScriptRoot `
  41. -buildRoot $buildRoot -binpath $binpath
  42. $buildName = ConstructBuildName -arch $arch -flavor $flavor -subtype $subtype
  43. $buildlogsPath = Join-Path $buildRoot $buildlogsSubdir
  44. $logFile = UseValueOrDefault $logFile (Join-Path $buildRoot "logs\run_build.${buildName}.log")
  45. # Clear the log file
  46. if (($logFile -ne "") -and (Test-Path $logFile)) {
  47. Remove-Item $logFile -Force
  48. }
  49. #
  50. # NuGet restore
  51. #
  52. # To support both local builds where NuGet's location is known,
  53. # and VSO builds where the location is not known and which use a NuGet restore task instead:
  54. # Run `nuget restore` IFF $nugetExe exists.
  55. if (Get-Command $nugetExe -ErrorAction SilentlyContinue) {
  56. ExecuteCommand "& $nugetExe restore $solutionFile -NonInteractive"
  57. }
  58. #
  59. # Setup
  60. #
  61. $msbuildExe = Locate-MSBuild
  62. if (-not $msbuildExe) {
  63. WriteErrorMessage "Could not find msbuild.exe -- exiting..."
  64. exit 1
  65. }
  66. $skipPogo = $skipPogo -or (Test-Path Env:\SKIP_POGO)
  67. # If $binpath is not set, then infer it. A missing path is okay because it will be created.
  68. if (-not $binpath) {
  69. $binpath = Join-Path $buildRoot "bin\${buildName}"
  70. }
  71. $defaultParams = "$solutionFile /nologo /m /nr:false /p:platform=`"${arch}`" /p:configuration=`"${flavor}`""
  72. $loggingParams = @(
  73. "/fl1 `"/flp1:logfile=${buildlogsPath}\build.${buildName}.log;verbosity=normal`"",
  74. "/fl2 `"/flp2:logfile=${buildlogsPath}\build.${buildName}.err;errorsonly`"",
  75. "/fl3 `"/flp3:logfile=${buildlogsPath}\build.${buildName}.wrn;warningsonly`"",
  76. "/verbosity:normal"
  77. ) -join " "
  78. $targets = ""
  79. if ($clean) {
  80. $targets += "`"/t:Clean,Rebuild`""
  81. }
  82. #
  83. # Build
  84. #
  85. function Build($targets="", $extraParams) {
  86. $buildCommand = "& `"$msbuildExe`" $targets $defaultParams $loggingParams $extraParams"
  87. ExecuteCommand "$buildCommand"
  88. if ($global:LastExitCode -ne 0) {
  89. WriteErrorMessage "Failed msbuild command:`n$buildCommand`n"
  90. WriteErrorMessage "Build failed. Exiting..."
  91. exit 1
  92. }
  93. }
  94. if ($subtype -eq "pogo") {
  95. if ($scenarios.Length -eq 0) {
  96. WriteMessage "No training scenarios selected for pogo build. Please specify training scenarios using the -scenarios parameter."
  97. exit 1
  98. }
  99. Build -extraParams "`"/p:POGO_TYPE=PGI`"" -targets "$targets"
  100. if (-not $skipPogo) {
  101. $scenariosParamValue = $scenarios -join ','
  102. $binary = Join-Path $binpath $binaryName
  103. if (($binary -ne "") -and (Test-Path $binary)) {
  104. $pogoTrainingCommand = "& `"${PSScriptRoot}\pgo\pogo_training.ps1`" -arch $arch -flavor $flavor -subtype $subtype -binary $binary -scenarios $scenariosParamValue"
  105. ExecuteCommand "$pogoTrainingCommand"
  106. } else {
  107. WriteMessage "Binary not found at `"$binary`". Exiting..."
  108. exit 1
  109. }
  110. Build -extraParams "`"/p:POGO_TYPE=PGO`""
  111. }
  112. } else {
  113. $subtypeParams = ""
  114. if ($subtype -eq "codecoverage") {
  115. $subtypeParams = "/p:ENABLE_CODECOVERAGE=true"
  116. }
  117. Build -extraParams $subTypeParams -targets $targets
  118. }
  119. #
  120. # Clean up
  121. #
  122. if (("$binpath" -ne "") -and (Test-Path $binpath)) {
  123. # remove *.pgc, *.pgd, and pgort*
  124. Get-ChildItem -Recurse -Path $binpath "*" `
  125. | Where-Object { $_.Name -match "(.*\.pg[cd]|pgort.*)" } `
  126. | ForEach-Object { Remove-Item -Force $_.FullName }
  127. }
  128. exit $global:lastexitcode