trace-cmd: Update libtracecmd.pc if prefix is different

If a build is done with different DESTDIR and the prefix is changed, the
the libtracecmd.pc is not updated with the new prefix, and the one used may
not work with the installation being performed.

Add a "build_prefix" dependency, that creates a file "build_prefix" that has
the last prefix used to build the library. And if a new prefix is used, then
that file gets updated and so does libtracecmd.pc.

Link: https://lore.kernel.org/linux-trace-devel/20201216044214.856860622@goodmis.org

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
diff --git a/.gitignore b/.gitignore
index 1e8c1d4..9ce25c8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -24,3 +24,4 @@
 trace_python_dir
 tracecmd_plugin_dir
 libtracecmd.pc
+build_prefix
diff --git a/Makefile b/Makefile
index ddceab3..396a679 100644
--- a/Makefile
+++ b/Makefile
@@ -343,7 +343,12 @@
 # Build with "BUILD_TYPE=Release" to remove cmake debug info
 BUILD_TYPE ?= RelWithDebInfo
 
-$(PKG_CONFIG_FILE) : ${PKG_CONFIG_SOURCE_FILE}.template
+BUILD_PREFIX := $(BUILD_OUTPUT)/build_prefix
+
+$(BUILD_PREFIX): force
+	$(Q)$(call build_prefix,$(prefix))
+
+$(PKG_CONFIG_FILE) : ${PKG_CONFIG_SOURCE_FILE}.template $(BUILD_PREFIX)
 	$(Q) $(call do_make_pkgconfig_file,$(prefix))
 
 $(kshark-dir)/build/Makefile: $(kshark-dir)/CMakeLists.txt
diff --git a/scripts/utils.mk b/scripts/utils.mk
index c8ef44b..c068887 100644
--- a/scripts/utils.mk
+++ b/scripts/utils.mk
@@ -121,6 +121,16 @@
 	fi);
 endef
 
+define build_prefix
+	(echo $1 > $@.tmp;	\
+	if [ -r $@ ] && cmp -s $@ $@.tmp; then				\
+		rm -f $@.tmp;						\
+	else								\
+		$(print_update)						\
+		mv -f $@.tmp $@;					\
+	fi);
+endef
+
 define do_install
 	$(print_install)				\
 	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\