Skip to content

Commit

Permalink
cmake: add option BUILD_SHARED_LIBS (by default ON). Use OFF for static.
Browse files Browse the repository at this point in the history
This simplifies the whole lib target creation thing...
  • Loading branch information
adriweb committed Aug 25, 2024
1 parent 57dbafa commit bdd3b78
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 122 deletions.
53 changes: 53 additions & 0 deletions .cmake/create_lib_target_with_deps.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function(create_lib_target_with_deps basename)
set(lib lib_${basename})
add_library(${lib} ${SRC_FILES})

include(CheckIPOSupported)
check_ipo_supported(RESULT lto_supported OUTPUT error)
if(lto_supported)
set_target_properties(${lib} PROPERTIES
INTERPROCEDURAL_OPTIMIZATION_DEBUG FALSE
INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE
)
else()
message(STATUS "IPO/LTO not supported: <${error}>")
endif()

# Internal deps
foreach(idep ${ARGN})
add_dependencies(${lib} lib_${idep})
if(${idep} MATCHES "^(ti[a-z]+)2$")
set(INTERNAL_DEP_LIB_DIR ${PROJECT_BINARY_DIR}/../../lib${CMAKE_MATCH_1}/trunk)
set(INTERNAL_DEP_INC_DIR ${PROJECT_SOURCE_DIR}/../../lib${CMAKE_MATCH_1}/trunk/src)
else()
set(INTERNAL_DEP_LIB_DIR ${PROJECT_BINARY_DIR}/../../lib${idep}/trunk)
set(INTERNAL_DEP_INC_DIR ${PROJECT_SOURCE_DIR}/../../lib${idep}/trunk/src)
endif()
if(GEN_IS_MULTI_CONFIG)
set(INTERNAL_DEP_LIB_DIR "${INTERNAL_DEP_LIB_DIR}/$<CONFIG>")
endif()
target_include_directories(${lib} PRIVATE ${INTERNAL_DEP_INC_DIR})
if (BUILD_SHARED_LIBS)
target_link_directories(${lib} PRIVATE "${INTERNAL_DEP_LIB_DIR}")
target_link_libraries(${lib} PRIVATE ${idep})
else()
target_link_libraries(${lib} PRIVATE "${INTERNAL_DEP_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${idep}${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif()
endforeach()

# Main properties
set_target_properties(${lib} PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}")

# Defines
target_compile_definitions(${lib} PRIVATE PACKAGE="${PROJECT_NAME}" VERSION="${PROJECT_VERSION}")

# Stuff to install and developer-related things
install(TARGETS ${lib}
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tilp2")

configure_and_install_pc_file(${basename} ${PROJECT_VERSION})
endfunction()
73 changes: 0 additions & 73 deletions .cmake/create_targets_both_lib_types.cmake

This file was deleted.

2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ endif()

project(tilibs C CXX)

option(BUILD_SHARED_LIBS "Whether to build shared libs instead of static ones" ON)

# Our modules
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/.cmake)
file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/.cmake/*.cmake")
Expand Down
24 changes: 24 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
"VCPKG_TARGET_TRIPLET": {
"type": "STRING",
"value": "x64-windows-static"
},
"BUILD_SHARED_LIBS": {
"type": "BOOL",
"value": "OFF"
}
}
},
Expand All @@ -103,6 +107,10 @@
"VCPKG_TARGET_TRIPLET": {
"type": "STRING",
"value": "x86-windows-static"
},
"BUILD_SHARED_LIBS": {
"type": "BOOL",
"value": "OFF"
}
}
},
Expand All @@ -113,6 +121,10 @@
"VCPKG_TARGET_TRIPLET": {
"type": "STRING",
"value": "x64-osx"
},
"BUILD_SHARED_LIBS": {
"type": "BOOL",
"value": "OFF"
}
}
},
Expand All @@ -123,6 +135,10 @@
"VCPKG_TARGET_TRIPLET": {
"type": "STRING",
"value": "arm64-osx"
},
"BUILD_SHARED_LIBS": {
"type": "BOOL",
"value": "OFF"
}
}
},
Expand All @@ -133,6 +149,10 @@
"VCPKG_TARGET_TRIPLET": {
"type": "STRING",
"value": "x64-linux"
},
"BUILD_SHARED_LIBS": {
"type": "BOOL",
"value": "OFF"
}
}
},
Expand All @@ -143,6 +163,10 @@
"VCPKG_TARGET_TRIPLET": {
"type": "STRING",
"value": "x64-linux-dynamic"
},
"BUILD_SHARED_LIBS": {
"type": "BOOL",
"value": "ON"
}
}
}
Expand Down
30 changes: 13 additions & 17 deletions libticables/trunk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,44 +48,40 @@ if(NOT WIN32)
endif()

# auto-creation of all targets with flags etc.
create_targets_both_lib_types(ticables2)
create_lib_target_with_deps(ticables2)

if(APPLE)
find_library(IOKitFramework IOKit REQUIRED)
find_library(SecurityFramework Security REQUIRED)
target_link_libraries(ticables2_objlib PRIVATE ${IOKitFramework} ${SecurityFramework})
target_link_libraries(ticables2_shared PRIVATE ${IOKitFramework} ${SecurityFramework})
target_link_libraries(lib_ticables2 PRIVATE ${IOKitFramework} ${SecurityFramework})
endif()

if(LINUX)
target_link_libraries(ticables2_objlib PRIVATE Threads::Threads)
target_link_libraries(lib_ticables2 PRIVATE Threads::Threads)
endif()

# external deps lookup
if(WIN32)
find_library(LIBUSB0 NAMES libusb0.lib usb0 REQUIRED)
target_link_libraries(ticables2_objlib PRIVATE ${LIBUSB0})
target_link_libraries(ticables2_shared PRIVATE ${LIBUSB0})
target_link_libraries(lib_ticables2 PRIVATE ${LIBUSB0})
else()
pkg_check_modules(libusb REQUIRED IMPORTED_TARGET libusb-1.0>=1.0.16)
target_link_libraries(ticables2_objlib PRIVATE PkgConfig::libusb)
target_link_libraries(ticables2_shared PRIVATE PkgConfig::libusb)
target_link_libraries(lib_ticables2 PRIVATE PkgConfig::libusb)
endif()
pkg_check_modules(glib REQUIRED IMPORTED_TARGET glib-2.0)
target_link_libraries(ticables2_objlib PRIVATE PkgConfig::glib)
target_link_libraries(ticables2_shared PRIVATE PkgConfig::glib)
target_link_libraries(lib_ticables2 PRIVATE PkgConfig::glib)

if(NOT WIN32)
# additional internal defines
target_compile_definitions(ticables2_objlib PUBLIC HAVE_LIBUSB_1_0=1 HAVE_LIBUSB10_STRERROR=1 HAVE_TERMIOS_H=1)
target_compile_definitions(lib_ticables2 PUBLIC HAVE_LIBUSB_1_0=1 HAVE_LIBUSB10_STRERROR=1 HAVE_TERMIOS_H=1)
endif()

set_target_properties(ticables2_shared PROPERTIES VERSION 8.0.0 SOVERSION 8)
set_target_properties(lib_ticables2 PROPERTIES VERSION 8.0.0 SOVERSION 8)

# Takes care of the i18n po/pot/gmo/mo files
if(ENABLE_NLS)
i18n_mo_from_po_pot()
add_dependencies(ticables2_objlib potfiles_2)
add_dependencies(lib_ticables2 potfiles_2)
endif()

# check includes for parallel and serial support
Expand All @@ -94,19 +90,19 @@ if(LINUX)
CHECK_INCLUDE_FILE(linux/parport.h HAVE_LINUX_PARPORT_H)
CHECK_INCLUDE_FILE(linux/serial.h HAVE_LINUX_SERIAL_H)
if (HAVE_LINUX_PARPORT_H)
target_compile_definitions(ticables2_objlib PUBLIC HAVE_LINUX_PARPORT_H=1)
target_compile_definitions(lib_ticables2 PUBLIC HAVE_LINUX_PARPORT_H=1)
endif()
if (HAVE_LINUX_SERIAL_H)
target_compile_definitions(ticables2_objlib PUBLIC HAVE_LINUX_SERIAL_H=1)
target_compile_definitions(lib_ticables2 PUBLIC HAVE_LINUX_SERIAL_H=1)
endif()
endif()

# public export define
target_compile_definitions(ticables2_objlib PUBLIC TICABLES_EXPORTS)
target_compile_definitions(lib_ticables2 PUBLIC TICABLES_EXPORTS)

option(ENABLE_LOGGING "Whether to build with cables logging enabled (default ON)" ON)
if(ENABLE_LOGGING)
target_compile_definitions(ticables2_objlib PUBLIC ENABLE_LOGGING=1)
target_compile_definitions(lib_ticables2 PUBLIC ENABLE_LOGGING=1)
else()
message("Building ${PROJECT_NAME} with logging disabled")
endif()
Expand Down
2 changes: 1 addition & 1 deletion libticables/trunk/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ foreach(tar torture_ticables test_ticables_2)
endif()
target_include_directories(${tar} PRIVATE ${PROJECT_SOURCE_DIR}/../src)
target_link_directories(${tar} PRIVATE ${PROJECT_BINARY_DIR}/..)
target_link_libraries(${tar} PRIVATE PkgConfig::glib ticables2_objlib)
target_link_libraries(${tar} PRIVATE PkgConfig::glib lib_ticables2)
endforeach()

add_custom_target(ticables2_check
Expand Down
19 changes: 8 additions & 11 deletions libticalcs/trunk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,35 @@ set(PUBLIC_HEADERS
src/calclabequipmentdata.h)

# auto-creation of all targets with flags etc., alongside with internal deps
create_targets_both_lib_types(ticalcs2 tifiles2 ticables2 ticonv)
create_lib_target_with_deps(ticalcs2 tifiles2 ticables2 ticonv)

if(LINUX)
target_link_libraries(ticalcs2_objlib PRIVATE Threads::Threads)
target_link_libraries(lib_ticalcs2 PRIVATE Threads::Threads)
endif()

# external deps lookup
if(WIN32)
find_library(LIBUSB0 NAMES libusb0.lib usb0 REQUIRED)
target_link_libraries(ticalcs2_objlib PRIVATE ${LIBUSB0})
target_link_libraries(ticalcs2_shared PRIVATE ${LIBUSB0})
target_link_libraries(lib_ticalcs2 PRIVATE ${LIBUSB0})
else()
pkg_check_modules(libusb REQUIRED IMPORTED_TARGET libusb-1.0>=1.0.16)
target_link_libraries(ticalcs2_objlib PRIVATE PkgConfig::libusb)
target_link_libraries(ticalcs2_shared PRIVATE PkgConfig::libusb)
target_link_libraries(lib_ticalcs2 PRIVATE PkgConfig::libusb)
endif()
pkg_check_modules(glib REQUIRED IMPORTED_TARGET glib-2.0)
find_package(ZLIB REQUIRED) # for libtifiles
find_package(LibArchive REQUIRED) # for libticables
target_link_libraries(ticalcs2_objlib PRIVATE ZLIB::ZLIB PkgConfig::glib LibArchive::LibArchive)
target_link_libraries(ticalcs2_shared PRIVATE ZLIB::ZLIB PkgConfig::glib LibArchive::LibArchive)
target_link_libraries(lib_ticalcs2 PRIVATE ZLIB::ZLIB PkgConfig::glib LibArchive::LibArchive)

set_target_properties(ticalcs2_shared PROPERTIES VERSION 13.0.3 SOVERSION 13)
set_target_properties(lib_ticalcs2 PROPERTIES VERSION 13.0.3 SOVERSION 13)

# Takes care of the i18n po/pot/gmo/mo files
if(ENABLE_NLS)
i18n_mo_from_po_pot()
add_dependencies(ticalcs2_objlib potfiles_3)
add_dependencies(lib_ticalcs2 potfiles_3)
endif()

# public export define
target_compile_definitions(ticalcs2_objlib PUBLIC TICALCS_EXPORTS)
target_compile_definitions(lib_ticalcs2 PUBLIC TICALCS_EXPORTS)

# tests
add_subdirectory(tests)
2 changes: 1 addition & 1 deletion libticalcs/trunk/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ foreach(tar torture_ticalcs test_ticalcs_2)
${PROJECT_SOURCE_DIR}/../src)

target_link_directories(${tar} PRIVATE ${PROJECT_BINARY_DIR}/..)
target_link_libraries(${tar} PRIVATE ticonv_objlib tifiles2_objlib ticables2_objlib ticalcs2_objlib)
target_link_libraries(${tar} PRIVATE lib_ticonv lib_tifiles2 lib_ticables2 lib_ticalcs2)
endforeach()

add_custom_target(ticalcs2_check
Expand Down
15 changes: 7 additions & 8 deletions libticonv/trunk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,28 @@ option(USE_ICONV "Use libiconv at runtime for libticonv (whether to link with th
pkg_check_modules(glib REQUIRED IMPORTED_TARGET glib-2.0)

# auto-creation of all targets with flags etc., alongside with internal deps
create_targets_both_lib_types(ticonv)
create_lib_target_with_deps(ticonv)

if(LINUX)
target_link_libraries(ticonv_objlib PRIVATE Threads::Threads)
target_link_libraries(lib_ticonv PRIVATE Threads::Threads)
endif()
target_link_libraries(ticonv_objlib PRIVATE PkgConfig::glib)
target_link_libraries(ticonv_shared PRIVATE PkgConfig::glib)
target_link_libraries(lib_ticonv PRIVATE PkgConfig::glib)

set_target_properties(ticonv_shared PROPERTIES VERSION 9.0.4 SOVERSION 9)
set_target_properties(lib_ticonv PROPERTIES VERSION 9.0.4 SOVERSION 9)

if(USE_ICONV)
find_package(Iconv REQUIRED)
add_compile_definitions(USE_ICONV)
if(Iconv_FOUND AND NOT Iconv_IS_BUILT_IN)
target_include_directories(ticonv_objlib PRIVATE ${Iconv_INCLUDE_DIRS})
target_link_libraries(ticonv_shared PRIVATE ${Iconv_LIBRARIES})
target_include_directories(lib_ticonv PRIVATE ${Iconv_INCLUDE_DIRS})
target_link_libraries(lib_ticonv PRIVATE ${Iconv_LIBRARIES})
elseif(NOT Iconv_FOUND)
message(FATAL_ERROR "USE_ICONV true but it is not built-in and libiconv could not be found!")
endif()
endif()

# public export define
target_compile_definitions(ticonv_objlib PUBLIC TICONV_EXPORTS)
target_compile_definitions(lib_ticonv PUBLIC TICONV_EXPORTS)

# tests
add_subdirectory(tests)
2 changes: 1 addition & 1 deletion libticonv/trunk/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ foreach(tar torture_ticonv test_ticonv)
${PROJECT_SOURCE_DIR}/../src)

target_link_directories(${tar} PRIVATE ${PROJECT_BINARY_DIR}/..)
target_link_libraries(${tar} PRIVATE PkgConfig::glib ticonv_objlib)
target_link_libraries(${tar} PRIVATE PkgConfig::glib lib_ticonv)
endforeach()

add_custom_target(ticonv_check
Expand Down
Loading

0 comments on commit bdd3b78

Please sign in to comment.