diff --git a/cmake/modules/SanitizeHelper.cmake b/cmake/modules/SanitizeHelper.cmake index 170819036..e01b8e414 100644 --- a/cmake/modules/SanitizeHelper.cmake +++ b/cmake/modules/SanitizeHelper.cmake @@ -1,32 +1,39 @@ macro(_sanitize REPLACEMENT_REGEX REPLACEMENT_STRING RAW_VAR SANITIZED_VAR) string(REGEX REPLACE "${REPLACEMENT_REGEX}" "${REPLACEMENT_STRING}" ${SANITIZED_VAR} "${RAW_VAR}") endmacro() # Sanitize a variable according to cmake rules # https://cmake.org/cmake/help/v3.10/manual/cmake-language.7.html#variable-references # The NUL and ';' characters cannot be escaped in this context (see CMP0053) macro(sanitize_variable PREFIX RAW_VAR SANITIZED_VAR) # Escaping characters not in the supported list (see documentation) will # work as long as the variable is not cached. # Variable caching is achieved by writing the variable to a CMakeCache.txt # file, where the escaped chars get interpreted. The issue occurs when the # cache is read, as the chars are not getting escaped again and cause the # read to fail. # The safe way to sanitize a variable is not to escape these chars, but # rather to replace them with a known supported one, here '_' is chosen. # Not: this could lead to name collision in some rare case. These case can # be handled manually by using a different prefix. _sanitize("([^a-zA-Z0-9/_.+-])" "_" "${PREFIX}${RAW_VAR}" ${SANITIZED_VAR}) endmacro() # Sanitize a variable intended to be used in a C/CXX #define statement. # This is useful when using CHECK__COMPILER_FLAG or similar functions. macro(sanitize_c_cxx_definition PREFIX RAW_VAR SANITIZED_VAR) # Only allow for alphanum chars plus underscore. This will prevent the # compiler to issue a warning like: # `ISO C99 requires whitespace after the macro name [-Wc99-extensions]` _sanitize("([^a-zA-Z0-9_])" "_" "${PREFIX}${RAW_VAR}" ${SANITIZED_VAR}) endmacro() + +# Sanitize a variable intended to be used as a cmake target name. +macro(sanitize_target_name PREFIX RAW_VAR SANITIZED_VAR) + # Target names may contain upper and lower case letters, numbers, + # underscore, dot, plus and minus. + _sanitize("([^a-zA-Z0-9_.+-])" "-" "${PREFIX}${RAW_VAR}" ${SANITIZED_VAR}) +endmacro() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8d9a57592..cae08c2aa 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,98 +1,95 @@ ### # Create config.ini file for tests ### set(abs_top_srcdir ${CMAKE_SOURCE_DIR}) set(abs_top_builddir ${CMAKE_BINARY_DIR}) if(CMAKE_SYSTEM_NAME MATCHES "Windows") set(EXEEXT ".exe") endif() if(NOT BUILD_BITCOIN_WALLET) set(ENABLE_WALLET_TRUE "#") endif() if(NOT BUILD_BITCOIN_TX OR NOT BUILD_BITCOIN_TX) set(BUILD_BITCOIN_UTILS_TRUE "#") endif() if(NOT BUILD_BITCOIN_ZMQ) set(ENABLE_ZMQ_TRUE "#") endif() # Create build ini file configure_file(config.ini.in config.ini @ONLY) ### # Setup symlinks for testing ### -macro(make_link src dest) +include(SanitizeHelper) +function(make_link file) + set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}") + set(dest "${CMAKE_CURRENT_BINARY_DIR}/${file}") add_custom_command( OUTPUT "${dest}" COMMAND ${CMAKE_COMMAND} -E create_symlink "${src}" "${dest}" - COMMENT "make_link ${src} -> ${dest}" + COMMENT "link ${file}" MAIN_DEPENDENCY "${src}" ) -endmacro() + # Add a phony target to make sure the files are linked by default. + sanitize_target_name("link-" "${file}" NAME) + add_custom_target(${NAME} ALL DEPENDS "${dest}") +endfunction() file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/functional/) file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/util/) -make_link( - ${CMAKE_CURRENT_SOURCE_DIR}/functional/test_runner.py - ${CMAKE_CURRENT_BINARY_DIR}/functional/test_runner.py -) -make_link( - ${CMAKE_CURRENT_SOURCE_DIR}/util/bitcoin-util-test.py - ${CMAKE_CURRENT_BINARY_DIR}/util/bitcoin-util-test.py -) -make_link( - ${CMAKE_CURRENT_SOURCE_DIR}/util/rpcauth-test.py - ${CMAKE_CURRENT_BINARY_DIR}/util/rpcauth-test.py -) +make_link(functional/test_runner.py) +make_link(util/bitcoin-util-test.py) +make_link(util/rpcauth-test.py) add_custom_target(check-functional COMMENT "Run functional tests..." COMMAND "${PYTHON_EXECUTABLE}" ./functional/test_runner.py DEPENDS ../src/bitcoind ../src/bitcoin-cli ${CMAKE_CURRENT_BINARY_DIR}/functional/test_runner.py USES_TERMINAL ) add_dependencies(check-all check-functional) if(BUILD_BITCOIN_TX) add_custom_target(check-bitcoin-util COMMENT "Test Bitcoin utilities..." COMMAND "${PYTHON_EXECUTABLE}" ./util/bitcoin-util-test.py DEPENDS ../src/bitcoin-tx ${CMAKE_CURRENT_BINARY_DIR}/util/bitcoin-util-test.py ) add_dependencies(check check-bitcoin-util) endif() add_custom_target(check-rpcauth COMMENT "Test Bitcoin RPC authentication..." COMMAND "${PYTHON_EXECUTABLE}" ./util/rpcauth-test.py DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/util/rpcauth-test.py ) add_dependencies(check check-rpcauth) include(PackageHelper) exclude_from_source_package( # Subdirectories "cache/" "lint/" "sanitizer_suppressions/" )