Description: Collect and inject standard options for gcc backend
 The gcc backend of GHDL does not set any of the standard options for machine
 architecture, ABI, etc. when calling the backend compiler unlike other GCC
 frontends. This will break on some architectures, when object files are
 incompatible with the setup of the linked executable and possibly cause other
 subtle problems even if linking succeeds.
 .
 This patch adds some kludgy code to allow presetting the default options at
 compile time and code to add them to the command line for the backend call.
 The other half is code in debian/rules to collect the default options from the
 C compiler and pass them to the configure script.
Author: Andreas Bombe <aeb@debian.org>
Forwarded: no
Last-Update: 2024-08-28
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- a/configure
+++ b/configure
@@ -71,6 +71,7 @@
 with_sundials sundials_incflags sundials_ldflags
 COMPILER_GCC COMPILER_DEBUG COMPILER_MCODE COMPILER_LLVM POST_PROCESSOR
 INSTALL_PREFIX LIBDIR_SUFFIX LIBGHDLDIR_SUFFIX INCDIR_SUFFIX LIBNATIVEDIR_SUFFIX
+GCC_DEFAULT_OPTIONS
 LLVM_LDFLAGS
 "
 
--- a/default_paths.ads.in
+++ b/default_paths.ads.in
@@ -41,6 +41,8 @@
    Post_Processor : constant String :=
      "@POST_PROCESSOR@";
 
+   Default_Gcc_Options : constant String := "@GCC_DEFAULT_OPTIONS@";
+
    Shared_Library_Extension : constant String := "@SOEXT@";
    Executable_Extension : constant String := "@EXEEXT@";
    Default_Pie : constant Boolean := "@default_pic@" = String'("true");
--- a/scripts/gcc/Make-lang.in.in
+++ b/scripts/gcc/Make-lang.in.in
@@ -120,6 +120,8 @@
 	echo "   Default_Pie : constant Boolean := False;" >> tmp-dpaths.ads
 	echo "   Backend_Version : constant String :=" >> tmp-dpaths.ads
 	echo "     \"@backend_version@\";" >> tmp-dpaths.ads
+	echo "   Default_Gcc_Options : constant String :=" >> tmp-dpaths.ads
+	echo "     \"@GCC_DEFAULT_OPTIONS@\";" >> tmp-dpaths.ads
 	echo "end Default_Paths;" >> tmp-dpaths.ads
 	$(srcdir)/../move-if-change tmp-dpaths.ads $@
 
--- a/src/ghdldrv/ghdldrv.adb
+++ b/src/ghdldrv/ghdldrv.adb
@@ -574,6 +574,33 @@
 
    procedure Setup_Compiler (Cmd : in out Command_Comp'Class; Load : Boolean)
    is
+      procedure Add_Gcc_Default_Arguments is
+         Opts : constant String := Default_Paths.Default_Gcc_Options;
+         Start : Positive := Opts'First;
+         Stop : Positive;
+      begin
+         while Start <= Opts'Last loop
+            if Opts (Start) /= ''' then
+               Start := Start + 1;
+            else
+               Stop := Start + 1;
+
+               while Stop <= Opts'Last loop
+                  exit when Opts (Stop) = ''';
+                  Stop := Stop + 1;
+               end loop;
+
+               if Stop > Opts'Last then
+                  return;
+               end if;
+
+               Add_Argument (Cmd.Compiler_Args,
+                             new String'(Opts (Start + 1 .. Stop - 1)));
+               Start := Stop + 1;
+            end if;
+         end loop;
+      end Add_Gcc_Default_Arguments;
+
       use Libraries;
    begin
       Set_Tools_Name (Cmd);
@@ -581,6 +608,9 @@
          raise Option_Error;
       end if;
       Locate_Tools (Cmd);
+      if Backend = Backend_Gcc then
+         Add_Gcc_Default_Arguments;
+      end if;
       for I in 2 .. Get_Nbr_Paths loop
          Add_Argument (Cmd.Compiler_Args,
                        new String'("-P" & Image (Get_Path (I))));
