diff --git a/Cabal-tests/tests/UnitTests/Distribution/Simple/Program/GHC.hs b/Cabal-tests/tests/UnitTests/Distribution/Simple/Program/GHC.hs index daf64ccbb54..708ff943ab0 100644 --- a/Cabal-tests/tests/UnitTests/Distribution/Simple/Program/GHC.hs +++ b/Cabal-tests/tests/UnitTests/Distribution/Simple/Program/GHC.hs @@ -10,8 +10,8 @@ import Distribution.Types.ParStrat import Distribution.Simple.Flag import Distribution.Simple.Compiler (Compiler(..), CompilerId(..), CompilerFlavor(..), AbiTag(NoAbiTag)) import Distribution.PackageDescription (emptyPackageDescription) -import Distribution.Simple.Program.GHC (normaliseGhcArgs, renderGhcOptions, ghcOptNumJobs) -import Distribution.Version (mkVersion) +import Distribution.Simple.Program.GHC (GhcOptions, normaliseGhcArgs, renderGhcOptions, ghcOptNumJobs, ghcOptJSppOptions) +import Distribution.Version (mkVersion, Version) tests :: TestTree tests = testGroup "Distribution.Simple.Program.GHC" @@ -59,9 +59,32 @@ tests = testGroup "Distribution.Simple.Program.GHC" (Platform X86_64 Linux) (mempty { ghcOptNumJobs = Flag (NumJobs (Just 4)) }) assertListEquals flags ["-j4", "-clear-package-db"] + , testCase "JS preprocessor options (GHC >= 9.10.2)" $ do + let flags = renderWith (mkVersion [9,12,1]) + (mempty { ghcOptJSppOptions = ["-DJS"] }) + assertBool ("expected -optJSP-DJS in " ++ show flags) ("-optJSP-DJS" `elem` flags) + , testCase "JS preprocessor options gated out before GHC 9.10.2" $ do + let flags = renderWith (mkVersion [9,10,1]) + (mempty { ghcOptJSppOptions = ["-DJS"] }) + assertBool ("unexpected -optJSP in " ++ show flags) ("-optJSP-DJS" `notElem` flags) ] ] +-- | Render 'GhcOptions' as GHC arguments for a GHC of the given version. +renderWith :: Version -> GhcOptions -> [String] +renderWith ver = + renderGhcOptions + (Compiler + { compilerId = CompilerId GHC ver + , compilerAbiTag = NoAbiTag + , compilerCompat = [] + , compilerLanguages = [] + , compilerExtensions = [] + , compilerProperties = Map.singleton "Support parallel --make" "YES" + , compilerWiredInUnitIds = Nothing + }) + (Platform X86_64 Linux) + assertListEquals :: (Eq a, Show a) => [a] -> [a] -> Assertion assertListEquals xs ys | xs == ys = return () diff --git a/Cabal/src/Distribution/Simple/Program/GHC.hs b/Cabal/src/Distribution/Simple/Program/GHC.hs index 1b6a1857d11..8aa750b5306 100644 --- a/Cabal/src/Distribution/Simple/Program/GHC.hs +++ b/Cabal/src/Distribution/Simple/Program/GHC.hs @@ -521,7 +521,8 @@ data GhcOptions = GhcOptions , ghcOptCppOptions :: [String] -- ^ Options to pass through to CPP; the @ghc -optP@ flag. , ghcOptJSppOptions :: [String] - -- ^ Options to pass through to CPP; the @ghc -optJSP@ flag. @since 3.16.0.0 + -- ^ Options to pass through to the JavaScript preprocessor; the + -- @ghc -optJSP@ flag (GHC >= 9.10.2). @since 3.16.0.0 , ghcOptCppIncludePath :: NubListR (SymbolicPath Pkg (Dir Include)) -- ^ Search path for CPP includes like header files; the @ghc -I@ flag. , ghcOptCppIncludes :: NubListR (SymbolicPath Pkg File) @@ -874,7 +875,12 @@ renderGhcOptions comp _platform@(Platform _arch os) opts ["-I" ++ u dir | dir <- flags ghcOptCppIncludePath] , ["-optP" ++ opt | opt <- ghcOptCppOptions opts] - , ["-optJSP" ++ opt | opt <- ghcOptJSppOptions opts] + , -- The JavaScript preprocessor flag (-optJSP) only exists since GHC + -- 9.10.2; passing it to an older GHC is an error. + let ghc912 = case compilerCompatVersion GHC comp of + Just v -> v >= mkVersion [9, 10, 2] + Nothing -> False + in ["-optJSP" ++ opt | ghc912, opt <- ghcOptJSppOptions opts] , concat [ ["-optP-include", "-optP" ++ u inc] | inc <- flags ghcOptCppIncludes diff --git a/changelog.d/jspp-options-ghc-912.md b/changelog.d/jspp-options-ghc-912.md new file mode 100644 index 00000000000..184149c5839 --- /dev/null +++ b/changelog.d/jspp-options-ghc-912.md @@ -0,0 +1,11 @@ +--- +synopsis: Do not pass `-optJSP` to GHC older than 9.10.2 +packages: [Cabal] +prs: 12398 +significance: +--- + +The `jspp-options` field is rendered as `ghc -optJSP`, but that flag only +exists since GHC 9.10.2. Passing it to an older GHC made the invocation fail, so +a package using `jspp-options` could not be built at all with GHC < 9.10.2. The +options are now omitted when the compiler is older than 9.10.2.