Allow override of BUILD_SHARED_LIBS

The cmake option BUILD_SHARED_LIBS is a cmake built-in option
to control if libraries are by default SHARED or STATIC.

For large projects, it is desired to be able to design gtsam as a
shared or static library regardless of the value of BUILD_SHARED_LIBS.
This change is unobtrusive, two new cmake options are created, to
force gtsam to be a shared or static library. If neither option is
set (this is the default), the behavior of gtsam remains unchanged
which is to use BUILD_SHARED_LIBS decision.
release/4.3a0
Lawrence Ibarria 2023-02-12 13:29:30 -08:00
parent 16bffa645f
commit 1bed89e942
8 changed files with 37 additions and 18 deletions

View File

@ -82,7 +82,7 @@ else()
endif () endif ()
if (MSVC) if (MSVC)
if (BUILD_SHARED_LIBS) if (GTSAM_LIBRARY_TYPE STREQUAL "SHARED")
# mute eigen static assert to avoid errors in shared lib # mute eigen static assert to avoid errors in shared lib
list_append_cache(GTSAM_COMPILE_DEFINITIONS_PUBLIC EIGEN_NO_STATIC_ASSERT) list_append_cache(GTSAM_COMPILE_DEFINITIONS_PUBLIC EIGEN_NO_STATIC_ASSERT)
endif() endif()

View File

