# ----------------------------------------------------------------------------
# Utility functions for CMakeLists.txt
# ----------------------------------------------------------------------------
include(CheckCCompilerFlag)
include(CheckCSourceCompiles)
include(CheckIncludeFiles)
include(CheckSymbolExists)
include(CMakePushCheckState)
include(GNUInstallDirs)
include("${CMAKE_CURRENT_LIST_DIR}/JoinPaths.cmake")
# Set the allowed values of the cache string `var` and validate its value (case-insensitive, and
# boolean constants map to ON or OFF); the value is normalized to the spelling of the allowed value.
function(set_property_and_validate var)
set_property(CACHE ${var} PROPERTY STRINGS ${ARGN})
string(TOUPPER "${${var}}" value)
if(value MATCHES "^(ON|YES|Y|TRUE|1)$")
set(value "ON")
elseif(value MATCHES "^(OFF|NO|N|FALSE|0|)$")
set(value "OFF")
endif()
foreach(allowed IN LISTS ARGN)
string(TOUPPER "${allowed}" allowed_upper)
if(value STREQUAL allowed_upper)
set(${var} "${allowed}" PARENT_SCOPE)
return()
endif()
endforeach()
string(REPLACE ";" ", " allowed "${ARGN}")
message(FATAL_ERROR "Invalid option value for ${var}: ${${var}} (expecting one of: ${allowed})")
endfunction()
# Define a cache string option `name` with a list of `allowed` values (where the first one is the default)
function(option_list name allowed description)
list(GET allowed 0 default)
set(${name} "${default}" CACHE STRING "${description}")
set_property_and_validate(${name} ${allowed})
set(${name} "${${name}}" PARENT_SCOPE)
endfunction()
# case insensitive comparison
function(STRIEQUAL str1 str2 is_equal)
string(TOUPPER "${str1}" str1_upper)
string(TOUPPER "${str2}" str2_upper)
if(str1_upper STREQUAL str2_upper)
set(${is_equal} TRUE PARENT_SCOPE)
else()
set(${is_equal} FALSE PARENT_SCOPE)
endif()
endfunction()
# check if `symbol` is declared in `headers` and links when also linking with `libs`
function(check_symbol_exists_with_libs symbol headers libs outvar)
cmake_push_check_state()
list(APPEND CMAKE_REQUIRED_LIBRARIES ${libs})
check_symbol_exists(${symbol} "${headers}" ${outvar})
cmake_pop_check_state()
endfunction()
# we prefer -l<lib> test over `find_library` as sometimes core libraries
# like `libatomic` are not on the system path (see issue #898)
function(find_link_library libname outlibname)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES ${libname})
check_c_source_compiles("int main(void) { return 0; }" mi_has_lib${libname})
cmake_pop_check_state()
if (mi_has_lib${libname})
message(VERBOSE "link library: -l${libname}")
set(${outlibname} ${libname} PARENT_SCOPE)
else()
find_library(MI_LIBPATH_${libname} ${libname})
if (MI_LIBPATH_${libname})
message(VERBOSE "link library ${libname} at ${MI_LIBPATH_${libname}}")
set(${outlibname} ${MI_LIBPATH_${libname}} PARENT_SCOPE)
else()
message(VERBOSE "link library not found: ${libname}")
set(${outlibname} "" PARENT_SCOPE)
endif()
endif()
endfunction()
# describe the compiler for `lang` (C or CXX) as its path (with symlinks resolved), id, and version
function(compiler_info lang outvar)
set(compiler "${CMAKE_${lang}_COMPILER}")
get_filename_component(compiler_path "${compiler}" REALPATH)
if(NOT compiler_path STREQUAL compiler)
string(APPEND compiler " -> ${compiler_path}")
endif()
set(${outvar} "${compiler} (${CMAKE_${lang}_COMPILER_ID} ${CMAKE_${lang}_COMPILER_VERSION}, ${lang})" PARENT_SCOPE)
endfunction()