build.sh 6.7 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 8 9
set -u

usage()
{
T
Tomáš Matoušek 已提交
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
  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"
  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"
  echo "  --skipAnalyzers            Do not run analyzers during build operations"
  echo "  --prepareMachine           Prepare machine for CI run, clean up processes after build"
  echo ""
  echo "Command line arguments starting with '/p:' are passed through to MSBuild."
T
Tomáš Matoušek 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
}

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 已提交
51
rebuild=false
T
Tomáš Matoušek 已提交
52
pack=false
T
Tomáš Matoušek 已提交
53 54 55 56 57 58 59
publish=false
test_core_clr=false
test_mono=false

configuration="Debug"
verbosity='minimal'
binary_log=false
T
Tomáš Matoušek 已提交
60
ci=false
T
Tomáš Matoušek 已提交
61 62 63 64
bootstrap=false
skip_analyzers=false
prepare_machine=false
properties=""
T
Tomáš Matoušek 已提交
65

T
Tomáš Matoušek 已提交
66 67
docker=false
args=""
T
Tomáš Matoušek 已提交
68 69 70

if [[ $# = 0 ]]
then
T
Tomáš Matoušek 已提交
71 72
  usage
  exit 1
T
Tomáš Matoušek 已提交
73 74
fi

T
Tomáš Matoušek 已提交
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
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
      ;;
    --ci)
      ci=true
      ;;
    --bootstrap)
      bootstrap=true
      ;;
    --skipanalyzers)
      skip_analyzers=true
      ;;
    --preparemachine)
      prepare_machine=true
      ;;
    --docker)
      docker=true
      shift
      continue
      ;;
    /p:*)
      properties="$properties $1"
      ;;
    *)
      echo "Invalid argument: $1"
      usage
      exit 1
      ;;
  esac
  args="$args $1"
  shift
T
Tomáš Matoušek 已提交
144 145
done

T
Tomáš Matoušek 已提交
146
if [[ "$test_mono" == true && "$docker" == true ]]
T
Tomáš Matoušek 已提交
147
then
T
Tomáš Matoušek 已提交
148 149 150
  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 已提交
151
  BUILD_COMMAND=/opt/code/eng/build.sh "$scriptroot"/docker/mono.sh $args
152 153 154 155 156 157
  lastexitcode=$?
  if [[ $lastexitcode != 0 ]]; then
    echo "Docker build failed (exit code '$lastexitcode')." >&2
    exit $lastexitcode
  fi

T
Tomáš Matoušek 已提交
158 159 160 161 162 163 164
  # 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 已提交
165 166
fi

167
# Import Arcade functions
T
Tomáš Matoušek 已提交
168
. "$scriptroot/common/tools.sh"
169

T
Tomáš Matoušek 已提交
170 171
function MakeBootstrapBuild {
  echo "Building bootstrap compiler"
172

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

T
Tomáš Matoušek 已提交
175 176
  rm -rf $dir
  mkdir -p $dir
T
Tomáš Matoušek 已提交
177

T
Tomáš Matoušek 已提交
178 179
  local package_name="Microsoft.NETCore.Compilers"
  local project_path=src/NuGet/$package_name/$package_name.Package.csproj
T
Tomáš Matoušek 已提交
180

T
Tomáš Matoušek 已提交
181 182 183
  dotnet pack -nologo "$project_path" /p:DotNetUseShippingVersions=true /p:InitialDefineConstants=BOOTSTRAP /p:PackageOutputPath="$dir"
  unzip "$dir/$package_name.*.nupkg" -d "$dir"
  chmod -R 755 "$dir"
T
Tomáš Matoušek 已提交
184

T
Tomáš Matoušek 已提交
185 186
  echo "Cleaning Bootstrap compiler artifacts"
  dotnet clean "$project_path"
T
Tomáš Matoušek 已提交
187

T
Tomáš Matoušek 已提交
188 189 190
  if [[ "$node_reuse" == true ]]; then
    dotnet build-server shutdown
  fi
T
Tomáš Matoušek 已提交
191

T
Tomáš Matoušek 已提交
192 193 194
  # return value
  _MakeBootstrapBuild=$dir
}
T
Tomáš Matoušek 已提交
195

T
Tomáš Matoušek 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
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
  local enable_analyzers=!$skip_analyzers
  UNAME="$(uname)"
  if [[ "$UNAME" == "Darwin" ]]; then
    enable_analyzers=false
  fi

  local quiet_restore=""
  if [[ "$ci" != true ]]; then
    quiet_restore=true
  fi

  local test=false
  local test_runtime=""
  local mono_tool=""
  if [[ "$test_mono" == true ]]; then
    # Echo out the mono version to the comamnd line so it's visible in CI logs. It's not fixed
    # as we're using a feed vs. a hard coded package.
    if [[ "$ci" == true ]]; then
      mono --version
    fi
T
Tomáš Matoušek 已提交
231

T
Tomáš Matoušek 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    test=true
    test_runtime="/p:TestRuntime=Mono"
    mono_path=`command -v mono`
    mono_tool="/p:MonoTool=\"$mono_path\""
  elif [[ "$test_core_clr" == true ]]; then
    test=true
    test_runtime="/p:TestRuntime=Core"
    mono_tool=""
  fi

  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 \
    /p:UseRoslynAnalyzers=$enable_analyzers \
    /p:BootstrapBuildPath="$bootstrap_dir" \
    /p:ContinuousIntegrationBuild=$ci \
    /p:QuietRestore=$quiet_restore \
    /p:QuietRestoreBinaryLog="$binary_log" \
    $test_runtime \
    $mono_tool \
    $properties
}
T
Tomáš Matoušek 已提交
262

T
Tomáš Matoušek 已提交
263
InitializeDotNetCli $restore
T
Tomáš Matoušek 已提交
264

T
Tomáš Matoušek 已提交
265
export PATH="$DOTNET_INSTALL_DIR:$PATH"
T
Tomáš Matoušek 已提交
266

T
Tomáš Matoušek 已提交
267 268 269 270
bootstrap_dir=""
if [[ "$bootstrap" == true ]]; then
  MakeBootstrapBuild
  bootstrap_dir=$_MakeBootstrapBuild
T
Tomáš Matoušek 已提交
271 272
fi

T
Tomáš Matoušek 已提交
273 274
BuildSolution
ExitWithExitCode 0