diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d74b87c81..6e6c9ef3e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,6 +69,70 @@ set(BITPIT_INSTALL_CMAKEDIR "${CMAKE_INSTALL_DATAROOTDIR}/${CMAKE_PROJECT_NAME}/ #------------------------------------------------------------------------------------# # Functions #------------------------------------------------------------------------------------# +function(configureBitpitTargetDependencies TARGET_NAME DEPENDENCIES_SCOPE) + + if (MPI_FOUND) + target_link_libraries(${TARGET_NAME} ${DEPENDENCIES_SCOPE} MPI::MPI_CXX) + target_include_directories(${TARGET_NAME} ${DEPENDENCIES_SCOPE} $) + target_compile_definitions(${TARGET_NAME} ${DEPENDENCIES_SCOPE} $) + if ("Fortran" IN_LIST BITPIT_LANGUAGES) + target_link_libraries(${TARGET_NAME} ${DEPENDENCIES_SCOPE} MPI::MPI_Fortran) + target_include_directories(${TARGET_NAME} ${DEPENDENCIES_SCOPE} $) + target_compile_definitions(${TARGET_NAME} ${DEPENDENCIES_SCOPE} $) + endif() + + target_compile_definitions(${TARGET_NAME} ${DEPENDENCIES_SCOPE} "BITPIT_ENABLE_MPI=1") + else() + target_compile_definitions(${TARGET_NAME} ${DEPENDENCIES_SCOPE} "BITPIT_ENABLE_MPI=0") + endif() + + if (BLAS_FOUND) + target_link_libraries(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${BLAS_LIBRARIES}) + target_include_directories(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${BLAS_INCLUDE_DIRS}) + endif() + + if (CBLAS_FOUND) + set(CBLAS_PRIVATE_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/external/CBLAS/include") + + target_link_libraries(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${CBLAS_LIBRARIES}) + target_include_directories(${TARGET_NAME} PRIVATE ${CBLAS_PRIVATE_INCLUDE_DIR}) + target_include_directories(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${CBLAS_INCLUDE_DIRS}) + endif() + + if (LAPACK_FOUND) + target_link_libraries(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${LAPACK_LIBRARIES}) + target_include_directories(${TARGET_NAME} ${DEPENDENCIES_SCOPE}${LAPACK_INCLUDE_DIRS}) + endif() + + if (LAPACKE_FOUND) + set(LAPACKE_PRIVATE_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/external/LAPACKE/include") + + target_link_libraries(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${LAPACKE_LIBRARIES}) + target_include_directories(${TARGET_NAME} PRIVATE ${LAPACKE_PRIVATE_INCLUDE_DIR}) + target_include_directories(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${LAPACKE_INCLUDE_DIRS}) + endif() + + if (PETSC_FOUND) + target_link_libraries(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${PETSC_LIBRARIES}) + target_include_directories(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${PETSC_INCLUDES}) + endif() + unset(_PETSc_index) + + if (Boost_FOUND) + target_link_libraries(${TARGET_NAME} ${DEPENDENCIES_SCOPE} Boost::headers) + target_link_libraries(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${Boost_LIBRARIES}) + endif() + + if (METIS_FOUND) + target_link_libraries(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${METIS_LIBRARIES}) + target_include_directories(${TARGET_NAME} ${DEPENDENCIES_SCOPE} ${METIS_INCLUDE_DIRS}) + + target_compile_definitions(${BITPIT_LIBRARY} ${DEPENDENCIES_SCOPE} "BITPIT_ENABLE_METIS=1") + else() + target_compile_definitions(${BITPIT_LIBRARY} ${DEPENDENCIES_SCOPE} "BITPIT_ENABLE_METIS=0") + endif() +endfunction() + function(getModuleEnableName MODULE_NAME FLAG_NAME) string(TOUPPER ${MODULE_NAME} UPPER_MODULE_NAME) set(${FLAG_NAME} "BITPIT_MODULE_${UPPER_MODULE_NAME}" PARENT_SCOPE) @@ -122,19 +186,6 @@ function(hideModule MODULE_NAME) endfunction() function(addModuleIncludeDirectories TARGET_NAME MODULE_NAME) - set (EXTRA_ARGUMENTS ${ARGN}) - list(LENGTH EXTRA_ARGUMENTS EXTRA_ARGUMENT_COUNT) - if (${EXTRA_ARGUMENT_COUNT} GREATER 0) - list(GET EXTRA_ARGUMENTS 0 INCLUDE_EXTERNAL) - else () - set(INCLUDE_EXTERNAL "TRUE") - endif () - - # External includes - if (INCLUDE_EXTERNAL) - target_include_directories(${TARGET_NAME} PUBLIC "${BITPIT_EXTERNAL_INCLUDE_DIRS}") - endif () - # Add dependiecies string(TOUPPER ${MODULE_NAME} UPPER_MODULE_NAME) foreach (DEPENDENCY_NAME IN LISTS ${UPPER_MODULE_NAME}_DEPS) @@ -362,83 +413,6 @@ function(addCoverageAnalysis GENERATE_GCOVR_REPORTS GENERATE_LCOV_REPORTS EXCLUD endfunction() -#------------------------------------------------------------------------------------# -# Initialize library target -#------------------------------------------------------------------------------------# -set(BITPIT_LIBRARY ${PROJECT_NAME} CACHE INTERNAL "bitpit library name" FORCE) - -add_library(${BITPIT_LIBRARY}) - -target_compile_features(${BITPIT_LIBRARY} PUBLIC cxx_std_17) -set_target_properties(${BITPIT_LIBRARY} PROPERTIES CXX_STANDARD 17) -set_target_properties(${BITPIT_LIBRARY} PROPERTIES CXX_STANDARD_REQUIRED ON) - -if (BITPIT_ENABLE_MPI) - set_target_properties(${BITPIT_LIBRARY} PROPERTIES DEBUG_POSTFIX "_MPI_D") - set_target_properties(${BITPIT_LIBRARY} PROPERTIES RELWITHDEBINFO_POSTFIX "_MPI_D") - set_target_properties(${BITPIT_LIBRARY} PROPERTIES RELEASE_POSTFIX "_MPI") -else() - set_target_properties(${BITPIT_LIBRARY} PROPERTIES DEBUG_POSTFIX "_D") - set_target_properties(${BITPIT_LIBRARY} PROPERTIES RELWITHDEBINFO_POSTFIX "_D") -endif() - - -#------------------------------------------------------------------------------------# -# Link Time Optimization (LTO) -#------------------------------------------------------------------------------------# - -# Detect if LTO can be enabled -if(DEFINED BITPIT_LTO_STRATEGY) - if (NOT "${BITPIT_LTO_STRATEGY}" STREQUAL "Disabled") - include(CheckIPOSupported) - - if ("${BITPIT_LTO_STRATEGY}" STREQUAL "Enabled") - if (NOT BUILD_SHARED_LIBS) - message(FATAL_ERROR "LTO can be enabled only when building a shared library." ) - endif() - if (WIN32) - message(FATAL_ERROR "LTO in not currently supported on Windows.") - endif() - check_ipo_supported() - set(ENABLE_LTO TRUE) - elseif ("${BITPIT_LTO_STRATEGY}" STREQUAL "Auto") - if (NOT WIN32) - if (BUILD_SHARED_LIBS) - check_ipo_supported(RESULT LTO_SUPPORTED) - if (${LTO_SUPPORTED} AND "${CMAKE_BUILD_TYPE}" STREQUAL "Release") - set(ENABLE_LTO TRUE) - else() - set(ENABLE_LTO FALSE) - endif() - else() - set(ENABLE_LTO FALSE) - endif() - else() - set(ENABLE_LTO FALSE) - endif() - else() - set(ENABLE_LTO FALSE) - endif() - else() - set(ENABLE_LTO FALSE) - endif() -else() - set(ENABLE_LTO FALSE) -endif() - -# Set LTO property for the specified target -function(set_lto_property TARGET_NAME) - if (${ENABLE_LTO}) - set_target_properties(${TARGET_NAME} PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE) - endif() -endfunction() - -# Set LTO property for the main library -# -# Here LTO property is set only for the main library, each module may need -# to set its own LTO property. -set_lto_property(${BITPIT_LIBRARY}) - #------------------------------------------------------------------------------------# # Sanitize #------------------------------------------------------------------------------------# @@ -465,82 +439,6 @@ if(NOT CMAKE_BUILD_TYPE) set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${SUPPORTED_BUILD_TYPE}) endif() -#------------------------------------------------------------------------------------# -# Version -#------------------------------------------------------------------------------------# - -# Version regex expressions -set(VERSION_MATCHER_REGEX "^${CMAKE_PROJECT_NAME}-((([0-9]*)(\\.([0-9]*))?(\\.([0-9]*)))(-(.*))?)$") -set(VERSION_TAG_MATCHER_REGEX "^${CMAKE_PROJECT_NAME}-[^-]*(-(.*))?$") - -# Initialize version string -set(BITPIT_VERSION_STRING "${CMAKE_PROJECT_NAME}-${BITPIT_VERSION}") - -string(REGEX MATCH "${VERSION_MATCHER_REGEX}" VERSION_STRING_VALID "${BITPIT_VERSION_STRING}") -if ("${VERSION_STRING_VALID}" STREQUAL "") - message(FATAL_ERROR "Version string \"${BITPIT_VERSION_STRING}\" is not a valid version.") -endif() - -# Initialize verbose version string -set(BITPIT_VERBOSE_VERSION_STRING "${BITPIT_VERSION_STRING}") - -get_git_head_revision(BITPIT_GIT_REFERENCE BITPIT_GIT_HASH) -if(NOT "${BITPIT_GIT_REFERENCE}" STREQUAL "GITDIR-NOTFOUND") - # Add git information - # - # Information about git are added only if git tag doens't match the version - # string. - git_get_exact_tag(BITPIT_GIT_TAG) - if(NOT "${BITPIT_GIT_TAG}" STREQUAL "${BITPIT_VERBOSE_VERSION_STRING}") - git_get_branch(BITPIT_GIT_BRANCH) - if(NOT "${BITPIT_GIT_BRANCH}" MATCHES "NOTFOUND$") - set(BITPIT_VERBOSE_VERSION_STRING "${BITPIT_VERBOSE_VERSION_STRING}-${BITPIT_GIT_BRANCH}") - else() - set(BITPIT_VERBOSE_VERSION_STRING "${BITPIT_VERBOSE_VERSION_STRING}-detached") - endif() - - git_get_short_hash(BITPIT_GIT_SHORT_HASH) - set(BITPIT_VERBOSE_VERSION_STRING "${BITPIT_VERBOSE_VERSION_STRING}-${BITPIT_GIT_SHORT_HASH}") - endif() - - # Report if there are uncommited changes - # - # It's not possible to automatically re-run cmake (and thus update the - # version) if there are uncommited changes. The label will be reliably - # added only if, after changing the code, cmake is run manually. - set(BITPIT_VERBOSE_VERSION_DIRTY_TAG "dirty" CACHE STRING "Tag that will be added to the verbose version string when there are uncommited changes") - mark_as_advanced(BITPIT_VERBOSE_VERSION_DIRTY_TAG) - - git_local_changes(BITPIT_GIT_STATUS) - if (BITPIT_GIT_STATUS STREQUAL "DIRTY") - set(BITPIT_VERBOSE_VERSION_STRING "${BITPIT_VERBOSE_VERSION_STRING}-${BITPIT_VERBOSE_VERSION_DIRTY_LABEL}") - endif() -endif() - -string(REGEX MATCH "${VERSION_MATCHER_REGEX}" VERSION_STRING_VALID "${BITPIT_VERBOSE_VERSION_STRING}") -if ("${VERSION_STRING_VALID}" STREQUAL "") - message(FATAL_ERROR "Version string \"${BITPIT_VERBOSE_VERSION_STRING}\" is not a valid verbose version.") -endif() - -# Extract version information -string(REGEX REPLACE "${VERSION_MATCHER_REGEX}" "\\1" BITPIT_VERBOSE_VERSION "${BITPIT_VERBOSE_VERSION_STRING}") - -string(REGEX REPLACE "${VERSION_MATCHER_REGEX}" "\\1" BITPIT_VERSION "${BITPIT_VERSION_STRING}") -string(REGEX REPLACE "${VERSION_MATCHER_REGEX}" "\\3" BITPIT_MAJOR_VERSION "${BITPIT_VERSION_STRING}") -string(REGEX REPLACE "${VERSION_MATCHER_REGEX}" "\\5" BITPIT_MINOR_VERSION "${BITPIT_VERSION_STRING}") -string(REGEX REPLACE "${VERSION_MATCHER_REGEX}" "\\7" BITPIT_PATCH_VERSION "${BITPIT_VERSION_STRING}") - -string(REGEX MATCH "${VERSION_TAG_MATCHER_REGEX}" dummy "${BITPIT_VERSION_STRING}") -if (NOT CMAKE_MATCH_2 STREQUAL "") - string(REGEX REPLACE "${VERSION_TAG_MATCHER_REGEX}" "\\2" BITPIT_TAG_VERSION "${BITPIT_VERSION_STRING}") -else () - set(BITPIT_TAG_VERSION "") -endif () - -# Set library version -set_target_properties(${BITPIT_LIBRARY} PROPERTIES VERSION "${BITPIT_MAJOR_VERSION}.${BITPIT_MINOR_VERSION}.${BITPIT_PATCH_VERSION}" - SOVERSION "${BITPIT_MAJOR_VERSION}") - #------------------------------------------------------------------------------------# # List of all avilable modules #------------------------------------------------------------------------------------# @@ -755,35 +653,16 @@ else() endforeach() endif() -# Add external dependencies +# Find external dependencies # # Low-level dependencies are processed first. set(BITPIT_EXTERNAL_DEPENDENCIES "") -set(BITPIT_EXTERNAL_VARIABLES_LIBRARIES "") -set(BITPIT_EXTERNAL_VARIABLES_INCLUDE_DIRS "") list(FIND EXTERNAL_DEPS "MPI" _MPI_index) if (${_MPI_index} GREATER -1) find_package(MPI) - target_compile_definitions(${BITPIT_LIBRARY} PUBLIC "BITPIT_ENABLE_MPI=1") - - if(MPI_CXX_COMPILE_FLAGS) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MPI_CXX_COMPILE_FLAGS}") - endif() - - if(MPI_CXX_LINK_FLAGS) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MPI_CXX_LINK_FLAGS}") - endif() - list (INSERT BITPIT_EXTERNAL_DEPENDENCIES 0 "MPI") - list (INSERT BITPIT_EXTERNAL_VARIABLES_LIBRARIES 0 "MPI_CXX_LIBRARIES") - if ("Fortran" IN_LIST BITPIT_LANGUAGES) - list (INSERT BITPIT_EXTERNAL_VARIABLES_LIBRARIES 0 "MPI_Fortran_LIBRARIES") - endif() - list (INSERT BITPIT_EXTERNAL_VARIABLES_INCLUDE_DIRS 0 "MPI_CXX_INCLUDE_PATH" ) -else() - target_compile_definitions(${BITPIT_LIBRARY} PUBLIC "BITPIT_ENABLE_MPI=0") endif() unset(_MPI_index) @@ -796,8 +675,6 @@ if (${_BLAS_index} GREATER -1) find_package(BLAS REQUIRED) list (INSERT BITPIT_EXTERNAL_DEPENDENCIES 0 "BLAS") - list (INSERT BITPIT_EXTERNAL_VARIABLES_LIBRARIES 0 "BLAS_LIBRARIES") - list (INSERT BITPIT_EXTERNAL_VARIABLES_INCLUDE_DIRS 0 "BLAS_INCLUDE_DIRS") endif() unset(_BLAS_index) @@ -807,11 +684,7 @@ if (${_CBLAS_index} GREATER -1) find_package(CBLAS REQUIRED) - set(CBLAS_PRIVATE_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/external/CBLAS/include") - list (INSERT BITPIT_EXTERNAL_DEPENDENCIES 0 "CBLAS") - list (INSERT BITPIT_EXTERNAL_VARIABLES_LIBRARIES 0 "CBLAS_LIBRARIES") - list (INSERT BITPIT_EXTERNAL_VARIABLES_INCLUDE_DIRS 0 "CBLAS_PRIVATE_INCLUDE_DIR" "CBLAS_INCLUDE_DIRS") endif() unset(_CBLAS_index) @@ -820,8 +693,6 @@ if (${_LAPACK_index} GREATER -1) find_package(LAPACK REQUIRED) list (INSERT BITPIT_EXTERNAL_DEPENDENCIES 0 "LAPACK") - list (INSERT BITPIT_EXTERNAL_VARIABLES_LIBRARIES 0 "LAPACK_LIBRARIES") - list (INSERT BITPIT_EXTERNAL_VARIABLES_INCLUDE_DIRS 0 "LAPACK_INCLUDE_DIRS") endif() unset(_LAPACK_index) @@ -834,11 +705,7 @@ if (${_LAPACKE_index} GREATER -1) find_package(LAPACKE REQUIRED) - set(LAPACKE_PRIVATE_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/external/LAPACKE/include") - list (INSERT BITPIT_EXTERNAL_DEPENDENCIES 0 "LAPACKE") - list (INSERT BITPIT_EXTERNAL_VARIABLES_LIBRARIES 0 "LAPACKE_LIBRARIES") - list (INSERT BITPIT_EXTERNAL_VARIABLES_INCLUDE_DIRS 0 "LAPACKE_PRIVATE_INCLUDE_DIR" "LAPACKE_INCLUDE_DIRS") endif() unset(_LAPACKE_index) @@ -863,8 +730,6 @@ if (${_PETSc_index} GREATER -1) find_package(PETSc REQUIRED) list (INSERT BITPIT_EXTERNAL_DEPENDENCIES 0 "PETSc") - list (INSERT BITPIT_EXTERNAL_VARIABLES_LIBRARIES 0 "PETSC_LIBRARIES") - list (INSERT BITPIT_EXTERNAL_VARIABLES_INCLUDE_DIRS 0 "PETSC_INCLUDES" ) endif() unset(_PETSc_index) @@ -880,8 +745,6 @@ if (${_Boost_index} GREATER -1) endif () list (INSERT BITPIT_EXTERNAL_DEPENDENCIES 0 "Boost") - list (INSERT BITPIT_EXTERNAL_VARIABLES_LIBRARIES 0 "Boost_LIBRARIES") - list (INSERT BITPIT_EXTERNAL_VARIABLES_INCLUDE_DIRS 0 "Boost_INCLUDE_DIR") endif() unset(_BoostTest_index) @@ -892,34 +755,169 @@ if (${_METIS_index} GREATER -1) find_package(METIS) if (METIS_FOUND) - target_compile_definitions(${BITPIT_LIBRARY} PUBLIC "BITPIT_ENABLE_METIS=1") - list (APPEND BITPIT_EXTERNAL_DEPENDENCIES "METIS") - list (APPEND BITPIT_EXTERNAL_VARIABLES_LIBRARIES "METIS_LIBRARIES") - list (APPEND BITPIT_EXTERNAL_VARIABLES_INCLUDE_DIRS "METIS_INCLUDE_DIRS") else() - target_compile_definitions(${BITPIT_LIBRARY} PUBLIC "BITPIT_ENABLE_METIS=0") - message(STATUS "METIS library not found, default patch partitioning support will be disabled.") endif() endif() unset(_METIS_index) -set(BITPIT_EXTERNAL_LIBRARIES "") -foreach (VARIABLE_NAME IN LISTS BITPIT_EXTERNAL_VARIABLES_LIBRARIES) - list (APPEND BITPIT_EXTERNAL_LIBRARIES "${${VARIABLE_NAME}}") -endforeach () +#------------------------------------------------------------------------------------# +# Initialize library target +#------------------------------------------------------------------------------------# +set(BITPIT_LIBRARY ${PROJECT_NAME} CACHE INTERNAL "bitpit library name" FORCE) -set(BITPIT_EXTERNAL_INCLUDE_DIRS "") -foreach (VARIABLE_NAME IN LISTS BITPIT_EXTERNAL_VARIABLES_INCLUDE_DIRS) - list (APPEND BITPIT_EXTERNAL_INCLUDE_DIRS "${${VARIABLE_NAME}}") -endforeach () +add_library(${BITPIT_LIBRARY}) +configureBitpitTargetDependencies(${BITPIT_LIBRARY} PUBLIC) + +set_target_properties(${BITPIT_LIBRARY} + PROPERTIES + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO +) + +if (BITPIT_ENABLE_MPI) + set_target_properties(${BITPIT_LIBRARY} PROPERTIES DEBUG_POSTFIX "_MPI_D") + set_target_properties(${BITPIT_LIBRARY} PROPERTIES RELWITHDEBINFO_POSTFIX "_MPI_D") + set_target_properties(${BITPIT_LIBRARY} PROPERTIES RELEASE_POSTFIX "_MPI") +else() + set_target_properties(${BITPIT_LIBRARY} PROPERTIES DEBUG_POSTFIX "_D") + set_target_properties(${BITPIT_LIBRARY} PROPERTIES RELWITHDEBINFO_POSTFIX "_D") +endif() + +#------------------------------------------------------------------------------------# +# Version +#------------------------------------------------------------------------------------# + +# Version regex expressions +set(VERSION_MATCHER_REGEX "^${CMAKE_PROJECT_NAME}-((([0-9]*)(\\.([0-9]*))?(\\.([0-9]*)))(-(.*))?)$") +set(VERSION_TAG_MATCHER_REGEX "^${CMAKE_PROJECT_NAME}-[^-]*(-(.*))?$") + +# Initialize version string +set(BITPIT_VERSION_STRING "${CMAKE_PROJECT_NAME}-${BITPIT_VERSION}") + +string(REGEX MATCH "${VERSION_MATCHER_REGEX}" VERSION_STRING_VALID "${BITPIT_VERSION_STRING}") +if ("${VERSION_STRING_VALID}" STREQUAL "") + message(FATAL_ERROR "Version string \"${BITPIT_VERSION_STRING}\" is not a valid version.") +endif() + +# Initialize verbose version string +set(BITPIT_VERBOSE_VERSION_STRING "${BITPIT_VERSION_STRING}") + +get_git_head_revision(BITPIT_GIT_REFERENCE BITPIT_GIT_HASH) +if(NOT "${BITPIT_GIT_REFERENCE}" STREQUAL "GITDIR-NOTFOUND") + # Add git information + # + # Information about git are added only if git tag doens't match the version + # string. + git_get_exact_tag(BITPIT_GIT_TAG) + if(NOT "${BITPIT_GIT_TAG}" STREQUAL "${BITPIT_VERBOSE_VERSION_STRING}") + git_get_branch(BITPIT_GIT_BRANCH) + if(NOT "${BITPIT_GIT_BRANCH}" MATCHES "NOTFOUND$") + set(BITPIT_VERBOSE_VERSION_STRING "${BITPIT_VERBOSE_VERSION_STRING}-${BITPIT_GIT_BRANCH}") + else() + set(BITPIT_VERBOSE_VERSION_STRING "${BITPIT_VERBOSE_VERSION_STRING}-detached") + endif() + + git_get_short_hash(BITPIT_GIT_SHORT_HASH) + set(BITPIT_VERBOSE_VERSION_STRING "${BITPIT_VERBOSE_VERSION_STRING}-${BITPIT_GIT_SHORT_HASH}") + endif() + + # Report if there are uncommited changes + # + # It's not possible to automatically re-run cmake (and thus update the + # version) if there are uncommited changes. The label will be reliably + # added only if, after changing the code, cmake is run manually. + set(BITPIT_VERBOSE_VERSION_DIRTY_TAG "dirty" CACHE STRING "Tag that will be added to the verbose version string when there are uncommited changes") + mark_as_advanced(BITPIT_VERBOSE_VERSION_DIRTY_TAG) + + git_local_changes(BITPIT_GIT_STATUS) + if (BITPIT_GIT_STATUS STREQUAL "DIRTY") + set(BITPIT_VERBOSE_VERSION_STRING "${BITPIT_VERBOSE_VERSION_STRING}-${BITPIT_VERBOSE_VERSION_DIRTY_LABEL}") + endif() +endif() + +string(REGEX MATCH "${VERSION_MATCHER_REGEX}" VERSION_STRING_VALID "${BITPIT_VERBOSE_VERSION_STRING}") +if ("${VERSION_STRING_VALID}" STREQUAL "") + message(FATAL_ERROR "Version string \"${BITPIT_VERBOSE_VERSION_STRING}\" is not a valid verbose version.") +endif() + +# Extract version information +string(REGEX REPLACE "${VERSION_MATCHER_REGEX}" "\\1" BITPIT_VERBOSE_VERSION "${BITPIT_VERBOSE_VERSION_STRING}") + +string(REGEX REPLACE "${VERSION_MATCHER_REGEX}" "\\1" BITPIT_VERSION "${BITPIT_VERSION_STRING}") +string(REGEX REPLACE "${VERSION_MATCHER_REGEX}" "\\3" BITPIT_MAJOR_VERSION "${BITPIT_VERSION_STRING}") +string(REGEX REPLACE "${VERSION_MATCHER_REGEX}" "\\5" BITPIT_MINOR_VERSION "${BITPIT_VERSION_STRING}") +string(REGEX REPLACE "${VERSION_MATCHER_REGEX}" "\\7" BITPIT_PATCH_VERSION "${BITPIT_VERSION_STRING}") + +string(REGEX MATCH "${VERSION_TAG_MATCHER_REGEX}" dummy "${BITPIT_VERSION_STRING}") +if (NOT CMAKE_MATCH_2 STREQUAL "") + string(REGEX REPLACE "${VERSION_TAG_MATCHER_REGEX}" "\\2" BITPIT_TAG_VERSION "${BITPIT_VERSION_STRING}") +else () + set(BITPIT_TAG_VERSION "") +endif () -# Windows shared libraries have to be linked against their dependencies. -if (WIN32 AND BUILD_SHARED_LIBS) - target_link_libraries(${BITPIT_LIBRARY} ${BITPIT_EXTERNAL_LIBRARIES}) +# Set library version +set_target_properties(${BITPIT_LIBRARY} PROPERTIES VERSION "${BITPIT_MAJOR_VERSION}.${BITPIT_MINOR_VERSION}.${BITPIT_PATCH_VERSION}" + SOVERSION "${BITPIT_MAJOR_VERSION}") + +#------------------------------------------------------------------------------------# +# Link Time Optimization (LTO) +#------------------------------------------------------------------------------------# + +# Detect if LTO can be enabled +if(DEFINED BITPIT_LTO_STRATEGY) + if (NOT "${BITPIT_LTO_STRATEGY}" STREQUAL "Disabled") + include(CheckIPOSupported) + + if ("${BITPIT_LTO_STRATEGY}" STREQUAL "Enabled") + if (NOT BUILD_SHARED_LIBS) + message(FATAL_ERROR "LTO can be enabled only when building a shared library." ) + endif() + if (WIN32) + message(FATAL_ERROR "LTO in not currently supported on Windows.") + endif() + check_ipo_supported() + set(ENABLE_LTO TRUE) + elseif ("${BITPIT_LTO_STRATEGY}" STREQUAL "Auto") + if (NOT WIN32) + if (BUILD_SHARED_LIBS) + check_ipo_supported(RESULT LTO_SUPPORTED) + if (${LTO_SUPPORTED} AND "${CMAKE_BUILD_TYPE}" STREQUAL "Release") + set(ENABLE_LTO TRUE) + else() + set(ENABLE_LTO FALSE) + endif() + else() + set(ENABLE_LTO FALSE) + endif() + else() + set(ENABLE_LTO FALSE) + endif() + else() + set(ENABLE_LTO FALSE) + endif() + else() + set(ENABLE_LTO FALSE) + endif() +else() + set(ENABLE_LTO FALSE) endif() +# Set LTO property for the specified target +function(set_lto_property BITPIT_LIBRARY) + if (${ENABLE_LTO}) + set_target_properties(${BITPIT_LIBRARY} PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE) + endif() +endfunction() + +# Set LTO property for the main library +# +# Here LTO property is set only for the main library, each module may need +# to set its own LTO property. +set_lto_property(${BITPIT_LIBRARY}) + #------------------------------------------------------------------------------------# # Compiler settings #------------------------------------------------------------------------------------# @@ -1168,6 +1166,29 @@ endif() #------------------------------------------------------------------------------------# get_property(BITPIT_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES) +#------------------------------------------------------------------------------------# +# Installation +#------------------------------------------------------------------------------------# + +install(TARGETS ${BITPIT_LIBRARY} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_PROJECT_NAME} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_PROJECT_NAME} +) + +if (MSVC) + # Install the exported debug information (if any, see OPTIONAL) related to bitpit library + install(FILES $ DESTINATION ${CMAKE_INSTALL_LIBDIR} OPTIONAL) +endif() + +target_include_directories( + ${BITPIT_LIBRARY} + INTERFACE + $ +) + #------------------------------------------------------------------------------------# # Subdirectories #------------------------------------------------------------------------------------# @@ -1194,20 +1215,3 @@ if (BUILD_TESTING) enable_testing() add_subdirectory(test) endif() - -#------------------------------------------------------------------------------------# -# Installation -#------------------------------------------------------------------------------------# - -install(TARGETS ${BITPIT_LIBRARY} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_PROJECT_NAME} - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_PROJECT_NAME} - ) - -if (MSVC) - # Install the exported debug information (if any, see OPTIONAL) related to bitpit library - install(FILES $ DESTINATION ${CMAKE_INSTALL_LIBDIR} OPTIONAL) -endif() diff --git a/cmake/BITPITConfig.cmake.in b/cmake/BITPITConfig.cmake.in index 3fd837744c..30efcc7e3c 100644 --- a/cmake/BITPITConfig.cmake.in +++ b/cmake/BITPITConfig.cmake.in @@ -1,112 +1,9 @@ -# BITPITConfig.cmake - bitpit CMake configuration file for external projects. -# ----------- -# -# This file is configured by bitpit and used by the UseBITPIT.cmake module -# to load bitpit's settings for an external project. - -# Compute the installation prefix from this BITPITConfig.cmake file location. -@BITPIT_INSTALL_PREFIX_CODE@ - -# If a different UseBITPIT.cmake was previoulsy loaded a reconfiguration -# is needed -if(NOT ("${BITPIT_INSTALL_PREFIX}/@BITPIT_INSTALL_CMAKEDIR@" STREQUAL BITPIT_INSTALL_CMAKEDIR_PREVIOUS)) - set(BITPIT_RECONFIGURE 1) - set(BITPIT_INSTALL_CMAKEDIR_PREVIOUS "${BITPIT_INSTALL_PREFIX}/@BITPIT_INSTALL_CMAKEDIR@" CACHE INTERNAL "Defines the previous bitpit CMake configuration file loaded") -endif() - -# The C and C++ flags added by bitpit to the cmake-configured flags. -SET(BITPIT_REQUIRED_C_FLAGS "") -SET(BITPIT_REQUIRED_CXX_FLAGS "") -SET(BITPIT_REQUIRED_EXE_LINKER_FLAGS "") -SET(BITPIT_REQUIRED_SHARED_LINKER_FLAGS "") -SET(BITPIT_REQUIRED_MODULE_LINKER_FLAGS "") - -# The bitpit version number -SET(BITPIT_MAJOR_VERSION "@BITPIT_MAJOR_VERSION@") -SET(BITPIT_MINOR_VERSION "@BITPIT_MINOR_VERSION@") -SET(BITPIT_PATCH_VERSION "@BITPIT_PATCH_VERSION@") -SET(BITPIT_VERSION "@BITPIT_VERSION@") - -# The location of the UseBITPIT.cmake file. -SET(BITPIT_CMAKE_DIR "${BITPIT_INSTALL_PREFIX}/@BITPIT_INSTALL_CMAKEDIR@") -SET(BITPIT_USE_FILE "${BITPIT_CMAKE_DIR}/UseBITPIT.cmake") - -# Flag for shared build -SET(BITPIT_SHARED "@BUILD_SHARED_LIBS@") - -# Include macros for finding packages -list(APPEND CMAKE_MODULE_PATH ${BITPIT_CMAKE_DIR}) -include(LibFindMacros) -include(FindPackageHandleStandardArgs) +@PACKAGE_INIT@ #----------------------------------------------------------------------------- -# Find bitpit libraries and headers +# Modules #----------------------------------------------------------------------------- -# Headers -if(BITPIT_RECONFIGURE) - unset(BITPIT_INCLUDE_DIR CACHE) -endif() - -find_path(BITPIT_INCLUDE_DIR "@PROJECT_NAME@.hpp" - HINTS "${BITPIT_INSTALL_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@/@PROJECT_NAME@/") - -mark_as_advanced(BITPIT_INCLUDE_DIR) - -# Library -if(BITPIT_RECONFIGURE) - unset(BITPIT_LIBRARY_RELEASE CACHE) - unset(BITPIT_LIBRARY_DEBUG CACHE) -endif() - -find_library(BITPIT_LIBRARY_RELEASE - NAMES @PROJECT_NAME@@BITPIT_RELEASE_POSTFIX@ @PROJECT_NAME@ - HINTS "${BITPIT_INSTALL_PREFIX}/@CMAKE_INSTALL_LIBDIR@") - -find_library(BITPIT_LIBRARY_DEBUG - NAMES @PROJECT_NAME@@BITPIT_DEBUG_POSTFIX@ @PROJECT_NAME@ - HINTS "${BITPIT_INSTALL_PREFIX}/@CMAKE_INSTALL_LIBDIR@") - -mark_as_advanced(BITPIT_LIBRARY_RELEASE) -mark_as_advanced(BITPIT_LIBRARY_DEBUG) - -# Choose good values for BITPIT_LIBRARY, BITPIT_LIBRARIES, -# BITPIT_LIBRARY_DEBUG, and BITPIT_LIBRARY_RELEASE depending on what -# has been found and set. If only BITPIT_LIBRARY_RELEASE is defined, -# BITPIT_LIBRARY will be set to the release value, and -# BITPIT_LIBRARY_DEBUG will be set to BITPIT_LIBRARY_DEBUG-NOTFOUND. -# If only BITPIT_LIBRARY_DEBUG is defined, then BITPIT_LIBRARY will -# take the debug value, and BITPIT_LIBRARY_RELEASE will be set to -# BITPIT_LIBRARY_RELEASE-NOTFOUND. -# -# If the generator supports configuration types, then BITPIT_LIBRARY -# and BITPIT_LIBRARIES will be set with debug and optimized flags -# specifying the library to be used for the given configuration. If no -# build type has been set or the generator in use does not support -# configuration types, then BITPIT_LIBRARY and BITPIT_LIBRARIES will -# take only the release value, or the debug value if the release one is -# not set. -if (BITPIT_LIBRARY_DEBUG AND BITPIT_LIBRARY_RELEASE AND - NOT BITPIT_LIBRARY_DEBUG STREQUAL BITPIT_LIBRARY_RELEASE AND - (CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE)) - set( BITPIT_LIBRARY "" ) - foreach( _libname IN LISTS BITPIT_LIBRARY_RELEASE ) - list( APPEND BITPIT_LIBRARY optimized "${_libname}" ) - endforeach() - foreach( _libname IN LISTS BITPIT_LIBRARY_DEBUG ) - list( APPEND BITPIT_LIBRARY debug "${_libname}" ) - endforeach() -elseif (BITPIT_LIBRARY_RELEASE) - set (BITPIT_LIBRARY ${BITPIT_LIBRARY_RELEASE}) -elseif (BITPIT_LIBRARY_DEBUG) - set (BITPIT_LIBRARY ${BITPIT_LIBRARY_DEBUG}) -else () - set( BITPIT_LIBRARY "BITPIT_LIBRARY-NOTFOUND") -endif () - -# bitpit Definitions -set(BITPIT_DEFINITIONS "@BITPIT_INTERFACE_COMPILE_DEFINITIONS@") - # List of currently enabled bitpit modules set(BITPIT_ENABLED_MODULE_LIST "@BITPIT_ENABLED_MODULE_LIST@") @@ -143,18 +40,24 @@ if(BITPIT_FIND_OPTIONAL_COMPONENTS) endforeach() endif() -# Unset the unneeded variables -if(BITPIT_RECONFIGURE) - unset(BITPIT_RECONFIGURE) -endif() +#----------------------------------------------------------------------------- +# Location of UseBITPIT.cmake file. +#----------------------------------------------------------------------------- + +SET(BITPIT_USE_FILE "@CMAKE_INSTALL_PREFIX@/@BITPIT_INSTALL_CMAKEDIR@/UseBITPIT.cmake") + +#----------------------------------------------------------------------------- +# Programming languages +#----------------------------------------------------------------------------- -# Let libfind_process initialize the appropriate variables -libfind_process(BITPIT) +set(BITPIT_LANGUAGES "@BITPIT_LANGUAGES@") #----------------------------------------------------------------------------- # Find bitpit external dependencies #----------------------------------------------------------------------------- +list(APPEND CMAKE_MODULE_PATH "@CMAKE_INSTALL_PREFIX@/@BITPIT_INSTALL_CMAKEDIR@") + # Set external dependencies information set(_EXTERNAL_DEPENDENCIES "@BITPIT_EXTERNAL_DEPENDENCIES@") set(_EXTERNAL_VARIABLES_LIBRARIES "@BITPIT_EXTERNAL_VARIABLES_LIBRARIES@") @@ -199,29 +102,21 @@ foreach (VARIABLE_NAME IN LISTS _EXTERNAL_VARIABLES_INCLUDE_DIRS) endforeach () #----------------------------------------------------------------------------- -# Set programming languages +# Include targets #----------------------------------------------------------------------------- -set(BITPIT_LANGUAGES "@BITPIT_LANGUAGES@") +include ( "${CMAKE_CURRENT_LIST_DIR}/@BITPIT_CMAKE_TARGETS_FILE@" ) #----------------------------------------------------------------------------- -# Create imported target +# Backwards compatibility #----------------------------------------------------------------------------- -if(BITPIT_SHARED) - set(LIBRARY_TYPE SHARED) -else() - set(LIBRARY_TYPE STATIC) -endif() -if(NOT TARGET bitpit::bitpit) - add_library(bitpit::bitpit ${LIBRARY_TYPE} IMPORTED GLOBAL) +# Definitions +get_target_property(BITPIT_DEFINITIONS @BITPIT_LIBRARY@::@BITPIT_LIBRARY@ INTERFACE_COMPILE_DEFINITIONS) - set_target_properties(bitpit::bitpit - PROPERTIES - IMPORTED_LOCATION "${BITPIT_LIBRARY}" - INTERFACE_INCLUDE_DIRECTORIES "${BITPIT_INCLUDE_DIRS}" - INTERFACE_COMPILE_DEFINITIONS "${BITPIT_DEFINITIONS}") +# Include directories +get_target_property(BITPIT_INCLUDE_DIRS @BITPIT_LIBRARY@::@BITPIT_LIBRARY@ INTERFACE_INCLUDE_DIRECTORIES) - # The property INTERFACE_LINK_LIBRARIES is set using target_link_libraries so that the debug - # and optimized keywords work. - target_link_libraries(bitpit::bitpit INTERFACE ${BITPIT_LIBRARIES}) -endif() +# Include libraries +get_target_property(BITPIT_LIBRARY @BITPIT_LIBRARY@::@BITPIT_LIBRARY@ LOCATION) +get_target_property(BITPIT_LINK_LIBRARIES @BITPIT_LIBRARY@::@BITPIT_LIBRARY@ INTERFACE_LINK_LIBRARIES) +set(BITPIT_LIBRARIES "${BITPIT_LINK_LIBRARIES};${BITPIT_LIBRARY}") diff --git a/cmake/BITPITConfigVersion.cmake.in b/cmake/BITPITConfigVersion.cmake.in deleted file mode 100644 index 6036276702..0000000000 --- a/cmake/BITPITConfigVersion.cmake.in +++ /dev/null @@ -1,47 +0,0 @@ -# This is a basic version file for the Config-mode of find_package(). -# It is used by write_basic_package_version_file() as input file for configure_file() -# to create a version-file which can be installed along a config.cmake file. -# -# The created file sets PACKAGE_VERSION_EXACT if the current version string and -# the requested version string are exactly the same and it sets -# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version, -# but only if the requested major version is the same as the current one. -# The variable CVF_VERSION must be set before calling configure_file(). - -set(PACKAGE_VERSION "@BITPIT_MAJOR_VERSION@.@BITPIT_MINOR_VERSION@.@BITPIT_PATCH_VERSION@") - -if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}" ) - set(PACKAGE_VERSION_COMPATIBLE FALSE) -else() - - if("@BITPIT_VERSION@" MATCHES "^([0-9]+)\\.") - set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}") - else() - set(CVF_VERSION_MAJOR "@BITPIT_VERSION@") - endif() - - if("${PACKAGE_FIND_VERSION_MAJOR}" STREQUAL "${CVF_VERSION_MAJOR}") - set(PACKAGE_VERSION_COMPATIBLE TRUE) - else() - set(PACKAGE_VERSION_COMPATIBLE FALSE) - endif() - - if( "${PACKAGE_FIND_VERSION}" STREQUAL "${PACKAGE_VERSION}") - set(PACKAGE_VERSION_EXACT TRUE) - endif() -endif() - -# Check that the installed version has the same 32/64bit-ness as the one which -# is currently searching. -# -# If the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, -# ignore it -if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "@CMAKE_SIZEOF_VOID_P@" STREQUAL "") - return() -endif() - -if(NOT "${CMAKE_SIZEOF_VOID_P}" STREQUAL "@CMAKE_SIZEOF_VOID_P@") - math(EXPR installedBits "8 * @CMAKE_SIZEOF_VOID_P@") - set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)") - set(PACKAGE_VERSION_UNSUITABLE TRUE) -endif() diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index fbb526351a..6efe8b676f 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -22,33 +22,23 @@ # #---------------------------------------------------------------------------*/ +include(CMakePackageConfigHelpers) + +string(TOUPPER ${BITPIT_LIBRARY} UPPER_BITPIT_LIBRARY_NAME) + #------------------------------------------------------------------------------------# -# Generate BITPITConfig.cmake +# CMake configuration file #------------------------------------------------------------------------------------# -set(BITPIT_INSTALL_PREFIX_CODE -"set(_bitpit_installed_prefix \"${CMAKE_INSTALL_PREFIX}/${BITPIT_INSTALL_CMAKEDIR}\") -set(_bitpit_requested_prefix \"\${CMAKE_CURRENT_LIST_DIR}\") -get_filename_component(_bitpit_installed_prefix_full \"\${_bitpit_installed_prefix}\" REALPATH) -get_filename_component(_bitpit_requested_prefix_full \"\${_bitpit_requested_prefix}\" REALPATH) -if (_bitpit_installed_prefix_full STREQUAL _bitpit_requested_prefix_full) - set(BITPIT_INSTALL_PREFIX \"${CMAKE_INSTALL_PREFIX}\") -else () - set(BITPIT_INSTALL_PREFIX \"\${CMAKE_CURRENT_LIST_DIR}\")") - -# Construct the proper number of get_filename_component(... PATH) -# calls to compute the installation prefix. -string(REGEX REPLACE "/" ";" _count "${BITPIT_INSTALL_CMAKEDIR}") -foreach(p ${_count}) - set(BITPIT_INSTALL_PREFIX_CODE "${BITPIT_INSTALL_PREFIX_CODE} - get_filename_component(BITPIT_INSTALL_PREFIX \"\${BITPIT_INSTALL_PREFIX}\" PATH)") -endforeach() -set(BITPIT_INSTALL_PREFIX_CODE "${BITPIT_INSTALL_PREFIX_CODE} -endif ()") +set (BITPIT_CMAKE_TARGETS_FILE "${UPPER_BITPIT_LIBRARY_NAME}Targets.cmake") -get_target_property(BITPIT_DEBUG_POSTFIX ${BITPIT_LIBRARY} DEBUG_POSTFIX) -get_target_property(BITPIT_RELEASE_POSTFIX ${BITPIT_LIBRARY} RELEASE_POSTFIX) -get_target_property(BITPIT_INTERFACE_COMPILE_DEFINITIONS ${BITPIT_LIBRARY} INTERFACE_COMPILE_DEFINITIONS) +install(TARGETS ${BITPIT_LIBRARY} EXPORT ${BITPIT_LIBRARY}Targets) + +install(EXPORT ${BITPIT_LIBRARY}Targets + FILE ${BITPIT_CMAKE_TARGETS_FILE} + NAMESPACE ${BITPIT_LIBRARY}:: + DESTINATION ${BITPIT_INSTALL_CMAKEDIR} +) set(BITPIT_ENABLED_MODULE_LIST "") foreach(MODULE_NAME IN LISTS BITPIT_MODULE_LIST) @@ -58,32 +48,50 @@ foreach(MODULE_NAME IN LISTS BITPIT_MODULE_LIST) endif() endforeach() -configure_file("BITPITConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/BITPITConfig.cmake" @ONLY) +set (BITPIT_CMAKE_PACKAGE_FILE "${UPPER_BITPIT_LIBRARY_NAME}Config.cmake") +configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/${BITPIT_CMAKE_PACKAGE_FILE}.in + "${CMAKE_CURRENT_BINARY_DIR}/${BITPIT_CMAKE_PACKAGE_FILE}" + INSTALL_DESTINATION "${BITPIT_INSTALL_CMAKEDIR}" + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO +) unset(BITPIT_ENABLED_MODULE_LIST) -unset(BITPIT_INTERFACE_COMPILE_DEFINITIONS) -unset(BITPIT_RELEASE_POSTFIX) -unset(BITPIT_DEBUG_POSTFIX) -unset(BITPIT_INSTALL_PREFIX_CODE) + +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/${BITPIT_CMAKE_PACKAGE_FILE} + DESTINATION "${BITPIT_INSTALL_CMAKEDIR}" +) #------------------------------------------------------------------------------------# -# Generate BITPITConfigVersion.cmake.in +# CMake version file #------------------------------------------------------------------------------------# -get_target_property(BITPIT_INTERFACE_COMPILE_DEFINITIONS ${BITPIT_LIBRARY} INTERFACE_COMPILE_DEFINITIONS) +set (BITPIT_CMAKE_CONFIG_VERSION_FILE "${UPPER_BITPIT_LIBRARY_NAME}ConfigVersion.cmake") +write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/${BITPIT_CMAKE_CONFIG_VERSION_FILE} + VERSION ${BITPIT_VERSION} + COMPATIBILITY AnyNewerVersion +) -configure_file("BITPITConfigVersion.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/BITPITConfigVersion.cmake" @ONLY) - -unset(BITPIT_INTERFACE_COMPILE_DEFINITIONS) +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/${BITPIT_CMAKE_CONFIG_VERSION_FILE} + DESTINATION "${BITPIT_INSTALL_CMAKEDIR}" +) #------------------------------------------------------------------------------------# -# CMake targets +# CMake find package file #------------------------------------------------------------------------------------# -add_custom_target(clean-cmake COMMAND ${CMAKE_MAKE_PROGRAM} clean WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) - -install(FILES "${CMAKE_CURRENT_BINARY_DIR}/BITPITConfig.cmake" DESTINATION "${BITPIT_INSTALL_CMAKEDIR}") -install(FILES "${CMAKE_CURRENT_BINARY_DIR}/BITPITConfigVersion.cmake" DESTINATION "${BITPIT_INSTALL_CMAKEDIR}") install(FILES "FindBITPIT.cmake" DESTINATION "${BITPIT_INSTALL_CMAKEDIR}") + +#------------------------------------------------------------------------------------# +# CMake use package file +#------------------------------------------------------------------------------------# + install(FILES "UseBITPIT.cmake" DESTINATION "${BITPIT_INSTALL_CMAKEDIR}") -install(FILES "LibFindMacros.cmake" DESTINATION "${BITPIT_INSTALL_CMAKEDIR}") + +#------------------------------------------------------------------------------------# +# CMake targets +#------------------------------------------------------------------------------------# +add_custom_target(clean-cmake COMMAND ${CMAKE_MAKE_PROGRAM} clean WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/cmake/FindBITPIT.cmake b/cmake/FindBITPIT.cmake index 8be77a6e74..3a23ef8534 100644 --- a/cmake/FindBITPIT.cmake +++ b/cmake/FindBITPIT.cmake @@ -34,6 +34,9 @@ # Assume not found. set(BITPIT_FOUND 0) +# Warn that the usage of this file is deprecated. +message(WARNING "FindBITPIT.cmake is deprecated and should not be used in new projects.") + # Use the Config mode of the find_package() command to find BITPITConfig. # If this succeeds (possibly because BITPIT_DIR is already set), the # command will have already loaded BITPITConfig.cmake and set BITPIT_FOUND. diff --git a/cmake/LibFindMacros.cmake b/cmake/LibFindMacros.cmake deleted file mode 100644 index 845bf13625..0000000000 --- a/cmake/LibFindMacros.cmake +++ /dev/null @@ -1,270 +0,0 @@ -# Version 2.2 -# Public Domain, originally written by Lasse Kärkkäinen -# Maintained at https://github.com/Tronic/cmake-modules -# Please send your improvements as pull requests on Github. - -# Find another package and make it a dependency of the current package. -# This also automatically forwards the "REQUIRED" argument. -# Usage: libfind_package( [extra args to find_package]) -macro (libfind_package PREFIX PKG) - set(${PREFIX}_args ${PKG} ${ARGN}) - if (${PREFIX}_FIND_REQUIRED) - set(${PREFIX}_args ${${PREFIX}_args} REQUIRED) - endif() - find_package(${${PREFIX}_args}) - set(${PREFIX}_DEPENDENCIES ${${PREFIX}_DEPENDENCIES};${PKG}) - unset(${PREFIX}_args) -endmacro() - -# A simple wrapper to make pkg-config searches a bit easier. -# Works the same as CMake's internal pkg_check_modules but is always quiet. -macro (libfind_pkg_check_modules) - find_package(PkgConfig QUIET) - if (PKG_CONFIG_FOUND) - pkg_check_modules(${ARGN} QUIET) - endif() -endmacro() - -# Avoid useless copy&pasta by doing what most simple libraries do anyway: -# pkg-config, find headers, find library. -# Usage: libfind_pkg_detect( FIND_PATH [other args] FIND_LIBRARY [other args]) -# E.g. libfind_pkg_detect(SDL2 sdl2 FIND_PATH SDL.h PATH_SUFFIXES SDL2 FIND_LIBRARY SDL2) -function (libfind_pkg_detect PREFIX) - # Parse arguments - set(argname pkgargs) - foreach (i ${ARGN}) - if ("${i}" STREQUAL "FIND_PATH") - set(argname pathargs) - elseif ("${i}" STREQUAL "FIND_LIBRARY") - set(argname libraryargs) - else() - set(${argname} ${${argname}} ${i}) - endif() - endforeach() - if (NOT pkgargs) - message(FATAL_ERROR "libfind_pkg_detect requires at least a pkg_config package name to be passed.") - endif() - # Find library - libfind_pkg_check_modules(${PREFIX}_PKGCONF ${pkgargs}) - if (pathargs) - find_path(${PREFIX}_INCLUDE_DIR NAMES ${pathargs} HINTS ${${PREFIX}_PKGCONF_INCLUDE_DIRS}) - endif() - if (libraryargs) - find_library(${PREFIX}_LIBRARY NAMES ${libraryargs} HINTS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}) - endif() -endfunction() - -# Extracts a version #define from a version.h file, output stored to _VERSION. -# Usage: libfind_version_header(Foobar foobar/version.h FOOBAR_VERSION_STR) -# Fourth argument "QUIET" may be used for silently testing different define names. -# This function does nothing if the version variable is already defined. -function (libfind_version_header PREFIX VERSION_H DEFINE_NAME) - # Skip processing if we already have a version or if the include dir was not found - if (${PREFIX}_VERSION OR NOT ${PREFIX}_INCLUDE_DIR) - return() - endif() - set(quiet ${${PREFIX}_FIND_QUIETLY}) - # Process optional arguments - foreach(arg ${ARGN}) - if (arg STREQUAL "QUIET") - set(quiet TRUE) - else() - message(AUTHOR_WARNING "Unknown argument ${arg} to libfind_version_header ignored.") - endif() - endforeach() - # Read the header and parse for version number - set(filename "${${PREFIX}_INCLUDE_DIR}/${VERSION_H}") - if (NOT EXISTS ${filename}) - if (NOT quiet) - message(AUTHOR_WARNING "Unable to find ${${PREFIX}_INCLUDE_DIR}/${VERSION_H}") - endif() - return() - endif() - file(READ "${filename}" header) - string(REGEX REPLACE ".*#[ \t]*define[ \t]*${DEFINE_NAME}[ \t]*\"([^\n]*)\".*" "\\1" match "${header}") - # No regex match? - if (match STREQUAL header) - if (NOT quiet) - message(AUTHOR_WARNING "Unable to find \#define ${DEFINE_NAME} \"\" from ${${PREFIX}_INCLUDE_DIR}/${VERSION_H}") - endif() - return() - endif() - # Export the version string - set(${PREFIX}_VERSION "${match}" PARENT_SCOPE) -endfunction() - -# Do the final processing once the paths have been detected. -# If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain -# all the variables, each of which contain one include directory. -# Ditto for ${PREFIX}_PROCESS_LIBS and library files. -# Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES. -# Also handles errors in case library detection was required, etc. -function (libfind_process PREFIX) - # Skip processing if already processed during this configuration run - if (${PREFIX}_FOUND) - return() - endif() - - set(found TRUE) # Start with the assumption that the package was found - - # Did we find any files? Did we miss includes? These are for formatting better error messages. - set(some_files FALSE) - set(missing_headers FALSE) - - # Shorthands for some variables that we need often - set(quiet ${${PREFIX}_FIND_QUIETLY}) - set(required ${${PREFIX}_FIND_REQUIRED}) - set(exactver ${${PREFIX}_FIND_VERSION_EXACT}) - set(findver "${${PREFIX}_FIND_VERSION}") - set(version "${${PREFIX}_VERSION}") - - # Lists of config option names (all, includes, libs) - unset(configopts) - set(includeopts ${${PREFIX}_PROCESS_INCLUDES}) - set(libraryopts ${${PREFIX}_PROCESS_LIBS}) - - # Process deps to add to - foreach (i ${PREFIX} ${${PREFIX}_DEPENDENCIES}) - if (DEFINED ${i}_INCLUDE_OPTS OR DEFINED ${i}_LIBRARY_OPTS) - # The package seems to export option lists that we can use, woohoo! - list(APPEND includeopts ${${i}_INCLUDE_OPTS}) - list(APPEND libraryopts ${${i}_LIBRARY_OPTS}) - else() - # If plural forms don't exist or they equal singular forms - if ((NOT DEFINED ${i}_INCLUDE_DIRS AND NOT DEFINED ${i}_LIBRARIES) OR - (${i}_INCLUDE_DIR STREQUAL ${i}_INCLUDE_DIRS AND ${i}_LIBRARY STREQUAL ${i}_LIBRARIES)) - # Singular forms can be used - if (DEFINED ${i}_INCLUDE_DIR) - list(APPEND includeopts ${i}_INCLUDE_DIR) - endif() - if (DEFINED ${i}_LIBRARY) - list(APPEND libraryopts ${i}_LIBRARY) - endif() - else() - # Oh no, we don't know the option names - message(FATAL_ERROR "We couldn't determine config variable names for ${i} includes and libs. Aieeh!") - endif() - endif() - endforeach() - - if (includeopts) - list(REMOVE_DUPLICATES includeopts) - endif() - - if (libraryopts) - list(REMOVE_DUPLICATES libraryopts) - endif() - - string(REGEX REPLACE ".*[ ;]([^ ;]*(_INCLUDE_DIRS|_LIBRARIES))" "\\1" tmp "${includeopts} ${libraryopts}") - if (NOT tmp STREQUAL "${includeopts} ${libraryopts}") - message(AUTHOR_WARNING "Plural form ${tmp} found in config options of ${PREFIX}. This works as before but is now deprecated. Please only use singular forms INCLUDE_DIR and LIBRARY, and update your find scripts for LibFindMacros > 2.0 automatic dependency system (most often you can simply remove the PROCESS variables entirely).") - endif() - - # Include/library names separated by spaces (notice: not CMake lists) - unset(includes) - unset(libs) - - # Process all includes and set found false if any are missing - foreach (i ${includeopts}) - list(APPEND configopts ${i}) - if (NOT "${${i}}" STREQUAL "${i}-NOTFOUND") - list(APPEND includes "${${i}}") - else() - set(found FALSE) - set(missing_headers TRUE) - endif() - endforeach() - - # Process all libraries and set found false if any are missing - foreach (i ${libraryopts}) - list(APPEND configopts ${i}) - if (NOT "${${i}}" STREQUAL "${i}-NOTFOUND") - list(APPEND libs "${${i}}") - else() - set (found FALSE) - endif() - endforeach() - - # Version checks - if (found AND findver) - if (NOT version) - message(WARNING "The find module for ${PREFIX} does not provide version information, so we'll just assume that it is OK. Please fix the module or remove package version requirements to get rid of this warning.") - elseif (version VERSION_LESS findver OR (exactver AND NOT version VERSION_EQUAL findver)) - set(found FALSE) - set(version_unsuitable TRUE) - endif() - endif() - - # If all-OK, hide all config options, export variables, print status and exit - if (found) - foreach (i ${configopts}) - mark_as_advanced(${i}) - endforeach() - if (NOT quiet) - message(STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}") - if (LIBFIND_DEBUG) - message(STATUS " ${PREFIX}_DEPENDENCIES=${${PREFIX}_DEPENDENCIES}") - message(STATUS " ${PREFIX}_INCLUDE_OPTS=${includeopts}") - message(STATUS " ${PREFIX}_INCLUDE_DIRS=${includes}") - message(STATUS " ${PREFIX}_LIBRARY_OPTS=${libraryopts}") - message(STATUS " ${PREFIX}_LIBRARIES=${libs}") - endif() - endif() - set (${PREFIX}_INCLUDE_OPTS ${includeopts} PARENT_SCOPE) - set (${PREFIX}_LIBRARY_OPTS ${libraryopts} PARENT_SCOPE) - set (${PREFIX}_INCLUDE_DIRS ${includes} PARENT_SCOPE) - set (${PREFIX}_LIBRARIES ${libs} PARENT_SCOPE) - set (${PREFIX}_FOUND TRUE PARENT_SCOPE) - return() - endif() - - # Format messages for debug info and the type of error - set(vars "Relevant CMake configuration variables:\n") - - foreach (i ${configopts}) - mark_as_advanced(CLEAR ${i}) - set(val ${${i}}) - if ("${val}" STREQUAL "${i}-NOTFOUND") - set (val "") - elseif (NOT val) - set(some_files TRUE) - else() - foreach (j ${val}) - if (NOT(j STREQUAL "optimized" OR j STREQUAL "debug") AND NOT EXISTS ${j}) - set (val "${j} (does not exist)") - endif() - endforeach() - endif() - set(vars "${vars} ${i}=${val}\n") - endforeach() - set(vars "${vars}You may use CMake GUI, cmake -D or ccmake to modify the values. Delete CMakeCache.txt to discard all values and force full re-detection if necessary.\n") - if (version_unsuitable) - set(msg "${PREFIX} ${${PREFIX}_VERSION} was found but") - if (exactver) - set(msg "${msg} only version ${findver} is acceptable.") - else() - set(msg "${msg} version ${findver} is the minimum requirement.") - endif() - else() - if (missing_headers) - set(msg "We could not find development headers for ${PREFIX}. Do you have the necessary dev package installed?") - elseif (some_files) - set(msg "We only found some files of ${PREFIX}, not all of them. Perhaps your installation is incomplete or maybe we just didn't look in the right place?") - if(findver) - set(msg "${msg} This could also be caused by incompatible version (if it helps, at least ${PREFIX} ${findver} should work).") - endif() - else() - set(msg "We were unable to find package ${PREFIX}.") - endif() - endif() - - # Fatal error out if REQUIRED - if (required) - set(msg "REQUIRED PACKAGE NOT FOUND\n${msg} This package is REQUIRED and you need to install it or adjust CMake configuration in order to continue building ${PROJECT_NAME}.") - message(FATAL_ERROR "${msg}\n${vars}") - endif() - # Otherwise just print a nasty warning - if (NOT quiet) - message(WARNING "WARNING: MISSING PACKAGE\n${msg} This package is NOT REQUIRED and you may ignore this warning but by doing so you may miss some functionality of ${PROJECT_NAME}. \n${vars}") - endif() -endfunction() diff --git a/cmake/UseBITPIT.cmake b/cmake/UseBITPIT.cmake index 1a1b03c255..aca7646b88 100644 --- a/cmake/UseBITPIT.cmake +++ b/cmake/UseBITPIT.cmake @@ -5,12 +5,18 @@ # compiler settings for a project to use BITPIT. It should not be # included directly, but rather through the BITPIT_USE_FILE setting # obtained from BITPITConfig.cmake. +# +# This file is no longer needed and it's here only tom maintain compatibility +# with older versions of BITPIT. It should not be used in new projects. if(BITPIT_USE_FILE_INCLUDED) return() endif() set(BITPIT_USE_FILE_INCLUDED 1) +# Warn that the usage of this file is deprecated. +message(WARNING "UseBITPIT.cmake is deprecated and should not be used in new projects.") + # Update CMAKE_MODULE_PATH so includes work. list(APPEND CMAKE_MODULE_PATH ${BITPIT_CMAKE_DIR}) diff --git a/environments/ubuntu/Dockerfile b/environments/ubuntu/Dockerfile index 2dbd29f88e..219f61d4a1 100644 --- a/environments/ubuntu/Dockerfile +++ b/environments/ubuntu/Dockerfile @@ -31,7 +31,7 @@ RUN cd /opt && \ FROM bitpit-ubuntu-base AS bitpit-ubuntu-boost LABEL stage=bitpit-ubuntu-boost RUN cd /opt && \ - wget --no-verbose https://boostorg.jfrog.io/artifactory/main/release/1.71.0/source/boost_1_71_0.tar.gz && \ + wget --no-verbose https://archives.boost.io/release/1.71.0/source/boost_1_71_0.tar.gz && \ tar xzf boost_1_71_0.tar.gz && \ cd boost_1_71_0 && \ ./bootstrap.sh --prefix=/opt/boost-install && ./b2 -j 10 install && \ diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 464b26f976..1eb07b9290 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -81,8 +81,8 @@ if(BITPIT_BUILD_EXAMPLES) set_target_properties(${EXAMPLE_NAME} PROPERTIES CXX_STANDARD 17) set_target_properties(${EXAMPLE_NAME} PROPERTIES CXX_STANDARD_REQUIRED ON) - target_link_libraries(${EXAMPLE_NAME} ${BITPIT_LIBRARY}) - target_link_libraries(${EXAMPLE_NAME} ${BITPIT_EXTERNAL_LIBRARIES}) + configureBitpitTargetDependencies(${EXAMPLE_NAME} PRIVATE) + target_link_libraries(${EXAMPLE_NAME} PRIVATE ${BITPIT_LIBRARY}) if (MSVC) target_compile_definitions(${EXAMPLE_NAME} PRIVATE "_CRT_SECURE_NO_WARNINGS") diff --git a/external/clang-format/cmake/ClangFormat.cmake b/external/clang-format/cmake/ClangFormat.cmake index e80947d1a7..70d8fbb894 100644 --- a/external/clang-format/cmake/ClangFormat.cmake +++ b/external/clang-format/cmake/ClangFormat.cmake @@ -102,6 +102,7 @@ function(ClangFormatTargetSources TARGET FILE_PATHS) # Add file add_custom_command( TARGET ${TARGET} + PRE_BUILD COMMAND ${CLANG_FORMAT_EXECUTABLE} -style=file -i ${FULL_FILE_PATH} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMENT "Formatting ${FULL_FILE_PATH}..." diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8c21289a9f..49c3f71301 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -77,6 +77,7 @@ function(configureModule MODULE_NAME) set_target_properties(${UPPER_MODULE_NAME}_TARGET_OBJECT PROPERTIES CXX_STANDARD_REQUIRED ON) set_target_properties(${UPPER_MODULE_NAME}_TARGET_OBJECT PROPERTIES POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}) set_lto_property(${UPPER_MODULE_NAME}_TARGET_OBJECT) + configureBitpitTargetDependencies(${UPPER_MODULE_NAME}_TARGET_OBJECT PRIVATE) addModuleIncludeDirectories(${UPPER_MODULE_NAME}_TARGET_OBJECT ${MODULE_NAME}) get_target_property(BITPIT_COMPILE_DEFINITIONS ${BITPIT_LIBRARY} COMPILE_DEFINITIONS) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a77ccc31e3..11444aa1b3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -187,11 +187,11 @@ function(addTest TEST_NAME TEST_TYPE TEST_MODULES TEST_LIBRARIES WORKING_DIRECTO set_target_properties(${TEST_TARGET_NAME} PROPERTIES CXX_STANDARD 17) set_target_properties(${TEST_TARGET_NAME} PROPERTIES CXX_STANDARD_REQUIRED ON) - target_link_libraries(${TEST_TARGET_NAME} ${BITPIT_LIBRARY}) - target_link_libraries(${TEST_TARGET_NAME} ${BITPIT_EXTERNAL_LIBRARIES}) - target_link_libraries(${TEST_TARGET_NAME} ${TEST_LIBRARIES}) + configureBitpitTargetDependencies(${TEST_TARGET_NAME} PRIVATE) + target_link_libraries(${TEST_TARGET_NAME} PRIVATE ${BITPIT_LIBRARY}) + target_link_libraries(${TEST_TARGET_NAME} PRIVATE ${TEST_LIBRARIES}) if (CMAKE_BUILD_TYPE STREQUAL "Coverage") - target_link_libraries(${TEST_TARGET_NAME} "gcov") + target_link_libraries(${TEST_TARGET_NAME} PRIVATE "gcov") endif() foreach (TEST_MODULE IN LISTS TEST_MODULES)