[v2,06/11] gdb/testsuite: Add mechanism to compile Windows native programs on Cygwin

Message ID 20260525191829.984105-7-pedro@palves.net
State New
Headers
Series Fix a few Cygwin/MinGW problems |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Pedro Alves May 25, 2026, 7:18 p.m. UTC
  This adds a new "win32" option to gdb_compile that lets us compile
native Windows programs when testing Cygwin.  Will be used in a
following patch.

The testsuite tries to find the compiler to use automatically.  You
can also explicitly set it with WIN32_CC_FOR_TARGET.

Change-Id: I86e954494cf7f88164cdf1c9127be46d5437986f
commit-id: c71b8812
---
 gdb/testsuite/README      |  22 ++++++++
 gdb/testsuite/lib/gdb.exp | 104 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 124 insertions(+), 2 deletions(-)
  

Patch

diff --git a/gdb/testsuite/README b/gdb/testsuite/README
index 60fd322ea4d..479e81f90f5 100644
--- a/gdb/testsuite/README
+++ b/gdb/testsuite/README
@@ -414,6 +414,28 @@  Example:
 
 	make check-gdb TESTS="gdb.multi/multi-arch.exp" RUNTESTFLAGS="ARM_CC_FOR_TARGET=arm-linux-gnueabihf-gcc"
 
