From ba1a917e2eb4b26e021a1d90044e89d06f3478f8 Mon Sep 17 00:00:00 2001
From: Iain Sandoe <iain@codesourcery.com>
Date: Thu, 20 Dec 2018 09:00:38 +0000
Subject: [PATCH] Driver: Provide a spec to insert rpaths for compiler lib
 dirs.

This provides a spec to insert "-rpath DDD" for each DDD corresponding
to a compiler startfile directory.  This allows a target to use @rpath
as the install path for libraries, and have the compiler provide the
necessary rpath to handle this.

gcc/ChangeLog:

	* config/darwin.h (LINK_COMMAND_SPEC_A): Add handling for rpath.
	(DARWIN_RPATH_SPEC): New.
	* gcc.c (do_spec_1): Provide '%P' as a spec to insert rpaths for
	each compiler startfile path.

Signed-off-by: Kirill A. Korinsky <kirill@korins.ky>
---
 gcc/config/darwin.h | 16 ++++++++++++++--
 gcc/gcc.c           | 18 ++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git gcc/config/darwin.h gcc/config/darwin.h
index 5649df2885b..37396ef7ad0 100644
--- gcc/config/darwin.h
+++ gcc/config/darwin.h
@@ -381,6 +381,7 @@ extern GTY(()) int darwin_ms_struct;
     DARWIN_NOPIE_SPEC \
     DARWIN_RDYNAMIC \
     DARWIN_NOCOMPACT_UNWIND \
+    "%{!r:%{!nostdlib:%{!rpath:%(darwin_rpaths)}}}" \
     "}}}}}}} %<pie %<no-pie %<rdynamic "
 
 /* Spec that controls whether the debug linker is run automatically for
@@ -521,7 +522,7 @@ extern GTY(()) int darwin_ms_struct;
                                %{!object:%{preload:-lgcrt0.o}		    \
                                  %{!preload:-lgcrt1.o                       \
                                  %:version-compare(>= 10.8 mmacosx-version-min= -no_new_main) \
-                                 %(darwin_crt2)}}}}    \
+				 %(darwin_crt2)}}}}                         \
                 %{!pg:%{static:-lcrt0.o}				    \
                       %{!static:%{object:-lcrt0.o}			    \
                                 %{!object:%{preload:-lcrt0.o}		    \
@@ -531,6 +532,7 @@ extern GTY(()) int darwin_ms_struct;
 
 /* We want a destructor last in the list.  */
 #define TM_DESTRUCTOR "%{fgnu-tm: -lcrttme.o}"
+
 #define ENDFILE_SPEC TM_DESTRUCTOR
 
 #define DARWIN_EXTRA_SPECS						\
@@ -538,7 +540,8 @@ extern GTY(()) int darwin_ms_struct;
   { "darwin_crt2", DARWIN_CRT2_SPEC },					\
   { "darwin_crt3", DARWIN_CRT3_SPEC },					\
   { "darwin_dylib1", DARWIN_DYLIB1_SPEC },				\
-  { "darwin_bundle1", DARWIN_BUNDLE1_SPEC },
+  { "darwin_bundle1", DARWIN_BUNDLE1_SPEC },				\
+  { "darwin_rpaths", DARWIN_RPATH_SPEC },
 
 #define DARWIN_CRT1_SPEC						\
   "%:version-compare(!> 10.5 mmacosx-version-min= -lcrt1.o)		\
@@ -564,6 +567,15 @@ extern GTY(()) int darwin_ms_struct;
 "%{!static:%:version-compare(< 10.6 mmacosx-version-min= -lbundle1.o)	\
 	   %{fgnu-tm: -lcrttms.o}}"
 
+/* FIXME: it would be great to have a version-compare that accepts multiple
+   arguments.  */
+#define DARWIN_RPATH_SPEC \
+  "%:version-compare(>= 10.5 mmacosx-version-min= -rpath)		\
+   %:version-compare(>= 10.5 mmacosx-version-min= @loader_path/.)	\
+   %:version-compare(>= 10.5 mmacosx-version-min= -rpath)		\
+   %:version-compare(>= 10.5 mmacosx-version-min= @loader_path/../lib)	\
+   %P "
+
 #ifdef HAVE_AS_MMACOSX_VERSION_MIN_OPTION
 /* Emit macosx version (but only major).  */
 #define ASM_MMACOSX_VERSION_MIN_SPEC \
diff --git gcc/gcc.c gcc/gcc.c
index 6517d9935dc..b6d08151e4a 100644
--- gcc/gcc.c
+++ gcc/gcc.c
@@ -5416,6 +5416,24 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
 	    }
 	    break;
 
+	  case 'P':
+	    {
+	      struct spec_path_info info;
+
+	      info.option = "-rpath";
+	      info.append_len = 0;
+#ifdef RELATIVE_PREFIX_NOT_LINKDIR
+	      /* See comment above.  */
+	      info.omit_relative = true;
+#else
+	      info.omit_relative = false;
+#endif
+	      info.separate_options = true;
+
+	      for_each_path (&startfile_prefixes, true, 0, spec_path, &info);
+	    }
+	    break;
+
 	  case 'e':
 	    /* %efoo means report an error with `foo' as error message
 	       and don't execute any more commands for this file.  */
-- 
2.42.1

