From 1bed89e9429a5cff20232836a9af2093690f8441 Mon Sep 17 00:00:00 2001 From: Lawrence Ibarria Date: Sun, 12 Feb 2023 13:29:30 -0800 Subject: [PATCH] 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. --- cmake/HandleEigen.cmake | 2 +- cmake/HandleGeneralOptions.cmake | 21 ++++++++++++++++++++- cmake/HandleGlobalBuildFlags.cmake | 4 ++-- cmake/HandlePrintConfiguration.cmake | 2 +- cmake/dllexport.h.in | 6 +++--- gtsam/CMakeLists.txt | 9 +++++---- gtsam_unstable/CMakeLists.txt | 5 ++--- matlab/CMakeLists.txt | 6 +++--- 8 files changed, 37 insertions(+), 18 deletions(-) diff --git a/cmake/HandleEigen.cmake b/cmake/HandleEigen.cmake index b3b4f66b61..e2e9835eeb 100644 --- a/cmake/HandleEigen.cmake +++ b/cmake/HandleEigen.cmake @@ -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() diff --git a/cmake/HandleGeneralOptions.cmake b/cmake/HandleGeneralOptions.cmake index 83cf9bbb97..c671fd0f39 100644 --- a/cmake/HandleGeneralOptions.cmake +++ b/cmake/HandleGeneralOptions.cmake @@ -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) @@ -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) diff --git a/cmake/HandleGlobalBuildFlags.cmake b/cmake/HandleGlobalBuildFlags.cmake index f33e12b94d..cb48f875b8 100644 --- a/cmake/HandleGlobalBuildFlags.cmake +++ b/cmake/HandleGlobalBuildFlags.cmake @@ -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() diff --git a/cmake/HandlePrintConfiguration.cmake b/cmake/HandlePrintConfiguration.cmake index dd3f6fc1fc..530f3ac903 100644 --- a/cmake/HandlePrintConfiguration.cmake +++ b/cmake/HandlePrintConfiguration.cmake @@ -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 ") diff --git a/cmake/dllexport.h.in b/cmake/dllexport.h.in index 7d757edeab..2ec4fcfb15 100644 --- a/cmake/dllexport.h.in +++ b/cmake/dllexport.h.in @@ -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 @@ -56,5 +56,5 @@ #endif #endif -#undef BUILD_SHARED_LIBS +#undef GTSAM_SHARED_LIB diff --git a/gtsam/CMakeLists.txt b/gtsam/CMakeLists.txt index a351d0d481..916ec9af7e 100644 --- a/gtsam/CMakeLists.txt +++ b/gtsam/CMakeLists.txt @@ -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}) @@ -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) diff --git a/gtsam_unstable/CMakeLists.txt b/gtsam_unstable/CMakeLists.txt index de59f547e0..30f096772a 100644 --- a/gtsam_unstable/CMakeLists.txt +++ b/gtsam_unstable/CMakeLists.txt @@ -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) @@ -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) diff --git a/matlab/CMakeLists.txt b/matlab/CMakeLists.txt index 81132fbcff..4c54992bfe 100644 --- a/matlab/CMakeLists.txt +++ b/matlab/CMakeLists.txt @@ -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." @@ -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() @@ -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()