@ -14,7 +14,8 @@ if(GTSAM_UNSTABLE_AVAILABLE)
option(GTSAM_UNSTABLE_BUILD_PYTHON "Enable/Disable Python wrapper for libgtsam_unstable" ON) option(GTSAM_UNSTABLE_BUILD_PYTHON "Enable/Disable Python wrapper for libgtsam_unstable" ON)
option(GTSAM_UNSTABLE_INSTALL_MATLAB_TOOLBOX "Enable/Disable MATLAB wrapper for libgtsam_unstable" OFF) option(GTSAM_UNSTABLE_INSTALL_MATLAB_TOOLBOX "Enable/Disable MATLAB wrapper for libgtsam_unstable" OFF)
endif() endif()
option(BUILD_SHARED_LIBS "Build shared gtsam library, instead of static" ON) option(GTSAM_FORCE_SHARED_LIB "Force gtsam to be a shared library, overriding BUILD_SHARED_LIBS" OFF)
option(GTSAM_FORCE_STATIC_LIB "Force gtsam to be a static library, overriding BUILD_SHARED_LIBS" OFF)
option(GTSAM_USE_QUATERNIONS "Enable/Disable using an internal Quaternion representation for rotations instead of rotation matrices. If enable, Rot3::EXPMAP is enforced by default." OFF) option(GTSAM_USE_QUATERNIONS "Enable/Disable using an internal Quaternion representation for rotations instead of rotation matrices. If enable, Rot3::EXPMAP is enforced by default." OFF)
option(GTSAM_POSE3_EXPMAP "Enable/Disable using Pose3::EXPMAP as the default mode. If disabled, Pose3::FIRST_ORDER will be used." ON) option(GTSAM_POSE3_EXPMAP "Enable/Disable using Pose3::EXPMAP as the default mode. If disabled, Pose3::FIRST_ORDER will be used." ON)
option(GTSAM_ROT3_EXPMAP "Ignore if GTSAM_USE_QUATERNIONS is OFF (Rot3::EXPMAP by default). Otherwise, enable Rot3::EXPMAP, or if disabled, use Rot3::CAYLEY." ON) option(GTSAM_ROT3_EXPMAP "Ignore if GTSAM_USE_QUATERNIONS is OFF (Rot3::EXPMAP by default). Otherwise, enable Rot3::EXPMAP, or if disabled, use Rot3::CAYLEY." ON)
@ -31,6 +32,24 @@ option(GTSAM_TANGENT_PREINTEGRATION "Use new ImuFactor with integration
option(GTSAM_SLOW_BUT_CORRECT_BETWEENFACTOR "Use the slower but correct version of BetweenFactor" OFF) option(GTSAM_SLOW_BUT_CORRECT_BETWEENFACTOR "Use the slower but correct version of BetweenFactor" OFF)
option(GTSAM_ENABLE_BOOST_SERIALIZATION "Enable Boost serialization" ON) option(GTSAM_ENABLE_BOOST_SERIALIZATION "Enable Boost serialization" ON)
if (GTSAM_FORCE_SHARED_LIB)
message(STATUS "GTSAM is a shared library due to GTSAM_FORCE_SHARED_LIB")
set(GTSAM_LIBRARY_TYPE SHARED CACHE STRING "" FORCE)
set(GTSAM_SHARED_LIB 1 CACHE BOOL "" FORCE)
elseif (GTSAM_FORCE_STATIC_LIB)
message(STATUS "GTSAM is a static library due to GTSAM_FORCE_STATIC_LIB")
set(GTSAM_LIBRARY_TYPE STATIC CACHE STRING "" FORCE)
set(GTSAM_SHARED_LIB 0 CACHE BOOL "" FORCE)
elseif (BUILD_SHARED_LIBS)
message(STATUS "GTSAM is a shared library due to BUILD_SHARED_LIBS is ON")
set(GTSAM_LIBRARY_TYPE SHARED CACHE STRING "" FORCE)
set(GTSAM_SHARED_LIB 1 CACHE BOOL "" FORCE)
else()
message(STATUS "GTSAM is a static library due to BUILD_SHARED_LIBS is OFF")
set(GTSAM_LIBRARY_TYPE STATIC CACHE STRING "" FORCE)
set(GTSAM_SHARED_LIB 0 CACHE BOOL "" FORCE)
endif()
#TODO(kartikarcot) defining it in config.h.in did not work #TODO(kartikarcot) defining it in config.h.in did not work
if (GTSAM_ENABLE_BOOST_SERIALIZATION) if (GTSAM_ENABLE_BOOST_SERIALIZATION)
add_definitions(-DGTSAM_ENABLE_BOOST_SERIALIZATION) add_definitions(-DGTSAM_ENABLE_BOOST_SERIALIZATION)

View File

@ -14,11 +14,11 @@ endif()
# or explicit instantiation will generate build errors. # or explicit instantiation will generate build errors.
# See: https://bitbucket.org/gtborg/gtsam/issues/417/fail-to-build-on-msvc-2017 # See: https://bitbucket.org/gtborg/gtsam/issues/417/fail-to-build-on-msvc-2017
# #
if(MSVC AND BUILD_SHARED_LIBS) if(MSVC AND GTSAM_SHARED_LIB)
list_append_cache(GTSAM_COMPILE_DEFINITIONS_PUBLIC EIGEN_NO_STATIC_ASSERT) list_append_cache(GTSAM_COMPILE_DEFINITIONS_PUBLIC EIGEN_NO_STATIC_ASSERT)
endif() endif()
if (APPLE AND BUILD_SHARED_LIBS) if (APPLE AND GTSAM_SHARED_LIB)
# Set the default install directory on macOS # Set the default install directory on macOS
set(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib") set(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib")
endif() endif()

View File

@ -14,7 +14,7 @@ print_enabled_config(${GTSAM_BUILD_TIMING_ALWAYS} "Build timing scripts
if (DOXYGEN_FOUND) if (DOXYGEN_FOUND)
print_enabled_config(${GTSAM_BUILD_DOCS} "Build Docs") print_enabled_config(${GTSAM_BUILD_DOCS} "Build Docs")
endif() endif()
print_enabled_config(${BUILD_SHARED_LIBS} "Build shared GTSAM libraries") print_enabled_config(${GTSAM_SHARED_LIB} "Build shared GTSAM libraries")
print_enabled_config(${GTSAM_BUILD_TYPE_POSTFIXES} "Put build type in library name") print_enabled_config(${GTSAM_BUILD_TYPE_POSTFIXES} "Put build type in library name")
if(GTSAM_UNSTABLE_AVAILABLE) if(GTSAM_UNSTABLE_AVAILABLE)
print_enabled_config(${GTSAM_BUILD_UNSTABLE} "Build libgtsam_unstable ") print_enabled_config(${GTSAM_BUILD_UNSTABLE} "Build libgtsam_unstable ")

View File

@ -31,10 +31,10 @@
// Whether GTSAM is compiled as static or DLL in windows. // Whether GTSAM is compiled as static or DLL in windows.
// This will be used to decide whether include __declspec(dllimport) or not in headers // This will be used to decide whether include __declspec(dllimport) or not in headers
#cmakedefine BUILD_SHARED_LIBS #cmakedefine GTSAM_SHARED_LIB
#ifdef _WIN32 #ifdef _WIN32
# ifndef BUILD_SHARED_LIBS # ifndef GTSAM_SHARED_LIB
# define @library_name@_EXPORT # define @library_name@_EXPORT
# define @library_name@_EXTERN_EXPORT extern # define @library_name@_EXTERN_EXPORT extern
# else # else
@ -56,5 +56,5 @@
#endif #endif
#endif #endif
#undef BUILD_SHARED_LIBS #undef GTSAM_SHARED_LIB

View File

@ -118,10 +118,11 @@ message(STATUS "GTSAM Version: ${gtsam_version}")
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}") message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
# build shared and static versions of the library # build shared and static versions of the library
message(STATUS "Building GTSAM - shared: ${BUILD_SHARED_LIBS}") message(STATUS "Building GTSAM - as a ${GTSAM_LIBRARY_TYPE} library")
# BUILD_SHARED_LIBS automatically defines static/shared libs: # BUILD_SHARED_LIBS automatically defines static/shared libs
add_library(gtsam ${gtsam_srcs}) # Here we ensure we control which type of library gtsam is specifically
add_library(gtsam ${GTSAM_LIBRARY_TYPE} ${gtsam_srcs})
target_link_libraries(gtsam PUBLIC ${GTSAM_BOOST_LIBRARIES}) target_link_libraries(gtsam PUBLIC ${GTSAM_BOOST_LIBRARIES})
target_link_libraries(gtsam PUBLIC ${GTSAM_ADDITIONAL_LIBRARIES}) target_link_libraries(gtsam PUBLIC ${GTSAM_ADDITIONAL_LIBRARIES})
@ -174,7 +175,7 @@ target_include_directories(gtsam SYSTEM BEFORE PUBLIC
) )
if(WIN32) # Add 'lib' prefix to static library to avoid filename collision with shared library if(WIN32) # Add 'lib' prefix to static library to avoid filename collision with shared library
if (NOT BUILD_SHARED_LIBS) if (NOT GTSAM_SHARED_LIB)
set_target_properties(gtsam PROPERTIES set_target_properties(gtsam PROPERTIES
PREFIX "lib" PREFIX "lib"
COMPILE_DEFINITIONS GTSAM_IMPORT_STATIC) COMPILE_DEFINITIONS GTSAM_IMPORT_STATIC)

