]> granicus.if.org Git - libevent/commitdiff
cmake: introduce EVENT__LIBRARY_TYPE option
authorAzat Khuzhin <a3at.mail@gmail.com>
Thu, 22 Nov 2018 20:00:11 +0000 (23:00 +0300)
committerAzat Khuzhin <a3at.mail@gmail.com>
Thu, 22 Nov 2018 21:02:18 +0000 (00:02 +0300)
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)

CMakeLists.txt
README.md
cmake/AddEventLibrary.cmake

index 3cf017bb9863a03626e9891eff6333eb0a3edfe1..c6026a34fd3c9f0a61fc3092eafff36238193e75 100644 (file)
 #       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()
+
+cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
 
 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!")
index 967fbdfeae8e8bc7276ca01bd8f4e82d42503918..61735731f65b17b8520c3888e1470badc7e08051 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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
 
index 3f4e800e02defb910fb9c5e756a8a7072f9949c1..9de4484c200934c96640edb95210089a05272b4b 100644 (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,41 +57,59 @@ macro(add_event_library LIB_NAME)
         ${ARGN}
     )
 
-    add_library("${LIB_NAME}_static" STATIC ${LIB_SOURCES})
-    add_library("${LIB_NAME}_shared" SHARED ${LIB_SOURCES})
+    set(ADD_EVENT_LIBRARY_TARGETS)
+    set(ADD_EVENT_LIBRARY_INTERFACE)
 
-    target_link_libraries("${LIB_NAME}_shared"
-        ${CMAKE_THREAD_LIBS_INIT}
-        ${LIB_PLATFORM}
-        ${LIB_LIBRARIES})
-
-    if (EVENT_SHARED_FLAGS)
-        set_event_shared_lib_flags("${LIB_NAME}" "${EVENT_SHARED_FLAGS}")
-    endif()
+    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}")
 
-    set_event_lib_properties("${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")
 
-    set_target_properties(
-        "${LIB_NAME}_shared" PROPERTIES
-        PUBLIC_HEADER "${HDR_PUBLIC}")
-    set_target_properties(
-        "${LIB_NAME}_static" PROPERTIES
-        PUBLIC_HEADER "${HDR_PUBLIC}")
+        set(ADD_EVENT_LIBRARY_INTERFACE "${LIB_NAME}_static")
+    endif()
 
-    set_target_properties(
-        "${LIB_NAME}_shared" PROPERTIES
-        SOVERSION ${EVENT_ABI_LIBVERSION}
-    )
+    if (${EVENT_LIBRARY_SHARED})
+        add_library("${LIB_NAME}_shared" SHARED ${LIB_SOURCES})
+
+        target_link_libraries("${LIB_NAME}_shared"
+            ${CMAKE_THREAD_LIBS_INIT}
+            ${LIB_PLATFORM}
+            ${LIB_LIBRARIES})
+
+        if (EVENT_SHARED_FLAGS)
+            set_event_shared_lib_flags("${LIB_NAME}" "${EVENT_SHARED_FLAGS}")
+        endif()
+
+        set_target_properties("${LIB_NAME}_shared" PROPERTIES
+            OUTPUT_NAME "${LIB_NAME}"
+            CLEAN_DIRECT_OUTPUT 1)
+        set_target_properties(
+            "${LIB_NAME}_shared" PROPERTIES
+            PUBLIC_HEADER "${HDR_PUBLIC}")
+        set_target_properties(
+            "${LIB_NAME}_shared" PROPERTIES
+            SOVERSION ${EVENT_ABI_LIBVERSION}
+        )
+
+        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 "${LIB_NAME}_static" "${LIB_NAME}_shared"
+    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()