• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1include(CMakeParseArguments)
2
3set(LIBEVENT_SHARED_LIBRARIES "")
4set(LIBEVENT_STATIC_LIBRARIES "")
5
6macro(set_event_shared_lib_flags LIB_NAME)
7    set_target_properties("${LIB_NAME}_shared" PROPERTIES
8        COMPILE_FLAGS ${ARGN})
9    set_target_properties("${LIB_NAME}_shared" PROPERTIES
10        LINK_FLAGS ${ARGN})
11endmacro()
12
13macro(generate_pkgconfig LIB_NAME)
14    set(prefix      ${CMAKE_INSTALL_PREFIX})
15    set(exec_prefix ${CMAKE_INSTALL_PREFIX})
16    set(libdir      ${CMAKE_INSTALL_PREFIX}/lib)
17    set(includedir  ${CMAKE_INSTALL_PREFIX}/include)
18
19    set(VERSION ${EVENT_ABI_LIBVERSION})
20
21    set(LIBS         "")
22    foreach (LIB ${LIB_PLATFORM})
23        set(LIBS "${LIBS} -L${LIB}")
24    endforeach()
25
26    set(OPENSSL_LIBS "")
27    foreach(LIB ${OPENSSL_LIBRARIES})
28        set(OPENSSL_LIBS "${OPENSSL_LIBS} -L${LIB}")
29    endforeach()
30
31    configure_file("lib${LIB_NAME}.pc.in" "lib${LIB_NAME}.pc" @ONLY)
32    install(
33        FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${LIB_NAME}.pc"
34        DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig"
35    )
36endmacro()
37
38# LIB_NAME maybe event_core, event_extra, event_openssl, event_pthreads or event.
39# Targets whose LIB_NAME is not 'event' should be exported and installed.
40macro(export_install_target TYPE LIB_NAME OUTER_INCLUDES)
41    if("${LIB_NAME}" STREQUAL "event")
42        install(TARGETS "${LIB_NAME}_${TYPE}"
43            LIBRARY DESTINATION "lib" COMPONENT lib
44            ARCHIVE DESTINATION "lib" COMPONENT lib
45            RUNTIME DESTINATION "lib" COMPONENT lib
46            COMPONENT dev
47        )
48    else()
49        string(REPLACE "event_" "" PURE_NAME ${LIB_NAME})
50        string(TOUPPER ${TYPE} UPPER_TYPE)
51        list(APPEND LIBEVENT_${UPPER_TYPE}_LIBRARIES "${PURE_NAME}")
52        set(OUTER_INCS)
53        if (NOT "${OUTER_INCLUDES}" STREQUAL "NONE")
54            set(OUTER_INCS ${OUTER_INCLUDES})
55        endif()
56        target_include_directories("${LIB_NAME}_${TYPE}"
57            PUBLIC  "$<INSTALL_INTERFACE:include>"
58                    "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
59                    "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>"
60                    ${OUTER_INCS}
61        )
62        set_target_properties("${LIB_NAME}_${TYPE}" PROPERTIES EXPORT_NAME ${PURE_NAME})
63        export(TARGETS "${LIB_NAME}_${TYPE}"
64            NAMESPACE ${PROJECT_NAME}::
65            FILE "${PROJECT_BINARY_DIR}/LibeventTargets-${TYPE}.cmake"
66            APPEND
67        )
68        install(TARGETS "${LIB_NAME}_${TYPE}"
69            EXPORT LibeventTargets-${TYPE}
70            LIBRARY DESTINATION "lib" COMPONENT lib
71            ARCHIVE DESTINATION "lib" COMPONENT lib
72            RUNTIME DESTINATION "lib" COMPONENT lib
73            COMPONENT dev
74        )
75    endif()
76endmacro()
77
78# Global variables that it uses:
79# - EVENT_ABI_LIBVERSION
80# - EVENT_ABI_LIBVERSION_CURRENT
81# - EVENT_ABI_LIBVERSION_REVISION
82# - EVENT_ABI_LIBVERSION_AGE
83# - EVENT_PACKAGE_RELEASE
84# - CMAKE_THREAD_LIBS_INIT LIB_PLATFORM
85# - OPENSSL_LIBRARIES
86# - EVENT_SHARED_FLAGS
87# - EVENT_LIBRARY_STATIC
88# - EVENT_LIBRARY_SHARED
89#
90# Exported variables:
91# - LIBEVENT_SHARED_LIBRARIES
92# - LIBEVENT_STATIC_LIBRARIES
93macro(add_event_library LIB_NAME)
94    cmake_parse_arguments(LIB
95        "" # Options
96        "VERSION" # One val
97        "SOURCES;LIBRARIES;INNER_LIBRARIES;OUTER_INCLUDES" # Multi val
98        ${ARGN}
99    )
100
101    if ("${LIB_OUTER_INCLUDES}" STREQUAL "")
102        set(LIB_OUTER_INCLUDES NONE)
103    endif()
104    set(ADD_EVENT_LIBRARY_INTERFACE)
105
106    if (${EVENT_LIBRARY_STATIC})
107        add_library("${LIB_NAME}_static" STATIC ${LIB_SOURCES})
108        set_target_properties("${LIB_NAME}_static" PROPERTIES
109            OUTPUT_NAME "${LIB_NAME}"
110            CLEAN_DIRECT_OUTPUT 1)
111
112        if(LIB_INNER_LIBRARIES)
113            set(INNER_LIBRARIES "${LIB_INNER_LIBRARIES}_static")
114        endif()
115        target_link_libraries("${LIB_NAME}_static"
116            ${CMAKE_THREAD_LIBS_INIT}
117            ${LIB_PLATFORM}
118            ${INNER_LIBRARIES}
119            ${LIB_LIBRARIES})
120
121        export_install_target(static "${LIB_NAME}" "${LIB_OUTER_INCLUDES}")
122
123        set(ADD_EVENT_LIBRARY_INTERFACE "${LIB_NAME}_static")
124    endif()
125
126    if (${EVENT_LIBRARY_SHARED})
127        add_library("${LIB_NAME}_shared" SHARED ${LIB_SOURCES})
128
129        if(LIB_INNER_LIBRARIES)
130            set(INNER_LIBRARIES "${LIB_INNER_LIBRARIES}_shared")
131        endif()
132        target_link_libraries("${LIB_NAME}_shared"
133            ${CMAKE_THREAD_LIBS_INIT}
134            ${LIB_PLATFORM}
135            ${INNER_LIBRARIES}
136            ${LIB_LIBRARIES})
137
138        if (EVENT_SHARED_FLAGS)
139            set_event_shared_lib_flags("${LIB_NAME}" "${EVENT_SHARED_FLAGS}")
140        endif()
141
142        if (WIN32)
143            set_target_properties(
144                "${LIB_NAME}_shared" PROPERTIES
145                OUTPUT_NAME "${LIB_NAME}"
146                SOVERSION ${EVENT_ABI_LIBVERSION})
147        elseif (APPLE)
148            math(EXPR COMPATIBILITY_VERSION "${EVENT_ABI_LIBVERSION_CURRENT}+1")
149            math(EXPR CURRENT_MINUS_AGE "${EVENT_ABI_LIBVERSION_CURRENT}-${EVENT_ABI_LIBVERSION_AGE}")
150            set_target_properties(
151                "${LIB_NAME}_shared" PROPERTIES
152                OUTPUT_NAME "${LIB_NAME}-${EVENT_PACKAGE_RELEASE}.${CURRENT_MINUS_AGE}"
153                INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib"
154                LINK_FLAGS "-compatibility_version ${COMPATIBILITY_VERSION} -current_version ${COMPATIBILITY_VERSION}.${EVENT_ABI_LIBVERSION_REVISION}")
155        else()
156            math(EXPR CURRENT_MINUS_AGE "${EVENT_ABI_LIBVERSION_CURRENT}-${EVENT_ABI_LIBVERSION_AGE}")
157            set_target_properties(
158                "${LIB_NAME}_shared" PROPERTIES
159                OUTPUT_NAME "${LIB_NAME}-${EVENT_PACKAGE_RELEASE}"
160                VERSION "${CURRENT_MINUS_AGE}.${EVENT_ABI_LIBVERSION_AGE}.${EVENT_ABI_LIBVERSION_REVISION}"
161                SOVERSION "${CURRENT_MINUS_AGE}"
162                INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
163        endif()
164
165        if (NOT WIN32)
166            set(LIB_LINK_NAME
167                "${CMAKE_SHARED_LIBRARY_PREFIX}${LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}")
168
169            add_custom_command(TARGET ${LIB_NAME}_shared
170                POST_BUILD
171                COMMAND ${CMAKE_COMMAND} -E create_symlink
172                    "$<TARGET_FILE_NAME:${LIB_NAME}_shared>"
173                    "${LIB_LINK_NAME}"
174                WORKING_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
175        endif()
176
177        export_install_target(shared "${LIB_NAME}" "${LIB_OUTER_INCLUDES}")
178
179        set(ADD_EVENT_LIBRARY_INTERFACE "${LIB_NAME}_shared")
180
181        if (NOT WIN32)
182            install(FILES
183                "$<TARGET_FILE_DIR:${LIB_NAME}_shared>/${LIB_LINK_NAME}"
184                DESTINATION "lib"
185                COMPONENT lib)
186        endif()
187    endif()
188
189    add_library(${LIB_NAME} INTERFACE)
190    target_link_libraries(${LIB_NAME} INTERFACE ${ADD_EVENT_LIBRARY_INTERFACE})
191
192    generate_pkgconfig("${LIB_NAME}")
193endmacro()
194