Skip to content

Commit f162a18

Browse files
committed
Add ability to set CMake options with env variables
1 parent d88e9df commit f162a18

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

CMakeLists.txt

+17-5
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,27 @@ else()
5151
add_compile_options(-g)
5252
endif()
5353

54-
option(BUILD_SHARED_LIBS "Build a shared library" OFF)
54+
macro(xoption OPTION_NAME OPTION_TEXT OPTION_DEFAULT)
55+
option(${OPTION_NAME} ${OPTION_TEXT} ${OPTION_DEFAULT})
56+
if(DEFINED ENV{${OPTION_NAME}})
57+
# Allow setting the option through an environment variable.
58+
set(${OPTION_NAME} $ENV{${OPTION_NAME}})
59+
endif()
60+
if(${OPTION_NAME})
61+
add_definitions(-D${OPTION_NAME})
62+
endif()
63+
message(STATUS " ${OPTION_NAME}: ${${OPTION_NAME}}")
64+
endmacro()
65+
66+
xoption(BUILD_SHARED_LIBS "Build a shared library" OFF)
5567
if(BUILD_SHARED_LIBS)
5668
message(STATUS "Building a shared library")
5769
endif()
5870

59-
option(BUILD_EXAMPLES "Build examples" OFF)
60-
option(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF)
61-
option(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF)
62-
option(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF)
71+
xoption(BUILD_EXAMPLES "Build examples" OFF)
72+
xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF)
73+
xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF)
74+
xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF)
6375

6476
if(CONFIG_ASAN)
6577
message(STATUS "Building with ASan")

0 commit comments

Comments
 (0)