Skip to content

Commit

Permalink
Use goto to exit in build.cmd so that error code propagates out of cmd (
Browse files Browse the repository at this point in the history
#990)

* Use goto to exit in build.cmd so that error code propagates out of cmd

* Add comment

* Fix windows format job
  • Loading branch information
safern authored Dec 19, 2019
1 parent 752a8d2 commit 639122a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 36 deletions.
92 changes: 56 additions & 36 deletions src/coreclr/build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set __ThisScriptFull="%~f0"
set __ThisScriptDir="%~dp0"

call "%__ThisScriptDir%"\setup_vs_tools.cmd
if NOT '%ERRORLEVEL%' == '0' exit /b 1
if NOT '%ERRORLEVEL%' == '0' goto ExitWithError

if defined VS160COMNTOOLS (
set "__VSToolsRoot=%VS160COMNTOOLS%"
Expand Down Expand Up @@ -376,7 +376,8 @@ powershell -NoProfile -ExecutionPolicy ByPass -NoLogo -File "%__RepoRootDir%\eng
%__CommonMSBuildArgs% %__UnprocessedBuildArgs%
if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: Failed to generate version headers.
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)

REM =========================================================================================
Expand All @@ -393,7 +394,8 @@ if %__RestoreOptData% EQU 1 (
%__CommonMSBuildArgs% %__UnprocessedBuildArgs%
if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: Failed to restore the optimization data package.
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)
)

Expand All @@ -406,11 +408,12 @@ powershell -NoProfile -ExecutionPolicy ByPass -NoLogo -File "%__RepoRootDir%\eng

if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%Failed to get PGO data package path.
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)
if not exist "!PgoDataPackagePathOutputFile!" (
echo %__ErrMsgPrefix%Failed to get PGO data package path.
exit /b 1
goto ExitWithError
)

set /p __PgoOptDataPath=<"!PgoDataPackagePathOutputFile!"
Expand All @@ -420,12 +423,13 @@ powershell -NoProfile -ExecutionPolicy ByPass -NoLogo -File "%__RepoRootDir%\eng

if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%Failed to get IBC data package path.
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)

if not exist "!IbcDataPackagePathOutputFile!" (
echo %__ErrMsgPrefix%Failed to get IBC data package path.
exit /b 1
goto ExitWithError
)

set /p __IbcOptDataPath=<"!IbcDataPackagePathOutputFile!"
Expand All @@ -447,12 +451,12 @@ set /p PYTHON=<%TEMP%\pythonlocation.txt

if NOT DEFINED PYTHON (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: Could not find a python installation
exit /b 1
goto ExitWithError
)

if %__BuildCoreLib% EQU 1 (
echo %__MsgPrefix%Laying out dynamically generated EventSource classes
"!PYTHON!" -B -Wall %__SourceDir%\scripts\genRuntimeEventSources.py --man %__SourceDir%\vm\ClrEtwAll.man --intermediate %__IntermediatesEventingDir% || exit /b 1
"!PYTHON!" -B -Wall %__SourceDir%\scripts\genRuntimeEventSources.py --man %__SourceDir%\vm\ClrEtwAll.man --intermediate %__IntermediatesEventingDir% || goto ExitWithError
)

REM =========================================================================================
Expand Down Expand Up @@ -485,14 +489,14 @@ if %__BuildCrossArchNative% EQU 1 (

if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: failed to generate native component build project!
exit /b 1
goto ExitWithError
)
@if defined _echo @echo on

:SkipConfigureCrossBuild
if not exist "%__CrossCompIntermediatesDir%\CMakeCache.txt" (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: unable to find generated native component build project!
exit /b 1
goto ExitWithError
)

if defined __ConfigureOnly goto SkipCrossCompBuild
Expand All @@ -511,7 +515,8 @@ if %__BuildCrossArchNative% EQU 1 (

if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: cross-arch components build failed.
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)

