Skip to content

Commit

Permalink
[WIP] cmake: fix static library build
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredh committed Dec 29, 2023
1 parent c64974a commit dcae77b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
42 changes: 34 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang")
endif()

set(re_DIR ${CMAKE_CURRENT_LIST_DIR}/cmake)
find_package(re CONFIG REQUIRED)
include("${CMAKE_CURRENT_LIST_DIR}/cmake/re-config.cmake")

list(APPEND RE_DEFINITIONS
-DRE_VERSION="${PROJECT_VERSION_FULL}"
Expand Down Expand Up @@ -648,14 +648,17 @@ endif()
if(LIBRE_BUILD_STATIC)
list(APPEND RE_INSTALL_TARGETS re)
add_library(re STATIC $<TARGET_OBJECTS:re-objs>)
target_link_libraries(re PUBLIC ${RE_LIBS})
target_link_libraries(re PRIVATE ${RE_LIBS})
target_include_directories(re PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
add_library(libre::re ALIAS re)

if(MSVC)
set_target_properties(re PROPERTIES OUTPUT_NAME "re-static")
if(NOT LIBRE_BUILD_SHARED)
set(PC_LIBNAME "re-static")
endif()
endif()
endif()

Expand All @@ -668,6 +671,28 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
add_subdirectory(packaging)
endif()

if(NOT PC_LIBNAME)
set(PC_LIBNAME "re")
endif()
set(PC_REQUIRES "")
set(PC_LINKLIBS "")
foreach(item IN LISTS LINKLIBS)
if(item STREQUAL "Threads::Threads")
list(APPEND PC_LINKLIBS ${CMAKE_THREADS_LIBS_INIT})
elseif(item STREQUAL "OpenSSL::Crypto")
list(APPEND PC_REQUIRES "libcrypto")
elseif(item STREQUAL "OpenSSL::SSL")
list(APPEND PC_REQUIRES "libssl")
elseif(item STREQUAL "ZLIB::ZLIB")
list(APPEND PC_REQUIRES "zlib")
elseif(item MATCHES "^-|/")
list(APPEND PC_LINKLIBS "${item}")
else()
list(APPEND PC_LINKLIBS "-l${item}")
endif()
endforeach()
list(JOIN PC_LINKLIBS " " PC_LINKLIBS)
list(JOIN PC_REQUIRES " " PC_REQUIRES)
configure_file(packaging/libre.pc.in libre.pc @ONLY)


Expand All @@ -679,6 +704,9 @@ configure_file(packaging/libre.pc.in libre.pc @ONLY)

install(TARGETS ${RE_INSTALL_TARGETS}
EXPORT libre
RUNTIME
DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT Libraries
LIBRARY
DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT Libraries
Expand All @@ -697,7 +725,9 @@ install(FILES ${HEADERS}

install(EXPORT libre
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libre
FILE libre-targets.cmake
NAMESPACE libre::
COMPONENT Development
)

if(LIBRE_BUILD_SHARED)
Expand All @@ -709,12 +739,8 @@ if(LIBRE_BUILD_SHARED)
)
endif()

install(FILES cmake/re-config.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re
COMPONENT Development
)

install(FILES cmake/libre-config.cmake
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/libre-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/cmake/libre-config.cmake" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/libre-config.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libre
COMPONENT Development
)
Expand Down
24 changes: 23 additions & 1 deletion cmake/libre-config.cmake
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
include("${CMAKE_CURRENT_LIST_DIR}/libre.cmake")
if("@LIBRE_BUILD_STATIC@")
include(CMakeFindDependencyMacro)
find_dependency(Threads)
if("@USE_OPENSSL@")
find_dependency(OpenSSL)
endif()
if("@ZLIB_FOUND@")
find_dependency(ZLIB)
endif()
endif()

include("${CMAKE_CURRENT_LIST_DIR}/libre-targets.cmake")

# convenience target libre::libre for uniform usage
if(NOT TARGET libre::libre)
if(TARGET libre::re_shared AND (BUILD_SHARED_LIBS OR NOT TARGET libre::re))
add_library(libre::libre INTERFACE IMPORTED)
set_target_properties(libre::libre PROPERTIES INTERFACE_LINK_LIBRARIES libre::re_shared)
elseif(TARGET libre::re AND (NOT BUILD_SHARED_LIBS OR NOT TARGET libre::re_shared))
add_library(libre::libre INTERFACE IMPORTED)
set_target_properties(libre::libre PROPERTIES INTERFACE_LINK_LIBRARIES libre::re)
endif()
endif()
5 changes: 3 additions & 2 deletions packaging/libre.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Name: libre
Description: @CMAKE_PROJECT_DESCRIPTION@
Version: @PROJECT_VERSION@
URL: @CMAKE_PROJECT_HOMEPAGE_URL@
Libs: -L${libdir} -lre
Libs.private: -L${libdir} -lre -ldl -lssl -lcrypto -lz -lpthread
Libs: -L${libdir} -l@PC_LIBNAME@
Libs.private: @PC_LINKLIBS@
Requires.private: @PC_REQUIRES@
Cflags: -I${includedir}

0 comments on commit dcae77b

Please sign in to comment.