From 12fb004766caeb376f40fff0ee4277414d6fdfc4 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Sun, 8 Sep 2019 11:54:31 +0200 Subject: [PATCH] Add param @ONLY to CMake configure_file() Restrict variable replacement to references of the form @VAR@. This is useful for configuring scripts that also use ${VAR} syntax. Adding @ONLY is precautionary action to prevent future bugs. Attn. cmake/config.h.in contains ${VAR} in #cmakedefine clauses! How to use CMake variable substitution. Wrong: #if @HAVE_FORK@ [..] #endif Because this will result as: #if 1 [..] #endif Better: /* Imported CMake variables created during build. */ #cmakedefine HAVE_FORK 1 #ifdef HAVE_FORK [..] #endif Because this will result as: /* Imported CMake variables created during build. */ #define HAVE_FORK 1 #ifdef HAVE_FORK [..] #endif Or: /* Imported CMake variables created during build. */ /* #undef HAVE_FORK */ [..] This is more readable. Now we know which decision actually was taken. If you need to pass the parameter to the definition, it is just as easy: /* Imported CMake variables created during build. */ #cmakedefine CORRECT_SIZE @CORRECT_SIZE@ #if CORRECT_SIZE >= 20 [..] #endif Signed-off-by: Mikko Johannes Koivunalho --- CMakeLists.txt | 3 ++- src/CMakeLists.txt | 2 +- tests/CMakeLists.txt | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b93c18..f4b958f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -358,11 +358,12 @@ endif (HAVE_SUBUNIT) # Generate "config.h" from "cmake/config.h.in" configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h) + # Param @ONLY not used on purpose! include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}) add_definitions(-DHAVE_CONFIG_H) set(CONFIG_HEADER ${CMAKE_CURRENT_BINARY_DIR}/config.h) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_stdint.h.in - ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h) + ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h @ONLY) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h DESTINATION include) ############################################################################### diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6a86811..8b12667 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,7 +43,7 @@ set(HEADERS check_print.h check_str.h) -configure_file(check.h.in check.h) +configure_file(check.h.in check.h @ONLY) add_library(check STATIC ${SOURCES} ${HEADERS}) target_link_libraries(check ${LIBM} ${LIBRT} ${SUBUNIT}) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7ce8389..82e3b43 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -38,7 +38,7 @@ if(WIN32) endif(WIN32) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/test_vars.in - ${CMAKE_CURRENT_BINARY_DIR}/test_vars) + ${CMAKE_CURRENT_BINARY_DIR}/test_vars @ONLY) set(CHECK_CHECK_SOURCES check_check_exit.c -- 2.50.1