2017-09-26 01:05:32 +08:00
|
|
|
## # vcpkg_build_cmake
|
|
|
|
##
|
|
|
|
## Build a cmake project.
|
|
|
|
##
|
|
|
|
## ## Usage:
|
|
|
|
## ```cmake
|
2017-10-06 08:49:28 +08:00
|
|
|
## vcpkg_build_cmake([DISABLE_PARALLEL] [TARGET <target>])
|
2017-09-26 01:05:32 +08:00
|
|
|
## ```
|
|
|
|
##
|
|
|
|
## ## Parameters:
|
|
|
|
## ### DISABLE_PARALLEL
|
2017-10-06 08:49:28 +08:00
|
|
|
## The underlying buildsystem will be instructed to not parallelize
|
|
|
|
##
|
|
|
|
## ### TARGET
|
|
|
|
## The target passed to the cmake build command (`cmake --build . --target <target>`). If not specified, no target will
|
|
|
|
## be passed.
|
2017-09-26 01:05:32 +08:00
|
|
|
##
|
2018-03-20 01:30:33 +08:00
|
|
|
## ### ADD_BIN_TO_PATH
|
|
|
|
## Adds the appropriate Release and Debug `bin\` directories to the path during the build such that executables can run against the in-tree DLLs.
|
|
|
|
##
|
2017-09-26 01:05:32 +08:00
|
|
|
## ## Notes:
|
|
|
|
## This command should be preceeded by a call to [`vcpkg_configure_cmake()`](vcpkg_configure_cmake.md).
|
2017-10-06 08:49:28 +08:00
|
|
|
## You can use the alias [`vcpkg_install_cmake()`](vcpkg_configure_cmake.md) function if your CMake script supports the
|
|
|
|
## "install" target
|
2017-09-26 01:05:32 +08:00
|
|
|
##
|
|
|
|
## ## Examples:
|
|
|
|
##
|
|
|
|
## * [zlib](https://github.com/Microsoft/vcpkg/blob/master/ports/zlib/portfile.cmake)
|
|
|
|
## * [cpprestsdk](https://github.com/Microsoft/vcpkg/blob/master/ports/cpprestsdk/portfile.cmake)
|
|
|
|
## * [poco](https://github.com/Microsoft/vcpkg/blob/master/ports/poco/portfile.cmake)
|
|
|
|
## * [opencv](https://github.com/Microsoft/vcpkg/blob/master/ports/opencv/portfile.cmake)
|
2016-09-19 11:50:08 +08:00
|
|
|
function(vcpkg_build_cmake)
|
2018-03-20 01:30:33 +08:00
|
|
|
cmake_parse_arguments(_bc "DISABLE_PARALLEL;ADD_BIN_TO_PATH" "TARGET;LOGFILE_ROOT" "" ${ARGN})
|
2017-01-14 09:30:48 +08:00
|
|
|
|
2017-10-06 08:49:28 +08:00
|
|
|
if(NOT _bc_LOGFILE_ROOT)
|
|
|
|
set(_bc_LOGFILE_ROOT "build")
|
2017-01-14 09:30:48 +08:00
|
|
|
endif()
|
|
|
|
|
2017-10-20 08:19:11 +08:00
|
|
|
set(PARALLEL_ARG)
|
|
|
|
set(NO_PARALLEL_ARG)
|
|
|
|
|
2017-10-06 08:49:28 +08:00
|
|
|
if(_VCPKG_CMAKE_GENERATOR MATCHES "Ninja")
|
|
|
|
set(BUILD_ARGS "-v") # verbose output
|
2018-01-04 12:11:35 +08:00
|
|
|
set(NO_PARALLEL_ARG "-j1")
|
2017-10-06 08:49:28 +08:00
|
|
|
elseif(_VCPKG_CMAKE_GENERATOR MATCHES "Visual Studio")
|
|
|
|
set(BUILD_ARGS
|
|
|
|
"/p:VCPkgLocalAppDataDisabled=true"
|
|
|
|
"/p:UseIntelMKL=No"
|
|
|
|
)
|
2018-01-04 12:11:35 +08:00
|
|
|
set(PARALLEL_ARG "/m")
|
2017-10-06 08:49:28 +08:00
|
|
|
elseif(_VCPKG_CMAKE_GENERATOR MATCHES "NMake")
|
|
|
|
# No options are currently added for nmake builds
|
|
|
|
else()
|
|
|
|
message(FATAL_ERROR "Unrecognized GENERATOR setting from vcpkg_configure_cmake(). Valid generators are: Ninja, Visual Studio, and NMake Makefiles")
|
2017-01-14 11:09:42 +08:00
|
|
|
endif()
|
|
|
|
|
2017-10-06 08:49:28 +08:00
|
|
|
if(_bc_TARGET)
|
|
|
|
set(TARGET_PARAM "--target" ${_bc_TARGET})
|
|
|
|
else()
|
|
|
|
set(TARGET_PARAM)
|
2017-02-04 00:16:13 +08:00
|
|
|
endif()
|
|
|
|
|
2018-01-04 12:11:35 +08:00
|
|
|
if(_bc_DISABLE_PARALLEL)
|
|
|
|
set(PARALLEL_ARG ${NO_PARALLEL_ARG})
|
|
|
|
endif()
|
|
|
|
|
2018-02-13 16:00:27 +08:00
|
|
|
foreach(BUILDTYPE "debug" "release")
|
2017-10-20 08:19:11 +08:00
|
|
|
if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL BUILDTYPE)
|
|
|
|
if(BUILDTYPE STREQUAL "debug")
|
|
|
|
set(SHORT_BUILDTYPE "dbg")
|
2018-03-20 01:30:33 +08:00
|
|
|
set(CONFIG "Debug")
|
2017-10-20 08:19:11 +08:00
|
|
|
else()
|
|
|
|
set(SHORT_BUILDTYPE "rel")
|
2018-03-20 01:30:33 +08:00
|
|
|
set(CONFIG "Release")
|
2017-10-20 08:19:11 +08:00
|
|
|
endif()
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2018-06-27 12:48:29 +08:00
|
|
|
message(STATUS "Building ${TARGET_TRIPLET}-${SHORT_BUILDTYPE}")
|
2017-10-20 08:19:11 +08:00
|
|
|
set(LOGPREFIX "${CURRENT_BUILDTREES_DIR}/${_bc_LOGFILE_ROOT}-${TARGET_TRIPLET}-${SHORT_BUILDTYPE}")
|
|
|
|
set(LOGS)
|
|
|
|
|
2018-03-20 01:30:33 +08:00
|
|
|
if(_bc_ADD_BIN_TO_PATH)
|
|
|
|
set(_BACKUP_ENV_PATH "$ENV{PATH}")
|
2018-08-09 01:51:33 +08:00
|
|
|
if(CMAKE_HOST_WIN32)
|
|
|
|
set(_PATHSEP ";")
|
|
|
|
else()
|
|
|
|
set(_PATHSEP ":")
|
|
|
|
endif()
|
2018-03-20 01:30:33 +08:00
|
|
|
if(BUILDTYPE STREQUAL "debug")
|
2018-08-09 01:51:33 +08:00
|
|
|
set(ENV{PATH} "${CURRENT_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug/bin${_PATHSEP}$ENV{PATH}")
|
2018-03-20 01:30:33 +08:00
|
|
|
else()
|
2018-08-09 01:51:33 +08:00
|
|
|
set(ENV{PATH} "${CURRENT_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/bin${_PATHSEP}$ENV{PATH}")
|
2018-03-20 01:30:33 +08:00
|
|
|
endif()
|
2017-10-20 08:19:11 +08:00
|
|
|
endif()
|
|
|
|
execute_process(
|
2018-01-07 01:54:59 +08:00
|
|
|
COMMAND ${CMAKE_COMMAND} --build . --config ${CONFIG} ${TARGET_PARAM} -- ${BUILD_ARGS} ${PARALLEL_ARG}
|
2017-10-20 08:19:11 +08:00
|
|
|
OUTPUT_FILE "${LOGPREFIX}-out.log"
|
|
|
|
ERROR_FILE "${LOGPREFIX}-err.log"
|
|
|
|
RESULT_VARIABLE error_code
|
|
|
|
WORKING_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-${SHORT_BUILDTYPE})
|
|
|
|
if(error_code)
|
|
|
|
file(READ "${LOGPREFIX}-out.log" out_contents)
|
|
|
|
file(READ "${LOGPREFIX}-err.log" err_contents)
|
|
|
|
|
|
|
|
if(out_contents)
|
|
|
|
list(APPEND LOGS "${LOGPREFIX}-out.log")
|
|
|
|
endif()
|
|
|
|
if(err_contents)
|
|
|
|
list(APPEND LOGS "${LOGPREFIX}-err.log")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(out_contents MATCHES "LINK : fatal error LNK1102:" OR out_contents MATCHES " fatal error C1060: ")
|
|
|
|
# The linker ran out of memory during execution. We will try continuing once more, with parallelism disabled.
|
2018-04-11 06:38:58 +08:00
|
|
|
message(STATUS "Restarting Build ${TARGET_TRIPLET}-${SHORT_BUILDTYPE} without parallelism because memory exceeded")
|
2017-10-20 08:19:11 +08:00
|
|
|
execute_process(
|
2018-01-07 01:54:59 +08:00
|
|
|
COMMAND ${CMAKE_COMMAND} --build . --config ${CONFIG} ${TARGET_PARAM} -- ${BUILD_ARGS} ${NO_PARALLEL_ARG}
|
2017-10-20 08:19:11 +08:00
|
|
|
OUTPUT_FILE "${LOGPREFIX}-out-1.log"
|
|
|
|
ERROR_FILE "${LOGPREFIX}-err-1.log"
|
|
|
|
RESULT_VARIABLE error_code
|
|
|
|
WORKING_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-${SHORT_BUILDTYPE})
|
|
|
|
|
|
|
|
if(error_code)
|
|
|
|
file(READ "${LOGPREFIX}-out-1.log" out_contents)
|
|
|
|
file(READ "${LOGPREFIX}-err-1.log" err_contents)
|
|
|
|
|
|
|
|
if(out_contents)
|
|
|
|
list(APPEND LOGS "${LOGPREFIX}-out-1.log")
|
|
|
|
endif()
|
|
|
|
if(err_contents)
|
|
|
|
list(APPEND LOGS "${LOGPREFIX}-err-1.log")
|
|
|
|
endif()
|
|
|
|
endif()
|
2018-04-11 06:38:58 +08:00
|
|
|
elseif(out_contents MATCHES ": No such file or directory")
|
|
|
|
# WSL workaround - WSL occassionally fails with no such file or directory. Detect if we are running in WSL and restart.
|
|
|
|
execute_process(COMMAND "uname" "-r"
|
|
|
|
OUTPUT_VARIABLE UNAME_R ERROR_VARIABLE UNAME_R
|
|
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE)
|
|
|
|
|
|
|
|
if (UNAME_R MATCHES "Microsoft")
|
2018-04-18 07:21:36 +08:00
|
|
|
set(ITERATION 0)
|
|
|
|
while (ITERATION LESS 10 AND out_contents MATCHES ": No such file or directory")
|
|
|
|
MATH(EXPR ITERATION "${ITERATION}+1")
|
|
|
|
message(STATUS "Restarting Build ${TARGET_TRIPLET}-${SHORT_BUILDTYPE} because of wsl subsystem issue. Iteration: ${ITERATION}")
|
|
|
|
execute_process(
|
|
|
|
COMMAND ${CMAKE_COMMAND} --build . --config ${CONFIG} ${TARGET_PARAM} -- ${BUILD_ARGS}
|
|
|
|
OUTPUT_FILE "${LOGPREFIX}-out-${ITERATION}.log"
|
|
|
|
ERROR_FILE "${LOGPREFIX}-err-${ITERATION}.log"
|
|
|
|
RESULT_VARIABLE error_code
|
|
|
|
WORKING_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-${SHORT_BUILDTYPE})
|
|
|
|
|
|
|
|
if(error_code)
|
|
|
|
file(READ "${LOGPREFIX}-out-${ITERATION}.log" out_contents)
|
|
|
|
file(READ "${LOGPREFIX}-err-${ITERATION}.log" err_contents)
|
|
|
|
|
|
|
|
if(out_contents)
|
|
|
|
list(APPEND LOGS "${LOGPREFIX}-out-${ITERATION}.log")
|
|
|
|
endif()
|
|
|
|
if(err_contents)
|
|
|
|
list(APPEND LOGS "${LOGPREFIX}-err-${ITERATION}.log")
|
|
|
|
endif()
|
|
|
|
else()
|
|
|
|
break()
|
2018-04-11 06:38:58 +08:00
|
|
|
endif()
|
2018-04-18 07:21:36 +08:00
|
|
|
endwhile()
|
2018-04-11 06:38:58 +08:00
|
|
|
endif()
|
2019-04-03 17:16:30 +08:00
|
|
|
elseif(out_contents MATCHES "mt : general error c101008d: ")
|
|
|
|
# Antivirus workaround - occasionally files are locked and cause mt.exe to fail
|
|
|
|
set(ITERATION 0)
|
|
|
|
while (ITERATION LESS 3 AND out_contents MATCHES "mt : general error c101008d: ")
|
|
|
|
MATH(EXPR ITERATION "${ITERATION}+1")
|
|
|
|
message(STATUS "Restarting Build ${TARGET_TRIPLET}-${SHORT_BUILDTYPE} because of mt.exe file locking issue. Iteration: ${ITERATION}")
|
|
|
|
execute_process(
|
|
|
|
COMMAND ${CMAKE_COMMAND} --build . --config ${CONFIG} ${TARGET_PARAM} -- ${BUILD_ARGS}
|
|
|
|
OUTPUT_FILE "${LOGPREFIX}-out-${ITERATION}.log"
|
|
|
|
ERROR_FILE "${LOGPREFIX}-err-${ITERATION}.log"
|
|
|
|
RESULT_VARIABLE error_code
|
|
|
|
WORKING_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-${SHORT_BUILDTYPE})
|
|
|
|
|
|
|
|
if(error_code)
|
|
|
|
file(READ "${LOGPREFIX}-out-${ITERATION}.log" out_contents)
|
|
|
|
file(READ "${LOGPREFIX}-err-${ITERATION}.log" err_contents)
|
|
|
|
|
|
|
|
if(out_contents)
|
|
|
|
list(APPEND LOGS "${LOGPREFIX}-out-${ITERATION}.log")
|
|
|
|
endif()
|
|
|
|
if(err_contents)
|
|
|
|
list(APPEND LOGS "${LOGPREFIX}-err-${ITERATION}.log")
|
|
|
|
endif()
|
|
|
|
else()
|
|
|
|
break()
|
|
|
|
endif()
|
|
|
|
endwhile()
|
2017-10-20 08:19:11 +08:00
|
|
|
endif()
|
|
|
|
|
|
|
|
if(error_code)
|
|
|
|
set(STRINGIFIED_LOGS)
|
|
|
|
foreach(LOG ${LOGS})
|
|
|
|
file(TO_NATIVE_PATH "${LOG}" NATIVE_LOG)
|
|
|
|
list(APPEND STRINGIFIED_LOGS " ${NATIVE_LOG}\n")
|
|
|
|
endforeach()
|
2019-04-03 17:16:30 +08:00
|
|
|
set(_eb_COMMAND ${CMAKE_COMMAND} --build . --config ${CONFIG} ${TARGET_PARAM} -- ${BUILD_ARGS} ${PARALLEL_ARG})
|
2017-10-20 08:19:11 +08:00
|
|
|
set(_eb_WORKING_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-${SHORT_BUILDTYPE})
|
|
|
|
message(FATAL_ERROR
|
|
|
|
" Command failed: ${_eb_COMMAND}\n"
|
|
|
|
" Working Directory: ${_eb_WORKING_DIRECTORY}\n"
|
|
|
|
" See logs for more information:\n"
|
|
|
|
${STRINGIFIED_LOGS})
|
|
|
|
endif()
|
|
|
|
endif()
|
2018-03-20 01:30:33 +08:00
|
|
|
if(_bc_ADD_BIN_TO_PATH)
|
|
|
|
set(ENV{PATH} "${_BACKUP_ENV_PATH}")
|
|
|
|
endif()
|
2017-10-20 08:19:11 +08:00
|
|
|
endif()
|
|
|
|
endforeach()
|
2016-09-19 11:50:08 +08:00
|
|
|
endfunction()
|