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