Skip to content

Commit

Permalink
Add a check for linking all static libraries together
Browse files Browse the repository at this point in the history
This ensures that we accidentally don't create two static libraries with
duplicate symbols. In any duplicate symbols should be hidden by using
unnamed namespaces or `static`.
  • Loading branch information
p12tic committed Aug 26, 2022
1 parent b605b93 commit c4a56ab
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ string(TOLOWER ${DEPS_CMAKE_BUILD_TYPE} DEPS_CMAKE_BUILD_TYPE_LOWERCASE)

option(ALICEVISION_BUILD_TESTS "Build AliceVision tests" OFF)

# Enabling this one will cause an expensive link step after pretty much any
# modification to the sources. Use it only in cases where rebuilding full project
# e.g. in continuous integration tests.
option(ALICEVISION_BUILD_STATIC_LIB_SYMBOL_TEST
"Build an internal test for checking against duplicate symbols" OFF)

set(ALICEVISION_BUNDLE_PREFIX "${CMAKE_INSTALL_PREFIX}/bundle" CACHE STRING "Path for bundle installation")

set(ALICEVISION_ROOT ${PROJECT_BINARY_DIR})
Expand Down
22 changes: 22 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ message("** Build Shared libs: " ${BUILD_SHARED_LIBS})
message("** Build SfM part: " ${ALICEVISION_BUILD_SFM})
message("** Build MVS part: " ${ALICEVISION_BUILD_MVS})
message("** Build AliceVision tests: " ${ALICEVISION_BUILD_TESTS})
message("** Build AliceVision static lib symbol test: " ${ALICEVISION_BUILD_STATIC_LIB_SYMBOL_TEST})
message("** Build AliceVision documentation: " ${ALICEVISION_HAVE_DOC})
message("** Build AliceVision samples programs: " ${ALICEVISION_BUILD_EXAMPLES})
message("** Build AliceVision+OpenCV samples programs: " ${ALICEVISION_HAVE_OPENCV})
Expand Down Expand Up @@ -1005,6 +1006,8 @@ add_subdirectory(dependencies)
# AliceVision modules
# ==============================================================================

set_property(GLOBAL PROPERTY global_all_static_libs)

# software(s) under patent or commercial licence
# Included for research purpose only
if(ALICEVISION_BUILD_SFM)
Expand All @@ -1024,6 +1027,25 @@ if(ALICEVISION_BUILD_SOFTWARE)
add_subdirectory(software)
endif()

# Create a unused dynamic library to ensure that there are no name clashes
# between symbols exported from source files passed to alicevision_add_software
#
# Since we're using global property to collect the list of all libraries, this
# is the only place where we can put the definition of such test.
get_property(all_static_libs GLOBAL PROPERTY global_all_static_libs)
if(CMAKE_COMPILER_IS_GNUCXX AND ALICEVISION_BUILD_STATIC_LIB_SYMBOL_TEST)
# CMake 3.24 will have cross-platform feature to force inclusion of all static
# library members. Until then, do this test only on GNU systems.
set(empty_cpp ${CMAKE_CURRENT_BINARY_DIR}/test_aliceVision_combined_library_empty.cpp)
if(NOT EXISTS ${empty_cpp})
file(WRITE ${empty_cpp} "")
endif()

add_executable(test_aliceVision_combined_libraries aliceVision/system/empty_main.cpp)
target_link_libraries(test_aliceVision_combined_libraries
PUBLIC -Wl,--whole-archive ${all_static_libs} -Wl,--no-whole-archive)
endif()

# ==============================================================================
# Install rules
# ==============================================================================
Expand Down
13 changes: 13 additions & 0 deletions src/aliceVision/system/empty_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file is part of the AliceVision project.
// Copyright (c) 2016 AliceVision contributors.
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

// This file is used in static library symbol duplication test that is enabled
// via ALICEVISION_BUILD_STATIC_LIB_SYMBOL_TEST.

int main()
{
return 0;
}
4 changes: 4 additions & 0 deletions src/cmake/Helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ function(alicevision_add_software software_name)
PUBLIC ${SOFTWARE_INCLUDE_DIRS}
)

get_property(all_static_libs GLOBAL PROPERTY global_all_static_libs)
set(all_static_libs ${all_static_libs} ${software_name}_static_lib)
set_property(GLOBAL PROPERTY global_all_static_libs ${all_static_libs})

# The executable will depend on the static library and will only include a main() function that
# calls the aliceVision_main symbol.
add_executable(${software_name}_exe ${SOFTWARE_SOURCE}
Expand Down

0 comments on commit c4a56ab

Please sign in to comment.