Description: let projects allow build flags and dynamic linking.
 Allow the user to set ADAFLAGS (and LDFLAGS for dynamic links).
 .
 Stop testing if LIBDIR and OBJDIR exist.
 The Makefile does not set them.
 .
 If SOVERSION is not empty, build a shared library.
 This should not affect the default behaviour,
 since the Makefile does not set this variable.
 .
 Use different object and library directories for shared and static
 builds, so the patch modifies the directory names.
 As far as I know, in normal usage, these directories are only used
 internally by gprbuild/install/clean.
 .
 Forwarded by private mail to report@adacore.com on 2018/6/11.
Author: Nicolas Boulenguez <nicolas@debian.org>
Forwarded: yes

--- a/lib/gnat/aunit.gpr
+++ b/lib/gnat/aunit.gpr
@@ -14,14 +14,27 @@
       "../../include/aunit/framework/" & AUnit_Shared.Calend,
       "../../include/aunit/framework/" & AUnit_Shared.Memory);
 
-   for Library_Dir use AUnit_Shared.Library_Dir;
-
-   Obj_Dir := external ("OBJDIR", "../aunit-obj/"
-                        & AUnit_Shared.Target & "-" & AUnit_Shared.Runtime);
-   for Object_Dir use Obj_Dir;
+   for Library_Dir use "../aunit/"
+     & Aunit_Shared.Target & "-"
+     & Aunit_Shared.Runtime & "-"
+     & Aunit_Shared.Library_Kind;
+
+   for Object_Dir use "../aunit-obj/"
+     & AUnit_Shared.Target & "-"
+     & AUnit_Shared.Runtime & "-"
+     & Aunit_Shared.Library_Kind;
 
    for Library_Name use "aunit";
-   for Library_Kind use "static";
+   for Library_Kind use AUnit_Shared.Library_Kind;
+   case AUnit_Shared.Library_Kind is
+      when "dynamic" =>
+         for Library_Version use "lib" & Project'Library_Name & ".so."
+           & Aunit_Shared.Soversion;
+         --  Put options like --as-needed before the libraries.
+         for Leading_Library_Options use Aunit_Shared.Ldflags;
+      when "static" =>
+         null;
+   end case;
 
    --------------
    -- Compiler --
@@ -38,6 +51,9 @@
             for Default_Switches ("ada") use
               ("-O2", "-gnatp", "-gnatn", "-gnatwa.X");
       end case;
+      --  Allow user flags to override default flags.
+      for Default_Switches ("ada") use Compiler'Default_Switches ("ada")
+        & Aunit_Shared.Adaflags;
 
       for Switches ("aunit.adb") use
         Compiler'Default_Switches ("ada") & ("-fno-strict-aliasing");
--- a/lib/gnat/aunit_shared.gpr
+++ b/lib/gnat/aunit_shared.gpr
@@ -13,7 +13,20 @@
 
    Runtime : Runtime_Type := external ("RUNTIME", "full");
 
-   Library_Dir := external ("LIBDIR", "../aunit/" & Target & "-" & Runtime);
+   --  The default is empty, and requires a static build.
+   --  A non-empty soversion requires a shared library.
+   Soversion := External ("SOVERSION", "");
+   type A_Library_Kind is ("dynamic", "static");
+   Library_Kind : A_Library_Kind := "static";
+   case Soversion is
+      when "" =>
+         null;
+      when others =>
+         Library_Kind := "dynamic";
+   end case;
+
+   Adaflags  := External_As_List ("ADAFLAGS", " ");
+   Ldflags   := External_As_List ("LDFLAGS",  " ");
 
    for Source_Dirs use ();
 
