Add ability to set CMake options with env variables

This commit is contained in:
Saúl Ibarra Corretgé 2023-11-17 10:37:45 +01:00
parent d88e9df9de
commit f162a181b5

View file

@ -51,15 +51,27 @@ else()
add_compile_options(-g) add_compile_options(-g)
endif() endif()
option(BUILD_SHARED_LIBS "Build a shared library" OFF) macro(xoption OPTION_NAME OPTION_TEXT OPTION_DEFAULT)
option(${OPTION_NAME} ${OPTION_TEXT} ${OPTION_DEFAULT})
if(DEFINED ENV{${OPTION_NAME}})
# Allow setting the option through an environment variable.
set(${OPTION_NAME} $ENV{${OPTION_NAME}})
endif()
if(${OPTION_NAME})
add_definitions(-D${OPTION_NAME})
endif()
message(STATUS " ${OPTION_NAME}: ${${OPTION_NAME}}")
endmacro()
xoption(BUILD_SHARED_LIBS "Build a shared library" OFF)
if(BUILD_SHARED_LIBS) if(BUILD_SHARED_LIBS)
message(STATUS "Building a shared library") message(STATUS "Building a shared library")
endif() endif()
option(BUILD_EXAMPLES "Build examples" OFF) xoption(BUILD_EXAMPLES "Build examples" OFF)
option(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF) xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF)
option(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF) xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF)
option(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF) xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF)
if(CONFIG_ASAN) if(CONFIG_ASAN)
message(STATUS "Building with ASan") message(STATUS "Building with ASan")