Skip to content

Commit 71555cb

Browse files
committed
fixup! maint: Modularize libmamba
1 parent 6e6ea04 commit 71555cb

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

libmamba/CMakeLists.txt

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -270,25 +270,42 @@ if(BUILD_SHARED)
270270
endif()
271271

272272
# Add DT_NEEDED entries to libmamba-common-dyn after all components are built This ensures the
273-
# dynamic linker loads solver and network libraries when common is loaded We use target_link_options
274-
# with generator expressions to add the libraries to the link line without creating CMake dependency
275-
# edges that would trigger cycle detection
273+
# dynamic linker loads solver and network libraries when common is loaded We use a post-build step
274+
# with patchelf (on Linux) to add DT_NEEDED entries after all libraries are built, avoiding the need
275+
# for the libraries to exist at link time and avoiding circular dependency issues
276276
if(BUILD_SHARED)
277277
if(
278278
TARGET libmamba-common-dyn
279279
AND TARGET libmamba-solver-dyn
280280
AND TARGET libmamba-network-dyn
281+
AND UNIX
282+
AND NOT APPLE
281283
)
282-
# Use --no-as-needed to ensure libraries are added to DT_NEEDED even if symbols aren't
283-
# directly referenced at link time (since we're using --allow-shlib-undefined)
284-
target_link_options(
285-
libmamba-common-dyn
286-
PRIVATE
287-
-Wl,--no-as-needed
288-
$<TARGET_FILE:mamba::libmamba-solver-dyn>
289-
$<TARGET_FILE:mamba::libmamba-network-dyn>
290-
-Wl,--as-needed
291-
)
284+
# Find patchelf utility
285+
find_program(PATCHELF_EXECUTABLE patchelf)
286+
if(PATCHELF_EXECUTABLE)
287+
# Add post-build step to add DT_NEEDED entries using patchelf This adds the solver and
288+
# network libraries to common's DT_NEEDED list Note: This runs after common is built,
289+
# but solver/network might not be built yet. We'll create a separate custom target that
290+
# runs after all libraries are built.
291+
add_custom_target(
292+
libmamba-common-dyn-add-dt-needed
293+
COMMAND
294+
${PATCHELF_EXECUTABLE} --add-needed
295+
$<TARGET_SONAME_FILE_NAME:mamba::libmamba-solver-dyn>
296+
$<TARGET_FILE:mamba::libmamba-common-dyn>
297+
COMMAND
298+
${PATCHELF_EXECUTABLE} --add-needed
299+
$<TARGET_SONAME_FILE_NAME:mamba::libmamba-network-dyn>
300+
$<TARGET_FILE:mamba::libmamba-common-dyn>
301+
DEPENDS libmamba-common-dyn libmamba-solver-dyn libmamba-network-dyn
302+
COMMENT "Adding DT_NEEDED entries to libmamba-common.so"
303+
)
304+
# Make the aggregated target depend on this custom target to ensure it runs
305+
if(TARGET libmamba-dyn)
306+
add_dependencies(libmamba-dyn libmamba-common-dyn-add-dt-needed)
307+
endif()
308+
endif()
292309
endif()
293310
endif()
294311

libmamba/archive/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ macro(libmamba_archive_create_target target_name linkage output_name)
6868
target_link_options(${target_name} PRIVATE -Wl,-undefined,dynamic_lookup)
6969
elseif(UNIX)
7070
target_link_options(${target_name} PRIVATE -Wl,--allow-shlib-undefined)
71+
elseif(WIN32)
72+
# On Windows, use /FORCE:UNRESOLVED to allow unresolved symbols. The symbols will be
73+
# resolved at runtime when the DLLs are loaded. The aggregated libmamba target will
74+
# ensure all DLLs are available together.
75+
target_link_options(${target_name} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/FORCE:UNRESOLVED>)
7176
endif()
7277
endif()
7378

libmamba/common/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ macro(libmamba_common_create_target target_name linkage output_name)
279279
# resolved by the aggregated target linking all components together. Note: We can't use
280280
# target_link_libraries here because it would create a circular dependency. Instead,
281281
# we'll add these as linker options after the components are built.
282+
elseif(WIN32)
283+
# On Windows, use /FORCE:UNRESOLVED to allow unresolved symbols. The symbols will be
284+
# resolved at runtime when the DLLs are loaded. The aggregated libmamba target will
285+
# ensure all DLLs are available together. Note: We can't use delay loading (/DELAYLOAD)
286+
# because the DLLs don't exist yet at link time. /FORCE:UNRESOLVED allows the DLL to be
287+
# created with unresolved symbols, which will be resolved when the DLLs are loaded
288+
# together.
289+
target_link_options(${target_name} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/FORCE:UNRESOLVED>)
282290
endif()
283291
endif()
284292

0 commit comments

Comments
 (0)