Skip to content

Instantly share code, notes, and snippets.

@dolinenkov
Created March 12, 2020 19:19
Show Gist options
  • Save dolinenkov/ef1a0e2d9782b5e76098df78b3f31191 to your computer and use it in GitHub Desktop.
Save dolinenkov/ef1a0e2d9782b5e76098df78b3f31191 to your computer and use it in GitHub Desktop.
simple script for building and installing cmake dependencies
if(NOT MAX_BUILD_JOBS)
set(MAX_BUILD_JOBS 15)
endif()
# A simple function for importing a cmake-friendly buildable (and installable!) project
function(build_cmake_package SRC_DIR BIN_DIR PKG_DIR CFG_TYPE CFG_ARGS)
set(_CFG_ARGS -G ${CMAKE_GENERATOR})
if(CMAKE_GENERATOR_PLATFORM)
set(_CFG_ARGS ${_CFG_ARGS} -A ${CMAKE_GENERATOR_PLATFORM})
endif()
if(CMAKE_GENERATOR_TOOLSET)
set(_CFG_ARGS ${_CFG_ARGS} -T ${CMAKE_GENERATOR_TOOLSET})
endif()
set(_CFG_ARGS ${_CFG_ARGS} ${CFG_ARGS})
# configure step
message(STATUS "configuring")
execute_process(
COMMAND ${CMAKE_COMMAND} -S ${SRC_DIR} -B ${BIN_DIR} -DCMAKE_INSTALL_PREFIX=${PKG_DIR} ${_CFG_ARGS}
OUTPUT_FILE ${CMAKE_BINARY_DIR}/build_deps.log
ERROR_FILE ${CMAKE_BINARY_DIR}/build_deps.log
RESULT_VARIABLE ERR
)
if(ERR AND ${ERR})
message(FATAL_ERROR "configure step failed")
endif()
foreach(CFG ${CFG_TYPE})
# build step
message(STATUS "building: ${CFG}")
execute_process(
COMMAND ${CMAKE_COMMAND} --build ${BIN_DIR} --config ${CFG} --parallel ${MAX_BUILD_JOBS}
OUTPUT_FILE ${CMAKE_BINARY_DIR}/build_deps.log
ERROR_FILE ${CMAKE_BINARY_DIR}/build_deps.log
RESULT_VARIABLE ERR
)
if(ERR AND ${ERR})
message(FATAL_ERROR "build step failed")
endif()
# install step
message(STATUS "installing: ${CFG}")
execute_process(
COMMAND ${CMAKE_COMMAND} --install ${BIN_DIR} --config ${CFG}
OUTPUT_FILE ${CMAKE_BINARY_DIR}/build_deps.log
ERROR_FILE ${CMAKE_BINARY_DIR}/build_deps.log
)
if(ERR AND ${ERR})
message(FATAL_ERROR "install step failed")
endif()
endforeach()
endfunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment