Skip to content

Commit

Permalink
cmake: improve package config file
Browse files Browse the repository at this point in the history
  • Loading branch information
yuangongji committed Nov 14, 2019
1 parent 1495f8b commit dc4be86
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ CTestTestfile.cmake
DartConfiguration.tcl
LibeventConfig.cmake
LibeventConfigVersion.cmake
LibeventTargets.cmake
LibeventTargets*.cmake
bin/
cmake_install.cmake
Uninstall.cmake
Expand Down
64 changes: 33 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -943,19 +943,21 @@ configure_file(
include(AddEventLibrary)
add_event_library(event_core SOURCES ${SRC_CORE})
add_event_library(event_extra
LIBRARIES event_core_shared
INNER_LIBRARIES event_core
SOURCES ${SRC_EXTRA})

if (NOT EVENT__DISABLE_OPENSSL)
add_event_library(event_openssl
LIBRARIES event_core_shared ${OPENSSL_LIBRARIES}
INNER_LIBRARIES event_core
OUTER_INCLUDES ${OPENSSL_INCLUDE_DIR}
LIBRARIES ${OPENSSL_LIBRARIES}
SOURCES ${SRC_OPENSSL})
endif()

if (CMAKE_USE_PTHREADS_INIT)
set(SRC_PTHREADS evthread_pthread.c)
add_event_library(event_pthreads
LIBRARIES event_core_shared
INNER_LIBRARIES event_core
SOURCES ${SRC_PTHREADS})
endif()

Expand Down Expand Up @@ -1449,18 +1451,24 @@ endif()
# Installation preparation.
#

if(WIN32 AND NOT CYGWIN)
set(DEF_INSTALL_CMAKE_DIR cmake)
else()
set(DEF_INSTALL_CMAKE_DIR lib/cmake/libevent)
endif()

set(EVENT_INSTALL_CMAKE_DIR
"${CMAKE_INSTALL_PREFIX}/${DEF_INSTALL_CMAKE_DIR}"
CACHE PATH "Installation directory for CMake files")
"${CMAKE_INSTALL_PREFIX}/lib/cmake/libevent")

export(PACKAGE libevent)

function(gen_package_config forinstall)
if(${forinstall})
set(CONFIG_FOR_INSTALL_TREE 1)
set(dir "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}")
else()
set(CONFIG_FOR_INSTALL_TREE 0)
set(dir "${PROJECT_BINARY_DIR}")
endif()
configure_file(${PROJECT_SOURCE_DIR}/cmake/LibeventConfig.cmake.in
"${dir}/LibeventConfig.cmake"
@ONLY)
endfunction()

