Hello,

Background for this patch is https://bugs.gentoo.org/948878 where are user had 
installed a standalone error implementation for a different package leading to 
a build failure.

Decided to automagically link to it instead of forcing it as a dependency as 
its only packaged on Gentoo.

https://hacktivis.me/git/error-standalone/
https://repology.org/project/error-standalone/versions

Followed the approach that was used for argp and fts to my best ability.

Sending as an attachment due to my poor choice of email provider.
From 01aa458f363fd5c7f150daba21037e1a0f6bc05e Mon Sep 17 00:00:00 2001
From: Alfred Wingate <par...@protonmail.com>
Date: Mon, 27 Jan 2025 04:25:43 +0200
Subject: [PATCH] Link against standalone error library if available

Musl doesn't provide error. Howewer its possible that standalone
implementations are installed leading to build failure because error.h
was found. This change links against liberror if its found.

https://hacktivis.me/git/error-standalone/

Bug: https://bugs.gentoo.org/948878
Signed-off-by: Alfred Wingate <par...@protonmail.com>
---
 configure.ac           | 10 +++++++
 debuginfod/Makefile.am |  4 +--
 libdw/Makefile.am      |  2 +-
 src/Makefile.am        | 38 ++++++++++++-------------
 tests/Makefile.am      | 64 +++++++++++++++++++++---------------------
 5 files changed, 64 insertions(+), 54 deletions(-)

diff --git a/configure.ac b/configure.ac
index 1da2a12c..4c7b0300 100644
--- a/configure.ac
+++ b/configure.ac
@@ -649,6 +649,16 @@ case "$ac_cv_search_argp_parse" in
 esac
 AC_SUBST([argp_LDADD])
 
+saved_LIBS="$LIBS"
+AC_SEARCH_LIBS([error], [error])
+LIBS="$saved_LIBS"
+case "$ac_cv_search_error" in
+        no) ;;
+        -l*) error_LDADD="$ac_cv_search_error" ;;
+        *) error_LDADD= ;;
+esac
+AC_SUBST([error_LDADD])
+
 saved_LIBS="$LIBS"
 AC_SEARCH_LIBS([fts_close], [fts])
 LIBS="$saved_LIBS"
diff --git a/debuginfod/Makefile.am b/debuginfod/Makefile.am
index 4727e5fa..1fa46e38 100644
--- a/debuginfod/Makefile.am
+++ b/debuginfod/Makefile.am
@@ -70,10 +70,10 @@ bin_PROGRAMS += debuginfod-find
 endif
 
 debuginfod_SOURCES = debuginfod.cxx
-debuginfod_LDADD = $(libdw) $(libelf) $(libeu) $(libdebuginfod) $(argp_LDADD) $(fts_LIBS) $(libmicrohttpd_LIBS) $(sqlite3_LIBS) $(libarchive_LIBS) $(rpm_LIBS) $(jsonc_LIBS) $(libcurl_LIBS) $(lzma_LIBS) -lpthread -ldl
+debuginfod_LDADD = $(libdw) $(libelf) $(libeu) $(libdebuginfod) $(argp_LDADD) $(error_LDADD) $(fts_LIBS) $(libmicrohttpd_LIBS) $(sqlite3_LIBS) $(libarchive_LIBS) $(rpm_LIBS) $(jsonc_LIBS) $(libcurl_LIBS) $(lzma_LIBS) -lpthread -ldl
 
 debuginfod_find_SOURCES = debuginfod-find.c
-debuginfod_find_LDADD = $(libdw) $(libelf) $(libeu) $(libdebuginfod) $(argp_LDADD) $(fts_LIBS) $(jsonc_LIBS)
+debuginfod_find_LDADD = $(libdw) $(libelf) $(libeu) $(libdebuginfod) $(argp_LDADD) $(error_LDADD) $(fts_LIBS) $(jsonc_LIBS)
 
 if LIBDEBUGINFOD
 noinst_LIBRARIES = libdebuginfod.a
