1*08ab5237SOystein Eftevaag## Utility CMake functions. 2*08ab5237SOystein Eftevaag 3*08ab5237SOystein Eftevaag# ---------------------------------------------------------------------------- 4*08ab5237SOystein Eftevaag## Convert boolean value to 0 or 1 5*08ab5237SOystein Eftevaagmacro (bool_to_int VAR) 6*08ab5237SOystein Eftevaag if (${VAR}) 7*08ab5237SOystein Eftevaag set (${VAR} 1) 8*08ab5237SOystein Eftevaag else () 9*08ab5237SOystein Eftevaag set (${VAR} 0) 10*08ab5237SOystein Eftevaag endif () 11*08ab5237SOystein Eftevaagendmacro () 12*08ab5237SOystein Eftevaag 13*08ab5237SOystein Eftevaag# ---------------------------------------------------------------------------- 14*08ab5237SOystein Eftevaag## Extract version numbers from version string 15*08ab5237SOystein Eftevaagfunction (version_numbers version major minor patch) 16*08ab5237SOystein Eftevaag if (version MATCHES "([0-9]+)(\\.[0-9]+)?(\\.[0-9]+)?(rc[1-9][0-9]*|[a-z]+)?") 17*08ab5237SOystein Eftevaag if (CMAKE_MATCH_1) 18*08ab5237SOystein Eftevaag set (_major ${CMAKE_MATCH_1}) 19*08ab5237SOystein Eftevaag else () 20*08ab5237SOystein Eftevaag set (_major 0) 21*08ab5237SOystein Eftevaag endif () 22*08ab5237SOystein Eftevaag if (CMAKE_MATCH_2) 23*08ab5237SOystein Eftevaag set (_minor ${CMAKE_MATCH_2}) 24*08ab5237SOystein Eftevaag string (REGEX REPLACE "^\\." "" _minor "${_minor}") 25*08ab5237SOystein Eftevaag else () 26*08ab5237SOystein Eftevaag set (_minor 0) 27*08ab5237SOystein Eftevaag endif () 28*08ab5237SOystein Eftevaag if (CMAKE_MATCH_3) 29*08ab5237SOystein Eftevaag set (_patch ${CMAKE_MATCH_3}) 30*08ab5237SOystein Eftevaag string (REGEX REPLACE "^\\." "" _patch "${_patch}") 31*08ab5237SOystein Eftevaag else () 32*08ab5237SOystein Eftevaag set (_patch 0) 33*08ab5237SOystein Eftevaag endif () 34*08ab5237SOystein Eftevaag else () 35*08ab5237SOystein Eftevaag set (_major 0) 36*08ab5237SOystein Eftevaag set (_minor 0) 37*08ab5237SOystein Eftevaag set (_patch 0) 38*08ab5237SOystein Eftevaag endif () 39*08ab5237SOystein Eftevaag set ("${major}" "${_major}" PARENT_SCOPE) 40*08ab5237SOystein Eftevaag set ("${minor}" "${_minor}" PARENT_SCOPE) 41*08ab5237SOystein Eftevaag set ("${patch}" "${_patch}" PARENT_SCOPE) 42*08ab5237SOystein Eftevaagendfunction () 43*08ab5237SOystein Eftevaag 44*08ab5237SOystein Eftevaag# ---------------------------------------------------------------------------- 45*08ab5237SOystein Eftevaag## Determine if cache entry exists 46*08ab5237SOystein Eftevaagmacro (gflags_is_cached retvar varname) 47*08ab5237SOystein Eftevaag if (DEFINED ${varname}) 48*08ab5237SOystein Eftevaag get_property (${retvar} CACHE ${varname} PROPERTY TYPE SET) 49*08ab5237SOystein Eftevaag else () 50*08ab5237SOystein Eftevaag set (${retvar} FALSE) 51*08ab5237SOystein Eftevaag endif () 52*08ab5237SOystein Eftevaagendmacro () 53*08ab5237SOystein Eftevaag 54*08ab5237SOystein Eftevaag# ---------------------------------------------------------------------------- 55*08ab5237SOystein Eftevaag## Add gflags configuration variable 56*08ab5237SOystein Eftevaag# 57*08ab5237SOystein Eftevaag# The default value of the (cached) configuration value can be overridden either 58*08ab5237SOystein Eftevaag# on the CMake command-line or the super-project by setting the GFLAGS_<varname> 59*08ab5237SOystein Eftevaag# variable. When gflags is a subproject of another project (GFLAGS_IS_SUBPROJECT), 60*08ab5237SOystein Eftevaag# the variable is not added to the CMake cache. Otherwise it is cached. 61*08ab5237SOystein Eftevaagmacro (gflags_define type varname docstring default) 62*08ab5237SOystein Eftevaag # note that ARGC must be expanded here, as it is not a "real" variable 63*08ab5237SOystein Eftevaag # (see the CMake documentation for the macro command) 64*08ab5237SOystein Eftevaag if ("${ARGC}" GREATER 5) 65*08ab5237SOystein Eftevaag message (FATAL_ERROR "gflags_variable: Too many macro arguments") 66*08ab5237SOystein Eftevaag endif () 67*08ab5237SOystein Eftevaag if (NOT DEFINED GFLAGS_${varname}) 68*08ab5237SOystein Eftevaag if (GFLAGS_IS_SUBPROJECT AND "${ARGC}" EQUAL 5) 69*08ab5237SOystein Eftevaag set (GFLAGS_${varname} "${ARGV4}") 70*08ab5237SOystein Eftevaag else () 71*08ab5237SOystein Eftevaag set (GFLAGS_${varname} "${default}") 72*08ab5237SOystein Eftevaag endif () 73*08ab5237SOystein Eftevaag endif () 74*08ab5237SOystein Eftevaag if (GFLAGS_IS_SUBPROJECT) 75*08ab5237SOystein Eftevaag if (NOT DEFINED ${varname}) 76*08ab5237SOystein Eftevaag set (${varname} "${GFLAGS_${varname}}") 77*08ab5237SOystein Eftevaag endif () 78*08ab5237SOystein Eftevaag else () 79*08ab5237SOystein Eftevaag set (${varname} "${GFLAGS_${varname}}" CACHE ${type} "${docstring}") 80*08ab5237SOystein Eftevaag endif () 81*08ab5237SOystein Eftevaagendmacro () 82*08ab5237SOystein Eftevaag 83*08ab5237SOystein Eftevaag# ---------------------------------------------------------------------------- 84*08ab5237SOystein Eftevaag## Set property of cached gflags configuration variable 85*08ab5237SOystein Eftevaagmacro (gflags_property varname property value) 86*08ab5237SOystein Eftevaag gflags_is_cached (_cached ${varname}) 87*08ab5237SOystein Eftevaag if (_cached) 88*08ab5237SOystein Eftevaag # note that property must be expanded here, as it is not a "real" variable 89*08ab5237SOystein Eftevaag # (see the CMake documentation for the macro command) 90*08ab5237SOystein Eftevaag if ("${property}" STREQUAL "ADVANCED") 91*08ab5237SOystein Eftevaag if (${value}) 92*08ab5237SOystein Eftevaag mark_as_advanced (FORCE ${varname}) 93*08ab5237SOystein Eftevaag else () 94*08ab5237SOystein Eftevaag mark_as_advanced (CLEAR ${varname}) 95*08ab5237SOystein Eftevaag endif () 96*08ab5237SOystein Eftevaag else () 97*08ab5237SOystein Eftevaag set_property (CACHE ${varname} PROPERTY "${property}" "${value}") 98*08ab5237SOystein Eftevaag endif () 99*08ab5237SOystein Eftevaag endif () 100*08ab5237SOystein Eftevaag unset (_cached) 101*08ab5237SOystein Eftevaagendmacro () 102*08ab5237SOystein Eftevaag 103*08ab5237SOystein Eftevaag# ---------------------------------------------------------------------------- 104*08ab5237SOystein Eftevaag## Modify value of gflags configuration variable 105*08ab5237SOystein Eftevaagmacro (gflags_set varname value) 106*08ab5237SOystein Eftevaag gflags_is_cached (_cached ${varname}) 107*08ab5237SOystein Eftevaag if (_cached) 108*08ab5237SOystein Eftevaag set_property (CACHE ${varname} PROPERTY VALUE "${value}") 109*08ab5237SOystein Eftevaag else () 110*08ab5237SOystein Eftevaag set (${varname} "${value}") 111*08ab5237SOystein Eftevaag endif () 112*08ab5237SOystein Eftevaag unset (_cached) 113*08ab5237SOystein Eftevaagendmacro () 114*08ab5237SOystein Eftevaag 115*08ab5237SOystein Eftevaag# ---------------------------------------------------------------------------- 116*08ab5237SOystein Eftevaag## Configure public header files 117*08ab5237SOystein Eftevaagfunction (configure_headers out) 118*08ab5237SOystein Eftevaag set (tmp) 119*08ab5237SOystein Eftevaag foreach (src IN LISTS ARGN) 120*08ab5237SOystein Eftevaag if (IS_ABSOLUTE "${src}") 121*08ab5237SOystein Eftevaag list (APPEND tmp "${src}") 122*08ab5237SOystein Eftevaag elseif (EXISTS "${PROJECT_SOURCE_DIR}/src/${src}.in") 123*08ab5237SOystein Eftevaag configure_file ("${PROJECT_SOURCE_DIR}/src/${src}.in" "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/${src}" @ONLY) 124*08ab5237SOystein Eftevaag list (APPEND tmp "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/${src}") 125*08ab5237SOystein Eftevaag else () 126*08ab5237SOystein Eftevaag configure_file ("${PROJECT_SOURCE_DIR}/src/${src}" "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/${src}" COPYONLY) 127*08ab5237SOystein Eftevaag list (APPEND tmp "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/${src}") 128*08ab5237SOystein Eftevaag endif () 129*08ab5237SOystein Eftevaag endforeach () 130*08ab5237SOystein Eftevaag set (${out} "${tmp}" PARENT_SCOPE) 131*08ab5237SOystein Eftevaagendfunction () 132*08ab5237SOystein Eftevaag 133*08ab5237SOystein Eftevaag# ---------------------------------------------------------------------------- 134*08ab5237SOystein Eftevaag## Configure source files with .in suffix 135*08ab5237SOystein Eftevaagfunction (configure_sources out) 136*08ab5237SOystein Eftevaag set (tmp) 137*08ab5237SOystein Eftevaag foreach (src IN LISTS ARGN) 138*08ab5237SOystein Eftevaag if (src MATCHES ".h$" AND EXISTS "${PROJECT_SOURCE_DIR}/src/${src}.in") 139*08ab5237SOystein Eftevaag configure_file ("${PROJECT_SOURCE_DIR}/src/${src}.in" "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/${src}" @ONLY) 140*08ab5237SOystein Eftevaag list (APPEND tmp "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/${src}") 141*08ab5237SOystein Eftevaag else () 142*08ab5237SOystein Eftevaag list (APPEND tmp "${PROJECT_SOURCE_DIR}/src/${src}") 143*08ab5237SOystein Eftevaag endif () 144*08ab5237SOystein Eftevaag endforeach () 145*08ab5237SOystein Eftevaag set (${out} "${tmp}" PARENT_SCOPE) 146*08ab5237SOystein Eftevaagendfunction () 147*08ab5237SOystein Eftevaag 148*08ab5237SOystein Eftevaag# ---------------------------------------------------------------------------- 149*08ab5237SOystein Eftevaag## Add usage test 150*08ab5237SOystein Eftevaag# 151*08ab5237SOystein Eftevaag# Using PASS_REGULAR_EXPRESSION and FAIL_REGULAR_EXPRESSION would 152*08ab5237SOystein Eftevaag# do as well, but CMake/CTest does not allow us to specify an 153*08ab5237SOystein Eftevaag# expected exit status. Moreover, the execute_test.cmake script 154*08ab5237SOystein Eftevaag# sets environment variables needed by the --fromenv/--tryfromenv tests. 155*08ab5237SOystein Eftevaagmacro (add_gflags_test name expected_rc expected_output unexpected_output cmd) 156*08ab5237SOystein Eftevaag set (args "--test_tmpdir=${PROJECT_BINARY_DIR}/Testing/Temporary" 157*08ab5237SOystein Eftevaag "--srcdir=${PROJECT_SOURCE_DIR}/test") 158*08ab5237SOystein Eftevaag add_test ( 159*08ab5237SOystein Eftevaag NAME ${name} 160*08ab5237SOystein Eftevaag COMMAND "${CMAKE_COMMAND}" "-DCOMMAND:STRING=$<TARGET_FILE:${cmd}>;${args};${ARGN}" 161*08ab5237SOystein Eftevaag "-DEXPECTED_RC:STRING=${expected_rc}" 162*08ab5237SOystein Eftevaag "-DEXPECTED_OUTPUT:STRING=${expected_output}" 163*08ab5237SOystein Eftevaag "-DUNEXPECTED_OUTPUT:STRING=${unexpected_output}" 164*08ab5237SOystein Eftevaag -P "${PROJECT_SOURCE_DIR}/cmake/execute_test.cmake" 165*08ab5237SOystein Eftevaag WORKING_DIRECTORY "${GFLAGS_FLAGFILES_DIR}" 166*08ab5237SOystein Eftevaag ) 167*08ab5237SOystein Eftevaagendmacro () 168*08ab5237SOystein Eftevaag 169*08ab5237SOystein Eftevaag# ------------------------------------------------------------------------------ 170*08ab5237SOystein Eftevaag## Register installed package with CMake 171*08ab5237SOystein Eftevaag# 172*08ab5237SOystein Eftevaag# This function adds an entry to the CMake registry for packages with the 173*08ab5237SOystein Eftevaag# path of the directory where the package configuration file of the installed 174*08ab5237SOystein Eftevaag# package is located in order to help CMake find the package in a custom 175*08ab5237SOystein Eftevaag# installation prefix. This differs from CMake's export(PACKAGE) command 176*08ab5237SOystein Eftevaag# which registers the build directory instead. 177*08ab5237SOystein Eftevaagfunction (register_gflags_package CONFIG_DIR) 178*08ab5237SOystein Eftevaag if (NOT IS_ABSOLUTE "${CONFIG_DIR}") 179*08ab5237SOystein Eftevaag set (CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/${CONFIG_DIR}") 180*08ab5237SOystein Eftevaag endif () 181*08ab5237SOystein Eftevaag string (MD5 REGISTRY_ENTRY "${CONFIG_DIR}") 182*08ab5237SOystein Eftevaag if (WIN32) 183*08ab5237SOystein Eftevaag install (CODE 184*08ab5237SOystein Eftevaag "execute_process ( 185*08ab5237SOystein Eftevaag COMMAND reg add \"HKCU\\\\Software\\\\Kitware\\\\CMake\\\\Packages\\\\${PACKAGE_NAME}\" /v \"${REGISTRY_ENTRY}\" /d \"${CONFIG_DIR}\" /t REG_SZ /f 186*08ab5237SOystein Eftevaag RESULT_VARIABLE RT 187*08ab5237SOystein Eftevaag ERROR_VARIABLE ERR 188*08ab5237SOystein Eftevaag OUTPUT_QUIET 189*08ab5237SOystein Eftevaag ) 190*08ab5237SOystein Eftevaag if (RT EQUAL 0) 191*08ab5237SOystein Eftevaag message (STATUS \"Register: Added HKEY_CURRENT_USER\\\\Software\\\\Kitware\\\\CMake\\\\Packages\\\\${PACKAGE_NAME}\\\\${REGISTRY_ENTRY}\") 192*08ab5237SOystein Eftevaag else () 193*08ab5237SOystein Eftevaag string (STRIP \"\${ERR}\" ERR) 194*08ab5237SOystein Eftevaag message (STATUS \"Register: Failed to add registry entry: \${ERR}\") 195*08ab5237SOystein Eftevaag endif ()" 196*08ab5237SOystein Eftevaag ) 197*08ab5237SOystein Eftevaag elseif (IS_DIRECTORY "$ENV{HOME}") 198*08ab5237SOystein Eftevaag file (WRITE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-registry-entry" "${CONFIG_DIR}") 199*08ab5237SOystein Eftevaag install ( 200*08ab5237SOystein Eftevaag FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-registry-entry" 201*08ab5237SOystein Eftevaag DESTINATION "$ENV{HOME}/.cmake/packages/${PACKAGE_NAME}" 202*08ab5237SOystein Eftevaag RENAME "${REGISTRY_ENTRY}" 203*08ab5237SOystein Eftevaag ) 204*08ab5237SOystein Eftevaag endif () 205*08ab5237SOystein Eftevaagendfunction () 206