# Generate the config file for the build-tree.
set(EVENT__INCLUDE_DIRS
"${PROJECT_SOURCE_DIR}/include"
Expand All @@ -1470,26 +1478,10 @@ set(LIBEVENT_INCLUDE_DIRS
${EVENT__INCLUDE_DIRS}
CACHE PATH "Libevent include directories")

configure_file(${PROJECT_SOURCE_DIR}/cmake/LibeventConfigBuildTree.cmake.in
${PROJECT_BINARY_DIR}/LibeventConfig.cmake
@ONLY)
gen_package_config(0)

# Generate the config file for the installation tree.
# Calculate the relative directory from the Cmake dir.
file(RELATIVE_PATH
REL_INCLUDE_DIR
"${EVENT_INSTALL_CMAKE_DIR}"
"${CMAKE_INSTALL_PREFIX}/include")

# Note the LIBEVENT_CMAKE_DIR is defined in LibeventConfig.cmake.in,
# we escape it here so it's evaluated when it is included instead
# so that the include dirs are given relative to where the
# config file is located.
set(EVENT_INSTALL_INCLUDE_DIR "\${LIBEVENT_CMAKE_DIR}/${REL_INCLUDE_DIR}")

configure_file(${PROJECT_SOURCE_DIR}/cmake/LibeventConfig.cmake.in
${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/LibeventConfig.cmake
@ONLY)
gen_package_config(1)

# Generate version info for both build-tree and install-tree.
configure_file(${PROJECT_SOURCE_DIR}/cmake/LibeventConfigVersion.cmake.in
Expand All @@ -1514,9 +1506,19 @@ install(FILES
COMPONENT dev)

# Install exports for the install-tree.
install(EXPORT LibeventTargets
DESTINATION "${DEF_INSTALL_CMAKE_DIR}"
macro(install_export type)
install(EXPORT LibeventTargets-${type}
NAMESPACE ${PROJECT_NAME}::
DESTINATION "${EVENT_INSTALL_CMAKE_DIR}"
COMPONENT dev)
endmacro()

if (${EVENT_LIBRARY_STATIC})
install_export(static)
endif()
if (${EVENT_LIBRARY_SHARED})
install_export(shared)
endif()

# Install the scripts.
install(PROGRAMS
Expand Down
88 changes: 64 additions & 24 deletions cmake/AddEventLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,45 @@ macro(generate_pkgconfig LIB_NAME)
)
endmacro()

# LIB_NAME maybe event_core, event_extra, event_openssl, event_pthreads or event.
# Targets whose LIB_NAME is not 'event' should be exported and installed.
macro(export_install_target TYPE LIB_NAME OUTER_INCLUDES)
if("${LIB_NAME}" STREQUAL "event")
install(TARGETS "${LIB_NAME}_${TYPE}"
LIBRARY DESTINATION "lib" COMPONENT lib
ARCHIVE DESTINATION "lib" COMPONENT lib
RUNTIME DESTINATION "lib" COMPONENT lib
COMPONENT dev
)
else()
string(REPLACE "event_" "" PURE_NAME ${LIB_NAME})
string(TOUPPER ${TYPE} UPPER_TYPE)
list(APPEND LIBEVENT_${UPPER_TYPE}_LIBRARIES "${PURE_NAME}")
set(OUTER_INCS)
if (NOT "${OUTER_INCLUDES}" STREQUAL "NONE")
set(OUTER_INCS ${OUTER_INCLUDES})
endif()
target_include_directories("${LIB_NAME}_${TYPE}"
PUBLIC "$<INSTALL_INTERFACE:include>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>"
${OUTER_INCS}
)
set_target_properties("${LIB_NAME}_${TYPE}" PROPERTIES EXPORT_NAME ${PURE_NAME})
export(TARGETS "${LIB_NAME}_${TYPE}"
NAMESPACE ${PROJECT_NAME}::
FILE "${PROJECT_BINARY_DIR}/LibeventTargets-${TYPE}.cmake"
APPEND
)
install(TARGETS "${LIB_NAME}_${TYPE}"
EXPORT LibeventTargets-${TYPE}
LIBRARY DESTINATION "lib" COMPONENT lib
ARCHIVE DESTINATION "lib" COMPONENT lib
RUNTIME DESTINATION "lib" COMPONENT lib
COMPONENT dev
)
endif()
endmacro()

# Global variables that it uses:
# - EVENT_ABI_LIBVERSION
Expand All @@ -55,11 +94,13 @@ macro(add_event_library LIB_NAME)
cmake_parse_arguments(LIB
"" # Options
"VERSION" # One val
"SOURCES;LIBRARIES" # Multi val
"SOURCES;LIBRARIES;INNER_LIBRARIES;OUTER_INCLUDES" # Multi val
${ARGN}
)

set(ADD_EVENT_LIBRARY_TARGETS)
if ("${LIB_OUTER_INCLUDES}" STREQUAL "")
set(LIB_OUTER_INCLUDES NONE)
endif()
set(ADD_EVENT_LIBRARY_INTERFACE)

if (${EVENT_LIBRARY_STATIC})
Expand All @@ -68,18 +109,30 @@ macro(add_event_library LIB_NAME)
OUTPUT_NAME "${LIB_NAME}"
CLEAN_DIRECT_OUTPUT 1)

list(APPEND LIBEVENT_STATIC_LIBRARIES "${LIB_NAME}_static")
list(APPEND ADD_EVENT_LIBRARY_TARGETS "${LIB_NAME}_static")
if(LIB_INNER_LIBRARIES)
set(INNER_LIBRARIES "${LIB_INNER_LIBRARIES}_static")
endif()
target_link_libraries("${LIB_NAME}_static"
${CMAKE_THREAD_LIBS_INIT}
${LIB_PLATFORM}
${INNER_LIBRARIES}
${LIB_LIBRARIES})

export_install_target(static "${LIB_NAME}" "${LIB_OUTER_INCLUDES}")

set(ADD_EVENT_LIBRARY_INTERFACE "${LIB_NAME}_static")
endif()

if (${EVENT_LIBRARY_SHARED})
add_library("${LIB_NAME}_shared" SHARED ${LIB_SOURCES})

if(LIB_INNER_LIBRARIES)
set(INNER_LIBRARIES "${LIB_INNER_LIBRARIES}_shared")
endif()
target_link_libraries("${LIB_NAME}_shared"
${CMAKE_THREAD_LIBS_INIT}
${LIB_PLATFORM}
${INNER_LIBRARIES}
${LIB_LIBRARIES})

if (EVENT_SHARED_FLAGS)
Expand Down Expand Up @@ -120,29 +173,16 @@ macro(add_event_library LIB_NAME)
WORKING_DIRECTORY "lib")
endif()

list(APPEND LIBEVENT_SHARED_LIBRARIES "${LIB_NAME}_shared")
list(APPEND ADD_EVENT_LIBRARY_TARGETS "${LIB_NAME}_shared")
export_install_target(shared "${LIB_NAME}" "${LIB_OUTER_INCLUDES}")

set(ADD_EVENT_LIBRARY_INTERFACE "${LIB_NAME}_shared")
endif()

export(TARGETS ${ADD_EVENT_LIBRARY_TARGETS}
FILE "${PROJECT_BINARY_DIR}/LibeventTargets.cmake"
APPEND
)

install(TARGETS ${ADD_EVENT_LIBRARY_TARGETS}
EXPORT LibeventTargets
LIBRARY DESTINATION "lib" COMPONENT lib
ARCHIVE DESTINATION "lib" COMPONENT lib
RUNTIME DESTINATION "lib" COMPONENT lib
COMPONENT dev
)
if (NOT WIN32 AND ${EVENT_LIBRARY_SHARED})
install(FILES
"$<TARGET_FILE_DIR:${LIB_NAME}_shared>/${LIB_LINK_NAME}"
DESTINATION "lib"
COMPONENT lib)
if (NOT WIN32)
install(FILES
"$<TARGET_FILE_DIR:${LIB_NAME}_shared>/${LIB_LINK_NAME}"
DESTINATION "lib"
COMPONENT lib)
endif()
endif()

add_library(${LIB_NAME} INTERFACE)
Expand Down
Loading

0 comments on commit dc4be86

Please sign in to comment.