From 15f9355a56ace2ef2018cb97cd0bacb77b734b22 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vivek=20Das=C2=A0Mohapatra?= <vivek@collabora.co.uk>
Date: Mon, 15 May 2017 18:27:39 +0100
Subject: [PATCH] Modify generated file dependencies to allow optional
 dependencies

We now reserve the shim/lib<FOO>.so.c.<WHATEVER> pattern for the
dependencies of the generated .so.c file.

The shim/lib<FOO>.so.<WHATEVER> pattern is now used for generated
intermediates.

This allows two useful things: We don't occasionally get a second
rebuild of the generated .so.c file due to timer resolution problems
and we can depend on extra .so.c.* files if they exist and ignore
them otherwise: we will need the latter as some libraries (libGL)
need bespoke dlopen/dlmopen wrapper logic and others can just live
with the default implementation.
---
 .gitignore          |  2 ++
 Makefile.am         | 11 +++++++----
 generate-stublib.sh | 14 ++++++++------
 3 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore
index 54de19e3e..a594f86d0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -30,3 +30,5 @@ elf-dump
 print-libstubs
 TAGS
 tags
+shim/lib*.so.map
+shim/lib*.so.symbols
diff --git a/Makefile.am b/Makefile.am
index dbbfe01e6..1e5688c1b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -41,17 +41,18 @@ libcapsule_la_LDFLAGS  = -lelf
 
 libz_la_SOURCES        = shim/libz.so.c
 libz_la_LDFLAGS        = -lcapsule -version-info 1:1:0 \
-                         -Wl,--version-script=shim/libz.so.c.map
+                         -Wl,--version-script=shim/libz.so.map
 
 libGL_la_SOURCES       = shim/libGL.so.c
 libGL_la_LDFLAGS       = -lcapsule -version-info 1:1:0
 
 shims      = z GL
 shim_srcs  = $(foreach l,$(shims),shim/lib$l.so.c)
-shim_files = $(foreach y,map symbols,$(foreach x,$(shim_srcs),$x.$y))
+shim_base  = $(basename $(shim_srcs))
+shim_files = $(foreach y,map symbols,$(foreach x,$(shim_base),$x.$y))
 
 GENSTUB_V1 = 
-GENSTUB_V0 = @echo "  GENSTUB " $@.excluded $@.shared $@;
+GENSTUB_V0 = @echo "  GENSTUB " $(subst $(word 1, $(basename $(filter shim/lib%,$^))),,$(filter shim/lib%,$^)) : $@;
 GENSTUB    = $(GENSTUB_V$(V))
 
 CLEANFILES 			   = $(shim_files) $(shim_srcs)
@@ -59,7 +60,9 @@ BUILT_SOURCES 		   = $(shim_srcs)
 
 include disabled.mk
 
-shim/lib%.so.c: generate-stublib.sh print-libstubs capsule-shim.h shim/lib%.so.c.excluded shim/lib%.so.c.shared
+# explicitly depend on the generators and .excluded and .shared metafiles
+# depend on the .dlopen file IFF it exists
+shim/lib%.so.c: generate-stublib.sh print-libstubs capsule-shim.h shim/lib%.so.c.*
 	$(GENSTUB)$(top_srcdir)/$< $$(basename $@ .c) $@.excluded $@.shared $@
 
 debug:
diff --git a/generate-stublib.sh b/generate-stublib.sh
index 490fb6686..e04637ec6 100755
--- a/generate-stublib.sh
+++ b/generate-stublib.sh
@@ -28,8 +28,10 @@ proxied_dso=$1;    shift;
 proxy_excluded=$1; shift;
 proxy_extra=$1;    shift;
 proxy_src=$1;      shift;
-echo -n > $proxy_src.symbols;
-echo -n > $proxy_src.map;
+symbol_file=${proxy_src%.c}.symbols;
+map_file=${proxy_src%.c}.map;
+echo -n > $symbol_file;
+echo -n > $map_file;
 
 exec >& $proxy_src;
 
@@ -38,7 +40,7 @@ cat $top/capsule-shim.h;
 for pt in $proxied_dso $(cat $proxy_extra);
 do
     $top/print-libstubs $pt;
-done > $proxy_src.symbols;
+done > $symbol_file;
 
 while read symbol version dependency;
 do
@@ -52,7 +54,7 @@ do
             echo "UNVERSIONED_STUB( $symbol );";
             ;;
     esac;
-done < $proxy_src.symbols;
+done < $symbol_file;
 
 cat - <<EOF
 static Lmid_t symbol_ns;
@@ -97,7 +99,7 @@ static void __attribute__ ((constructor)) _capsule_init (void)
        {
 EOF
 
-while read sym x; do echo "         { \"$sym\" },"; done < $proxy_src.symbols;
+while read sym x; do echo "         { \"$sym\" },"; done < $symbol_file;
 
 cat - <<EOF
          { NULL }
@@ -133,7 +135,7 @@ cat - <<EOF
 }
 EOF
 
-exec >& $proxy_src.map;
+exec >& $map_file;
 
 for node in ${!NODE[@]};
 do
-- 
GitLab