diff --git a/libdw/Makefile.am b/libdw/Makefile.am
index 8cbe0f32..d7bcaf15 100644
--- a/libdw/Makefile.am
+++ b/libdw/Makefile.am
@@ -109,7 +109,7 @@ libdw_so_LIBS = ../libebl/libebl_pic.a ../backends/libebl_backends_pic.a \
 		../libcpu/libcpu_pic.a libdw_pic.a ../libdwelf/libdwelf_pic.a \
 		../libdwfl/libdwfl_pic.a
 libdw_so_DEPS = ../lib/libeu.a ../libelf/libelf.so
-libdw_so_LDLIBS = $(libdw_so_DEPS) -ldl -lz $(argp_LDADD) $(fts_LIBS) $(obstack_LIBS) $(zip_LIBS) -pthread
+libdw_so_LDLIBS = $(libdw_so_DEPS) -ldl -lz $(argp_LDADD) $(error_LDADD) $(fts_LIBS) $(obstack_LIBS) $(zip_LIBS) -pthread
 libdw.so: $(srcdir)/libdw.map $(libdw_so_LIBS) $(libdw_so_DEPS)
 	$(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ \
 		-Wl,--soname,$@.$(VERSION),--enable-new-dtags \
diff --git a/src/Makefile.am b/src/Makefile.am
index ed245fc1..74d45739 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -89,28 +89,28 @@ ranlib_no_Wstack_usage = yes
 ar_no_Wstack_usage = yes
 unstrip_no_Wstack_usage = yes
 
-readelf_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(argp_LDADD)
-nm_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(argp_LDADD) $(obstack_LIBS) \
-	   $(demanglelib)
-size_LDADD = $(libelf) $(libeu) $(argp_LDADD)
-strip_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD)
-elflint_LDADD  = $(libebl) $(libdw) $(libelf) $(libeu) $(argp_LDADD)
-findtextrel_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD)
-addr2line_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD) $(demanglelib)
-elfcmp_LDADD = $(libebl) $(libdw) $(libelf) $(libeu) $(argp_LDADD)
-objdump_LDADD  = $(libasm) $(libebl) $(libdw) $(libelf) $(libeu) $(argp_LDADD)
-ranlib_LDADD = libar.a $(libelf) $(libeu) $(argp_LDADD) $(obstack_LIBS)
-strings_LDADD = $(libelf) $(libeu) $(argp_LDADD)
-ar_LDADD = libar.a $(libelf) $(libeu) $(argp_LDADD) $(obstack_LIBS)
-unstrip_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD)
-stack_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(demanglelib)
+readelf_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD)
+nm_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD) \
+	   $(obstack_LIBS) $(demanglelib)
+size_LDADD = $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD)
+strip_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(error_LDADD)
+elflint_LDADD  = $(libebl) $(libdw) $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD)
+findtextrel_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD)
+addr2line_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD) $(demanglelib)
+elfcmp_LDADD = $(libebl) $(libdw) $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD)
+objdump_LDADD  = $(libasm) $(libebl) $(libdw) $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD)
+ranlib_LDADD = libar.a $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD) $(obstack_LIBS)
+strings_LDADD = $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD)
+ar_LDADD = libar.a $(libelf) $(libeu) $(argp_LDADD) $(error_LDADD) $(obstack_LIBS)
+unstrip_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(error_LDADD)
+stack_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(error_LDADD) $(demanglelib)
 if ENABLE_STACKTRACE
-stacktrace_LDADD = $(libelf) $(libdw) $(libeu) $(argp_LDADD)
+stacktrace_LDADD = $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(error_LDADD)
 endif
-elfcompress_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD)
-elfclassify_LDADD = $(libelf) $(libdw) $(libeu) $(argp_LDADD)
+elfcompress_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(error_LDADD)
+elfclassify_LDADD = $(libelf) $(libdw) $(libeu) $(argp_LDADD) $(error_LDADD)
 srcfiles_SOURCES = srcfiles.cxx
