Skip to content

Commit

Permalink
Merge pull request #116 from TauferLab/cmake_export
Browse files Browse the repository at this point in the history
Completes the WIP CMake config for find_package
  • Loading branch information
wangvsa authored Dec 16, 2024
2 parents e3fdfb9 + 22dc5dd commit b24680e
Show file tree
Hide file tree
Showing 12 changed files with 548 additions and 606 deletions.
149 changes: 48 additions & 101 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ endif ()
#
if (CMAKE_INSTALL_LIBDIR)
message(STATUS "CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR}")
set(DYAD_LIBDIR ${CMAKE_INSTALL_LIBDIR})
set(DYAD_INSTALL_INCLUDE_DIR
set(DYAD_INSTALL_BINDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR})
set(DYAD_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
set(DYAD_INSTALL_INCLUDEDIR
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
set(DYAD_INSTALL_DOCDIR
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DOCDIR})
set(DYAD_INSTALL_SYSCONFDIR
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SYSCONFDIR}/modulefiles)
else ()
set(DYAD_LIBDIR "lib")
set(DYAD_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_PREFIX}/include")
set(DYAD_INSTALL_BINDIR "${CMAKE_INSTALL_PREFIX}/bin")
set(DYAD_INSTALL_LIBDIR "${CMAKE_INSTALL_PREFIX}/lib")
set(DYAD_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_PREFIX}/include")
set(DYAD_INSTALL_DOCDIR "${CMAKE_INSTALL_PREFIX}/doc")
set(DYAD_INSTALL_SYSCONFDIR "${CMAKE_INSTALL_PREFIX}/etc/modulefiles")
message(STATUS "DYAD_LIBDIR set to ${DYAD_LIBDIR}")
message(STATUS "DYAD_INSTALL_LIBDIR set to ${DYAD_INSTALL_LIBDIR}")
endif ()

#------------------------------------------------------------------------------
Expand All @@ -54,17 +56,19 @@ endif ()
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${CMAKE_BINARY_DIR} ${CMAKE_INSTALL_PREFIX})
# This sets where to look for dependent library's cmake files
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}/${DYAD_LIBDIR}/cmake)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}/lib/cmake)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}/lib64/cmake)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}/share/cmake)

#------------------------------------------------------------------------------
if (NOT DYAD_EXTERNALLY_CONFIGURED)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin CACHE PATH "Single Directory for all Executables.")
set(CMAKE_INCLUDE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/include CACHE PATH "Store the headers.")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${DYAD_LIBDIR} CACHE PATH "Single Directory for all Libraries")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${DYAD_LIBDIR} CACHE PATH "Single Directory for all static libraries.")
endif ()
# This doesn't really need to be in an 'if' statement since they're cache variables
# if (NOT DYAD_EXTERNALLY_CONFIGURED)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin CACHE PATH "Single Directory for all Executables.")
set(CMAKE_INCLUDE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/include CACHE PATH "Store the headers.")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib CACHE PATH "Single Directory for all Libraries")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib CACHE PATH "Single Directory for all static libraries.")
# endif ()

#-----------------------------------------------------------------------------
# Targets built within this project are exported at Install time for use
Expand Down Expand Up @@ -285,123 +289,65 @@ include_directories(${CMAKE_SOURCE_DIR}/include) # public header
add_subdirectory(src/dyad)
#cmake_policy(SET CMP0079 NEW) # In case that we need more control over the target building order


#-----------------------------------------------------------------------------
# Configure the config.cmake file for the build directory
#-----------------------------------------------------------------------------
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/CMake/${PROJECT_NAME}-config.cmake.build.in
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config.cmake @ONLY
)

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/CMake/${PROJECT_NAME}-config.cmake.install.in
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/install/${PROJECT_NAME}-config.cmake @ONLY
)
install(
FILES
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/install/${PROJECT_NAME}-config.cmake
DESTINATION
${DYAD_LIBDIR}/cmake/${PROJECT_NAME}
)
#-----------------------------------------------------------------------------
# Configure the ${PROJECT_NAME}-config-version .cmake file for the install directory
#-----------------------------------------------------------------------------
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/CMake/${PROJECT_NAME}-config-version.cmake.in
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config-version.cmake @ONLY
)

install(
FILES
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config-version.cmake
DESTINATION
${DYAD_LIBDIR}/cmake/${PROJECT_NAME}
)

export(EXPORT ${DYAD_EXPORTED_TARGETS}
FILE "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/${PROJECT_NAME}Targets.cmake")