View File

@ -87,8 +87,7 @@ set(gtsam_unstable_soversion ${GTSAM_VERSION_MAJOR})
message(STATUS "GTSAM_UNSTABLE Version: ${gtsam_unstable_version}") message(STATUS "GTSAM_UNSTABLE Version: ${gtsam_unstable_version}")
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}") message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
# BUILD_SHARED_LIBS automatically defines static/shared libs: add_library(gtsam_unstable ${GTSAM_LIBRARY_TYPE} ${gtsam_unstable_srcs})
add_library(gtsam_unstable ${gtsam_unstable_srcs})
# Apply build flags: # Apply build flags:
gtsam_apply_build_flags(gtsam_unstable) gtsam_apply_build_flags(gtsam_unstable)
@ -102,7 +101,7 @@ target_link_libraries(gtsam_unstable PUBLIC gtsam)
# No need to link against Boost here, it's inherited from gtsam PUBLIC interface # No need to link against Boost here, it's inherited from gtsam PUBLIC interface
if(WIN32) # Add 'lib' prefix to static library to avoid filename collision with shared library if(WIN32) # Add 'lib' prefix to static library to avoid filename collision with shared library
if (NOT BUILD_SHARED_LIBS) if (NOT GTSAM_SHARED_LIB)
set_target_properties(gtsam_unstable PROPERTIES set_target_properties(gtsam_unstable PROPERTIES
PREFIX "lib" PREFIX "lib"
COMPILE_DEFINITIONS GTSAM_UNSTABLE_IMPORT_STATIC) COMPILE_DEFINITIONS GTSAM_UNSTABLE_IMPORT_STATIC)

View File

@ -42,7 +42,7 @@ endif()
include(MatlabWrap) include(MatlabWrap)
if(NOT BUILD_SHARED_LIBS) if(NOT GTSAM_SHARED_LIB)
message( message(
FATAL_ERROR FATAL_ERROR
"GTSAM_INSTALL_MATLAB_TOOLBOX and BUILD_SHARED_LIBS=OFF." "GTSAM_INSTALL_MATLAB_TOOLBOX and BUILD_SHARED_LIBS=OFF."
@ -54,7 +54,7 @@ endif()
# ############################################################################## # ##############################################################################
# Generate, build and install toolbox # Generate, build and install toolbox
set(mexFlags "${GTSAM_BUILD_MEX_BINARY_FLAGS}") set(mexFlags "${GTSAM_BUILD_MEX_BINARY_FLAGS}")
if(NOT BUILD_SHARED_LIBS) if(NOT GTSAM_SHARED_LIB)
list(APPEND mexFlags -DGTSAM_IMPORT_STATIC) list(APPEND mexFlags -DGTSAM_IMPORT_STATIC)
endif() endif()
@ -89,7 +89,7 @@ matlab_wrap("${interface_files}" "gtsam" "${GTSAM_ADDITIONAL_LIBRARIES}"
if(GTSAM_UNSTABLE_INSTALL_MATLAB_TOOLBOX) if(GTSAM_UNSTABLE_INSTALL_MATLAB_TOOLBOX)
# Generate, build and install toolbox # Generate, build and install toolbox
set(mexFlags "${GTSAM_BUILD_MEX_BINARY_FLAGS}") set(mexFlags "${GTSAM_BUILD_MEX_BINARY_FLAGS}")
if(NOT BUILD_SHARED_LIBS) if(NOT GTSAM_SHARED_LIB)
list(APPEND mexFlags -DGTSAM_IMPORT_STATIC) list(APPEND mexFlags -DGTSAM_IMPORT_STATIC)
endif() endif()