Skip to content

Commit

Permalink
Merge branch 'madebr-cmake-targets'
Browse files Browse the repository at this point in the history
  • Loading branch information
babelouest committed Nov 2, 2022
2 parents aa274ba + 71d5f97 commit 99e36b8
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 54 deletions.
178 changes: 127 additions & 51 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ list(APPEND CMAKE_MODULE_PATH "${Y_CMAKE_MODULE_PATH}")

include(GNUInstallDirs)
include(CheckSymbolExists)
include(CMakePackageConfigHelpers)

# check if _GNU_SOURCE is available

Expand Down Expand Up @@ -94,87 +95,137 @@ set(LIB_SRC

# dependencies

option(WITH_JOURNALD "Check journald." ON)
set(WITH_JOURNALD_DEFAULT ON)
if(WIN32)
set(WITH_JOURNALD_DEFAULT OFF)
endif()
option(WITH_JOURNALD "Check journald." ${WITH_JOURNALD_DEFAULT})

if (WITH_JOURNALD)
include(FindSystemd)
find_package(Systemd REQUIRED)
if (SYSTEMD_FOUND)
set(SYSTEMD_LIBRARIES systemd)
include_directories(${SYSTEMD_INCLUDE_DIRS})
set(Y_DISABLE_JOURNALD OFF)
endif ()
set(SYSTEMD_LIBRARIES Systemd::Systemd)
set(Y_DISABLE_JOURNALD OFF)
else()
set(Y_DISABLE_JOURNALD ON)
set(SYSTEMD_LIBRARIES )
endif ()

# shared library
option(BUILD_SHARED "Build shared library." ON)
option(BUILD_STATIC "Build static library." OFF)

add_library(yder SHARED ${LIB_SRC})
if (NOT MSVC)
set_target_properties(yder PROPERTIES
COMPILE_OPTIONS "-Wextra;-Wconversion"
PUBLIC_HEADER "${INC_DIR}/yder.h;${PROJECT_BINARY_DIR}/yder-cfg.h"
VERSION "${LIBRARY_VERSION}"
SOVERSION "${LIBRARY_SOVERSION}")
endif()
if (WIN32)
set_target_properties(yder PROPERTIES SUFFIX "-${LIBRARY_VERSION_MAJOR}.dll")
if (NOT BUILD_SHARED AND NOT BUILD_STATIC)
message(FATAL_ERROR "BUILD_SHARED and BUILD_STATIC cannot be both disabled")
endif ()

# static library

option(BUILD_STATIC "Build static library." OFF)

if (BUILD_STATIC)
add_library(yder_static STATIC ${LIB_SRC})
target_compile_definitions(yder_static PUBLIC -DO_STATIC_LIBRARY)
add_library(Yder::Yder-static ALIAS yder_static)
target_include_directories(yder_static
PUBLIC "$<BUILD_INTERFACE:${INC_DIR}>"
PUBLIC "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>"
PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_compile_definitions(yder_static PUBLIC O_STATIC_LIBRARY)
set_target_properties(yder_static PROPERTIES
OUTPUT_NAME yder)
PUBLIC_HEADER "${INC_DIR}/yder.h;${PROJECT_BINARY_DIR}/yder-cfg.h"
OUTPUT_NAME yder
EXPORT_NAME Yder-static)
if (MSVC)
set_target_properties(yder_static PROPERTIES
OUTPUT_NAME yder-static)
else ()
target_compile_options(yder_static PRIVATE -Wextra -Wconversion)
endif ()
set(yder_lib yder_static)
endif ()

# shared library

if (BUILD_SHARED)
add_library(yder SHARED ${LIB_SRC})
add_library(Yder::Yder ALIAS yder)
target_include_directories(yder
PUBLIC "$<BUILD_INTERFACE:${INC_DIR}>"
PUBLIC "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>"
PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
set_target_properties(yder PROPERTIES
PUBLIC_HEADER "${INC_DIR}/yder.h;${PROJECT_BINARY_DIR}/yder-cfg.h"
VERSION "${LIBRARY_VERSION}"
SOVERSION "${LIBRARY_SOVERSION}"
WINDOWS_EXPORT_ALL_SYMBOLS TRUE
EXPORT_NAME Yder)
if (WIN32)
set_target_properties(yder PROPERTIES
SUFFIX "-${LIBRARY_VERSION_MAJOR}.dll")
endif ()
if (NOT MSVC)
target_compile_options(yder PRIVATE -Wextra -Wconversion)
endif ()
set(yder_lib yder)
endif()

option(DOWNLOAD_DEPENDENCIES "Download required dependencies" ON)

option(SEARCH_ORCANIA "Search for Orcania library" ON)
if (SEARCH_ORCANIA)
set(Orcania_FIND_QUIETLY ON) # force to find Orcania quietly
include(FindOrcania)
find_package(Orcania ${ORCANIA_VERSION_REQUIRED} QUIET) # try to find orcania
if (NOT ORCANIA_FOUND)
if (NOT Orcania_FOUND)
if (DOWNLOAD_DEPENDENCIES)
include(DownloadProject)
download_project(PROJ orcania # ... otherwise, download archive
URL "https://github.com/babelouest/orcania/archive/v${ORCANIA_VERSION_REQUIRED}.tar.gz"
QUIET)
add_subdirectory(${orcania_SOURCE_DIR} ${orcania_BINARY_DIR})
include_directories(${orcania_SOURCE_DIR}/include)
include_directories(${orcania_BINARY_DIR})
add_dependencies(yder orcania)
set(ORCANIA_LIBRARIES orcania)
set(LIBS ${LIBS} ${ORCANIA_LIBRARIES})
# The following lines can be removed once Orcania has full target support
if (TARGET orcania)
if (NOT TARGET Orcania::Orcania)
add_library(Orcania::Orcania INTERFACE IMPORTED)
set_target_properties(Orcania::Orcania PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${orcania_SOURCE_DIR};${orcania_SOURCE_DIR}/include;${orcania_BINARY_DIR}"
INTERFACE_LINK_LIBRARIES orcania)
endif ()
endif ()
if (TARGET orcania_static)
if (NOT TARGET Orcania::Orcania-static)
add_library(Orcania::Orcania-static INTERFACE IMPORTED)
set_target_properties(Orcania::Orcania-static PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${orcania_SOURCE_DIR};${orcania_SOURCE_DIR}/include;${orcania_BINARY_DIR}"
INTERFACE_LINK_LIBRARIES orcania-_static)
endif ()
endif ()
else ()
message( FATAL_ERROR "Orcania not found")
endif ()
else()
if ("${ORCANIA_VERSION_STRING}" VERSION_GREATER_EQUAL "${ORCANIA_VERSION_REQUIRED}")
message(STATUS "Orcania found: ${ORCANIA_VERSION_STRING}")
set(LIBS ${LIBS} ${ORCANIA_LIBRARIES})
include_directories(${ORCANIA_INCLUDE_DIRS})
include_directories(${orcania_BINARY_DIR})
else ()
message( FATAL_ERROR "Orcania version required: ${ORCANIA_VERSION_REQUIRED} - version installed: ${ORCANIA_VERSION_STRING}")
endif ()
endif ()
else ()
if (NOT TARGET Orcania::Orcania)
add_library(Orcania::Orcania INTERFACE IMPORTED)
set_target_properties(Orcania::Orcania PROPERTIES INTERFACE_LINK_LIBRARIES "orcania")
endif ()
endif ()

target_link_libraries(yder ${LIBS} ${ORCANIA_LIBRARIES} ${SYSTEMD_LIBRARIES})
if (BUILD_SHARED)
target_link_libraries(yder PRIVATE $<TARGET_NAME:Orcania::Orcania> ${SYSTEMD_LIBRARIES})
endif ()
if (BUILD_STATIC)
if(TARGET Orcania::Orcania-static)
target_link_libraries(yder_static PRIVATE $<TARGET_NAME:Orcania::Orcania-static> ${SYSTEMD_LIBRARIES})
else()
target_link_libraries(yder_static PRIVATE $<TARGET_NAME:Orcania::Orcania> ${SYSTEMD_LIBRARIES})
endif()
endif ()

set(PKGCONF_REQ_PRIVATE "liborcania")
set(PKGCONF_REQ "")
if (WITH_JOURNALD)
set(PKGCONF_REQ "")
set(PKGCONF_REQ_PRIVATE "libsystemd, liborcania")
else ()
set(PKGCONF_REQ "")
set(PKGCONF_REQ_PRIVATE "liborcania")
string(APPEND PKGCONF_REQ_PRIVATE ", libsystemd")
endif ()

# documentation
Expand Down Expand Up @@ -209,11 +260,9 @@ include_directories(${PROJECT_BINARY_DIR})
option(BUILD_YDER_TESTING "Build the testing tree." OFF) # because we don not use include(CTest)

if (BUILD_YDER_TESTING)
include(FindCheck)
find_package(Check)
if (CHECK_FOUND)
if (NOT WIN32 AND NOT APPLE)
include(FindSubunit)
find_package(Subunit REQUIRED)
endif ()

Expand All @@ -222,16 +271,16 @@ if (BUILD_YDER_TESTING)
set(CMAKE_CTEST_COMMAND ctest -V)

set(TST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test)
set(LIBS yder ${LIBS} ${CHECK_LIBRARIES} ${ORCANIA_LIBRARIES})
set(TEST_LIBS ${yder_lib} Check::Check Orcania::Orcania)
if (NOT WIN32)
find_package(Threads REQUIRED)
set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT} m)
list(APPEND TEST_LIBS ${CMAKE_THREAD_LIBS_INIT} m)
endif ()
if (NOT APPLE)
set(LIBS ${LIBS} rt)
list(APPEND TEST_LIBS rt)
endif ()
if (NOT WIN32 AND NOT APPLE)
set(LIBS ${LIBS} ${SUBUNIT_LIBRARIES})
list(APPEND TEST_LIBS Subunit::Subunit)
endif ()

