From 2a992306c0acf938b29fa0cd5fbc160e24424209 Mon Sep 17 00:00:00 2001 From: Yegor Yefremov Date: Wed, 11 Jun 2025 11:54:16 +0200 Subject: [PATCH] Add SHAREDLIBS option This option is needed to still produce .so files when building static libs only. --- CMakeLists.txt | 49 +++++++++++++++++++++++++++++-------------- CMakeOptions.txt | 1 + ftdi_eeprom/CMakeLists.txt | 3 ++ ftdipp/CMakeLists.txt | 40 +++++++++++++++++------------------ src/CMakeLists.txt | 39 +++++++++++++++++------------------ 5 files changed, 75 insertions(+), 57 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 385952e..ce9ccb4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -190,22 +190,38 @@ endif () set ( LIBFTDI_INCLUDE_DIR ${includedir} ) set ( LIBFTDI_INCLUDE_DIRS ${LIBFTDI_INCLUDE_DIR} ) -set ( LIBFTDI_LIBRARY ftdi1 ) -set ( LIBFTDI_LIBRARIES ${LIBFTDI_LIBRARY} ) -list ( APPEND LIBFTDI_LIBRARIES ${LIBUSB_LIBRARIES} ) -set ( LIBFTDI_STATIC_LIBRARY ftdi1.a ) -set ( LIBFTDI_STATIC_LIBRARIES ${LIBFTDI_STATIC_LIBRARY} ) -list ( APPEND LIBFTDI_STATIC_LIBRARIES ${LIBUSB_LIBRARIES} ) -if ( FTDIPP ) - set ( LIBFTDIPP_LIBRARY ftdipp1 ) - set ( LIBFTDIPP_LIBRARIES ${LIBFTDIPP_LIBRARY} ) - list ( APPEND LIBFTDIPP_LIBRARIES ${LIBUSB_LIBRARIES} ) -endif () -set ( LIBFTDI_LIBRARY_DIRS ${libdir} ) -set ( LIBFTDI_ROOT_DIR ${prefix} ) -set ( LIBFTDI_VERSION_STRING ${VERSION_STRING} ) -set ( LIBFTDI_VERSION_MAJOR ${MAJOR_VERSION} ) -set ( LIBFTDI_VERSION_MINOR ${MINOR_VERSION} ) + +# Set up library variables based on what's being built +if(SHAREDLIBS) + set(LIBFTDI_LIBRARY ftdi1) + set(LIBFTDI_LIBRARIES ${LIBFTDI_LIBRARY}) + list(APPEND LIBFTDI_LIBRARIES ${LIBUSB_LIBRARIES}) +endif() + +if(STATICLIBS) + set(LIBFTDI_STATIC_LIBRARY ftdi1.a) + set(LIBFTDI_STATIC_LIBRARIES ${LIBFTDI_STATIC_LIBRARY}) + list(APPEND LIBFTDI_STATIC_LIBRARIES ${LIBUSB_LIBRARIES}) +endif() + +if(FTDIPP) + if(SHAREDLIBS) + set(LIBFTDIPP_LIBRARY ftdipp1) + set(LIBFTDIPP_LIBRARIES ${LIBFTDIPP_LIBRARY}) + list(APPEND LIBFTDIPP_LIBRARIES ${LIBUSB_LIBRARIES}) + endif() + if(STATICLIBS) + set(LIBFTDIPP_STATIC_LIBRARY ftdipp1.a) + set(LIBFTDIPP_STATIC_LIBRARIES ${LIBFTDIPP_STATIC_LIBRARY}) + list(APPEND LIBFTDIPP_STATIC_LIBRARIES ${LIBUSB_LIBRARIES}) + endif() +endif() + +set(LIBFTDI_LIBRARY_DIRS ${libdir}) +set(LIBFTDI_ROOT_DIR ${prefix}) +set(LIBFTDI_VERSION_STRING ${VERSION_STRING}) +set(LIBFTDI_VERSION_MAJOR ${MAJOR_VERSION}) +set(LIBFTDI_VERSION_MINOR ${MINOR_VERSION}) set ( LIBFTDI_USE_FILE ${CMAKE_INSTALL_PREFIX}/${LIBFTDI_CMAKE_CONFIG_DIR}/UseLibFTDI1.cmake ) @@ -241,6 +257,7 @@ include(CPack) message (STATUS "Summary of build options: + Build shared libs: ${SHAREDLIBS} Build static libs: ${STATICLIBS} Build C++ bindings: ${FTDIPP} Build Python bindings: ${PYTHON_BINDINGS} diff --git a/CMakeOptions.txt b/CMakeOptions.txt index 07b5887..c20743c 100644 --- a/CMakeOptions.txt +++ b/CMakeOptions.txt @@ -1,4 +1,5 @@ option ( STATICLIBS "Build static libraries" ON ) +option ( SHAREDLIBS "Build shared/dynamic libraries" ON ) option ( BUILD_TESTS "Build unit tests with Boost Unit Test framework" OFF ) option ( DOCUMENTATION "Generate API documentation with Doxygen" OFF ) option ( EXAMPLES "Build example programs" ON ) diff --git a/ftdi_eeprom/CMakeLists.txt b/ftdi_eeprom/CMakeLists.txt index 525421e..3432d4c 100644 --- a/ftdi_eeprom/CMakeLists.txt +++ b/ftdi_eeprom/CMakeLists.txt @@ -40,5 +40,8 @@ target_link_libraries ( ftdi_eeprom ftdi1 ${CONFUSE_LIBRARIES} ) if ( LIBINTL_FOUND ) target_link_libraries ( ftdi_eeprom ${LIBINTL_LIBRARIES} ) endif () +if ( NOT SHAREDLIBS AND STATICLIBS ) + target_link_libraries ( ftdi_eeprom ${LIBUSB_LIBRARIES} ) +endif () install ( TARGETS ftdi_eeprom DESTINATION bin ) install ( FILES example.conf DESTINATION ${CMAKE_INSTALL_DOCDIR} ) diff --git a/ftdipp/CMakeLists.txt b/ftdipp/CMakeLists.txt index 71e6055..4172d75 100644 --- a/ftdipp/CMakeLists.txt +++ b/ftdipp/CMakeLists.txt @@ -9,36 +9,34 @@ include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR} ) # Shared library -add_library(ftdipp1 SHARED ${cpp_sources}) - -math(EXPR VERSION_FIXUP "${MAJOR_VERSION} + 1") # Compatibility with previous releases -set_target_properties(ftdipp1 PROPERTIES VERSION ${VERSION_FIXUP}.${MINOR_VERSION}.0 SOVERSION 3) - -# Prevent clobbering each other during the build -set_target_properties(ftdipp1 PROPERTIES CLEAN_DIRECT_OUTPUT 1) - -# Dependencies -target_link_libraries(ftdipp1 ftdi1 ${LIBUSB_LIBRARIES}) - -install ( TARGETS ftdipp1 +if ( SHAREDLIBS ) + add_library(ftdipp1 SHARED ${cpp_sources}) + math(EXPR VERSION_FIXUP "${MAJOR_VERSION} + 1") # Compatibility with previous releases + set_target_properties(ftdipp1 PROPERTIES VERSION ${VERSION_FIXUP}.${MINOR_VERSION}.0 SOVERSION 3) + # Prevent clobbering each other during the build + set_target_properties(ftdipp1 PROPERTIES CLEAN_DIRECT_OUTPUT 1) + # Dependencies + target_link_libraries(ftdipp1 ftdi1 ${LIBUSB_LIBRARIES}) + install(TARGETS ftdipp1 RUNTIME DESTINATION bin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} -) + ) +endif() # Static library if ( STATICLIBS ) add_library(ftdipp1-static STATIC ${cpp_sources}) set_target_properties(ftdipp1-static PROPERTIES OUTPUT_NAME "ftdipp1") set_target_properties(ftdipp1-static PROPERTIES CLEAN_DIRECT_OUTPUT 1) - - install ( TARGETS ftdipp1-static - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - COMPONENT staticlibs + target_link_libraries(ftdipp1-static ftdi1-static ${LIBUSB_LIBRARIES}) + install(TARGETS ftdipp1-static + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT staticlibs ) -endif () +endif() -install ( FILES ${cpp_headers} - DESTINATION include/${PROJECT_NAME} - COMPONENT headers +install(FILES ${cpp_headers} + DESTINATION include/${PROJECT_NAME} + COMPONENT headers ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e145af1..5798ab0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,35 +21,34 @@ configure_file(ftdi_version_i.h.in "${CMAKE_CURRENT_BINARY_DIR}/ftdi_version_i.h set(c_sources ${CMAKE_CURRENT_SOURCE_DIR}/ftdi.c ${CMAKE_CURRENT_SOURCE_DIR}/ftdi_stream.c CACHE INTERNAL "List of c sources" ) set(c_headers ${CMAKE_CURRENT_SOURCE_DIR}/ftdi.h CACHE INTERNAL "List of c headers" ) -add_library(ftdi1 SHARED ${c_sources}) - -math(EXPR VERSION_FIXUP "${MAJOR_VERSION} + 1") # Compatibility with previous releases -set_target_properties(ftdi1 PROPERTIES VERSION ${VERSION_FIXUP}.${MINOR_VERSION}.0 SOVERSION 2) -# Prevent clobbering each other during the build -set_target_properties ( ftdi1 PROPERTIES CLEAN_DIRECT_OUTPUT 1 ) - - -# Dependencies -target_link_libraries(ftdi1 ${LIBUSB_LIBRARIES}) - -install ( TARGETS ftdi1 +# Shared library +if ( SHAREDLIBS ) + add_library(ftdi1 SHARED ${c_sources}) + math(EXPR VERSION_FIXUP "${MAJOR_VERSION} + 1") # Compatibility with previous releases + set_target_properties(ftdi1 PROPERTIES VERSION ${VERSION_FIXUP}.${MINOR_VERSION}.0 SOVERSION 2) + # Prevent clobbering each other during the build + set_target_properties(ftdi1 PROPERTIES CLEAN_DIRECT_OUTPUT 1) + target_link_libraries(ftdi1 ${LIBUSB_LIBRARIES}) + install(TARGETS ftdi1 RUNTIME DESTINATION bin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} -) + ) +endif() +# Static library if ( STATICLIBS ) add_library(ftdi1-static STATIC ${c_sources}) target_link_libraries(ftdi1-static ${LIBUSB_LIBRARIES}) set_target_properties(ftdi1-static PROPERTIES OUTPUT_NAME "ftdi1") set_target_properties(ftdi1-static PROPERTIES CLEAN_DIRECT_OUTPUT 1) - install ( TARGETS ftdi1-static - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - COMPONENT staticlibs + install(TARGETS ftdi1-static + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT staticlibs ) -endif () +endif() -install ( FILES ${c_headers} - DESTINATION include/${PROJECT_NAME} - COMPONENT headers +install(FILES ${c_headers} + DESTINATION include/${PROJECT_NAME} + COMPONENT headers ) -- 1.7.1