# Write the configure file
configure_file("${CMAKE_SOURCE_DIR}/cmake/configure_files/dyad_config.hpp.in"
"${CMAKE_INCLUDE_OUTPUT_DIRECTORY}/dyad/dyad_config.hpp" @ONLY)


################################################################
# Install DYAD
################################################################

include(CMakePackageConfigHelpers)

set(DYAD_BUILD_CMAKE_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}")
set(DYAD_INSTALL_CMAKE_DIR "${DYAD_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
set(DYAD_INSTALL_TMP_CMAKE_DIR "${CMAKE_BINARY_DIR}/install_cmake_scripts")

# Write the version file. This is independent of build/install tree.
write_basic_package_version_file(
DYADConfigVersion.cmake
${DYAD_BUILD_CMAKE_DIR}/DYADConfigVersion.cmake
VERSION "${DYAD_PACKAGE_VERSION}"
COMPATIBILITY SameMajorVersion)
file(COPY ${DYAD_BUILD_CMAKE_DIR}/DYADConfigVersion.cmake
DESTINATION ${DYAD_INSTALL_TMP_CMAKE_DIR})

#...........................................
# TODO: There are two different places where cmake config is handled currently.
# Need to clean up and merge

if (WIP_package_config)
# This is for the build tree
#set(INCLUDE_INSTALL_DIRS
# "${CMAKE_SOURCE_DIR}/src/dyad/common"
# "${CMAKE_SOURCE_DIR}/src/dyad/core"
# "${CMAKE_SOURCE_DIR}/src/dyad/dtl"
# "${CMAKE_SOURCE_DIR}/src/dyad/module"
# "${CMAKE_SOURCE_DIR}/src/dyad/wrapper"
# "${CMAKE_SOURCE_DIR}/src/dyad/utils"
# "${CMAKE_SOURCE_DIR}/src/dyad/utils/base64")
# "${CMAKE_SOURCE_DIR}/include/dyad/stream")
set(EXTRA_CMAKE_MODULE_DIR "${CMAKE_SOURCE_DIR}/cmake/modules")

configure_package_config_file(cmake/configure_files/DYADConfig.cmake.in
"${CMAKE_BINARY_DIR}/DYADConfig.cmake"
INSTALL_DESTINATION "${CMAKE_BINARY_DIR}/src/dyad/${DYAD_LIBDIR}/cmake/dyad"
PATH_VARS CMAKE_BINARY_DIR)
#PATH_VARS INCLUDE_INSTALL_DIRS CMAKE_BINARY_DIR)
set(INCLUDE_INSTALL_DIRS ${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
set(LIB_INSTALL_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/configure_files/DYADConfig.cmake.in
"${DYAD_BUILD_CMAKE_DIR}/DYADConfig.cmake"
INSTALL_DESTINATION "${DYAD_BUILD_CMAKE_DIR}"
PATH_VARS INCLUDE_INSTALL_DIRS LIB_INSTALL_DIR)

# Write the configure file for the install tree
set(INCLUDE_INSTALL_DIRS include)
set(EXTRA_CMAKE_MODULE_DIR)
set(EXTRA_CMAKE_MODULE_DIR "")
set(INCLUDE_INSTALL_DIRS "${DYAD_INSTALL_INCLUDEDIR}")
set(LIB_INSTALL_DIR "${DYAD_INSTALL_LIBDIR}")
configure_package_config_file(cmake/configure_files/DYADConfig.cmake.in
"${CMAKE_BINARY_DIR}/DYADConfig.cmake.install"
INSTALL_DESTINATION "${DYAD_LIBDIR}/cmake/dyad"
PATH_VARS INCLUDE_INSTALL_DIRS DYAD_LIBDIR)
"${DYAD_INSTALL_TMP_CMAKE_DIR}/DYADConfig.cmake"
INSTALL_DESTINATION "${DYAD_INSTALL_CMAKE_DIR}"
PATH_VARS INCLUDE_INSTALL_DIRS LIB_INSTALL_DIR)

# Install export
install(EXPORT DYADTargets
install(EXPORT ${DYAD_EXPORTED_TARGETS} # DYADTargets
NAMESPACE DYAD::
FILE DYADTargets.cmake
DESTINATION "${DYAD_LIBDIR}/cmake/dyad")
DESTINATION "${DYAD_INSTALL_CMAKE_DIR}")

# Install the cmake stuff
install(FILES "${PROJECT_BINARY_DIR}/DYADConfig.cmake.install"
DESTINATION "${DYAD_LIBDIR}/cmake/dyad"
RENAME "DYADConfig.cmake")

install(FILES "${PROJECT_BINARY_DIR}/DYADConfigVersion.cmake"
DESTINATION "${DYAD_LIBDIR}/cmake/dyad")
install(DIRECTORY "${DYAD_INSTALL_TMP_CMAKE_DIR}/" # Trailing slash ensures directory structure is not copied
DESTINATION "${DYAD_INSTALL_CMAKE_DIR}"
FILES_MATCHING PATTERN "*.cmake")

install(DIRECTORY cmake/modules
DESTINATION "${DYAD_LIBDIR}/cmake/dyad"
DESTINATION "${DYAD_INSTALL_CMAKE_DIR}"
FILES_MATCHING PATTERN "*.cmake")
endif (WIP_package_config)
#...........................................

install(FILES
"${CMAKE_INCLUDE_OUTPUT_DIRECTORY}/dyad/dyad_config.hpp"
DESTINATION
${DYAD_INSTALL_INCLUDE_DIR}/dyad)

install(EXPORT
${DYAD_EXPORTED_TARGETS}
DESTINATION
${DYAD_LIBDIR}/cmake/${PROJECT_NAME}
FILE
${DYAD_EXPORTED_TARGETS}.cmake
)
${DYAD_INSTALL_INCLUDEDIR}/dyad)

# Install license and readme
install(FILES
Expand Down Expand Up @@ -455,10 +401,11 @@ string(APPEND _str
" DYAD_PACKAGE_VERSION: ${DYAD_PACKAGE_VERSION}\n")
string(APPEND _str
" DYAD_GIT_VERSION: ${DYAD_GIT_VERSION}\n")
string(APPEND _str
" DYAD_ENABLE_UCX_DATA: ${DYAD_ENABLE_UCX_DATA}\n")
string(APPEND _str
" DYAD_ENABLE_UCX_DATA_RMA: ${DYAD_ENABLE_UCX_DATA_RMA}\n")
# Comment out since they'll be printed again below
# string(APPEND _str
# " DYAD_ENABLE_UCX_DATA: ${DYAD_ENABLE_UCX_DATA}\n")
# string(APPEND _str
# " DYAD_ENABLE_UCX_DATA_RMA: ${DYAD_ENABLE_UCX_DATA_RMA}\n")
string(APPEND _str
" DYAD_ENABLE_TESTS: ${DYAD_ENABLE_TESTS}\n")
string(APPEND _str
Expand Down
15 changes: 9 additions & 6 deletions cmake/configure_files/DYADConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/modules")
list(APPEND CMAKE_MODULE_PATH "@EXTRA_CMAKE_MODULE_DIR@")

#include(GNUInstallDirs)
include(ExternalProject)
include(DYADCMakeUtilities)
include(CMakePackageConfigHelpers)
# None of these are currently needed
# include(GNUInstallDirs)
# include(ExternalProject)
# include(DYADCMakeUtilities)
# include(CMakePackageConfigHelpers)


set(DYAD_VERSION ${PACKAGE_VERSION})
Expand All @@ -26,8 +27,10 @@ set(DYAD_CXX_FLAGS "@CMAKE_CXX_FLAGS@")
set(DYAD_C_STANDARD "@CMAKE_C_STANDARD@")
set(DYAD_CXX_STANDARD "@CMAKE_CXX_STANDARD@")

set(CMAKE_C_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Using DYAD-specific versions of these variables so that we don't risk
# polluting user CMake scripts
set(DYAD_C_STANDARD_REQUIRED TRUE)
set(DYAD_CXX_STANDARD_REQUIRED TRUE)

# Record the various flags and switches accumlated in DYAD
set(DYAD_GNU_LINUX @DYAD_GNU_LINUX@)
Expand Down
67 changes: 27 additions & 40 deletions cmake/modules/SetupCompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ include(CheckIncludeFileCXX)

MACRO (CHECK_GLIBC_VERSION)
EXECUTE_PROCESS (
COMMAND ${CMAKE_C_COMPILER} -print-file-name=libc.so.6
OUTPUT_VARIABLE GLIBC
OUTPUT_STRIP_TRAILING_WHITESPACE)

GET_FILENAME_COMPONENT (GLIBC ${GLIBC} REALPATH)
GET_FILENAME_COMPONENT (GLIBC_VERSION ${GLIBC} NAME)
STRING (REPLACE "libc-" "" GLIBC_VERSION ${GLIBC_VERSION})
STRING (REPLACE ".so" "" GLIBC_VERSION ${GLIBC_VERSION})
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_SOURCE_DIR}/cmake/tests/check_glibc_version.c -o ${CMAKE_BINARY_DIR}/check_glibc_version
RESULT_VARIABLE COULD_COMPILE_GLIBC_CHECKER)
if (NOT COULD_COMPILE_GLIBC_CHECKER EQUAL 0)
message(FATAL_ERROR "Could not compile glibc version checker")
endif ()
EXECUTE_PROCESS(
COMMAND ${CMAKE_BINARY_DIR}/check_glibc_version
OUTPUT_VARIABLE GLIBC_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE COULD_GET_GLIBC_VERSION)
if (NOT COULD_GET_GLIBC_VERSION EQUAL 0)
message(FATAL_ERROR "Could not get glibc version")
endif ()

IF (NOT GLIBC_VERSION MATCHES "^[0-9.]+$")
MESSAGE (FATAL_ERROR "Unknown glibc version: ${GLIBC_VERSION}")
ENDIF (NOT GLIBC_VERSION MATCHES "^[0-9.]+$")
Expand Down Expand Up @@ -89,11 +95,11 @@ macro(dyad_add_c_flags MY_FLAGS)
endmacro()

dyad_add_cxx_flags(CMAKE_CXX_FLAGS
-fPIC -Wall -Wextra -pedantic -Wno-unused-parameter -Wnon-virtual-dtor
-Wall -Wextra -pedantic -Wno-unused-parameter -Wnon-virtual-dtor
-Wno-deprecated-declarations)

dyad_add_c_flags(CMAKE_C_FLAGS
-fPIC -Wall -Wextra -pedantic -Wno-unused-parameter
-Wall -Wextra -pedantic -Wno-unused-parameter
-Wno-deprecated-declarations)

if (${GLIBC_VERSION} VERSION_GREATER_EQUAL "2.19")
Expand All @@ -105,24 +111,13 @@ endif ()
# Promote a compiler warning as an error for project targets
################################################################

if (DYAD_WARNINGS_AS_ERRORS)
dyad_add_cxx_flags(_WERROR_FLAGS -Werror)
separate_arguments(_WERROR_FLAGS NATIVE_COMMAND "${_WERROR_FLAGS}")
if (NOT TARGET DYAD_CXX_FLAGS_werror)
add_library(DYAD_CXX_FLAGS_werror INTERFACE)
set_property(TARGET DYAD_CXX_FLAGS_werror PROPERTY
INTERFACE_COMPILE_OPTIONS $<$<COMPILE_LANGUAGE:CXX>:${_WERROR_FLAGS}>)

add_library(DYAD_C_FLAGS_werror INTERFACE)
set_property(TARGET DYAD_C_FLAGS_werror PROPERTY
INTERFACE_COMPILE_OPTIONS $<$<COMPILE_LANGUAGE:C>:${_WERROR_FLAGS}>)

# Add the "library" to the export
install(TARGETS DYAD_C_FLAGS_werror EXPORT ${DYAD_EXPORTED_TARGETS})
install(TARGETS DYAD_CXX_FLAGS_werror EXPORT ${DYAD_EXPORTED_TARGETS})
endif ()
endif ()

macro(dyad_add_werror_if_needed target)
if (DYAD_WARNINGS_AS_ERRORS)
target_compile_options(${target} PRIVATE
$<$<COMPILER_LANGUAGE:CXX>:"-Werror">
$<$<COMPILER_LANGUAGE:C>:"-Werror">)
endif()
endmacro(dyad_add_werror_if_needed target)

################################################################
# Handle compiler dependent behaviors
Expand Down Expand Up @@ -194,25 +189,17 @@ set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

# Add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

if (NOT DYAD_LIBDIR)
if (CMAKE_INSTALL_LIBDIR)
set(DYAD_LIBDIR ${CMAKE_INSTALL_LIBDIR})
else ()
set(DYAD_LIBDIR "lib")
endif ()
endif ()
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "")

set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${DYAD_LIBDIR}")
set(CMAKE_INSTALL_RPATH "${DYAD_INSTALL_LIBDIR}" CACHE STRING "")

list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES
"${CMAKE_INSTALL_PREFIX}/${DYAD_LIBDIR}" _IS_SYSTEM_DIR)
"${DYAD_INSTALL_LIBDIR}" _IS_SYSTEM_DIR)

if (${_IS_SYSTEM_DIR} STREQUAL "-1")
# Set the install RPATH correctly
list(APPEND CMAKE_INSTALL_RPATH
"${CMAKE_INSTALL_PREFIX}/${DYAD_LIBDIR}")
"${DYAD_INSTALL_LIBDIR}")
endif ()


Expand Down
8 changes: 8 additions & 0 deletions cmake/tests/check_glibc_version.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
#include <gnu/libc-version.h>

int main(void)
{
puts(gnu_get_libc_version());
return 0;
}
Loading

0 comments on commit b24680e

Please sign in to comment.