Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
9e58b8a1 by Cheng Shao at 2026-02-11T16:08:10-05:00
ghc-toolchain: add C11 check
This patch partially reverts commit
b8307eab80c5809df5405d76c822bf86877f5960 that removed C99 check in
autoconf/ghc-toolchain. Now we:
- No longer re-implement `FP_SET_CFLAGS_C11` similar to
`FP_SET_CFLAGS_C99` in the past, since autoconf doesn't provide a
convenient `AC_PROG_CC_C11` function. ghc-toolchain will handle it
anyway.
- The Cmm CPP C99 check is relanded and repurposed for C11.
- The C99 logic in ghc-toolchain is relanded and repurposed for C11.
- The C99 check in Stg.h is corrected to check for C11. The obsolete
_ISOC99_SOURCE trick is dropped.
- Usages of `-std=gnu99` in the testsuite are corrected to use
`-std=gnu11`.
Closes #26908.
- - - - -
7 changed files:
- configure.ac
- distrib/configure.ac.in
- m4/fp_cmm_cpp_cmd_with_args.m4
- rts/include/Stg.h
- testsuite/tests/ffi/should_run/all.T
- utils/ghc-toolchain/src/GHC/Toolchain/Tools/Cc.hs
- utils/ghc-toolchain/src/GHC/Toolchain/Tools/Cpp.hs
Changes:
=====================================
configure.ac
=====================================
@@ -671,8 +671,8 @@ FP_CC_IGNORE_UNUSED_ARGS([$CC], [CONF_CC_OPTS_STAGE2])
# CPP, CPPFLAGS
# --with-cpp/-with-cpp-flags
-dnl Note that we must do this after setting and using the C99 CPPFLAGS, or
-dnl otherwise risk trying to configure the C99 and LD flags using -E as a CPPFLAG
+dnl Note that we must do this after setting and using the C11 CPPFLAGS, or
+dnl otherwise risk trying to configure the C11 and LD flags using -E as a CPPFLAG
FP_CPP_CMD_WITH_ARGS([$CC_STAGE0],[CPPCmd_STAGE0],[CONF_CPP_OPTS_STAGE0])
FP_CPP_CMD_WITH_ARGS([$CC],[CPPCmd],[CONF_CPP_OPTS_STAGE1])
FP_CPP_CMD_WITH_ARGS([$CC],[CPPCmd],[CONF_CPP_OPTS_STAGE2])
=====================================
distrib/configure.ac.in
=====================================
@@ -313,8 +313,8 @@ FP_CC_IGNORE_UNUSED_ARGS([$CC], [CONF_CC_OPTS_STAGE2])
# CPP, CPPFLAGS
# --with-cpp/-with-cpp-flags
-dnl Note that we must do this after setting and using the C99 CPPFLAGS, or
-dnl otherwise risk trying to configure the C99 and LD flags using -E as a CPPFLAG
+dnl Note that we must do this after setting and using the C11 CPPFLAGS, or
+dnl otherwise risk trying to configure the C11 and LD flags using -E as a CPPFLAG
FP_CPP_CMD_WITH_ARGS([$CC_STAGE0],[CPPCmd_STAGE0],[CONF_CPP_OPTS_STAGE0])
FP_CPP_CMD_WITH_ARGS([$CC],[CPPCmd],[CONF_CPP_OPTS_STAGE1])
FP_CPP_CMD_WITH_ARGS([$CC],[CPPCmd],[CONF_CPP_OPTS_STAGE2])
=====================================
m4/fp_cmm_cpp_cmd_with_args.m4
=====================================
@@ -56,6 +56,27 @@ else
AC_MSG_RESULT([no])
fi
+AC_MSG_CHECKING([the C-- preprocessor for C11 support])
+cat > conftest.c <
+#if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112L
+# error "Compiler does not advertise C11 conformance"
+#endif
+EOF
+if "$CMM_CPP_CMD" $CMM_CPP_ARGS conftest.c -o conftest -g0 >/dev/null 2>&1; then
+ AC_MSG_RESULT([yes])
+else
+ # Try -std=gnu11
+ if "$CMM_CPP_CMD" -std=gnu11 $CMM_CPP_ARGS conftest.c -o conftest -g0 >/dev/null 2>&1; then
+ $3="-std=gnu11 $$3"
+ AC_MSG_RESULT([needs -std=gnu11])
+ else
+ AC_MSG_ERROR([C11-compatible compiler needed])
+ fi
+fi
+rm -f conftest.c conftest.o conftest
+
+
$2="$CMM_CPP_CMD"
$3="$$3 $CMM_CPP_ARGS"
=====================================
rts/include/Stg.h
=====================================
@@ -31,8 +31,8 @@
#define __STDC_VERSION__ 0
#endif
-#if !(__STDC_VERSION__ >= 199901L) && !(__cplusplus >= 201103L)
-# error __STDC_VERSION__ does not advertise C99, C++11 or later
+#if !(__STDC_VERSION__ >= 201112L) && !(__cplusplus >= 201103L)
+# error __STDC_VERSION__ does not advertise C11, C++11 or later
#endif
/*
@@ -49,10 +49,6 @@
#if !defined(IN_STG_CODE)
# define IN_STG_CODE 1
-// Turn on C99 for .hc code. This gives us the INFINITY and NAN
-// constants from math.h, which we occasionally need to use in .hc (#1861)
-# define _ISOC99_SOURCE
-
// We need _BSD_SOURCE so that math.h defines things like gamma
// on Linux
# define _BSD_SOURCE
=====================================
testsuite/tests/ffi/should_run/all.T
=====================================
@@ -103,7 +103,7 @@ test('T2276_ghci', [ only_ghci,
pre_cmd('$MAKE -s --no-print-directory T2276_ghci_setup ghciWayFlags=' + config.ghci_way_flags) ],
compile_and_run, ['-fobject-code T2276_ghci_c.o'])
-test('T2469', normal, compile_and_run, ['-optc-std=gnu99'])
+test('T2469', normal, compile_and_run, ['-optc-std=gnu11'])
test('T2594', [req_c], compile_and_run, ['T2594_c.c'])
=====================================
utils/ghc-toolchain/src/GHC/Toolchain/Tools/Cc.hs
=====================================
@@ -11,6 +11,7 @@ module GHC.Toolchain.Tools.Cc
, compileC
, compileAsm
, addPlatformDepCcFlags
+ , checkC11Support
) where
import Control.Monad
@@ -50,8 +51,12 @@ findCc archOs llvmTarget progOpt = do
cc1 <- ignoreUnusedArgs cc0
cc2 <- ccSupportsTarget archOs llvmTarget cc1
checking "whether Cc works" $ checkCcWorks cc2
- checkCcSupportsExtraViaCFlags cc2
- return cc2
+ cc3 <- oneOf "cc doesn't support C11" $ map checkC11Support
+ [ cc2
+ , cc2 & _ccFlags %++ "-std=gnu11"
+ ]
+ checkCcSupportsExtraViaCFlags cc3
+ return cc3
checkCcWorks :: Cc -> M ()
checkCcWorks cc = withTempDir $ \dir -> do
@@ -83,6 +88,17 @@ ccSupportsTarget archOs target cc =
checking "whether Cc supports --target" $
supportsTarget archOs _ccProgram checkCcWorks target cc
+checkC11Support :: Cc -> M Cc
+checkC11Support cc = checking "for C11 support" $ withTempDir $ \dir -> do
+ let test_o = dir > "test.o"
+ compileC cc test_o $ unlines
+ [ "#include "
+ , "#if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112L"
+ , "# error \"Compiler does not advertise C11 conformance\""
+ , "#endif"
+ ]
+ return cc
+
checkCcSupportsExtraViaCFlags :: Cc -> M ()
checkCcSupportsExtraViaCFlags cc = checking "whether cc supports extra via-c flags" $ withTempDir $ \dir -> do
let test_o = dir > "test.o"
=====================================
utils/ghc-toolchain/src/GHC/Toolchain/Tools/Cpp.hs
=====================================
@@ -19,7 +19,7 @@ import GHC.Toolchain.Prelude
import GHC.Toolchain.Program
import GHC.Toolchain.Tools.Cc
-import GHC.Toolchain.Utils (withTempDir, expectFileExists)
+import GHC.Toolchain.Utils (withTempDir, oneOf, expectFileExists)
newtype Cpp = Cpp { cppProgram :: Program
}
@@ -160,7 +160,13 @@ findJsCpp progOpt cc = checking "for JavaScript C preprocessor" $ do
findCmmCpp :: ProgOpt -> Cc -> M CmmCpp
findCmmCpp progOpt cc = checking "for a Cmm preprocessor" $ do
-- Use the specified CPP or try to use the c compiler
- cpp <- findProgram "Cmm preprocessor" progOpt [] <|> pure (programFromOpt progOpt (prgPath $ ccProgram cc) [])
+ foundCppProg <- findProgram "Cmm preprocessor" progOpt [] <|> pure (programFromOpt progOpt (prgPath $ ccProgram cc) [])
+ -- Check whether the C preprocessor needs -std=gnu11 (only very old toolchains need this)
+ Cc cpp <- oneOf "cc doesn't support C11" $ map checkC11Support
+ [ Cc foundCppProg
+ , Cc (foundCppProg & _prgFlags %++ "-std=gnu11")
+ ]
+
cmmCppSupportsG0 <- withTempDir $ \dir -> do
let conftest = dir > "conftest.c"
writeFile conftest "int main(void) {}"
@@ -175,9 +181,14 @@ findCmmCpp progOpt cc = checking "for a Cmm preprocessor" $ do
findCpp :: ProgOpt -> Cc -> M Cpp
findCpp progOpt cc = checking "for C preprocessor" $ do
-- Use the specified CPP or try to use the c compiler
- cpp <- findProgram "C preprocessor" progOpt [] <|> pure (programFromOpt progOpt (prgPath $ ccProgram cc) [])
+ foundCppProg <- findProgram "C preprocessor" progOpt [] <|> pure (programFromOpt progOpt (prgPath $ ccProgram cc) [])
+ -- Check whether the C preprocessor needs -std=gnu11 (only very old toolchains need this)
+ Cc cpp2 <- oneOf "cc doesn't support C11" $ map checkC11Support
+ [ Cc foundCppProg
+ , Cc (foundCppProg & _prgFlags %++ "-std=gnu11")
+ ]
-- Always add the -E flag to the CPP, regardless of the user options
- let cppProgram = addFlagIfNew "-E" cpp
+ let cppProgram = addFlagIfNew "-E" cpp2
return Cpp{cppProgram}
--------------------------------------------------------------------------------
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/9e58b8a14b7c2190dbd4c72e26fd47bc...
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/9e58b8a14b7c2190dbd4c72e26fd47bc...
You're receiving this email because of your account on gitlab.haskell.org.