set(TESTS yder_test)
Expand All @@ -243,8 +292,8 @@ if (BUILD_YDER_TESTING)

foreach (t ${TESTS})
add_executable(${t} EXCLUDE_FROM_ALL ${TST_DIR}/${t}.c)
target_include_directories(${t} PUBLIC ${TST_DIR})
target_link_libraries(${t} PUBLIC ${LIBS})
target_include_directories(${t} PRIVATE ${TST_DIR})
target_link_libraries(${t} PUBLIC ${TEST_LIBS})
add_test(NAME ${t}
WORKING_DIRECTORY ${TST_DIR}
COMMAND ${t})
Expand Down Expand Up @@ -273,13 +322,16 @@ configure_file(libyder.pc.in libyder.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libyder.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

set(TARGETS yder)
set(TARGETS )
if (BUILD_SHARED)
list(APPEND TARGETS yder)
endif ()
if (BUILD_STATIC)
set(TARGETS ${TARGETS} yder_static)
list(APPEND TARGETS yder_static)
endif ()

if (INSTALL_HEADER)
install(TARGETS ${TARGETS}
install(TARGETS ${TARGETS} EXPORT YderExports
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand All @@ -289,12 +341,35 @@ if (INSTALL_HEADER)
install(FILES README.md
DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT runtime)
else ()
install(TARGETS ${TARGETS}
install(TARGETS ${TARGETS} EXPORT YderExports
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif ()

if (INSTALL_HEADER)
set(YDER_INSTALL_CMAKEDIR_DEFAULT "${CMAKE_INSTALL_LIBDIR}/cmake/Yder")
if (WIN32 AND NOT MINGW)
set(YDER_INSTALL_CMAKEDIR_DEFAULT "cmake")
endif ()
set(YDER_INSTALL_CMAKEDIR ${YDER_INSTALL_CMAKEDIR_DEFAULT} CACHE STRING "Location where to install the cmake config files")

install(EXPORT YderExports DESTINATION "${YDER_INSTALL_CMAKEDIR}"
NAMESPACE "Yder::"
FILE "YderTargets.cmake")

configure_package_config_file(cmake-modules/YderConfig.cmake.in YderConfig.cmake
INSTALL_DESTINATION "${YDER_INSTALL_CMAKEDIR}")
write_basic_package_version_file(YderConfigVersion.cmake
COMPATIBILITY AnyNewerVersion)

install(FILES
cmake-modules/FindSystemd.cmake
"${PROJECT_BINARY_DIR}/YderConfig.cmake"
"${PROJECT_BINARY_DIR}/YderConfigVersion.cmake"
DESTINATION "${YDER_INSTALL_CMAKEDIR}")
endif ()

# uninstall target

if (NOT TARGET uninstall)
Expand Down Expand Up @@ -361,6 +436,7 @@ add_custom_target(dist_y
COMMAND ${CMAKE_MAKE_PROGRAM} package_source)

message(STATUS "Journald support: ${WITH_JOURNALD}")
message(STATUS "Build shared library: ${BUILD_SHARED}")
message(STATUS "Build static library: ${BUILD_STATIC}")
message(STATUS "Build testing tree: ${BUILD_YDER_TESTING}")
message(STATUS "Install the header files: ${INSTALL_HEADER}")
Expand Down
8 changes: 7 additions & 1 deletion cmake-modules/FindCheck.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ find_package_handle_standard_args(Check
if (CHECK_FOUND)
set(CHECK_LIBRARIES ${CHECK_LIBRARY})
set(CHECK_INCLUDE_DIRS ${CHECK_INCLUDE_DIR})
if (NOT TARGET Check::Check)
add_library(Check::Check UNKNOWN IMPORTED)
set_target_properties(Check::Check PROPERTIES
IMPORTED_LOCATION "${CHECK_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${CHECK_INCLUDE_DIR}")
endif ()
endif ()

mark_as_advanced(CHECK_INCLUDE_DIR CHECK_LIBRARY)
mark_as_advanced(CHECK_INCLUDE_DIR CHECK_LIBRARY)
10 changes: 8 additions & 2 deletions cmake-modules/FindOrcania.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ find_path(ORCANIA_INCLUDE_DIR
HINTS ${PC_ORCANIA_INCLUDEDIR} ${PC_ORCANIA_INCLUDE_DIRS})

find_library(ORCANIA_LIBRARY
NAMES orcania liborcania
NAMES orcania liborcania orcania-static
HINTS ${PC_ORCANIA_LIBDIR} ${PC_ORCANIA_LIBRARY_DIRS})

set(ORCANIA_VERSION_STRING 0.0.0)
Expand All @@ -66,12 +66,18 @@ find_package_handle_standard_args(Orcania
VERSION_VAR ORCANIA_VERSION_STRING)

if (PC_ORCANIA_FOUND)
set(ORCANIA_FOUND 1)
set(Orcania_FOUND TRUE)
endif ()

if (ORCANIA_FOUND)
set(ORCANIA_LIBRARIES ${ORCANIA_LIBRARY})
set(ORCANIA_INCLUDE_DIRS ${ORCANIA_INCLUDE_DIR})
if (NOT TARGET Orcania::Orcania)
add_library(Orcania::Orcania UNKNOWN IMPORTED)
set_target_properties(Orcania::Orcania PROPERTIES
IMPORTED_LOCATION "${ORCANIA_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${ORCANIA_INCLUDE_DIR}")
endif ()
endif ()

mark_as_advanced(ORCANIA_INCLUDE_DIR ORCANIA_LIBRARY)
6 changes: 6 additions & 0 deletions cmake-modules/FindSubunit.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ find_package_handle_standard_args(Subunit
if (SUBUNIT_FOUND)
set(SUBUNIT_LIBRARIES ${SUBUNIT_LIBRARY})
set(SUBUNIT_INCLUDE_DIRS ${SUBUNIT_INCLUDE_DIR})
if (NOT TARGET Subunit::Subunit)
add_library(Subunit::Subunit UNKNOWN IMPORTED)
set_target_properties(Subunit::Subunit PROPERTIES
IMPORTED_LOCATION "${SUBUNIT_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SUBUNIT_INCLUDE_DIR}")
endif ()
endif ()

mark_as_advanced(SUBUNIT_INCLUDE_DIR SUBUNIT_LIBRARY)
6 changes: 6 additions & 0 deletions cmake-modules/FindSystemd.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ find_package_handle_standard_args(Systemd
if (SYSTEMD_FOUND)
set(SYSTEMD_LIBRARIES ${SYSTEMD_LIBRARY})
set(SYSTEMD_INCLUDE_DIRS ${SYSTEMD_INCLUDE_DIR})
if (NOT TARGET Systemd:Systemd)
add_library(Systemd::Systemd IMPORTED UNKNOWN)
set_target_properties(Systemd::Systemd PROPERTIES
IMPORTED_LOCATION "${SYSTEMD_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SYSTEMD_INCLUDE_DIR}")
endif ()
endif ()
mark_as_advanced(SYSTEMD_INCLUDE_DIR SYSTEMD_LIBRARY)
35 changes: 35 additions & 0 deletions cmake-modules/YderConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/YderTargets.cmake")

set(YDER_JOURNALD @WITH_JOURNALD@)

set(CMAKE_CURRENT_LIST_DIR ${_original_cmake_module_path})

if(TARGET Yder::Yder-static)
set(ORCANIA_INCLUDE_DIRS $<TARGET_PROPERTY:Yder::Yder-static,INTERFACE_INCLUDE_DIRECTORIES>)
set(ORCANIA_LIBRARIES Yder::Yder-static)
endif()

if(TARGET Yder::Yder)
set(ORCANIA_INCLUDE_DIRS $<TARGET_PROPERTY:Yder::Yder,INTERFACE_INCLUDE_DIRECTORIES>)
set(ORCANIA_LIBRARIES Yder::Yder)
endif()

include(CMakeFindDependencyMacro)

set(_original_cmake_module_path ${CMAKE_MODULE_PATH})
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")

find_dependency(Orcania)

if(TARGET Yder::Yder-static)
if(YDER_JOURNALD)
find_dependency(Systemd)
endif()
endif()

set(CMAKE_MODULE_PATH ${_original_cmake_module_path})

set(YDER_VERSION_STRING "@PROJECT_VERSION@")
set(Yder_FOUND TRUE)

0 comments on commit 99e36b8

Please sign in to comment.