Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: [PATCH v2 06/11] gdb/testsuite: Add mechanism to compile Windows native programs on Cygwin
Date: Mon, 25 May 2026 20:18:24 +0100	[thread overview]
Message-ID: <20260525191829.984105-7-pedro@palves.net> (raw)
In-Reply-To: <20260525191829.984105-1-pedro@palves.net>

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(-)

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
-- 
2.53.0


  parent reply	other threads:[~2026-05-25 19:19 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-25 19:18 [PATCH v2 00/11] Fix a few Cygwin/MinGW problems Pedro Alves
2026-05-25 19:18 ` [PATCH v2 01/11] Adjust gdb.base/exitsignal.exp for MinGW, trigger fault Pedro Alves
2026-05-25 19:18 ` [PATCH v2 02/11] Adjust gdb.base/exitsignal.exp for MinGW, second-chance SIGSEGV Pedro Alves
2026-05-26 11:18   ` Eli Zaretskii
2026-05-27 12:56     ` Pedro Alves
2026-05-25 19:18 ` [PATCH v2 03/11] Adjust gdb.base/exitsignal.exp for MinGW, separate program names Pedro Alves
2026-05-27 21:59   ` Thiago Jung Bauermann
2026-06-12 14:00     ` Pedro Alves
2026-05-25 19:18 ` [PATCH v2 04/11] gdb.base/exitsignal.exp: Exit with non-zero Pedro Alves
2026-05-25 19:18 ` [PATCH v2 05/11] gdb.base/exitsignal.exp: Test attaching too Pedro Alves
2026-05-25 19:18 ` Pedro Alves [this message]
2026-05-25 19:18 ` [PATCH v2 07/11] Windows gdb+gdbserver: Share exit status logic Pedro Alves
2026-05-25 19:18 ` [PATCH v2 08/11] Windows gdb+gdbserver: Decode Cygwin ExitProcess codes Pedro Alves
2026-05-26 11:31   ` Eli Zaretskii
2026-05-27 13:58     ` Pedro Alves
2026-05-27 14:12       ` Eli Zaretskii
2026-05-27 22:00   ` Thiago Jung Bauermann
2026-05-25 19:18 ` [PATCH v2 09/11] Adjust gdb.python/py-events.exp for Cygwin/MinGW, thread IDs Pedro Alves
2026-05-25 19:18 ` [PATCH v2 10/11] Adjust gdb.python/py-events.exp for Cygwin/MinGW, no fork Pedro Alves
2026-05-25 19:18 ` [PATCH v2 11/11] Adjust gdb.python/py-events.exp for Cygwin/MinGW, "info proc" => "inferior" Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260525191829.984105-7-pedro@palves.net \
    --to=pedro@palves.net \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox