build.sh 8.2 KB
Newer Older
T
Tomáš Matoušek 已提交
1 2 3 4
#!/usr/bin/env bash
# Copyright (c) .NET Foundation and contributors. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.

T
Tomáš Matoušek 已提交
5
# Stop script if unbound variable found (use ${var:-} if intentional)
T
Tomáš Matoušek 已提交
6 7
set -u

J
Jared Parsons 已提交
8 9 10
# Stop script if subcommand fails
set -e 

T
Tomáš Matoušek 已提交
11 12
usage()
{
T
Tomáš Matoušek 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  echo "Common settings:"
  echo "  --configuration <value>    Build configuration: 'Debug' or 'Release' (short: -c)"
  echo "  --verbosity <value>        Msbuild verbosity: q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic] (short: -v)"
  echo "  --binaryLog                Create MSBuild binary log (short: -bl)"
  echo ""
  echo "Actions:"
  echo "  --restore                  Restore projects required to build (short: -r)"
  echo "  --build                    Build all projects (short: -b)"
  echo "  --rebuild                  Rebuild all projects"
  echo "  --pack                     Build nuget packages"
  echo "  --publish                  Publish build artifacts"
  echo "  --help                     Print help and exit"
  echo ""
  echo "Test actions:"     
  echo "  --testCoreClr              Run unit tests on .NET Core (short: --test, -t)"
  echo "  --testMono                 Run unit tests on Mono"
F
Fredric Silberberg 已提交
29
  echo "  --testIOperation           Run unit tests with the IOperation test hook"
T
Tomáš Matoušek 已提交
30 31 32 33 34
  echo ""
  echo "Advanced settings:"
  echo "  --ci                       Building in CI"
  echo "  --docker                   Run in a docker container if applicable"
  echo "  --bootstrap                Build using a bootstrap compilers"
35
  echo "  --runAnalyzers             Run analyzers during build operations"
T
Tomáš Matoušek 已提交
36
  echo "  --prepareMachine           Prepare machine for CI run, clean up processes after build"
37
  echo "  --warnAsError              Treat all warnings as errors"
38
  echo "  --sourceBuild              Simulate building for source-build"
T
Tomáš Matoušek 已提交
39 40
  echo ""
  echo "Command line arguments starting with '/p:' are passed through to MSBuild."
T
Tomáš Matoušek 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
}

source="${BASH_SOURCE[0]}"

