summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Scott <ryan.gl.scott@gmail.com>2022-09-11 13:08:37 -0400
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-09-12 13:29:41 -0400
commit3a815f30bcba5672085e823aeef90863253b0b1a (patch)
tree24a05e2a5283a93d86e0e88014ff3980a457a30c
parenta5f9c35f5831ef5108e87813a96eac62803852ab (diff)
downloadhaskell-3a815f30bcba5672085e823aeef90863253b0b1a.tar.gz
Windows: Always define _UCRT when compiling C code
As seen in #22159, this is required to ensure correct behavior when MinGW-w64 headers are in the `C_INCLUDE_PATH`. Fixes #22159.
-rw-r--r--m4/fp_setup_windows_toolchain.m46
-rw-r--r--testsuite/tests/ffi/should_run/Makefile7
-rw-r--r--testsuite/tests/ffi/should_run/T22159.hs23
-rw-r--r--testsuite/tests/ffi/should_run/T22159.stdout1
-rw-r--r--testsuite/tests/ffi/should_run/T22159_c.c6
-rw-r--r--testsuite/tests/ffi/should_run/all.T5
6 files changed, 47 insertions, 1 deletions
diff --git a/m4/fp_setup_windows_toolchain.m4 b/m4/fp_setup_windows_toolchain.m4
index e1a2244c37..1f44a388fe 100644
--- a/m4/fp_setup_windows_toolchain.m4
+++ b/m4/fp_setup_windows_toolchain.m4
@@ -82,7 +82,11 @@ AC_DEFUN([FP_SETUP_WINDOWS_TOOLCHAIN],[
CC="${mingwbin}clang.exe"
CXX="${mingwbin}clang++.exe"
- cflags="--rtlib=compiler-rt"
+
+ # Signal that we are linking against UCRT with the _UCRT macro. This is
+ # necessary to ensure correct behavior when MinGW-w64 headers are in the
+ # header include path (#22159).
+ cflags="--rtlib=compiler-rt -D_UCRT"
CFLAGS="$cflags"
CONF_CC_OPTS_STAGE1="$cflags"
CONF_CC_OPTS_STAGE2="$cflags"
diff --git a/testsuite/tests/ffi/should_run/Makefile b/testsuite/tests/ffi/should_run/Makefile
index cd264113c7..03177f4e8a 100644
--- a/testsuite/tests/ffi/should_run/Makefile
+++ b/testsuite/tests/ffi/should_run/Makefile
@@ -49,3 +49,10 @@ T15933:
'$(TEST_HC)' $(TEST_HC_OPTS) -c T15933.hs
'$(TEST_HC)' $(TEST_HC_OPTS) T15933_c.o T15933.o -o T15933
./T15933
+
+.PHONY: T22159
+T22159:
+ C_INCLUDE_PATH=/mingw64/include '$(TEST_HC)' $(TEST_HC_OPTS) -c T22159.hs
+ C_INCLUDE_PATH=/mingw64/include '$(TEST_HC)' $(TEST_HC_OPTS) -c T22159_c.c
+ C_INCLUDE_PATH=/mingw64/include '$(TEST_HC)' $(TEST_HC_OPTS) T22159.o T22159_c.o -o T22159
+ ./T22159
diff --git a/testsuite/tests/ffi/should_run/T22159.hs b/testsuite/tests/ffi/should_run/T22159.hs
new file mode 100644
index 0000000000..24e9511d3b
--- /dev/null
+++ b/testsuite/tests/ffi/should_run/T22159.hs
@@ -0,0 +1,23 @@
+{-# LANGUAGE CPP #-}
+module Main (main) where
+
+#if defined(i386_HOST_ARCH)
+# define WINDOWS_CCONV stdcall
+#elif defined(x86_64_HOST_ARCH)
+# define WINDOWS_CCONV ccall
+#else
+# error Unknown mingw32 arch
+#endif
+
+import Foreign.C.String (peekCWString)
+import Foreign.C.Types (CWchar)
+import Foreign.Marshal.Alloc (allocaBytes)
+import Foreign.Ptr (Ptr)
+
+foreign import WINDOWS_CCONV "hello" c_hello :: Ptr CWchar -> IO ()
+
+main :: IO ()
+main = allocaBytes 12 $ \buf -> do
+ c_hello buf
+ str <- peekCWString buf
+ putStrLn str
diff --git a/testsuite/tests/ffi/should_run/T22159.stdout b/testsuite/tests/ffi/should_run/T22159.stdout
new file mode 100644
index 0000000000..ce01362503
--- /dev/null
+++ b/testsuite/tests/ffi/should_run/T22159.stdout
@@ -0,0 +1 @@
+hello
diff --git a/testsuite/tests/ffi/should_run/T22159_c.c b/testsuite/tests/ffi/should_run/T22159_c.c
new file mode 100644
index 0000000000..0f0c5df5a0
--- /dev/null
+++ b/testsuite/tests/ffi/should_run/T22159_c.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+#include <wchar.h>
+
+void hello(wchar_t *buf) {
+ swprintf_s(buf, 12, L"hello");
+}
diff --git a/testsuite/tests/ffi/should_run/all.T b/testsuite/tests/ffi/should_run/all.T
index b6d0a8e1c3..79783c4776 100644
--- a/testsuite/tests/ffi/should_run/all.T
+++ b/testsuite/tests/ffi/should_run/all.T
@@ -229,3 +229,8 @@ test('T19237', normal, compile_and_run, ['T19237_c.c'])
test('T21305', omit_ways(['ghci']), multi_compile_and_run,
['T21305', [('T21305_cmm.cmm', '')], ''])
+
+test('T22159',
+ [unless(opsys('mingw32'), skip),
+ extra_files(['T22159_c.c'])],
+ makefile_test, ['T22159'])