:SkipCrossCompBuild
Expand Down Expand Up @@ -554,7 +559,7 @@ if %__BuildNative% EQU 1 (

if not defined VSINSTALLDIR (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: VSINSTALLDIR variable not defined.
exit /b 1
goto ExitWithError
)
if not exist "!VSINSTALLDIR!DIA SDK" goto NoDIA

Expand All @@ -566,15 +571,15 @@ if %__BuildNative% EQU 1 (
call "%__SourceDir%\pal\tools\gen-buildsys.cmd" "%__ProjectDir%" "%__IntermediatesDir%" %__VSVersion% %__BuildArch% !__ExtraCmakeArgs!
if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: failed to generate native component build project!
exit /b 1
goto ExitWithError
)

@if defined _echo @echo on

:SkipConfigure
if not exist "%__IntermediatesDir%\CMakeCache.txt" (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: unable to find generated native component build project!
exit /b 1
goto ExitWithError
)

if defined __ConfigureOnly goto SkipNativeBuild
Expand All @@ -593,7 +598,8 @@ if %__BuildNative% EQU 1 (

if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: native component build failed.
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)

:SkipNativeBuild
Expand Down Expand Up @@ -640,7 +646,8 @@ if %__BuildCoreLib% EQU 1 (
echo !__BuildLog!
echo !__BuildWrn!
echo !__BuildErr!
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)

powershell -NoProfile -ExecutionPolicy ByPass -NoLogo -Command "%__RepoRootDir%\eng\common\msbuild.ps1" %__ArcadeScriptArgs%^
Expand All @@ -651,7 +658,8 @@ if %__BuildCoreLib% EQU 1 (
echo !__BuildLog!
echo !__BuildWrn!
echo !__BuildErr!
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)

if "%__BuildManagedTools%" == "1" (
Expand All @@ -663,7 +671,8 @@ if %__BuildCoreLib% EQU 1 (
echo !__BuildLog!
echo !__BuildWrn!
echo !__BuildErr!
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)

copy /Y "%__BinDir%\clrjit.dll" "%__BinDir%\crossgen2\clrjitilc.dll"
Expand All @@ -679,11 +688,12 @@ if %__BuildCoreLib% EQU 1 (

if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%Failed to determine IBC Merge path.
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)
if not exist "!IbcMergePackagePathOutputFile!" (
echo %__ErrMsgPrefix%Failed to determine IBC Merge path.
exit /b 1
goto ExitWithError
)

set /p __IbcMergePath=<"!IbcMergePackagePathOutputFile!"
Expand Down Expand Up @@ -716,7 +726,7 @@ if %__BuildCoreLib% EQU 1 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: IbcMerge of System.Private.CoreLib build failed. Refer to %__CrossGenCoreLibLog%
REM Put it in the same log, helpful for Jenkins
type %__CrossGenCoreLibLog%
goto CrossgenFailure
goto ExitWithError
)

REM Verify that the optimization data has been merged
Expand All @@ -727,7 +737,7 @@ if %__BuildCoreLib% EQU 1 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: IbcMerge of System.Private.CoreLib build failed. Refer to %__CrossGenCoreLibLog%
REM Put it in the same log, helpful for Jenkins
type %__CrossGenCoreLibLog%
goto CrossgenFailure
goto ExitWithError
)

REM Save the module as *.pgo to match the convention expected
Expand All @@ -746,7 +756,7 @@ if %__BuildCoreLib% EQU 1 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: IbcMerge of System.Private.CoreLib build failed. Refer to %__CrossGenCoreLibLog%
REM Put it in the same log, helpful for Jenkins
type %__CrossGenCoreLibLog%
goto CrossgenFailure
goto ExitWithError
)

REM Verify that the optimization data has been applied
Expand All @@ -757,18 +767,18 @@ if %__BuildCoreLib% EQU 1 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: IbcMerge of System.Private.CoreLib build failed. Refer to %__CrossGenCoreLibLog%
REM Put it in the same log, helpful for Jenkins
type %__CrossGenCoreLibLog%
goto CrossgenFailure
goto ExitWithError
)
) else (
echo %__MsgPrefix%!TargetOptimizationDataFile! does not exist >> %__CrossGenCoreLibLog%
echo %__ErrMsgPrefix%%__MsgPrefix%Error: IbcMerge of System.Private.CoreLib build failed. Refer to %__CrossGenCoreLibLog%
REM Put it in the same log, helpful for Jenkins
type %__CrossGenCoreLibLog%
goto CrossgenFailure
goto ExitWithError
)
) else (
echo Could not find IBCMerge at !IbcMergePath!. Have you restored src/.nuget/optdata/ibcmerge.csproj?
goto CrossgenFailure
goto ExitWithError
)
)

Expand Down Expand Up @@ -801,7 +811,7 @@ if %__BuildNativeCoreLib% EQU 1 (
@if defined _echo @echo on
if NOT !errorlevel! == 0 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: Failed to load native tools environment for !__VCExecArch!
goto CrossgenFailure
goto ExitWithError
)