# resolve $source until the file is no longer a symlink
while [[ -h "$source" ]]; do
  scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
  source="$(readlink "$source")"
  # if $source was a relative symlink, we need to resolve it relative to the path where the
  # symlink file was located
  [[ $source != /* ]] && source="$scriptroot/$source"
done
scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"

restore=false
build=false
T
Tomáš Matoušek 已提交
57
rebuild=false
T
Tomáš Matoušek 已提交
58
pack=false
T
Tomáš Matoušek 已提交
59 60 61
publish=false
test_core_clr=false
test_mono=false
F
Fredric Silberberg 已提交
62
test_ioperation=false
T
Tomáš Matoušek 已提交
63 64 65 66

configuration="Debug"
verbosity='minimal'
binary_log=false
T
Tomáš Matoušek 已提交
67
ci=false
T
Tomáš Matoušek 已提交
68
bootstrap=false
M
Manish Vasani 已提交
69
run_analyzers=false
T
Tomáš Matoušek 已提交
70
prepare_machine=false
71
warn_as_error=false
T
Tomáš Matoušek 已提交
72
properties=""
73
disable_parallel_restore=false
74
source_build=false
T
Tomáš Matoušek 已提交
75

T
Tomáš Matoušek 已提交
76 77
docker=false
args=""
T
Tomáš Matoušek 已提交
78 79 80

if [[ $# = 0 ]]
then
T
Tomáš Matoušek 已提交
81 82
  usage
  exit 1
T
Tomáš Matoušek 已提交
83 84
fi

T
Tomáš Matoušek 已提交
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
while [[ $# > 0 ]]; do
  opt="$(echo "$1" | awk '{print tolower($0)}')"
  case "$opt" in
    --help|-h)
      usage
      exit 0
      ;;
    --configuration|-c)
      configuration=$2
      args="$args $1"
      shift
      ;;
    --verbosity|-v)
      verbosity=$2
      args="$args $1"
      shift
      ;;
    --binarylog|-bl)
      binary_log=true
      ;;
    --restore|-r)
      restore=true
      ;;
    --build|-b)
      build=true
      ;;
    --rebuild)
      rebuild=true
      ;;
    --pack)
      pack=true
      ;;
    --publish)
      publish=true
      ;;
    --testcoreclr|--test|-t)
      test_core_clr=true
      ;;
    --testmono)
      test_mono=true
      ;;
F
Fredric Silberberg 已提交
126 127 128
    --testioperation)
      test_ioperation=true
      ;;
T
Tomáš Matoušek 已提交
129 130 131 132 133
    --ci)
      ci=true
      ;;
    --bootstrap)
      bootstrap=true
A
Andy Gocke 已提交
134 135
      # Bootstrap requires restore
      restore=true
T
Tomáš Matoušek 已提交
136
      ;;
137
    --runanalyzers)
M
Manish Vasani 已提交
138
      run_analyzers=true
T
Tomáš Matoušek 已提交
139 140 141 142
      ;;
    --preparemachine)
      prepare_machine=true
      ;;
143 144 145
    --warnaserror)
      warn_as_error=true
      ;;
T
Tomáš Matoušek 已提交
146 147 148 149 150
    --docker)
      docker=true
      shift
      continue
      ;;
151 152 153
    --sourcebuild)
      source_build=true
      ;;
T
Tomáš Matoušek 已提交
154 155 156 157 158 159 160 161 162 163 164
    /p:*)
      properties="$properties $1"
      ;;
    *)
      echo "Invalid argument: $1"
      usage
      exit 1
      ;;
  esac
  args="$args $1"
  shift
T
Tomáš Matoušek 已提交
165 166
done

167
if [[ "$docker" == true ]]
T
Tomáš Matoušek 已提交
168
then
T
Tomáš Matoušek 已提交
169 170 171
  echo "Docker exec: $args"

  # Run this script with the same arguments (except for --docker) in a container that has Mono installed.
T
Tomáš Matoušek 已提交
172
  BUILD_COMMAND=/opt/code/eng/build.sh "$scriptroot"/docker/mono.sh $args
173 174 175 176 177 178
  lastexitcode=$?
  if [[ $lastexitcode != 0 ]]; then
    echo "Docker build failed (exit code '$lastexitcode')." >&2
    exit $lastexitcode
  fi

T
Tomáš Matoušek 已提交
179 180 181 182 183 184 185
  # Ensure that all docker containers are stopped.
  # Hence exit with true even if "kill" failed as it will fail if they stopped gracefully
  if [[ "$prepare_machine" == true ]]; then
    docker kill $(docker ps -q) || true
  fi

  exit
T
Tomáš Matoušek 已提交
186 187
fi

188
# Import Arcade functions
T
Tomáš Matoušek 已提交
189
. "$scriptroot/common/tools.sh"
190

T
Tomáš Matoušek 已提交
191 192
function MakeBootstrapBuild {
  echo "Building bootstrap compiler"
193

T
Tomáš Matoušek 已提交
194
  local dir="$artifacts_dir/Bootstrap"
T
Tomáš Matoušek 已提交
195

T
Tomáš Matoušek 已提交
196 197
  rm -rf $dir
  mkdir -p $dir
T
Tomáš Matoušek 已提交
198

J
Jared Parsons 已提交
199
  local package_name="Microsoft.Net.Compilers.Toolset"
T
Tomáš Matoušek 已提交
200
  local project_path=src/NuGet/$package_name/$package_name.Package.csproj
T
Tomáš Matoušek 已提交
201

J
Jared Parsons 已提交
202
  dotnet pack -nologo "$project_path" -p:ContinuousIntegrationBuild=$ci -p:DotNetUseShippingVersions=true -p:InitialDefineConstants=BOOTSTRAP -p:PackageOutputPath="$dir" -bl:"$log_dir/Bootstrap.binlog"
T
Tomáš Matoušek 已提交
203 204
  unzip "$dir/$package_name.*.nupkg" -d "$dir"
  chmod -R 755 "$dir"
T
Tomáš Matoušek 已提交
205

T
Tomáš Matoušek 已提交
206 207
  echo "Cleaning Bootstrap compiler artifacts"
  dotnet clean "$project_path"
T
Tomáš Matoušek 已提交
208

T
Tomáš Matoušek 已提交
209 210 211
  if [[ "$node_reuse" == true ]]; then
    dotnet build-server shutdown
  fi
T
Tomáš Matoušek 已提交
212

T
Tomáš Matoušek 已提交
213 214 215
  # return value
  _MakeBootstrapBuild=$dir
}
T
Tomáš Matoušek 已提交
216

T
Tomáš Matoušek 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
function BuildSolution {
  local solution="Compilers.sln"
  echo "$solution:"

  InitializeToolset
  local toolset_build_proj=$_InitializeToolset
  
  local bl=""
  if [[ "$binary_log" = true ]]; then
    bl="/bl:\"$log_dir/Build.binlog\""
  fi
  
  local projects="$repo_root/$solution" 
  
  # https://github.com/dotnet/roslyn/issues/23736
  UNAME="$(uname)"
  if [[ "$UNAME" == "Darwin" ]]; then
M
Manish Vasani 已提交
234
    run_analyzers=false
T
Tomáš Matoušek 已提交
235 236
  fi

237 238 239
  # NuGet often exceeds the limit of open files on Mac and Linux
  # https://github.com/NuGet/Home/issues/2163
  if [[ "$UNAME" == "Darwin" || "$UNAME" == "Linux" ]]; then
240
    disable_parallel_restore=true
241 242
  fi

F
Fredric Silberberg 已提交
243 244 245 246 247 248 249 250
  if [[ "$test_ioperation" == true ]]; then
    export ROSLYN_TEST_IOPERATION="true"

    if [[ "$test_mono" != true && "$test_core_clr" != true ]]; then
      test_core_clr=true
    fi
  fi

T
Tomáš Matoušek 已提交
251 252 253
  local test=false
  local test_runtime=""
  local mono_tool=""
254
  local test_runtime_args=""
T
Tomáš Matoušek 已提交
255
  if [[ "$test_mono" == true ]]; then
256 257
    mono_path=`command -v mono`
    # Echo out the mono version to the command line so it's visible in CI logs. It's not fixed
T
Tomáš Matoušek 已提交
258 259 260 261
    # as we're using a feed vs. a hard coded package.
    if [[ "$ci" == true ]]; then
      mono --version
    fi
T
Tomáš Matoušek 已提交
262

T
Tomáš Matoušek 已提交
263 264 265
    test=true
    test_runtime="/p:TestRuntime=Mono"
    mono_tool="/p:MonoTool=\"$mono_path\""
266
    test_runtime_args="--debug"
T
Tomáš Matoušek 已提交
267 268
  elif [[ "$test_core_clr" == true ]]; then
    test=true
J
Jared Parsons 已提交
269
    test_runtime="/p:TestRuntime=Core /p:TestTargetFrameworks=net5.0%3Bnetcoreapp3.1"
T
Tomáš Matoušek 已提交
270 271 272
    mono_tool=""
  fi

273 274 275 276
  # Setting /p:TreatWarningsAsErrors=true is a workaround for https://github.com/Microsoft/msbuild/issues/3062.
  # We don't pass /warnaserror to msbuild (warn_as_error is set to false by default above), but set 
  # /p:TreatWarningsAsErrors=true so that compiler reported warnings, other than IDE0055 are treated as errors. 
  # Warnings reported from other msbuild tasks are not treated as errors for now.
T
Tomáš Matoušek 已提交
277 278 279 280 281 282 283 284 285 286 287
  MSBuild $toolset_build_proj \
    $bl \
    /p:Configuration=$configuration \
    /p:Projects="$projects" \
    /p:RepoRoot="$repo_root" \
    /p:Restore=$restore \
    /p:Build=$build \
    /p:Rebuild=$rebuild \
    /p:Test=$test \
    /p:Pack=$pack \
    /p:Publish=$publish \
M
Manish Vasani 已提交
288
    /p:UseRoslynAnalyzers=$run_analyzers \
T
Tomáš Matoušek 已提交
289 290
    /p:BootstrapBuildPath="$bootstrap_dir" \
    /p:ContinuousIntegrationBuild=$ci \
291
    /p:TreatWarningsAsErrors=true \
292
    /p:RestoreDisableParallel=$disable_parallel_restore \
293
    /p:TestRuntimeAdditionalArguments=$test_runtime_args \
294
    /p:DotNetBuildFromSource=$source_build \
T
Tomáš Matoušek 已提交
295 296 297 298
    $test_runtime \
    $mono_tool \
    $properties
}
T
Tomáš Matoušek 已提交
299

T
Tomáš Matoušek 已提交
300
InitializeDotNetCli $restore
301 302 303
if [[ "$restore" == true ]]; then
  dotnet tool restore
fi
T
Tomáš Matoušek 已提交
304

T
Tomáš Matoušek 已提交
305 306 307 308
bootstrap_dir=""
if [[ "$bootstrap" == true ]]; then
  MakeBootstrapBuild
  bootstrap_dir=$_MakeBootstrapBuild
T
Tomáš Matoušek 已提交
309 310
fi

T
Tomáš Matoušek 已提交
311 312
BuildSolution
ExitWithExitCode 0