diff options
author | Martin Braun <martin.braun@ettus.com> | 2015-01-09 18:39:29 +0100 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2015-01-19 09:07:16 +0100 |
commit | ad1ef1b64590b72a3286046f8baa79ac055f9923 (patch) | |
tree | 05950e99844a12c4c37a9a7a4f8b2cbbe6a59be4 /host | |
parent | 734180c44b438cf707c4ff48bf4b11a8c5a94136 (diff) | |
download | uhd-ad1ef1b64590b72a3286046f8baa79ac055f9923.tar.gz uhd-ad1ef1b64590b72a3286046f8baa79ac055f9923.tar.bz2 uhd-ad1ef1b64590b72a3286046f8baa79ac055f9923.zip |
cmake: Added ENABLE_STATIC_LIBS option
- Allows building static libraries
- For users calling find_package(UHD), provides extra options
for building apps statically linking UHD.
- Updated the init_usrp example to link UHD statically.
Diffstat (limited to 'host')
-rw-r--r-- | host/CMakeLists.txt | 31 | ||||
-rw-r--r-- | host/cmake/Modules/UHDConfig.cmake.in (renamed from host/cmake/Modules/UHDConfig.cmake) | 25 | ||||
-rw-r--r-- | host/examples/init_usrp/CMakeLists.txt | 32 | ||||
-rw-r--r-- | host/lib/CMakeLists.txt | 13 |
4 files changed, 95 insertions, 6 deletions
diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt index 67a824c8f..c90c5de14 100644 --- a/host/CMakeLists.txt +++ b/host/CMakeLists.txt @@ -66,6 +66,14 @@ INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}/include) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) ######################################################################## +# Static Lib Configuration +######################################################################## +OPTION(ENABLE_STATIC_LIBS "Enable building of static libraries" OFF) +IF(ENABLE_STATIC_LIBS) + MESSAGE(STATUS "Building Static Libraries: ${ENABLE_STATIC_LIBS}") +ENDIF(ENABLE_STATIC_LIBS) + +######################################################################## # On Apple only, set install name and use rpath correctly, if not already set ######################################################################## IF(APPLE) @@ -315,14 +323,35 @@ IF(NOT CMAKE_MODULES_DIR) SET(CMAKE_MODULES_DIR lib${LIB_SUFFIX}/cmake) ENDIF(NOT CMAKE_MODULES_DIR) +# UHDConfig.cmake needs UHD_LINK_LIST_STATIC set: +LIST(APPEND UHD_LINK_LIST_STATIC "dl") +LIST(APPEND UHD_LINK_LIST_STATIC "pthread") +FOREACH(Boost_Comp ${BOOST_REQUIRED_COMPONENTS}) + IF(NOT ${Boost_Comp} STREQUAL "unit_test_framework") + LIST(APPEND UHD_LINK_LIST_STATIC "boost_${Boost_Comp}") + ENDIF(NOT ${Boost_Comp} STREQUAL "unit_test_framework") +ENDFOREACH(Boost_Comp) +IF(ENABLE_USB) + LIST(APPEND UHD_LINK_LIST_STATIC "usb-1.0") +ENDIF(ENABLE_USB) +IF(ENABLE_ORC) + LIST(APPEND UHD_LINK_LIST_STATIC "orc-0.4") +ENDIF(ENABLE_ORC) +message(STATUS "libs: ${UHD_LINK_LIST_STATIC}") + CONFIGURE_FILE( ${CMAKE_SOURCE_DIR}/cmake/Modules/UHDConfigVersion.cmake.in ${CMAKE_BINARY_DIR}/cmake/Modules/UHDConfigVersion.cmake @ONLY ) +CONFIGURE_FILE( + ${CMAKE_SOURCE_DIR}/cmake/Modules/UHDConfig.cmake.in + ${CMAKE_BINARY_DIR}/cmake/Modules/UHDConfig.cmake + @ONLY +) SET(cmake_configs - ${CMAKE_SOURCE_DIR}/cmake/Modules/UHDConfig.cmake + ${CMAKE_BINARY_DIR}/cmake/Modules/UHDConfig.cmake ${CMAKE_BINARY_DIR}/cmake/Modules/UHDConfigVersion.cmake ) diff --git a/host/cmake/Modules/UHDConfig.cmake b/host/cmake/Modules/UHDConfig.cmake.in index cf841cc85..78f01706f 100644 --- a/host/cmake/Modules/UHDConfig.cmake +++ b/host/cmake/Modules/UHDConfig.cmake.in @@ -60,6 +60,19 @@ ENDIF() LIST(APPEND UHD_INCLUDE_HINTS ${CMAKE_INSTALL_PREFIX}/include) LIST(APPEND UHD_LIBDIR_HINTS ${CMAKE_INSTALL_PREFIX}/lib) + +# Search for static libs if so required +IF( UHD_USE_STATIC_LIBS ) + SET( _UHD_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) + IF(WIN32) + SET(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES}) + ELSE() + SET(CMAKE_FIND_LIBRARY_SUFFIXES .a ) + ENDIF() + # This is set during build of UHD to match the installed version: + SET(UHD_STATIC_LIB_DEPS "@UHD_LINK_LIST_STATIC@") +ENDIF() + # Verify that <uhd/config.hpp> and libuhd are available, and, if a # version is provided, that UHD meets the version requirements -- no # matter what pkg-config might think. @@ -80,6 +93,18 @@ FIND_LIBRARY( /usr/lib ) +# Set up linker flags for static linking: +IF(UHD_USE_STATIC_LIBS) + IF(WIN32) + MESSAGE(FATAL_ERROR "Static linking not available on Windows") + ELSE(WIN32) + # This works for gcc and Clang: + SET(UHD_STATIC_LIB_LINK_FLAG "-Wl,-whole-archive ${UHD_LIBRARIES} -Wl,-no-whole-archive") + ENDIF(WIN32) + # Restore the library suffixes, if we changed them: + SET(CMAKE_FIND_LIBRARY_SUFFIXES ${_UHD_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES}) +ENDIF(UHD_USE_STATIC_LIBS) + IF(UHD_LIBRARIES AND UHD_INCLUDE_DIRS) INCLUDE(FindPackageHandleStandardArgs) diff --git a/host/examples/init_usrp/CMakeLists.txt b/host/examples/init_usrp/CMakeLists.txt index 420e79993..3560dbd45 100644 --- a/host/examples/init_usrp/CMakeLists.txt +++ b/host/examples/init_usrp/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright 2014 Ettus Research LLC +# Copyright 2014-2015 Ettus Research LLC # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -18,6 +18,11 @@ cmake_minimum_required(VERSION 2.8) ### Set up build environment ################################################## +# Choose a static or shared-library build (shared is default, and static will +# probably need some special care!) +# Set this to ON in order to link a static build of UHD: +option(UHD_USE_STATIC_LIBS OFF) + # This example also requires Boost: set(BOOST_REQUIRED_COMPONENTS program_options @@ -35,10 +40,10 @@ endif(MSVC) find_package(Boost "1.46" REQUIRED ${BOOST_REQUIRED_COMPONENTS}) # To add UHD as a dependency to this project, add a line such as this: -find_package(UHD "3.7.1" REQUIRED) +find_package(UHD "3.8.0" REQUIRED) # The version in ^^^^^ here is a minimum version. # To specify an exact version: -#find_package(UHD 3.7.1 EXACT REQUIRED) +#find_package(UHD 3.8.1 EXACT REQUIRED) ### Configure Compiler ######################################################## include_directories( @@ -49,7 +54,26 @@ link_directories(${Boost_LIBRARY_DIRS}) ### Make the executable ####################################################### add_executable(init_usrp init_usrp.cpp) -target_link_libraries(init_usrp ${UHD_LIBRARIES} ${Boost_LIBRARIES}) +# Shared library case: All we need to do is link against the library, and +# anything else we need (in this case, some Boost libraries): +if(NOT UHD_USE_STATIC_LIBS) + message(STATUS "Linking against shared UHD library.") + target_link_libraries(init_usrp ${UHD_LIBRARIES} ${Boost_LIBRARIES}) +# Shared library case: All we need to do is link against the library, and +# anything else we need (in this case, some Boost libraries): +else(NOT UHD_USE_STATIC_LIBS) + message(STATUS "Linking against static UHD library.") + target_link_libraries(init_usrp + # We could use ${UHD_LIBRARIES}, but linking requires some extra flags, + # so we use this convenience variable provided to us + ${UHD_STATIC_LIB_LINK_FLAG} + # Also, when linking statically, we need to pull in all the deps for + # UHD as well, because the dependencies don't get resolved automatically + ${UHD_STATIC_LIB_DEPS} + ) +endif(NOT UHD_USE_STATIC_LIBS) + +### Once it's built... ######################################################## # Here, you would have commands to install your program. # We will skip these in this example. diff --git a/host/lib/CMakeLists.txt b/host/lib/CMakeLists.txt index eed8b642c..5755a6c4c 100644 --- a/host/lib/CMakeLists.txt +++ b/host/lib/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright 2010-2014 Ettus Research LLC +# Copyright 2010-2015 Ettus Research LLC # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -128,3 +128,14 @@ IF(NOT UHDHOST_PKG) #Syntax makes it unusable by UHD_INSTALL RUNTIME DESTINATION ${RUNTIME_DIR} COMPONENT libraries # .dll file ) ENDIF(NOT UHDHOST_PKG) + +####################################################### +# Setup libuhd library (static) +####################################################### +IF(ENABLE_STATIC_LIBS) + ADD_LIBRARY(uhd_static STATIC ${libuhd_sources}) + SET_TARGET_PROPERTIES(uhd_static PROPERTIES OUTPUT_NAME uhd) + INSTALL(TARGETS uhd_static + ARCHIVE DESTINATION lib${LIB_SUFFIX} # .lib or .a file + ) +ENDIF(ENABLE_STATIC_LIBS) |