Skip to content

Commit

Permalink
Link against shared library by default
Browse files Browse the repository at this point in the history
This fixes the incessant ODR violations
  • Loading branch information
nathanwbrei committed Dec 16, 2024
1 parent 89fe2cf commit 29a4760
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
8 changes: 4 additions & 4 deletions cmake/AddJanaPlugin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
macro(add_jana_plugin plugin_name)

# Parse remaining arguments
set(options LINK_SHARED)
set(options LINK_STATIC)
set(oneValueArgs EXPORT)
set(multiValueArgs SOURCES PUBLIC_HEADER TESTS)

cmake_parse_arguments(PLUGIN "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if (LINK_SHARED)
set(PLUGIN_JANA_LIB jana2_shared_lib)
else()
if (LINK_STATIC)
set(PLUGIN_JANA_LIB jana2_static_lib)
else()
set(PLUGIN_JANA_LIB jana2_shared_lib)
endif()

if (NOT PLUGIN_SOURCES AND NOT PLUGIN_PUBLIC_HEADER AND NOT PLUGIN_TESTS)
Expand Down
19 changes: 17 additions & 2 deletions cmake/AddJanaTest.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

macro(add_jana_test test_target_name)

cmake_parse_arguments(JANATEST "" "" "SOURCES" ${ARGN})
set(options LINK_STATIC)
cmake_parse_arguments(JANATEST "LINK_STATIC" "" "SOURCES" ${ARGN})

if (NOT JANATEST_SOURCES)
file(GLOB JANATEST_SOURCES "*.c*")
Expand All @@ -10,7 +11,21 @@ macro(add_jana_test test_target_name)
# Set up target
add_executable(${test_target_name} ${JANATEST_SOURCES})

target_link_libraries(${test_target_name} PRIVATE jana2_static_lib VendoredCatch2)
if (${PROJECT_NAME} STREQUAL "jana2")
# This is an internal plugin
set(JANA_NAMESPACE "")
else()
# This is an external plugin
set(JANA_NAMESPACE "JANA::")
endif()

if (LINK_STATIC)
set(PLUGIN_JANA_LIB jana2_static_lib)
else()
set(PLUGIN_JANA_LIB jana2_shared_lib)
endif()

target_link_libraries(${test_target_name} PRIVATE "${JANA_NAMESPACE}${PLUGIN_JANA_LIB}" VendoredCatch2)

set_target_properties(${test_target_name} PROPERTIES
SKIP_BUILD_RPATH FALSE
Expand Down
38 changes: 17 additions & 21 deletions src/libraries/JANA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ elseif (${USE_ROOT})
endif()


# static library, always there
# Build static library
add_library(jana2_static_lib STATIC $<TARGET_OBJECTS:jana2> $<TARGET_OBJECTS:VendoredMD5>)
set_target_properties(jana2_static_lib PROPERTIES PREFIX "lib" OUTPUT_NAME "JANA")

Expand All @@ -94,28 +94,24 @@ elseif (${USE_ROOT})
endif()
install(TARGETS jana2_static_lib EXPORT jana2_targets DESTINATION lib)

# optionally build shared lib
if (BUILD_SHARED_LIBS)
message(STATUS "Building both shared and static libraries")
add_library(jana2_shared_lib SHARED $<TARGET_OBJECTS:jana2> $<TARGET_OBJECTS:VendoredMD5>)
set_target_properties(jana2_shared_lib PROPERTIES PREFIX "lib" OUTPUT_NAME "JANA")

target_include_directories(jana2_shared_lib PUBLIC $<INSTALL_INTERFACE:include>)
target_link_libraries(jana2_shared_lib PUBLIC ${CMAKE_DL_LIBS} Threads::Threads)
target_link_libraries(jana2_shared_lib PUBLIC VendoredTomlPlusPlus)

if (${USE_PODIO})
target_link_libraries(jana2_shared_lib PUBLIC podio::podio podio::podioRootIO ${ROOT_LIBRARIES})
elseif (${USE_ROOT})
target_link_libraries(jana2_shared_lib PUBLIC ${ROOT_LIBRARIES})
endif()

install(TARGETS jana2_shared_lib EXPORT jana2_targets DESTINATION lib)
set(INSTALL_RPATH_USE_LINK_PATH True)
else()
message(STATUS "Building static library only")

# Build shared library
add_library(jana2_shared_lib SHARED $<TARGET_OBJECTS:jana2> $<TARGET_OBJECTS:VendoredMD5>)
set_target_properties(jana2_shared_lib PROPERTIES PREFIX "lib" OUTPUT_NAME "JANA")

target_include_directories(jana2_shared_lib PUBLIC $<INSTALL_INTERFACE:include>)
target_link_libraries(jana2_shared_lib PUBLIC ${CMAKE_DL_LIBS} Threads::Threads)
target_link_libraries(jana2_shared_lib PUBLIC VendoredTomlPlusPlus)

if (${USE_PODIO})
target_link_libraries(jana2_shared_lib PUBLIC podio::podio podio::podioRootIO ${ROOT_LIBRARIES})
elseif (${USE_ROOT})
target_link_libraries(jana2_shared_lib PUBLIC ${ROOT_LIBRARIES})
endif()

install(TARGETS jana2_shared_lib EXPORT jana2_targets DESTINATION lib)
set(INSTALL_RPATH_USE_LINK_PATH True)


# Install "public" header files

Expand Down
2 changes: 1 addition & 1 deletion src/programs/jana/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
add_executable(jana jana.cc )

find_package(Threads REQUIRED)
target_link_libraries(jana jana2_static_lib Threads::Threads)
target_link_libraries(jana jana2_shared_lib)
target_link_options(jana PRIVATE -rdynamic)
install(TARGETS jana DESTINATION bin)

0 comments on commit 29a4760

Please sign in to comment.