cmake: Fix Boost settings for static builds

Building static libraries with UHD was broken due to the way that
UHDBoost was structured. The following changes to UHDBoost.cmake were
applied:

- This file no longer observes ENABLE_STATIC_LIBS. That's because in UHD,
  setting ENABLE_STATIC_LIBS is supposed to build both static and shared
  libraries, but the way UHDBoost.cmake was written, it would apply
  settings for static builds to all libraries. If ENABLE_STATIC_LIBS was
  set, then this would cause linker errors.
  - As a consequence, users of UHDBoost.cmake must specify
    Boost_USE_STATIC_LIBS manually before including it.
- BUILD_SHARED_LIBS is no longer modified. UHD always specifies the
  correct library modifier (SHARED, STATIC, etc.) directly in the
  add_library() call, and that is good practice. By overriding
  BUILD_SHARED_LIBS in UHDBoost.cmake, this file can make changes to
  CMake files that call add_library() without a modifier.

In host/lib/CMakeLists.txt, we now call UHDBoost a second time for
static builds but set Boost_USE_STATIC_LIBS beforehand. This allows to
specify linker targets for the static library independently from the
shared library.

We also factored out some common library property manipulations that are
shared between uhd and uhd_static.
This commit is contained in:
Martin Braun 2025-09-17 11:10:01 +02:00 committed by Jörg Hofrichter
parent 22c01dd9c0
commit 3c6df18579
2 changed files with 47 additions and 39 deletions

View File

