[PATCH 1/3] Add early_flags to default_target_compile

Tom Tromey <[email protected]> Fri, 19 Jun 2020 07:52:42 -0600
Newsgroups gmane.comp.sysutils.dejagnu.general
Message-ID <[email protected]>
This adds early_flags support to default_target_compile.  This
originated in this gdb patch:

    commit 6ebea266fd0a7a56c90db3ab6237ff9f6c919747
    Author: Doug Evans <[email protected]>
    Date:   Fri Jul 24 15:24:37 2015 -0700

	Workaround debian change to default value of --as-needed.

	gdb/testsuite/ChangeLog:

		* lib/future.exp (gdb_default_target_compile): New option
		"early_flags".
		* lib/gdb.exp (gdb_compile): Undo debian's change in default of
		--as-needed.

This patch also pulls in the "linker_opts_order" code, though nothing
uses it yet.  A use will come in a subsequent patch.
---
 ChangeLog        |  6 ++++++
 doc/dejagnu.texi |  5 ++++-
 lib/target.exp   | 33 +++++++++++++++++++++++++++++----
 3 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 69a80ef..216b4ec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-06-19  Tom Tromey  <[email protected]>
+
+	* lib/target.exp (default_target_compile): Add early_flags.
+	* doc/dejagnu.texi (target_compile procedure): Document
+	early_flags.
+
 2020-06-02  Jacob Bachmeyer  <[email protected]>
 
 	PR 41647
diff --git a/doc/dejagnu.texi b/doc/dejagnu.texi
index bb386e8..fd7f16f 100644
--- a/doc/dejagnu.texi
+++ b/doc/dejagnu.texi
@@ -10,7 +10,7 @@
 
 @copying
 @c man begin COPYRIGHT
-Copyright @copyright{} 1992-2019 Free Software Foundation, Inc.
+Copyright @copyright{} 1992-2020 Free Software Foundation, Inc.
 
 Permission is granted to copy, distribute and/or modify this document
 under the terms of the GNU Free Documentation License, Version 1.3 or
@@ -4595,6 +4595,9 @@ this option is given multiple times, only the last use is significant.
 @item compiler=@var{command}
 Override the defaults and use @var{command} as the compiler.  If
 this option is given multiple times, only the last use is significant.
+@item early_flags=@var{flags}
+Prepend @var{flags} to the set of arguments to be passed to the compiler.
+Multiple uses of this option specify additional arguments.
 @item additional_flags=@var{flags}
 Add @var{flags} to the set of arguments to be passed to the compiler.
 Multiple uses of this option specify additional arguments.
diff --git a/lib/target.exp b/lib/target.exp
index d240007..c98fbd0 100644
--- a/lib/target.exp
+++ b/lib/target.exp
@@ -311,6 +311,11 @@ proc default_target_compile {source destfile type options} {
 	error "Must supply an output filename for the compile to default_target_compile"
     }
 
+    set early_flags ""
+    # linker_opts_order is one of "sources-then-flags", "flags-then-sources".
+    # The order matters for things like -Wl,--as-needed.  The default is to
+    # preserve existing behavior.
+    set linker_opts_order "sources-then-flags"
     set add_flags ""
     set libs ""
     set compiler_type "c"
@@ -407,6 +412,10 @@ proc default_target_compile {source destfile type options} {
 	    regsub "^compiler=" $i "" tmp
 	    set compiler $tmp
 	}
+	if {[regexp "^early_flags=" $i]} {
+	    regsub "^early_flags=" $i "" tmp
+	    append early_flags " $tmp"
+	}
 	if {[regexp "^additional_flags=" $i]} {
 	    regsub "^additional_flags=" $i "" tmp
 	    append add_flags " $tmp"
@@ -634,10 +643,26 @@ proc default_target_compile {source destfile type options} {
     # This is obscure: we put SOURCES at the end when building an
     # object, because otherwise, in some situations, libtool will
     # become confused about the name of the actual source file.
-    if {$type eq "object"} {
-	set opts "$add_flags $sources"
-    } else {
-	set opts "$sources $add_flags"
+    switch $type {
+	"object" {
+	    set opts "$early_flags $add_flags $sources"
+	}
+	"executable" {
+	    switch $linker_opts_order {
+		"flags-then-sources" {
+		    set opts "$early_flags $add_flags $sources"
+		}
+		"sources-then-flags" {
+		    set opts "$early_flags $sources $add_flags"
+		}
+		default {
+		    error "Invalid value for board_info linker_opts_order"
+		}
+	    }
+	}
+	default {
+	    set opts "$early_flags $sources $add_flags"
+	}
     }
 
     if {[isremote host]} {
-- 
2.21.3