问题
The last few weeks I'm playing with build automation. With the help of Cmake, I'm able to generate Visual Studio solution and MinGW makefile for Windows and also GCC makefile for Linux. The Cmake task is executed through a batch file on Windows respectively through a shell script on Linux. Everything looks correct and works as expected. My plan is to setup different test servers where the whole build and test process will be automated and the results will be reported somewhere.
One thing I was not able to figure out yet is how to obtain the result of the cmake command. I would like to know whether the cmake command ended successfully or not, so in case of error the fail will be reported. At this moment I'm able to parse the result and look for "Build files have been written to: ..." sentence, but I think it is not really robust solution.
Is there a way to determine whether the cmake command was successful or not? I don't want to stick necessarily to batch files, Python (or other) scripts are also welcome. Thanks.
回答1:
Just make sure your scripts do exit with the error levels reported by the programs you're calling.
Let me explain this with showing an example:
vs2015_x86_build.cmd
@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
:: usage:
:: vs2015_x86_build.cmd <target> <config>
:: <target> - target to be built (default: ALL_BUILD)
:: <config> - configuration to be used for build (default: Debug)
if NOT "%1" == "" (SET CMAKE_TARGET=%1) else (SET CMAKE_TARGET=ALL_BUILD)
if NOT "%2" == "" (set CMAKE_BUILD_TYPE=%2) else (set CMAKE_BUILD_TYPE=Debug)
SET CMAKE_BINARY_DIR=vs2015_x86
IF NOT EXIST "%CMAKE_BINARY_DIR%\*.sln" (
cmake -H"." -B"%CMAKE_BINARY_DIR%" -G"Visual Studio 14 2015"
SET GENERATE_ERRORLEVEL=!ERRORLEVEL!
IF NOT "!GENERATE_ERRORLEVEL!"=="0" (
DEL /F /Q "%CMAKE_BINARY_DIR%\*.sln"
EXIT /B !GENERATE_ERRORLEVEL!
)
)
cmake --build "%CMAKE_BINARY_DIR%" --target "%CMAKE_TARGET%" --config "%CMAKE_BUILD_TYPE%"
SET BUILD_ERRORLEVEL=!ERRORLEVEL!
IF NOT "!BUILD_ERRORLEVEL!"=="0" (
EXIT /B !BUILD_ERRORLEVEL!
)
ENDLOCAL
References
- Does CMake always generate configurations for all possible project configurations?
回答2:
Have an additional action associated with the ultimate target, like the creation of a file, or the appending of a time-stamped log entry to a given file.
来源:https://stackoverflow.com/questions/38253153/how-to-know-whether-the-cmake-project-generation-ended-with-success