-srcfiles_LDADD = $(libdw) $(libelf) $(libeu)  $(argp_LDADD) $(libarchive_LIBS) $(libdebuginfod)
+srcfiles_LDADD = $(libdw) $(libelf) $(libeu)  $(argp_LDADD) $(error_LDADD) $(libarchive_LIBS) $(libdebuginfod)
 
 installcheck-binPROGRAMS: $(bin_PROGRAMS)
 	bad=0; pid=$$$$; list="$(bin_PROGRAMS)"; for p in $$list; do \
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8f087798..7a2db653 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -733,7 +733,7 @@ installcheck-local:
 
 if BUILD_STATIC
 libdw = ../libdw/libdw.a -lz $(zip_LIBS) $(libelf) $(libebl) -ldl -lpthread
-libelf = ../libelf/libelf.a -lz $(zstd_LIBS) $(libeu)
+libelf = ../libelf/libelf.a -lz $(zstd_LIBS) $(libeu) $(error_LDADD)
 libasm = ../libasm/libasm.a
 else
 libdw = ../libdw/libdw.so
@@ -746,17 +746,17 @@ libeu = ../lib/libeu.a
 arextract_LDADD = $(libelf)
 arsymtest_LDADD = $(libelf)
 newfile_LDADD = $(libelf)
-saridx_LDADD = $(libeu) $(libelf)
+saridx_LDADD = $(libeu) $(libelf) $(error_LDADD)
 scnnames_LDADD = $(libelf)
-sectiondump_LDADD = $(libeu) $(libelf)
+sectiondump_LDADD = $(libeu) $(libelf) $(error_LDADD)
 showptable_LDADD = $(libelf)
 hash_LDADD = $(libelf)
 test_nlist_CFLAGS =-g -O0 $(EXTRA_NLIST_CFLAGS)
 test_nlist_LDADD = $(libelf)
 msg_tst_LDADD = $(libelf)
 newscn_LDADD = $(libelf)
-early_offscn_LDADD = $(libeu) $(libelf)
-ecp_LDADD = $(libeu) $(libelf)
+early_offscn_LDADD = $(libeu) $(libelf) $(error_LDADD)
+ecp_LDADD = $(libeu) $(libelf) $(error_LDADD)
 update1_LDADD = $(libelf)
 update2_LDADD = $(libelf)
 update3_LDADD = $(libdw) $(libelf)
@@ -771,12 +771,12 @@ get_files_define_file_LDADD = $(libdw) $(libelf)
 next_files_LDADD = $(libdw) $(libelf)
 get_aranges_LDADD = $(libdw) $(libelf)
 allfcts_LDADD = $(libdw) $(libelf)
-line2addr_LDADD = $(libeu) $(libdw) $(argp_LDADD)
-addrscopes_LDADD = $(libeu) $(libdw) $(argp_LDADD)
-funcscopes_LDADD = $(libeu) $(libdw) $(argp_LDADD)
-funcretval_LDADD = $(libeu) $(libdw) $(argp_LDADD)
-allregs_LDADD = $(libeu) $(libdw) $(argp_LDADD)
-find_prologues_LDADD = $(libeu) $(libdw) $(argp_LDADD)
+line2addr_LDADD = $(libeu) $(libdw) $(argp_LDADD) $(error_LDADD)
+addrscopes_LDADD = $(libeu) $(libdw) $(argp_LDADD) $(error_LDADD)
+funcscopes_LDADD = $(libeu) $(libdw) $(argp_LDADD) $(error_LDADD)
+funcretval_LDADD = $(libeu) $(libdw) $(argp_LDADD) $(error_LDADD)
+allregs_LDADD = $(libeu) $(libdw) $(argp_LDADD) $(error_LDADD)
+find_prologues_LDADD = $(libeu) $(libdw) $(argp_LDADD) $(error_LDADD)
 #show_ciefde_LDADD = ../libdwarf/libdwarf.so $(libelf)
 asm_tst1_LDADD = $(libasm) $(libebl) $(libelf) $(libdw)
 asm_tst2_LDADD = $(libasm) $(libebl) $(libelf) $(libdw)
