@@ -270,25 +270,42 @@ if(BUILD_SHARED)
270270endif ()
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
276276if (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 ()
293310endif ()
294311
0 commit comments