diff mbox series

[05/12] libtracecmd: Update ld.so.conf.d/trace.conf if needed

Message ID 20201216044213.730082526@goodmis.org (mailing list archive)
State Accepted
Commit 68532c8b12992600209d63da633c69854e8fc178
Headers show
Series trace-cmd: Updates to the Makefile | expand

Commit Message

Steven Rostedt Dec. 16, 2020, 4:41 a.m. UTC
From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

If DESTDIR is not defined (building for the current machine) check if after
installing libtracecmd, if it is in the ld.so path by building a test
program and running it, and making sure that the dynamic linker works, and
if not, then update /etc/ld.so.conf.d/trace.conf to include the path to
libtracecmd.so.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 Makefile               |  6 +++++-
 lib/trace-cmd/Makefile | 26 ++++++++++++++++++++++++++
 lib/trace-cmd/test.c   |  7 +++++++
 3 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 lib/trace-cmd/test.c
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index e956a2e6e8b6..101820733862 100644
--- a/Makefile
+++ b/Makefile
@@ -37,6 +37,10 @@  endef
 $(call allow-override,CC,$(CROSS_COMPILE)gcc)
 $(call allow-override,AR,$(CROSS_COMPILE)ar)
 $(call allow-override,PKG_CONFIG,pkg-config)
+$(call allow-override,LD_SO_CONF_PATH,/etc/ld.so.conf.d/)
+$(call allow-override,LDCONFIG,ldconfig)
+
+export LD_SO_CONF_PATH LDCONFIG
 
 EXT = -std=gnu99
 INSTALL = install
@@ -76,7 +80,7 @@  etcdir ?= /etc
 etcdir_SQ = '$(subst ','\'',$(etcdir))'
 
 export man_dir man_dir_SQ html_install html_install_SQ INSTALL
-export img_install img_install_SQ libdir_SQ includedir_SQ
+export img_install img_install_SQ libdir libdir_SQ includedir_SQ
 export DESTDIR DESTDIR_SQ
 
 ifeq ($(prefix),$(HOME))
diff --git a/lib/trace-cmd/Makefile b/lib/trace-cmd/Makefile
index 75e4fea4e3e0..a26b989a2c82 100644
--- a/lib/trace-cmd/Makefile
+++ b/lib/trace-cmd/Makefile
@@ -3,6 +3,7 @@ 
 include $(src)/scripts/utils.mk
 
 bdir:=$(obj)/lib/trace-cmd
+ldir:=$(src)/lib/trace-cmd
 
 DEFAULT_TARGET = $(LIBTRACECMD_STATIC)
 
@@ -59,11 +60,36 @@  $(DEPS): $(bdir)/.%.d: %.c
 
 $(OBJS): $(bdir)/%.o : $(bdir)/.%.d
 
+ifeq ("$(DESTDIR)", "")
+# If DESTDIR is not defined, then test if after installing the library
+# and running ldconfig, if the library is visible by ld.so.
+# If not, add the path to /etc/ld.so.conf.d/trace.conf and run ldconfig again.
+define install_ld_config
+	$(LDCONFIG); \
+	if ! grep "^$(libdir)$$" $(LD_SO_CONF_PATH)/* &> /dev/null ; then \
+		$(CC) -o $(bdir)/test $(ldir)/test.c -I $(includedir_SQ) \
+			-L $(libdir_SQ) -ltracecmd > /dev/null; \
+		if ! $(bdir)/test &> /dev/null; then \
+			$(call print_install,trace.conf,$(LD_SO_CONF_PATH)) \
+			echo $(libdir_SQ) >> $(LD_SO_CONF_PATH)/trace.conf; \
+			$(LDCONFIG); \
+		fi; \
+		$(RM) $(bdir)/test; \
+	fi
+endef
+else
+# If installing to a location for another machine or package, do not bother
+# with running ldconfig.
+define install_ld_config
+endef
+endif # DESTDIR = ""
+
 install_libs:
 	$(Q)$(call do_install,$(LIBTRACECMD_SHARED),$(libdir_SQ))
 	cp -fpR $(LIBTRACECMD_SHARED_VERSION) $(DESTDIR)$(libdir_SQ)
 	cp -fpR $(LIBTRACECMD_SHARED_SO) $(DESTDIR)$(libdir_SQ)
 	$(Q)$(call do_install,$(src)/include/trace-cmd/trace-cmd.h,$(includedir_SQ)/trace-cmd)
+	$(Q)$(call install_ld_config)
 
 dep_includes := $(wildcard $(DEPS))
 
diff --git a/lib/trace-cmd/test.c b/lib/trace-cmd/test.c
new file mode 100644
index 000000000000..5622d79c86c5
--- /dev/null
+++ b/lib/trace-cmd/test.c
@@ -0,0 +1,7 @@ 
+#include <trace-cmd/trace-cmd.h>
+
+int main()
+{
+	tracecmd_open_head("trace.dat");
+	return 0;
+}