mirror of
https://github.com/libevent/libevent.git
synced 2025-01-31 09:12:55 +08:00
cmake: introduce EVENT__LIBRARY_TYPE option
Long time ago in [1] cmake build was forced to compile both libraries (SHARED and STATIC), since this is how our autotools build works. [1]: 7182c2f561570cd9ceb704623ebe9ae3608c7b43 ("cmake: build SHARED and STATIC libraries (like autoconf does)") And there is no way to configure this (and indeed you need to do this for MSVC for example), so let's introduce option for this -- EVENT__LIBRARY_TYPE. Plus now we have INTERFACE libraries, that we can use internally in libevent's cmake rules to avoid strict to _shared/_static variant of the libraries to link with samples/tests (we prefer SHARED over STATIC for linking). Also bump minimal cmake required version to 3.1 by the following reasons: - 3.1 is required for RPATH configuration under APPLE - 3.0 is required for add_library(INTERFACE) (did not found it in 2.8.x documentation) - remove extra conditions (anyway 3.1 was release 4 years ago, so I guess that most of the systems will have it)
This commit is contained in:
parent
d705e8c0e9
commit
c9a073eae8
@ -18,11 +18,8 @@
|
|||||||
# cmake -G "Visual Studio 10" ..
|
# cmake -G "Visual Studio 10" ..
|
||||||
# start libevent.sln
|
# start libevent.sln
|
||||||
#
|
#
|
||||||
if (APPLE)
|
|
||||||
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
|
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
|
||||||
else()
|
|
||||||
cmake_minimum_required(VERSION 2.8.3 FATAL_ERROR)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if (POLICY CMP0054)
|
if (POLICY CMP0054)
|
||||||
cmake_policy(SET CMP0054 NEW)
|
cmake_policy(SET CMP0054 NEW)
|
||||||
@ -44,6 +41,9 @@ string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
|
|||||||
# what? why would you get id of other useful build types? - Ellzey
|
# what? why would you get id of other useful build types? - Ellzey
|
||||||
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Limited configurations" FORCE)
|
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Limited configurations" FORCE)
|
||||||
|
|
||||||
|
set(EVENT__LIBRARY_TYPE DEFAULT CACHE STRING
|
||||||
|
"Set library type to SHARED/STATIC/BOTH (default BOTH)")
|
||||||
|
|
||||||
project(libevent C)
|
project(libevent C)
|
||||||
|
|
||||||
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")
|
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")
|
||||||
@ -187,6 +187,26 @@ if ((${CMAKE_C_COMPILER_ID} STREQUAL GNU) OR (${CLANG}))
|
|||||||
set(GNUC 1)
|
set(GNUC 1)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
# Detect library type
|
||||||
|
set(EVENT_LIBRARY_TYPE)
|
||||||
|
if ("${EVENT__LIBRARY_TYPE}" STREQUAL "DEFAULT")
|
||||||
|
set(EVENT_LIBRARY_TYPE BOTH)
|
||||||
|
else()
|
||||||
|
string(TOUPPER "${EVENT__LIBRARY_TYPE}" EVENT_LIBRARY_TYPE)
|
||||||
|
endif()
|
||||||
|
set(EVENT_LIBRARY_STATIC OFF)
|
||||||
|
set(EVENT_LIBRARY_SHARED OFF)
|
||||||
|
if ("${EVENT_LIBRARY_TYPE}" STREQUAL "BOTH")
|
||||||
|
set(EVENT_LIBRARY_STATIC ON)
|
||||||
|
set(EVENT_LIBRARY_SHARED ON)
|
||||||
|
elseif ("${EVENT_LIBRARY_TYPE}" STREQUAL "STATIC")
|
||||||
|
set(EVENT_LIBRARY_STATIC ON)
|
||||||
|
elseif ("${EVENT_LIBRARY_TYPE}" STREQUAL "SHARED")
|
||||||
|
set(EVENT_LIBRARY_SHARED ON)
|
||||||
|
else()
|
||||||
|
message(FATAL_ERROR "${EVENT_LIBRARY_TYPE} is not supported")
|
||||||
|
endif()
|
||||||
|
|
||||||
# GNUC specific options.
|
# GNUC specific options.
|
||||||
if (${GNUC})
|
if (${GNUC})
|
||||||
option(EVENT__DISABLE_GCC_WARNINGS "Disable verbose warnings with GCC" OFF)
|
option(EVENT__DISABLE_GCC_WARNINGS "Disable verbose warnings with GCC" OFF)
|
||||||
@ -870,13 +890,13 @@ macro(add_sample_prog ssl name)
|
|||||||
add_executable(${name} ${ARGN})
|
add_executable(${name} ${ARGN})
|
||||||
|
|
||||||
target_link_libraries(${name}
|
target_link_libraries(${name}
|
||||||
event_extra_static
|
event_extra
|
||||||
event_core_static
|
event_core
|
||||||
${LIB_APPS}
|
${LIB_APPS}
|
||||||
${LIB_PLATFORM})
|
${LIB_PLATFORM})
|
||||||
|
|
||||||
if (${ssl})
|
if (${ssl})
|
||||||
target_link_libraries(${name} event_openssl_static)
|
target_link_libraries(${name} event_openssl)
|
||||||
endif()
|
endif()
|
||||||
endmacro()
|
endmacro()
|
||||||
if (NOT EVENT__DISABLE_SAMPLES)
|
if (NOT EVENT__DISABLE_SAMPLES)
|
||||||
@ -915,8 +935,8 @@ endif()
|
|||||||
macro(add_bench_prog prog)
|
macro(add_bench_prog prog)
|
||||||
add_executable(${prog} ${ARGN})
|
add_executable(${prog} ${ARGN})
|
||||||
target_link_libraries(${prog}
|
target_link_libraries(${prog}
|
||||||
event_extra_static
|
event_extra
|
||||||
event_core_static
|
event_core
|
||||||
${LIB_APPS}
|
${LIB_APPS}
|
||||||
${LIB_PLATFORM})
|
${LIB_PLATFORM})
|
||||||
endmacro()
|
endmacro()
|
||||||
@ -937,8 +957,8 @@ macro(add_test_prog prog)
|
|||||||
target_link_libraries(${prog}
|
target_link_libraries(${prog}
|
||||||
${LIB_APPS}
|
${LIB_APPS}
|
||||||
${LIB_PLATFORM}
|
${LIB_PLATFORM}
|
||||||
event_core_shared
|
event_core
|
||||||
event_extra_shared
|
event_extra
|
||||||
${ARGN})
|
${ARGN})
|
||||||
endmacro()
|
endmacro()
|
||||||
if (NOT EVENT__DISABLE_TESTS)
|
if (NOT EVENT__DISABLE_TESTS)
|
||||||
@ -1016,13 +1036,13 @@ if (NOT EVENT__DISABLE_TESTS)
|
|||||||
target_link_libraries(regress
|
target_link_libraries(regress
|
||||||
${LIB_APPS}
|
${LIB_APPS}
|
||||||
${LIB_PLATFORM}
|
${LIB_PLATFORM}
|
||||||
event_core_shared
|
event_core
|
||||||
event_extra_shared)
|
event_extra)
|
||||||
if (NOT EVENT__DISABLE_OPENSSL)
|
if (NOT EVENT__DISABLE_OPENSSL)
|
||||||
target_link_libraries(regress event_openssl_shared)
|
target_link_libraries(regress event_openssl)
|
||||||
endif()
|
endif()
|
||||||
if (CMAKE_USE_PTHREADS_INIT)
|
if (CMAKE_USE_PTHREADS_INIT)
|
||||||
target_link_libraries(regress event_pthreads_shared)
|
target_link_libraries(regress event_pthreads)
|
||||||
endif()
|
endif()
|
||||||
else()
|
else()
|
||||||
message(WARNING "No suitable Python interpreter found, cannot generate regress tests!")
|
message(WARNING "No suitable Python interpreter found, cannot generate regress tests!")
|
||||||
|
@ -27,6 +27,10 @@ The following Libevent specific CMake variables are as follows (the values being
|
|||||||
the default).
|
the default).
|
||||||
|
|
||||||
```
|
```
|
||||||
|
# Type of the library to build (SHARED or STATIC)
|
||||||
|
# Default is to build BOTH
|
||||||
|
EVENT__LIBRARY_TYPE:STRING=DEFAULT
|
||||||
|
|
||||||
# Installation directory for CMake files
|
# Installation directory for CMake files
|
||||||
EVENT_INSTALL_CMAKE_DIR:PATH=lib/cmake/libevent
|
EVENT_INSTALL_CMAKE_DIR:PATH=lib/cmake/libevent
|
||||||
|
|
||||||
|
@ -3,11 +3,6 @@ include(CMakeParseArguments)
|
|||||||
set(LIBEVENT_SHARED_LIBRARIES "")
|
set(LIBEVENT_SHARED_LIBRARIES "")
|
||||||
set(LIBEVENT_STATIC_LIBRARIES "")
|
set(LIBEVENT_STATIC_LIBRARIES "")
|
||||||
|
|
||||||
macro(set_event_lib_properties LIB_NAME)
|
|
||||||
set_target_properties("${LIB_NAME}_static" PROPERTIES ${ARGN})
|
|
||||||
set_target_properties("${LIB_NAME}_shared" PROPERTIES ${ARGN})
|
|
||||||
endmacro()
|
|
||||||
|
|
||||||
macro(set_event_shared_lib_flags LIB_NAME)
|
macro(set_event_shared_lib_flags LIB_NAME)
|
||||||
set_target_properties("${LIB_NAME}_shared" PROPERTIES
|
set_target_properties("${LIB_NAME}_shared" PROPERTIES
|
||||||
COMPILE_FLAGS ${ARGN})
|
COMPILE_FLAGS ${ARGN})
|
||||||
@ -48,6 +43,8 @@ endmacro()
|
|||||||
# - HDR_PUBLIC
|
# - HDR_PUBLIC
|
||||||
# - EVENT_INSTALL_INCLUDE_DIR
|
# - EVENT_INSTALL_INCLUDE_DIR
|
||||||
# - EVENT_SHARED_FLAGS
|
# - EVENT_SHARED_FLAGS
|
||||||
|
# - EVENT_LIBRARY_STATIC
|
||||||
|
# - EVENT_LIBRARY_SHARED
|
||||||
#
|
#
|
||||||
# Exported variables:
|
# Exported variables:
|
||||||
# - LIBEVENT_SHARED_LIBRARIES
|
# - LIBEVENT_SHARED_LIBRARIES
|
||||||
@ -60,7 +57,25 @@ macro(add_event_library LIB_NAME)
|
|||||||
${ARGN}
|
${ARGN}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set(ADD_EVENT_LIBRARY_TARGETS)
|
||||||
|
set(ADD_EVENT_LIBRARY_INTERFACE)
|
||||||
|
|
||||||
|
if (${EVENT_LIBRARY_STATIC})
|
||||||
add_library("${LIB_NAME}_static" STATIC ${LIB_SOURCES})
|
add_library("${LIB_NAME}_static" STATIC ${LIB_SOURCES})
|
||||||
|
set_target_properties("${LIB_NAME}_static" PROPERTIES
|
||||||
|
OUTPUT_NAME "${LIB_NAME}"
|
||||||
|
CLEAN_DIRECT_OUTPUT 1)
|
||||||
|
set_target_properties(
|
||||||
|
"${LIB_NAME}_static" PROPERTIES
|
||||||
|
PUBLIC_HEADER "${HDR_PUBLIC}")
|
||||||
|
|
||||||
|
list(APPEND LIBEVENT_STATIC_LIBRARIES "${LIB_NAME}_static")
|
||||||
|
list(APPEND ADD_EVENT_LIBRARY_TARGETS "${LIB_NAME}_static")
|
||||||
|
|
||||||
|
set(ADD_EVENT_LIBRARY_INTERFACE "${LIB_NAME}_static")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (${EVENT_LIBRARY_SHARED})
|
||||||
add_library("${LIB_NAME}_shared" SHARED ${LIB_SOURCES})
|
add_library("${LIB_NAME}_shared" SHARED ${LIB_SOURCES})
|
||||||
|
|
||||||
target_link_libraries("${LIB_NAME}_shared"
|
target_link_libraries("${LIB_NAME}_shared"
|
||||||
@ -72,29 +87,29 @@ macro(add_event_library LIB_NAME)
|
|||||||
set_event_shared_lib_flags("${LIB_NAME}" "${EVENT_SHARED_FLAGS}")
|
set_event_shared_lib_flags("${LIB_NAME}" "${EVENT_SHARED_FLAGS}")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set_event_lib_properties("${LIB_NAME}"
|
set_target_properties("${LIB_NAME}_shared" PROPERTIES
|
||||||
OUTPUT_NAME "${LIB_NAME}"
|
OUTPUT_NAME "${LIB_NAME}"
|
||||||
CLEAN_DIRECT_OUTPUT 1
|
CLEAN_DIRECT_OUTPUT 1)
|
||||||
)
|
|
||||||
|
|
||||||
set_target_properties(
|
set_target_properties(
|
||||||
"${LIB_NAME}_shared" PROPERTIES
|
"${LIB_NAME}_shared" PROPERTIES
|
||||||
PUBLIC_HEADER "${HDR_PUBLIC}")
|
PUBLIC_HEADER "${HDR_PUBLIC}")
|
||||||
set_target_properties(
|
|
||||||
"${LIB_NAME}_static" PROPERTIES
|
|
||||||
PUBLIC_HEADER "${HDR_PUBLIC}")
|
|
||||||
|
|
||||||
set_target_properties(
|
set_target_properties(
|
||||||
"${LIB_NAME}_shared" PROPERTIES
|
"${LIB_NAME}_shared" PROPERTIES
|
||||||
SOVERSION ${EVENT_ABI_LIBVERSION}
|
SOVERSION ${EVENT_ABI_LIBVERSION}
|
||||||
)
|
)
|
||||||
|
|
||||||
export(TARGETS "${LIB_NAME}_static" "${LIB_NAME}_shared"
|
list(APPEND LIBEVENT_SHARED_LIBRARIES "${LIB_NAME}_shared")
|
||||||
|
list(APPEND ADD_EVENT_LIBRARY_TARGETS "${LIB_NAME}_shared")
|
||||||
|
|
||||||
|
set(ADD_EVENT_LIBRARY_INTERFACE "${LIB_NAME}_shared")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
export(TARGETS ${ADD_EVENT_LIBRARY_TARGETS}
|
||||||
FILE "${PROJECT_BINARY_DIR}/LibeventTargets.cmake"
|
FILE "${PROJECT_BINARY_DIR}/LibeventTargets.cmake"
|
||||||
APPEND
|
APPEND
|
||||||
)
|
)
|
||||||
|
|
||||||
install(TARGETS "${LIB_NAME}_static" "${LIB_NAME}_shared"
|
install(TARGETS ${ADD_EVENT_LIBRARY_TARGETS}
|
||||||
EXPORT LibeventTargets
|
EXPORT LibeventTargets
|
||||||
LIBRARY DESTINATION "lib" COMPONENT lib
|
LIBRARY DESTINATION "lib" COMPONENT lib
|
||||||
ARCHIVE DESTINATION "lib" COMPONENT lib
|
ARCHIVE DESTINATION "lib" COMPONENT lib
|
||||||
@ -103,8 +118,8 @@ macro(add_event_library LIB_NAME)
|
|||||||
COMPONENT dev
|
COMPONENT dev
|
||||||
)
|
)
|
||||||
|
|
||||||
list(APPEND LIBEVENT_SHARED_LIBRARIES "${LIB_NAME}_shared")
|
add_library(${LIB_NAME} INTERFACE)
|
||||||
list(APPEND LIBEVENT_STATIC_LIBRARIES "${LIB_NAME}_static")
|
target_link_libraries(${LIB_NAME} INTERFACE ${ADD_EVENT_LIBRARY_INTERFACE})
|
||||||
|
|
||||||
generate_pkgconfig("${LIB_NAME}")
|
generate_pkgconfig("${LIB_NAME}")
|
||||||
endmacro()
|
endmacro()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user