[m-rev.] for review: support wildcard argument expansion with MSVC
Julien Fischer
jfischer at opturion.com
Tue Jan 6 01:04:34 AEDT 2026
For review by anyone.
The MinGW-w64 port of GCC has a similar feature which I will look into
enabling next, since the same issue exists for them.
------------------------------------
Support wildcard argument expansion with MSVC.
In Unix expansion of wildcard arguments is done by the shell. On Windows,
this is not the case and programs are responsible for expanding wildcard
argument themselves. When using source file mapping, we typically used
wildcards to specify the source files to map. When native Windows Mercury
compilers are used in the Windows command prompt or PowerShell, this does
not work, which is inconvenient to say the least. (In environments like
Cygwin or MSYS2, the shell does do wildcard expansion.)
Microsoft's C runtime has an optional component that programs can
link against which will do wildcard expansion on the argument vector
before main is called. Set up builds with MSVC to link against this
component when building the Mercury compiler.
scripts/parse_ml_options.sh-subr.in:
scripts/ml.in:
Add a new option --use-msvc-setargv-extension, which causes the the
wsetargv.obj file that enables the extension to be passed the linker.
(MSVC seems rather picky about where this is positioned. A a result
we do it within the ml script where we have more control over the matter.)
compiler/Mmakefile:
Pass --use-msvc-setargv-extension to the ml script if we are using MSVC.
Unrelated: Fix the identity of a compiler in a comment.
Unrelated: Add an XXX about something that looks suspicious.
util/mkinit.c:
Add a pointer to scripts/ml.in, saying that if the Windows entry
point (currently wmain) is updated, then the script may need to
be updated.
Julien.
diff --git a/compiler/Mmakefile b/compiler/Mmakefile
index 1f706b73f..ce3bfc296 100644
--- a/compiler/Mmakefile
+++ b/compiler/Mmakefile
@@ -2,7 +2,7 @@
# vim: ts=8 sw=8 noexpandtab ft=make
#-----------------------------------------------------------------------------#
# Copyright (C) 1995-2003, 2005-2012 The University of Melbourne.
-# Copyright (C) 2013-2017, 2019-2020, 2022-2023 The Mercury team.
+# Copyright (C) 2013-2017, 2019-2020, 2022-2024, 2026 The Mercury team.
# This file may only be copied under the terms of the GNU General
# Public Licence - see the file COPYING in the Mercury distribution.
#-----------------------------------------------------------------------------#
@@ -46,6 +46,7 @@ VPATH = \
MCFLAGS += --flags COMP_FLAGS $(CONFIG_OVERRIDE)
+# XXX Why does one branch of this set MLLIBS and the other MLOBJS?
ifeq ("$(filter csharp% java%,$(GRADE))","")
MLLIBS += $(THREAD_LIBS)
else
@@ -65,7 +66,7 @@ JAVACFLAGS += -J\"-Xmx2048m\"
endif
# The default C stack size of 1Mb on Windows is not enough to bootcheck the
-# system using a Mercury compiler built in the hlc.gc grade with MinGW64 GCC,
+# system using a Mercury compiler built in the hlc.gc grade with MinGW-w64 GCC,
# Cygwin GCC or MinGW64 clang.
# XXX For MSVC, we currently set the stack size in the ml script.
ifneq ("$(USING_MICROSOFT_CL_COMPILER)", "yes")
@@ -77,6 +78,13 @@ LDFLAGS += -Wl,--stack=8388608
endif
endif
+# If we are compiling using MSVC, then enable the use of the C runtime library
+# extension that enables wildcard expansion for command lines. On Windows,
+# wildcard is expansion is *not* done by the shell.
+ifeq ("$(USING_MICROSOFT_CL_COMPILER)", "yes")
+MLFLAGS += --use-msvc-setargv-extension
+endif
+
#-----------------------------------------------------------------------------#
# Targets.
diff --git a/scripts/ml.in b/scripts/ml.in
index ab803013b..a4665c216 100644
--- a/scripts/ml.in
+++ b/scripts/ml.in
@@ -559,17 +559,21 @@ case "${MKFIFO}" in
;;
esac
+# Changes to the value of MSVC_SETARGV below, may require changes to
+# value of the variable main_func in ../util/mkinit.c.
+MSVC_SETARGV=
+NOLOGO_OPTS=
case "${C_COMPILER_TYPE}" in
msvc*)
NOLOGO_OPTS="-nologo"
- ;;
- *)
- NOLOGO_OPTS=""
+ case "${use_msvc_setargv_extension}" in
+ true) MSVC_SETARGV="wsetargv.obj" ;;
+ esac
;;
esac
LINKER_PRE_FLAGS="${NOLOGO_OPTS} ${MSVCRT_OPTS} ${PRINT_MAP_OPT}
${UNDEF_OPT} ${STRIP_OPTS} ${LTO_OPTS} ${MAYBE_STATIC_OPT}
${ARCH_OPTS} ${SANITIZER_OPTS}"
-LINKER_POST_FLAGS="@LINK_OPT_SEP@ ${NODEFAULTLIB_FLAG} ${DEBUG_FLAG}
${LIBDIR_OPTS} ${RPATH_OPT_LIST} ${LIBS}"
+LINKER_POST_FLAGS="@LINK_OPT_SEP@ ${MSVC_SETARGV}
${NODEFAULTLIB_FLAG} ${DEBUG_FLAG} ${LIBDIR_OPTS} ${RPATH_OPT_LIST}
${LIBS}"
case "${verbose}" in
true)
diff --git a/scripts/parse_ml_options.sh-subr.in
b/scripts/parse_ml_options.sh-subr.in
index 9d80b162e..f7268f00e 100644
--- a/scripts/parse_ml_options.sh-subr.in
+++ b/scripts/parse_ml_options.sh-subr.in
@@ -2,7 +2,7 @@
# vim: ts=4 sw=4 expandtab ft=sh
#---------------------------------------------------------------------------#
# Copyright (C) 2001-2007, 2011 The University of Melbourne.
-# Copyright (C) 2019-2020 The Mercury team.
+# Copyright (C) 2019-2020, 2023, 2026 The Mercury team.
# This file may only be copied under the terms of the GNU General
# Public License - see the file COPYING in the Mercury distribution.
#---------------------------------------------------------------------------#
@@ -54,6 +54,7 @@ make_shared_lib=false
leave_shlib_dirs_relative=false
user_shlib_dirs=""
use_thread_libs=false
+use_msvc_setargv_extension=false
print_grade=false
print_gc_grade=false
print_link_command=false
@@ -214,6 +215,12 @@ Threads options:
if a C library being linked with uses threads, but the
Mercury code being linked doesn't.
+Other options:
+ --use-msvc-setargv-extension
+ If using MSVC, then enable the optional C runtime component
+ that provides wildcard argument expansion. This option has
+ no effect with non-MSVC compilers.
+
${grade_usage}
Environment variables:
@@ -402,6 +409,13 @@ EOF
use_thread_libs=false
;;
+ --use-msvc-setargv-extension)
+ use_msvc_setargv_extension=true
+ ;;
+ --no-use-msvc-setargv-extension)
+ use_msvc_setargv_extension=false
+ ;;
+
--print-grade)
print_grade=true
;;
diff --git a/util/mkinit.c b/util/mkinit.c
index 4b9c20300..6d72983f6 100644
--- a/util/mkinit.c
+++ b/util/mkinit.c
@@ -2,7 +2,7 @@
// vim:sw=4 ts=4 expandtab
//
// Copyright (C) 1995-2008, 2010-2012 The University of Melbourne.
-// Copyright (C) 2014-2016, 2019-2020, 2022, 2025 The Mercury team.
+// Copyright (C) 2014-2016, 2019-2020, 2022, 2025-2026 The Mercury team.
// This file may only be copied under the terms of the GNU General
// Public License - see the file COPYING in the Mercury distribution.
@@ -591,6 +591,9 @@ static const char mercury_grade_check_func[] =
"\n"
;
+// If you change main function for the MSVC case to something other than
+// wmain(), then you must also update the value of MSVC_SETARGV in
+// ../scripts/ml.in.
static const char main_func[] =
"#if defined(MR_MSVC)\n"
"\n"
More information about the reviews
mailing list