Skip to content

Commit

Permalink
Allow override of BUILD_SHARED_LIBS
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ibarria committed Feb 12, 2023
1 parent 16bffa6 commit 1bed89e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmake/HandleEigen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ else()
endif ()

if (MSVC)
if (BUILD_SHARED_LIBS)
if (GTSAM_LIBRARY_TYPE STREQUAL "SHARED")
# mute eigen static assert to avoid errors in shared lib
list_append_cache(GTSAM_COMPILE_DEFINITIONS_PUBLIC EIGEN_NO_STATIC_ASSERT)
endif()
Expand Down
21 changes: 20 additions & 1 deletion cmake/HandleGeneralOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ if(GTSAM_UNSTABLE_AVAILABLE)
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)
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_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)
Expand All @@ -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_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
if (GTSAM_ENABLE_BOOST_SERIALIZATION)
add_definitions(-DGTSAM_ENABLE_BOOST_SERIALIZATION)
Expand Down
4 changes: 2 additions & 2 deletions cmake/HandleGlobalBuildFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ endif()
# or explicit instantiation will generate build errors.
# 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)
endif()

if (APPLE AND BUILD_SHARED_LIBS)
if (APPLE AND GTSAM_SHARED_LIB)
# Set the default install directory on macOS
set(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib")
endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake/HandlePrintConfiguration.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ print_enabled_config(${GTSAM_BUILD_TIMING_ALWAYS} "Build timing scripts
if (DOXYGEN_FOUND)
print_enabled_config(${GTSAM_BUILD_DOCS} "Build Docs")
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")
if(GTSAM_UNSTABLE_AVAILABLE)
print_enabled_config(${GTSAM_BUILD_UNSTABLE} "Build libgtsam_unstable ")
Expand Down
6 changes: 3 additions & 3 deletions cmake/dllexport.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

// Whether GTSAM is compiled as static or DLL in windows.
// This will be used to decide whether include __declspec(dllimport) or not in headers
#cmakedefine BUILD_SHARED_LIBS
#cmakedefine GTSAM_SHARED_LIB

#ifdef _WIN32
# ifndef BUILD_SHARED_LIBS
# ifndef GTSAM_SHARED_LIB
# define @library_name@_EXPORT
# define @library_name@_EXTERN_EXPORT extern
# else
Expand All @@ -56,5 +56,5 @@
#endif
#endif

#undef BUILD_SHARED_LIBS
#undef GTSAM_SHARED_LIB

9 changes: 5 additions & 4 deletions gtsam/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ message(STATUS "GTSAM Version: ${gtsam_version}")
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")

# 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:
add_library(gtsam ${gtsam_srcs})
# BUILD_SHARED_LIBS automatically defines static/shared libs
# 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_ADDITIONAL_LIBRARIES})

Expand Down Expand Up @@ -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 (NOT BUILD_SHARED_LIBS)
if (NOT GTSAM_SHARED_LIB)
set_target_properties(gtsam PROPERTIES
PREFIX "lib"
COMPILE_DEFINITIONS GTSAM_IMPORT_STATIC)
Expand Down
5 changes: 2 additions & 3 deletions gtsam_unstable/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ set(gtsam_unstable_soversion ${GTSAM_VERSION_MAJOR})
message(STATUS "GTSAM_UNSTABLE Version: ${gtsam_unstable_version}")
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")

# BUILD_SHARED_LIBS automatically defines static/shared libs:
add_library(gtsam_unstable ${gtsam_unstable_srcs})
add_library(gtsam_unstable ${GTSAM_LIBRARY_TYPE} ${gtsam_unstable_srcs})

# Apply build flags:
gtsam_apply_build_flags(gtsam_unstable)
Expand All @@ -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

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
PREFIX "lib"
COMPILE_DEFINITIONS GTSAM_UNSTABLE_IMPORT_STATIC)
Expand Down
6 changes: 3 additions & 3 deletions matlab/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ endif()

include(MatlabWrap)

if(NOT BUILD_SHARED_LIBS)
if(NOT GTSAM_SHARED_LIB)
message(
FATAL_ERROR
"GTSAM_INSTALL_MATLAB_TOOLBOX and BUILD_SHARED_LIBS=OFF."
Expand All @@ -54,7 +54,7 @@ endif()
# ##############################################################################
# Generate, build and install toolbox
set(mexFlags "${GTSAM_BUILD_MEX_BINARY_FLAGS}")
if(NOT BUILD_SHARED_LIBS)
if(NOT GTSAM_SHARED_LIB)
list(APPEND mexFlags -DGTSAM_IMPORT_STATIC)
endif()

Expand Down Expand Up @@ -89,7 +89,7 @@ matlab_wrap("${interface_files}" "gtsam" "${GTSAM_ADDITIONAL_LIBRARIES}"
if(GTSAM_UNSTABLE_INSTALL_MATLAB_TOOLBOX)
# Generate, build and install toolbox
set(mexFlags "${GTSAM_BUILD_MEX_BINARY_FLAGS}")
if(NOT BUILD_SHARED_LIBS)
if(NOT GTSAM_SHARED_LIB)
list(APPEND mexFlags -DGTSAM_IMPORT_STATIC)
endif()

Expand Down

0 comments on commit 1bed89e

Please sign in to comment.