@@ -787,20 +787,20 @@ asm_tst6_LDADD = $(libasm) $(libebl) $(libelf) $(libdw)
 asm_tst7_LDADD = $(libasm) $(libebl) $(libelf) $(libdw)
 asm_tst8_LDADD = $(libasm) $(libebl) $(libelf) $(libdw)
 asm_tst9_LDADD = $(libasm) $(libebl) $(libelf) $(libdw)
-dwflmodtest_LDADD = $(libeu) $(libdw) $(libebl) $(libelf) $(argp_LDADD)
-rdwrmmap_LDADD = $(libeu) $(libelf)
+dwflmodtest_LDADD = $(libeu) $(libdw) $(libebl) $(libelf) $(argp_LDADD) $(error_LDADD)
+rdwrmmap_LDADD = $(libeu) $(libelf) $(error_LDADD)
 dwfl_bug_addr_overflow_LDADD = $(libdw) $(libebl) $(libelf)
 arls_LDADD = $(libelf)
-dwfl_bug_fd_leak_LDADD = $(libeu) $(libdw) $(libebl) $(libelf)
+dwfl_bug_fd_leak_LDADD = $(libeu) $(libdw) $(libebl) $(libelf) $(error_LDADD)
 dwfl_bug_report_LDADD = $(libdw) $(libebl) $(libelf)
-dwfl_bug_getmodules_LDADD = $(libeu) $(libdw) $(libebl) $(libelf)
-dwfl_addr_sect_LDADD = $(libeu) $(libdw) $(libebl) $(libelf) $(argp_LDADD)
+dwfl_bug_getmodules_LDADD = $(libeu) $(libdw) $(libebl) $(libelf) $(error_LDADD)
+dwfl_addr_sect_LDADD = $(libeu) $(libdw) $(libebl) $(libelf) $(argp_LDADD) $(error_LDADD)
 dwfl_core_noncontig_LDADD = $(libdw) $(libelf)
 dwarf_getmacros_LDADD = $(libdw)
 dwarf_ranges_LDADD = $(libdw)
 dwarf_getstring_LDADD = $(libdw)
 addrcfi_LDADD = $(libdw) $(libebl) $(libelf) $(argp_LDADD)
-dwarfcfi_LDADD = $(libeu) $(libdw) $(libelf)
+dwarfcfi_LDADD = $(libeu) $(libdw) $(libelf) $(error_LDADD)
 test_flag_nobits_LDADD = $(libelf)
 rerequest_tag_LDADD = $(libdw)
 alldts_LDADD = $(libdw) $(libelf)
@@ -809,37 +809,37 @@ typeiter2_LDADD = $(libdw) $(libelf)
 low_high_pc_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 test_elf_cntl_gelf_getshdr_LDADD = $(libelf)
 dwflsyms_LDADD = $(libdw) $(libelf) $(argp_LDADD)
-dwfllines_LDADD = $(libeu) $(libdw) $(libelf) $(argp_LDADD)
-dwfl_report_elf_align_LDADD = $(libeu) $(libdw)
-dwfl_report_offline_memory_LDADD = $(libeu) $(libdw) $(libelf)
+dwfllines_LDADD = $(libeu) $(libdw) $(libelf) $(argp_LDADD) $(error_LDADD)
+dwfl_report_elf_align_LDADD = $(libeu) $(libdw) $(error_LDADD)
+dwfl_report_offline_memory_LDADD = $(libeu) $(libdw) $(libelf) $(error_LDADD)
 dwfl_report_segment_contiguous_LDADD = $(libdw) $(libebl) $(libelf)