@ -6,11 +6,8 @@
# The following variables can be defined external to this script.
#
# ENABLE_STATIC_LIBS : whether to enable static libraries, which will
# require static Boost libraries too. If not using static libraries,
# shared libraries will be used. The default is to use shared
# libraries, and this default will be used if this variable is not
# defined or if is it NO/OFF/FALSE.
# Boost_*: Check the FindBoost CMake documentation. For example,
# Boost_USE_STATIC_LIBS can be set to require static libraries.
#
# UHD_BOOST_REQUIRED_COMPONENTS : Boost components that are required
# for Boost_FOUND to be true. The linkage (shared or static) must be
@ -162,23 +159,6 @@ if(NOT CMAKE_CXX_STANDARD)
message(WARNING "\nC++ standard not yet set; setting to C++14.\n")
endif()
# tell boost the linkage required
set(Boost_USE_STATIC_LIBS ${ENABLE_STATIC_LIBS})
# temporarily explicitly enable or disable shared libraries,
# build-wise; this can be disabled on a case by case basis for each
# library built.
if(BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS_SET TRUE)
set(OLD_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
else()
set(BUILD_SHARED_LIBS_SET FALSE)
endif()
if(ENABLE_STATIC_LIBS)
set(BUILD_SHARED_LIBS FALSE)
else()
set(BUILD_SHARED_LIBS TRUE)
endif()
if(${UHD_BOOST_OPTIONAL_COMPONENTS_LEN} GREATER 0)
message(STATUS " Looking for optional Boost components...")
find_package(Boost ${UHD_BOOST_MIN_VERSION} QUIET
@ -191,14 +171,6 @@ if(${UHD_BOOST_REQUIRED_COMPONENTS_LEN} GREATER 0)
COMPONENTS ${UHD_BOOST_REQUIRED_COMPONENTS} ${UHD_BOOST_REQUIRED})
endif()
# restore BUILD_SHARED_LIBS, if set
if(BUILD_SHARED_LIBS_SET)
set(BUILD_SHARED_LIBS ${OLD_BUILD_SHARED_LIBS})
unset(OLD_BUILD_SHARED_LIBS)
else()
unset(BUILD_SHARED_LIBS)
endif()
if(NOT Boost_FOUND)
# Boost was not found
# clear out return variables, just in case

View File

@ -187,10 +187,15 @@ target_link_libraries(uhd
${libuhd_libs}
PRIVATE uhd_rc
)
function(UHD_LIB_ADD_RPCLIB uhd_lib_name)
target_include_directories(${uhd_lib_name} PRIVATE deps/rpclib/include)
target_sources(${uhd_lib_name} PRIVATE $<TARGET_OBJECTS:uhd_rpclib>)
endfunction(UHD_LIB_ADD_RPCLIB)
if(ENABLE_MPMD)
target_include_directories(uhd PRIVATE deps/rpclib/include)
add_subdirectory(deps)
target_sources(uhd PRIVATE $<TARGET_OBJECTS:uhd_rpclib>)
UHD_LIB_ADD_RPCLIB(uhd)
endif()
if(ENABLE_DPDK)
add_definitions(-DHAVE_DPDK)
@ -217,12 +222,12 @@ if(DEFINED LIBUHD_OUTPUT_NAME)
set_target_properties(uhd PROPERTIES OUTPUT_NAME ${LIBUHD_OUTPUT_NAME})
endif(DEFINED LIBUHD_OUTPUT_NAME)
if(ENABLE_SIM)
function(UHD_LIB_ADD_PYTHON uhd_lib_name)
# Get python include dirs
# This conditional code somewhat duplicates logic also found in
# ../python/CMakeLists.txt but omits user messages. This avoid
# duplicate output, since SIM mode requires python support.
target_include_directories(uhd PUBLIC ${PYTHON_INCLUDE_DIRS})
target_include_directories(${uhd_lib_name} PUBLIC ${PYTHON_INCLUDE_DIRS})
if(NOT pybind11_FOUND)
set(PYBIND11_INCLUDE_DIR
"${UHD_SOURCE_DIR}/lib/deps/pybind11/include"
@ -231,12 +236,15 @@ if(ENABLE_SIM)
"Location of PyBind11 includes"
)
endif()
target_include_directories(uhd PRIVATE ${PYBIND11_INCLUDE_DIR})
target_include_directories(${uhd_lib_name} PRIVATE ${PYBIND11_INCLUDE_DIR})
# For PYUHD we don't link against the python libraries, but when calling
# python instead of being called by it, we have to.
target_link_libraries(uhd PUBLIC ${PYTHON_LIBRARIES})
target_link_libraries(${uhd_lib_name} PUBLIC ${PYTHON_LIBRARIES})
endfunction(UHD_LIB_ADD_PYTHON)
if(ENABLE_SIM)
UHD_LIB_ADD_PYTHON(uhd)
endif(ENABLE_SIM)
if(NOT UHDHOST_PKG) # Syntax makes it unusable by UHD_INSTALL
@ -250,13 +258,41 @@ endif(NOT UHDHOST_PKG)
#######################################################
# Setup libuhd library (static)
#######################################################
if(ENABLE_STATIC_LIBS)
# Create a function so we have a separate variable scope for Boost_USE_STATIC_LIBS
function(UHD_ADD_STATIC_LIB)
set(Boost_USE_STATIC_LIBS ON) # only find static libs
include(UHDBoost)
add_library(uhd_static STATIC ${libuhd_sources} $<TARGET_OBJECTS:uhd_rc>)
set_target_properties(uhd_static PROPERTIES OUTPUT_NAME uhd)
set_target_properties(uhd_static PROPERTIES COMPILE_DEFINITIONS UHD_STATIC_LIB)
target_link_libraries(uhd_static uhd_rc)
install(TARGETS uhd_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # .lib or .a file
target_link_libraries(uhd_static
PUBLIC
Boost::boost
Boost::chrono
Boost::date_time
Boost::filesystem
Boost::serialization
Boost::system
Boost::thread
)
target_include_directories(uhd_static
PRIVATE
${CMRC_INCLUDE_DIR}
)
if(ENABLE_MPMD)
UHD_LIB_ADD_RPCLIB(uhd_static)
endif()
if(ENABLE_SIM)
UHD_LIB_ADD_PYTHON(uhd_static)
endif()
install(TARGETS uhd_static
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries # .lib or .a file
)
endfunction(UHD_ADD_STATIC_LIB)
if(ENABLE_STATIC_LIBS)
UHD_ADD_STATIC_LIB()
endif(ENABLE_STATIC_LIBS)
if(ENABLE_EXTEND_WIN_PACKAGING)