diff --git a/CMakeLists.txt b/CMakeLists.txt index a9cc3380..c1dbacd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,14 @@ option(SOCI_TESTS "Enable building SOCI test cases" ${PROJECT_IS_TOP_LEVEL}) option(WITH_BOOST "Enable Boost support" ON) cmake_dependent_option(SOCI_LTO "Enable link time optimizations in release builds" ON "LTO_AVAILABLE" OFF) +# Determine if the CMake generator is single or multi config. This will be +# useful later when printing the SOCI configuration in show_config. +get_property(SOCI_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) + +# Default build type to Release for single-config generators +if(NOT SOCI_IS_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() # Configure LTO for anything but Debug builds (if enabled in the first place) set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ${SOCI_LTO}) @@ -159,10 +167,16 @@ endif() # Also make it possible to see SOCI configuration later. add_custom_target(show_config - COMMAND ${CMAKE_COMMAND} -E echo "SOCI ${SOCI_VERSION} configured with:" - COMMAND ${CMAKE_COMMAND} -E echo " Build type: ${CMAKE_BUILD_TYPE}" - COMMAND ${CMAKE_COMMAND} -E echo " Library type: ${SOCI_LIB_TYPE}" - COMMAND ${CMAKE_COMMAND} -E echo " Enabled backends: ${SOCI_ENABLED_BACKENDS}" + COMMAND ${CMAKE_COMMAND} -E echo + "SOCI ${SOCI_VERSION} configured with:" + COMMAND ${CMAKE_COMMAND} -E echo + # If using multi-config generator CMAKE_BUILD_TYPE is not meaningful. One + # would instead select a build type from CMAKE_CONFIGURATION_TYPES. + " Build type: $" + COMMAND ${CMAKE_COMMAND} -E echo + " Library type: ${SOCI_LIB_TYPE}" + COMMAND ${CMAKE_COMMAND} -E echo + " Enabled backends: ${SOCI_ENABLED_BACKENDS}" COMMENT "Show SOCI build configuration summary" VERBATIM diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7aff9587..f95535b2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -57,18 +57,18 @@ list(APPEND CONFIG_LINES "#endif") list(JOIN CONFIG_LINES "\n" CONFIG_CONTENT) -string(MD5 CONFIG_CONTENT_HASH "${CONFIG_CONTENT}") -if (EXISTS "${SOCI_GENERATED_INCLUDES_DIR}/soci/soci-config.h") - file(MD5 "${SOCI_GENERATED_INCLUDES_DIR}/soci/soci-config.h" CONFIG_FILE_HASH) -else() - set(CONFIG_FILE_HASH 0) -endif() - -# Only overwrite the soci-config.h file if the generated content is different from the -# file's content in order to avoid needless rebuilding -if (NOT (CONFIG_CONTENT_HASH STREQUAL CONFIG_FILE_HASH)) - file(WRITE "${SOCI_GENERATED_INCLUDES_DIR}/soci/soci-config.h" "${CONFIG_CONTENT}") -endif() +# We use file(GENERATE) to only overwrite soci-config.h if the generated +# content to be written has differed from the file's content. Note that we do +# *not* use file(WRITE) because the line endings would be CRLF on Windows, so +# the file MD5 hash would *never* equal that of the content hash, as the string +# content is of course using standard LF line endings. Using file(WRITE) would +# mean that soci-config.h is updated on each configure, so each subsequent +# cmake --build would result in the needless rebuilding we wanted to avoid. +# +file(GENERATE + OUTPUT "${SOCI_GENERATED_INCLUDES_DIR}/soci/soci-config.h" + CONTENT "${CONFIG_CONTENT}" NEWLINE_STYLE LF +) # Append the generated config header to the public headers of the core target target_sources(soci_core