-varlocs_LDADD = $(libeu) $(libdw) $(libelf) $(argp_LDADD)
-backtrace_LDADD = $(libeu) $(libdw) $(libelf) $(argp_LDADD)
+varlocs_LDADD = $(libeu) $(libdw) $(libelf) $(argp_LDADD) $(error_LDADD)
+backtrace_LDADD = $(libeu) $(libdw) $(libelf) $(argp_LDADD) $(error_LDADD)
 # backtrace-child-biarch also uses those *_CFLAGS and *_LDLAGS variables:
 backtrace_child_CFLAGS = $(fpie_CFLAGS)
 backtrace_child_LDFLAGS = -pie -pthread
 backtrace_child_biarch_SOURCES = backtrace-child.c
-backtrace_data_LDADD = $(libeu) $(libdw) $(libelf)
+backtrace_data_LDADD = $(libeu) $(libdw) $(libelf) $(error_LDADD)
 backtrace_dwarf_CFLAGS = -Wno-unused-parameter
-backtrace_dwarf_LDADD = $(libeu) $(libdw) $(libelf)
-debuglink_LDADD = $(libeu) $(libdw) $(libelf)
-debugaltlink_LDADD = $(libeu) $(libdw) $(libelf)
-buildid_LDADD = $(libeu) $(libdw) $(libelf)
+backtrace_dwarf_LDADD = $(libeu) $(libdw) $(libelf) $(error_LDADD)
+debuglink_LDADD = $(libeu) $(libdw) $(libelf) $(error_LDADD)
+debugaltlink_LDADD = $(libeu) $(libdw) $(libelf) $(error_LDADD)
+buildid_LDADD = $(libeu) $(libdw) $(libelf) $(error_LDADD)
 deleted_LDADD = ./deleted-lib.so
 deleted_lib_so_LDFLAGS = -shared
 deleted_lib_so_CFLAGS = $(fpic_CFLAGS) -fasynchronous-unwind-tables
 aggregate_size_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 peel_type_LDADD = $(libdw) $(libelf) $(argp_LDADD)
-vdsosyms_LDADD = $(libeu) $(libdw) $(libelf)
-getsrc_die_LDADD = $(libeu) $(libdw) $(libelf)
+vdsosyms_LDADD = $(libeu) $(libdw) $(libelf) $(error_LDADD)
+getsrc_die_LDADD = $(libeu) $(libdw) $(libelf) $(error_LDADD)
 strptr_LDADD = $(libelf)
 newdata_LDADD = $(libelf)
 newzdata_LDADD = $(libelf)
 elfstrtab_LDADD = $(libelf)
-dwfl_proc_attach_LDADD = $(libeu) $(libdw)
+dwfl_proc_attach_LDADD = $(libeu) $(libdw) $(error_LDADD)
 dwfl_proc_attach_LDFLAGS = -pthread -rdynamic $(AM_LDFLAGS)
 elfshphehdr_LDADD =$(libelf)
-elfstrmerge_LDADD = $(libeu) $(libdw) $(libelf)
+elfstrmerge_LDADD = $(libeu) $(libdw) $(libelf) $(error_LDADD)
 dwelfgnucompressed_LDADD = $(libelf) $(libdw)
 elfgetchdr_LDADD = $(libelf) $(libdw)
 elfgetzdata_LDADD = $(libelf)
@@ -855,7 +855,7 @@ get_units_split_LDADD = $(libdw)
 attr_integrate_skel_LDADD = $(libdw)
 all_dwarf_ranges_LDADD = $(libdw)
 unit_info_LDADD = $(libdw)
-next_cfi_LDADD = $(libeu) $(libelf) $(libdw)
+next_cfi_LDADD = $(libeu) $(libelf) $(libdw) $(error_LDADD)
 elfcopy_LDADD = $(libelf)
 addsections_LDADD = $(libelf)
 debuginfod_build_id_find_LDADD = $(libelf) $(libdw)
-- 
2.48.1

Reply via email to