REM HACK: Workaround for [dotnet/coreclr#13970](https://github.com/dotnet/coreclr/issues/13970)
Expand All @@ -812,7 +822,7 @@ if %__BuildNativeCoreLib% EQU 1 (
echo %__MsgPrefix%Copying "!__PgoRtPath!" into "%__BinDir%"
copy /y "!__PgoRtPath!" "%__BinDir%" || (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: copy failed
goto CrossgenFailure
goto ExitWithError
)
REM End HACK
)
Expand All @@ -838,7 +848,7 @@ if %__BuildNativeCoreLib% EQU 1 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: CrossGen System.Private.CoreLib build failed. Refer to %__CrossGenCoreLibLog%
REM Put it in the same log, helpful for Jenkins
type %__CrossGenCoreLibLog%
goto CrossgenFailure
goto ExitWithError
)

set NEXTCMD="%__CrossgenExe%" /Platform_Assemblies_Paths "%__BinDir%" /CreatePdb "%__BinDir%\PDB" "%__BinDir%\System.Private.CoreLib.dll"
Expand All @@ -849,7 +859,7 @@ if %__BuildNativeCoreLib% EQU 1 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: CrossGen /CreatePdb System.Private.CoreLib build failed. Refer to %__CrossGenCoreLibLog%
REM Put it in the same log, helpful for Jenkins
type %__CrossGenCoreLibLog%
goto CrossgenFailure
goto ExitWithError
)
)

Expand Down Expand Up @@ -880,7 +890,8 @@ if %__BuildPackages% EQU 1 (
if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: Nuget package generation failed. Refer to the build log file for details.
echo !__BuildLog!
exit /b !errorlevel!
set __exitCode=!errorlevel!
goto ExitWithCode
)

REM } Scope environment changes end
Expand All @@ -906,7 +917,7 @@ if %__BuildTests% EQU 1 (

if not !errorlevel! == 0 (
REM buildtest.cmd has already emitted an error message and mentioned the build log file to examine.
exit /b 1
goto ExitWithError
)
) else if %__GenerateLayout% EQU 1 (
echo %__MsgPrefix%Generating layout for %__BuildOS%.%__BuildArch%.%__BuildType%
Expand All @@ -917,7 +928,7 @@ if %__BuildTests% EQU 1 (

if not !errorlevel! == 0 (
REM runtest.cmd has already emitted an error message and mentioned the build log file to examine.
exit /b 1
goto ExitWithError
)
)

Expand Down Expand Up @@ -986,7 +997,7 @@ if %__AllBuildSuccess%==true (
echo %__MsgPrefix%Builds failed:
type %__BuildResultFile%
del /f /q %__BuildResultFile%
exit /b 1
goto ExitWithError
)

REM This code is unreachable, but leaving it nonetheless, just in case things change.
Expand All @@ -1010,9 +1021,18 @@ REM === Helper routines
REM ===
REM =========================================================================================

:CrossgenFailure

REM =========================================================================================
REM === These two routines are intended for the exit code to propagate to the parent process
REM === Like MSBuild or Powershell. If we directly exit /b 1 from within a if statement in
REM === any of the routines, the exit code is not propagated.
REM =========================================================================================
:ExitWithError
exit /b 1

:ExitWithCode
exit /b !__exitCode!

:Usage
echo.
echo Build the CoreCLR repo.
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/configurecompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cmake_policy(SET CMP0083 NEW)
include(CheckPIESupported)
include(CheckCXXCompilerFlag)


# All code we build should be compiled as position independent
check_pie_supported(OUTPUT_VARIABLE PIE_SUPPORT_OUTPUT LANGUAGES CXX)
if(NOT MSVC AND NOT CMAKE_CXX_LINK_PIE_SUPPORTED)
Expand Down Expand Up @@ -641,3 +642,7 @@ if(CLR_CMAKE_ENABLE_CODE_COVERAGE)
endif(CLR_CMAKE_PLATFORM_UNIX)

endif(CLR_CMAKE_ENABLE_CODE_COVERAGE)

if (CMAKE_BUILD_TOOL STREQUAL nmake)
set(CMAKE_RC_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY}")
endif(CMAKE_BUILD_TOOL STREQUAL nmake)

0 comments on commit 639122a

Please sign in to comment.