From f162a181b547ceb90bc22a0bd33bb627027c7871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Fri, 17 Nov 2023 10:37:45 +0100 Subject: [PATCH] Add ability to set CMake options with env variables --- CMakeLists.txt | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 927e534..a480b58 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,15 +51,27 @@ else() add_compile_options(-g) 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) message(STATUS "Building a shared library") endif() -option(BUILD_EXAMPLES "Build examples" OFF) -option(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF) -option(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF) -option(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF) +xoption(BUILD_EXAMPLES "Build examples" OFF) +xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF) +xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF) +xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF) if(CONFIG_ASAN) message(STATUS "Building with ASan")