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.
This commit is contained in:
Martin Braun 2015-01-09 18:39:29 +01:00
parent 734180c44b
commit ad1ef1b645
4 changed files with 95 additions and 6 deletions

View File

@ -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
)

View File

@ -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)

View File

@ -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.

View File

@ -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)