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:
Azat Khuzhin 2018-11-22 23:00:11 +03:00
parent d705e8c0e9
commit c9a073eae8
No known key found for this signature in database
GPG Key ID: B86086848EF8686D
3 changed files with 86 additions and 47 deletions

View File

@ -18,11 +18,8 @@
# cmake -G "Visual Studio 10" ..
# start libevent.sln
#
if (APPLE)
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
else()
cmake_minimum_required(VERSION 2.8.3 FATAL_ERROR)
endif()
if (POLICY CMP0054)
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
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)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")
@ -187,6 +187,26 @@ if ((${CMAKE_C_COMPILER_ID} STREQUAL GNU) OR (${CLANG}))
set(GNUC 1)
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.
if (${GNUC})
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})
target_link_libraries(${name}
event_extra_static
event_core_static
event_extra
event_core
${LIB_APPS}
${LIB_PLATFORM})
if (${ssl})
target_link_libraries(${name} event_openssl_static)
target_link_libraries(${name} event_openssl)
endif()
endmacro()
if (NOT EVENT__DISABLE_SAMPLES)
@ -915,8 +935,8 @@ endif()
macro(add_bench_prog prog)
add_executable(${prog} ${ARGN})
target_link_libraries(${prog}
event_extra_static
event_core_static
event_extra
event_core
${LIB_APPS}
${LIB_PLATFORM})
endmacro()
@ -937,8 +957,8 @@ macro(add_test_prog prog)
target_link_libraries(${prog}
${LIB_APPS}
${LIB_PLATFORM}
event_core_shared
event_extra_shared
event_core
event_extra
${ARGN})
endmacro()
if (NOT EVENT__DISABLE_TESTS)
@ -1016,13 +1036,13 @@ if (NOT EVENT__DISABLE_TESTS)
target_link_libraries(regress
${LIB_APPS}
${LIB_PLATFORM}
event_core_shared
event_extra_shared)
event_core
event_extra)
if (NOT EVENT__DISABLE_OPENSSL)
target_link_libraries(regress event_openssl_shared)
target_link_libraries(regress event_openssl)
endif()
if (CMAKE_USE_PTHREADS_INIT)
target_link_libraries(regress event_pthreads_shared)
target_link_libraries(regress event_pthreads)
endif()
else()
message(WARNING "No suitable Python interpreter found, cannot generate regress tests!")

View File

@ -27,6 +27,10 @@ The following Libevent specific CMake variables are as follows (the values being
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
EVENT_INSTALL_CMAKE_DIR:PATH=lib/cmake/libevent

View File

@ -3,11 +3,6 @@ include(CMakeParseArguments)
set(LIBEVENT_SHARED_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)
set_target_properties("${LIB_NAME}_shared" PROPERTIES
COMPILE_FLAGS ${ARGN})
@ -48,6 +43,8 @@ endmacro()
# - HDR_PUBLIC
# - EVENT_INSTALL_INCLUDE_DIR
# - EVENT_SHARED_FLAGS
# - EVENT_LIBRARY_STATIC
# - EVENT_LIBRARY_SHARED
#
# Exported variables:
# - LIBEVENT_SHARED_LIBRARIES
@ -60,7 +57,25 @@ macro(add_event_library LIB_NAME)
${ARGN}
)
set(ADD_EVENT_LIBRARY_TARGETS)
set(ADD_EVENT_LIBRARY_INTERFACE)
if (${EVENT_LIBRARY_STATIC})
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})
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}")
endif()
set_event_lib_properties("${LIB_NAME}"
set_target_properties("${LIB_NAME}_shared" PROPERTIES
OUTPUT_NAME "${LIB_NAME}"
CLEAN_DIRECT_OUTPUT 1
)
CLEAN_DIRECT_OUTPUT 1)
set_target_properties(
"${LIB_NAME}_shared" PROPERTIES
PUBLIC_HEADER "${HDR_PUBLIC}")
set_target_properties(
"${LIB_NAME}_static" PROPERTIES
PUBLIC_HEADER "${HDR_PUBLIC}")
set_target_properties(
"${LIB_NAME}_shared" PROPERTIES
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"
APPEND
)
install(TARGETS "${LIB_NAME}_static" "${LIB_NAME}_shared"
install(TARGETS ${ADD_EVENT_LIBRARY_TARGETS}
EXPORT LibeventTargets
LIBRARY DESTINATION "lib" COMPONENT lib
ARCHIVE DESTINATION "lib" COMPONENT lib
@ -103,8 +118,8 @@ macro(add_event_library LIB_NAME)
COMPONENT dev
)
list(APPEND LIBEVENT_SHARED_LIBRARIES "${LIB_NAME}_shared")
list(APPEND LIBEVENT_STATIC_LIBRARIES "${LIB_NAME}_static")
add_library(${LIB_NAME} INTERFACE)
target_link_libraries(${LIB_NAME} INTERFACE ${ADD_EVENT_LIBRARY_INTERFACE})
generate_pkgconfig("${LIB_NAME}")
endmacro()