+WIN32_CC_FOR_TARGET
+
+The Cygwin ports of GDB and GDBserver include basic support for
+debugging native Windows programs.  Some tests exercise this
+particular feature.
+
+By default, the testsuite tries to find a compiler capable of
+generating native Windows executables.  If no compiler is found, or if
+the executable generated by the found compiler can't be executed
+correctly, the tests are marked UNSUPPORTED.  The list of compiler
+names the testsuite tries can be found in
+gdb/testsuite/lib/gdb.exp:win32_cc_for_target.
+
+You can set WIN32_CC_FOR_TARGET to override the search and explicitly
+specify the compiler to use.  This variable should contain the command
+line for the compiler, including the full path to it, if the compiler
+is not in $PATH.
+
+Example:
+
+	make check-gdb TESTS="gdb.base/exitsignal.exp" RUNTESTFLAGS="WIN32_CC_FOR_TARGET=x86_64-w64-mingw32-gcc"
+
 Race detection
 **************
 
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 28709004570..e21c1c1224f 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -6128,6 +6128,10 @@  proc gdb_can_simple_compile_nodebug {name code {type object} {compile_flags ""}
 global gdb_saved_set_unbuffered_mode_obj
 set gdb_saved_set_unbuffered_mode_obj ""
 
+# The name of the set_unbuffered_mode_saved.o object.  Stored in a
+# global variable so it can be overridden in some cases.
+set gdb_saved_set_unbuffered_mode_obj_name set_unbuffered_mode_saved.o
+
 # Escape STR sufficiently for use on host commandline.
 
 proc escape_for_host { str } {
@@ -6201,6 +6205,7 @@  proc quote_for_host { args } {
 #     Fortran 90, Go or Rust.
 #   - debug: Build with debug information.
 #   - optimize: Build with optimization.
+#   - win32: Build a native Windows program.  Cygwin only.
 
 proc gdb_compile {source dest type options} {
     global GDB_TESTCASE_OPTIONS
@@ -6212,6 +6217,11 @@  proc gdb_compile {source dest type options} {
 
     set outdir [file dirname $dest]
 
+    # Handle compiling native Windows programs when testing Cygwin.
+    if { [istarget *-*-cygwin*] && [lsearch -exact $options win32] != -1} {
+	return [gdb_compile_win32 $source $dest $type $options]
+    }
+
     # GDB doesn't support minimal symbols in AIX, so fail the compilation
     # if nodebug is requested for an AIX target.
     if { [istarget *-*-aix*] && [lsearch -exact $options nodebug] != -1} {
@@ -6635,9 +6645,11 @@  proc gdb_compile {source dest type options} {
 		    return $result
 		}
 		if {[is_remote host]} {
-		    set gdb_saved_set_unbuffered_mode_obj set_unbuffered_mode_saved.o
+		    set gdb_saved_set_unbuffered_mode_obj \
+			$::gdb_saved_set_unbuffered_mode_obj_name
 		} else {
-		    set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
+		    set gdb_saved_set_unbuffered_mode_obj \
+			${objdir}/$::gdb_saved_set_unbuffered_mode_obj_name
 		}
 		# Link a copy of the output object, because the
 		# original may be automatically deleted.
@@ -7003,6 +7015,38 @@  proc gdb_compile_openmp {source dest type options} {
     return [gdb_compile $source $dest $type $options]
 }
 
+# Build a native Windows program from SOURCE.  See prefatory comment
+# for gdb_compile, above, for discussion of the parameters to this
+# proc.  This is meant to be used on Cygwin, to smoke test debugging
+# native Windows programs with the Cygwin native backend.
+
+proc gdb_compile_win32 {source dest type options} {
+    if {![istarget *-*-cygwin*]} {
+	error "cygwin procedure called on non-cygwin target"
+    }
+
+    save_vars {
+	::target_triplet
+	::CC_FOR_TARGET
+	::gdb_saved_set_unbuffered_mode_obj
+	::gdb_saved_set_unbuffered_mode_obj_name
+    } {
+	# So we trigger the set_unbuffered_mode_obj paths.
+	set ::target_triplet "x86_64-w64-mingw32"
+
+	# We need a Win32-specific compilation of the
+	# set_unbuffered_mode object.
+	set ::gdb_saved_set_unbuffered_mode_obj ""
+	set ::gdb_saved_set_unbuffered_mode_obj_name \
+	    set_unbuffered_mode_saved_win32.o
+
+	# Do this last, as this tries compiling internally.
+	set ::CC_FOR_TARGET [win32_cc_for_target]
+
+	return [gdb_compile $source $dest $type $options]
+    }
+}
+
 # Send a command to GDB.
 # For options for TYPE see gdb_stdin_log_write
 
@@ -11248,6 +11292,62 @@  gdb_caching_proc arm_cc_for_target {} {
     return ""
 }
 
+# Return the compiler that can generate native Windows executables.
+# Used when testing basic native Windows support on Cygwin.  If
+# WIN32_CC_FOR_TARGET is set, use that.  If not, try a few common
+# compiler names, making sure that the executable they produce can
+# run.  Note, this assumes global state has been set to target native
+# Windows programs.  See gdb_compile_win32.
+
+gdb_caching_proc win32_cc_for_target {} {
+    if {[info exists ::WIN32_CC_FOR_TARGET]} {
+	# If the user specified the compiler explicitly, then don't
+	# check whether the resulting binary runs outside GDB.  Assume
+	# that it does, and if it turns out it doesn't, then the user
+	# should get loud FAILs, instead of UNSUPPORTED.
+	return $::WIN32_CC_FOR_TARGET
+    }
+
+    # Fallback to a few common compiler names.  Also confirm the
+    # produced binary actually runs on the system before declaring
+    # we've found the right compiler.
+
+    # Only one compiler for now.
+    set compilers {
+	x86_64-w64-mingw32-gcc
+    }
+
+    save_vars {
+	::CC_FOR_TARGET
+    } {
+	foreach compiler $compilers {
+	    if {![is_remote host] && [which $compiler] == 0} {
+		# Avoid "default_target_compile: Can't find
+		# $compiler." warning issued from gdb_compile.
+		continue
+	    }
+
+	    set ::CC_FOR_TARGET $compiler
+
+	    set src { int main() { return 0; } }
+	    if {[gdb_simple_compile win32-test-prog $src executable]} {
+		set target_obj [gdb_remote_download target $obj]
+		set result [remote_exec target $target_obj]
+		set status [lindex $result 0]
+		set output [lindex $result 1]
+
+		file delete $obj
+
+		if { $output == "" && $status == 0} {
+		    return $compiler
+		}
+	    }
+	}
+    }
+
+    return ""
+}
+
 # Step until the pattern REGEXP is found.  Step at most
 # MAX_STEPS times, but stop stepping once REGEXP is found.
 # CURRENT matches current location