From ad1ef1b64590b72a3286046f8baa79ac055f9923 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Fri, 9 Jan 2015 18:39:29 +0100 Subject: 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. --- host/CMakeLists.txt | 31 ++++++++- host/cmake/Modules/UHDConfig.cmake | 93 -------------------------- host/cmake/Modules/UHDConfig.cmake.in | 118 +++++++++++++++++++++++++++++++++ host/examples/init_usrp/CMakeLists.txt | 32 +++++++-- host/lib/CMakeLists.txt | 13 +++- 5 files changed, 188 insertions(+), 99 deletions(-) delete mode 100644 host/cmake/Modules/UHDConfig.cmake create mode 100644 host/cmake/Modules/UHDConfig.cmake.in (limited to 'host') diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt index 67a824c8f..c90c5de14 100644 --- a/host/CMakeLists.txt +++ b/host/CMakeLists.txt @@ -65,6 +65,14 @@ SET(PKG_MAN_DIR share/man/man1) 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 ######################################################################## @@ -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 deleted file mode 100644 index cf841cc85..000000000 --- a/host/cmake/Modules/UHDConfig.cmake +++ /dev/null @@ -1,93 +0,0 @@ -# -# Copyright 2014 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 -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -######################################################################## -# -# Find the header and library "libuhd" for the USRP -# Hardware Driver. Priorty for prefix search is: -# 1) ENV(UHD_DIR) -# 2) pkg-config results, if available; -# 3) CMAKE_INSTALL_PREFIX -# 4) /usr/local/ -# 5) /usr/ -# -# Version info is handled by UHDConfigVersion.cmake only; not here. -# -######################################################################## - -# set that this file was found, for use in GNU Radio's FindUHD.cmake. -# Have to use the ENV, since this file might not allow CACHE changes. - -set(ENV{UHD_CONFIG_USED} TRUE) - -# set default values - -SET(UHD_FOUND TRUE) -SET(UHD_INCLUDE_HINTS) -SET(UHD_LIBDIR_HINTS) -SET(UHD_DIR $ENV{UHD_DIR}) - -IF(UHD_DIR) - LIST(APPEND UHD_INCLUDE_HINTS ${UHD_DIR}/include) - LIST(APPEND UHD_LIBDIR_HINTS ${UHD_DIR}/lib) -ENDIF() - -INCLUDE(FindPkgConfig) -IF(PKG_CONFIG_FOUND) - IF(NOT ${CMAKE_VERSION} VERSION_LESS "2.8.0") - SET(UHD_QUIET "QUIET") - ENDIF() - PKG_CHECK_MODULES(PC_UHD ${UHD_QUIET} uhd) - IF(PC_UHD_FOUND) - LIST(APPEND UHD_INCLUDE_HINTS ${PC_UHD_INCLUDEDIR}) - LIST(APPEND UHD_LIBDIR_HINTS ${PC_UHD_LIBDIR}) - ENDIF() -ENDIF() - -LIST(APPEND UHD_INCLUDE_HINTS ${CMAKE_INSTALL_PREFIX}/include) -LIST(APPEND UHD_LIBDIR_HINTS ${CMAKE_INSTALL_PREFIX}/lib) - -# Verify that and libuhd are available, and, if a -# version is provided, that UHD meets the version requirements -- no -# matter what pkg-config might think. - -FIND_PATH( - UHD_INCLUDE_DIRS - NAMES uhd/config.hpp - HINTS ${UHD_INCLUDE_HINTS} - PATHS /usr/local/include - /usr/include -) - -FIND_LIBRARY( - UHD_LIBRARIES - NAMES uhd - HINTS ${UHD_LIBDIR_HINTS} - PATHS /usr/local/lib - /usr/lib -) - -IF(UHD_LIBRARIES AND UHD_INCLUDE_DIRS) - - INCLUDE(FindPackageHandleStandardArgs) - FIND_PACKAGE_HANDLE_STANDARD_ARGS(UHD DEFAULT_MSG UHD_LIBRARIES UHD_INCLUDE_DIRS) - MARK_AS_ADVANCED(UHD_LIBRARIES UHD_INCLUDE_DIRS) - -ELSEIF(UHD_FIND_REQUIRED) - - MESSAGE(FATAL_ERROR "UHD is required, but was not found.") - -ENDIF() diff --git a/host/cmake/Modules/UHDConfig.cmake.in b/host/cmake/Modules/UHDConfig.cmake.in new file mode 100644 index 000000000..78f01706f --- /dev/null +++ b/host/cmake/Modules/UHDConfig.cmake.in @@ -0,0 +1,118 @@ +# +# Copyright 2014 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +######################################################################## +# +# Find the header and library "libuhd" for the USRP +# Hardware Driver. Priorty for prefix search is: +# 1) ENV(UHD_DIR) +# 2) pkg-config results, if available; +# 3) CMAKE_INSTALL_PREFIX +# 4) /usr/local/ +# 5) /usr/ +# +# Version info is handled by UHDConfigVersion.cmake only; not here. +# +######################################################################## + +# set that this file was found, for use in GNU Radio's FindUHD.cmake. +# Have to use the ENV, since this file might not allow CACHE changes. + +set(ENV{UHD_CONFIG_USED} TRUE) + +# set default values + +SET(UHD_FOUND TRUE) +SET(UHD_INCLUDE_HINTS) +SET(UHD_LIBDIR_HINTS) +SET(UHD_DIR $ENV{UHD_DIR}) + +IF(UHD_DIR) + LIST(APPEND UHD_INCLUDE_HINTS ${UHD_DIR}/include) + LIST(APPEND UHD_LIBDIR_HINTS ${UHD_DIR}/lib) +ENDIF() + +INCLUDE(FindPkgConfig) +IF(PKG_CONFIG_FOUND) + IF(NOT ${CMAKE_VERSION} VERSION_LESS "2.8.0") + SET(UHD_QUIET "QUIET") + ENDIF() + PKG_CHECK_MODULES(PC_UHD ${UHD_QUIET} uhd) + IF(PC_UHD_FOUND) + LIST(APPEND UHD_INCLUDE_HINTS ${PC_UHD_INCLUDEDIR}) + LIST(APPEND UHD_LIBDIR_HINTS ${PC_UHD_LIBDIR}) + ENDIF() +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 and libuhd are available, and, if a +# version is provided, that UHD meets the version requirements -- no +# matter what pkg-config might think. + +FIND_PATH( + UHD_INCLUDE_DIRS + NAMES uhd/config.hpp + HINTS ${UHD_INCLUDE_HINTS} + PATHS /usr/local/include + /usr/include +) + +FIND_LIBRARY( + UHD_LIBRARIES + NAMES uhd + HINTS ${UHD_LIBDIR_HINTS} + PATHS /usr/local/lib + /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) + FIND_PACKAGE_HANDLE_STANDARD_ARGS(UHD DEFAULT_MSG UHD_LIBRARIES UHD_INCLUDE_DIRS) + MARK_AS_ADVANCED(UHD_LIBRARIES UHD_INCLUDE_DIRS) + +ELSEIF(UHD_FIND_REQUIRED) + + MESSAGE(FATAL_ERROR "UHD is required, but was not found.") + +ENDIF() 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) -- cgit v1.2.3