Skip to content

Commit a9f00ff

Browse files
committed
Allow stack to build with lts-19.1 (GHC 9.0.2)
Uses C pre-processor (CPP) directives to not disturb the existing code that builds with versions of GHC before 9.0.2. Tested by building stack on Windows 11. The built stack executable was, in turn, then tested by using it to build stack on Windows 11.
1 parent 80c5860 commit a9f00ff

13 files changed

+209
-22
lines changed

src/Stack/Build.hs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE ConstraintKinds #-}
3+
{-# LANGUAGE CPP #-}
34
{-# LANGUAGE DeriveDataTypeable #-}
45
{-# LANGUAGE FlexibleContexts #-}
56
{-# LANGUAGE OverloadedStrings #-}
@@ -19,7 +20,12 @@ module Stack.Build
1920

2021
import Stack.Prelude hiding (loadPackage)
2122
import Data.Aeson (Value (Object, Array), (.=), object)
23+
#if MIN_VERSION_aeson(2,0,0)
24+
import qualified Data.Aeson.Key as Key
25+
import qualified Data.Aeson.KeyMap as KeyMap
26+
#else
2227
import qualified Data.HashMap.Strict as HM
28+
#endif
2329
import Data.List ((\\), isPrefixOf)
2430
import Data.List.Extra (groupSort)
2531
import qualified Data.List.NonEmpty as NE
@@ -293,7 +299,11 @@ queryBuildInfo selectors0 =
293299
select front (sel:sels) value =
294300
case value of
295301
Object o ->
302+
#if MIN_VERSION_aeson(2,0,0)
303+
case KeyMap.lookup (Key.fromText sel) o of
304+
#else
296305
case HM.lookup sel o of
306+
#endif
297307
Nothing -> err "Selector not found"
298308
Just value' -> cont value'
299309
Array v ->
@@ -328,15 +338,23 @@ rawBuildInfo = do
328338
wantedCompiler <- view $ wantedCompilerVersionL.to (utf8BuilderToText . display)
329339
actualCompiler <- view $ actualCompilerVersionL.to compilerVersionText
330340
return $ object
341+
#if MIN_VERSION_aeson(2,0,0)
342+
[ "locals" .= Object (KeyMap.fromList $ map localToPair locals)
343+
#else
331344
[ "locals" .= Object (HM.fromList $ map localToPair locals)
345+
#endif
332346
, "compiler" .= object
333347
[ "wanted" .= wantedCompiler
334348
, "actual" .= actualCompiler
335349
]
336350
]
337351
where
338352
localToPair lp =
353+
#if MIN_VERSION_aeson(2,0,0)
354+
(Key.fromText $ T.pack $ packageNameString $ packageName p, value)
355+
#else
339356
(T.pack $ packageNameString $ packageName p, value)
357+
#endif
340358
where
341359
p = lpPackage lp
342360
value = object
@@ -358,7 +376,11 @@ checkComponentsBuildable lps =
358376
checkSubLibraryDependencies :: HasLogFunc env => [ProjectPackage] -> RIO env ()
359377
checkSubLibraryDependencies proj = do
360378
forM_ proj $ \p -> do
379+
#if MIN_VERSION_Cabal(3,4,0)
380+
C.GenericPackageDescription _ _ _ lib subLibs foreignLibs exes tests benches <- liftIO $ cpGPD . ppCommon $ p
381+
#else
361382
C.GenericPackageDescription _ _ lib subLibs foreignLibs exes tests benches <- liftIO $ cpGPD . ppCommon $ p
383+
#endif
362384

363385
let dependencies = concatMap getDeps subLibs <>
364386
concatMap getDeps foreignLibs <>
@@ -372,7 +394,7 @@ checkSubLibraryDependencies proj = do
372394
(logWarn "SubLibrary dependency is not supported, this will almost certainly fail")
373395
where
374396
getDeps (_, C.CondNode _ dep _) = dep
375-
subLibDepExist lib =
397+
subLibDepExist lib =
376398
any (\x ->
377399
case x of
378400
C.LSubLibName _ -> True

src/Stack/Build/Execute.hs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE ConstraintKinds #-}
3+
{-# LANGUAGE CPP #-}
34
{-# LANGUAGE DataKinds #-}
45
{-# LANGUAGE FlexibleContexts #-}
56
{-# LANGUAGE MultiParamTypeClasses #-}
@@ -1218,7 +1219,11 @@ withSingleContext ActionContext {..} ee@ExecuteEnv {..} task@Task {..} allDeps m
12181219
let macroDeps = mapMaybe snd matchedDeps
12191220
cppMacrosFile = setupDir </> relFileSetupMacrosH
12201221
cppArgs = ["-optP-include", "-optP" ++ toFilePath cppMacrosFile]
1222+
#if MIN_VERSION_Cabal(3,4,0)
1223+
writeBinaryFileAtomic cppMacrosFile (encodeUtf8Builder (T.pack (C.generatePackageVersionMacros (packageVersion package) macroDeps)))
1224+
#else
12211225
writeBinaryFileAtomic cppMacrosFile (encodeUtf8Builder (T.pack (C.generatePackageVersionMacros macroDeps)))
1226+
#endif
12221227
return (packageDBArgs ++ depsArgs ++ cppArgs)
12231228

12241229
-- This branch is usually taken for builds, and

src/Stack/BuildPlan.hs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE ConstraintKinds #-}
3+
{-# LANGUAGE CPP #-}
34
{-# LANGUAGE DataKinds #-}
45
{-# LANGUAGE DeriveDataTypeable #-}
56
{-# LANGUAGE FlexibleContexts #-}
@@ -224,7 +225,11 @@ selectPackageBuildPlan platform compiler pool gpd =
224225
flagCombinations :: NonEmpty [(FlagName, Bool)]
225226
flagCombinations = mapM getOptions (genPackageFlags gpd)
226227
where
228+
#if MIN_VERSION_Cabal(3,4,0)
229+
getOptions :: C.PackageFlag -> NonEmpty (FlagName, Bool)
230+
#else
227231
getOptions :: C.Flag -> NonEmpty (FlagName, Bool)
232+
#endif
228233
getOptions f
229234
| flagManual f = (fname, flagDefault f) :| []
230235
| flagDefault f = (fname, True) :| [(fname, False)]

src/Stack/ConfigCmd.hs

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE ConstraintKinds #-}
3+
{-# LANGUAGE CPP #-}
34
{-# LANGUAGE FlexibleContexts #-}
45
{-# LANGUAGE OverloadedStrings #-}
56
{-# LANGUAGE ScopedTypeVariables #-}
@@ -17,9 +18,15 @@ module Stack.ConfigCmd
1718
,cfgCmdName) where
1819

1920
import Stack.Prelude
21+
#if MIN_VERSION_aeson(2,0,0)
22+
import qualified Data.Aeson.Key as Key
23+
import qualified Data.Aeson.KeyMap as KeyMap
24+
#endif
2025
import Data.ByteString.Builder (byteString)
2126
import qualified Data.Map.Merge.Strict as Map
27+
#if !MIN_VERSION_aeson(2,0,0)
2228
import qualified Data.HashMap.Strict as HMap
29+
#endif
2330
import qualified Data.Text as T
2431
import qualified Data.Yaml as Yaml
2532
import qualified Options.Applicative as OA
@@ -74,7 +81,11 @@ cfgCmdSet cmd = do
7481
liftIO (Yaml.decodeFileEither (toFilePath configFilePath)) >>= either throwM return
7582
newValue <- cfgCmdSetValue (parent configFilePath) cmd
7683
let cmdKey = cfgCmdSetOptionName cmd
84+
#if MIN_VERSION_aeson(2,0,0)
85+
config' = KeyMap.insert (Key.fromText cmdKey) newValue config
86+
#else
7787
config' = HMap.insert cmdKey newValue config
88+
#endif
7889
if config' == config
7990
then logInfo
8091
(fromString (toFilePath configFilePath) <>

src/Stack/Init.hs

+35-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE ConstraintKinds #-}
3+
{-# LANGUAGE CPP #-}
34
{-# LANGUAGE FlexibleContexts #-}
45
{-# LANGUAGE MultiParamTypeClasses #-}
56
{-# LANGUAGE OverloadedStrings #-}
@@ -11,10 +12,15 @@ module Stack.Init
1112
) where
1213

1314
import Stack.Prelude
15+
#if MIN_VERSION_aeson(2,0,0)
16+
import qualified Data.Aeson.KeyMap as KeyMap
17+
#endif
1418
import qualified Data.ByteString.Builder as B
1519
import qualified Data.ByteString.Char8 as BC
1620
import qualified Data.Foldable as F
21+
#if !MIN_VERSION_aeson(2,0,0)
1722
import qualified Data.HashMap.Strict as HM
23+
#endif
1824
import qualified Data.IntMap as IntMap
1925
import Data.List.Extra (groupSortOn)
2026
import qualified Data.List.NonEmpty as NonEmpty
@@ -83,29 +89,29 @@ initProject currDir initOpts mresolver = do
8389
let ignored = Map.difference bundle rbundle
8490
dupPkgMsg
8591
| dupPkgs /= [] =
86-
"Warning (added by new or init): Some packages were found to \
87-
\have names conflicting with others and have been commented \
88-
\out in the packages section.\n"
92+
"Warning (added by new or init): Some packages were found to " <>
93+
"have names conflicting with others and have been commented " <>
94+
"out in the packages section.\n"
8995
| otherwise = ""
9096

9197
missingPkgMsg
9298
| Map.size ignored > 0 =
93-
"Warning (added by new or init): Some packages were found to \
94-
\be incompatible with the resolver and have been left commented \
95-
\out in the packages section.\n"
99+
"Warning (added by new or init): Some packages were found to " <>
100+
"be incompatible with the resolver and have been left commented " <>
101+
"out in the packages section.\n"
96102
| otherwise = ""
97103

98104
extraDepMsg
99105
| Map.size extraDeps > 0 =
100-
"Warning (added by new or init): Specified resolver could not \
101-
\satisfy all dependencies. Some external packages have been \
102-
\added as dependencies.\n"
106+
"Warning (added by new or init): Specified resolver could not " <>
107+
"satisfy all dependencies. Some external packages have been " <>
108+
"added as dependencies.\n"
103109
| otherwise = ""
104110
makeUserMsg msgs =
105111
let msg = concat msgs
106112
in if msg /= "" then
107-
msg <> "You can omit this message by removing it from \
108-
\stack.yaml\n"
113+
msg <> "You can omit this message by removing it from " <>
114+
"stack.yaml\n"
109115
else ""
110116

111117
userMsg = makeUserMsg [dupPkgMsg, missingPkgMsg, extraDepMsg]
@@ -177,12 +183,20 @@ renderStackYaml p ignoredPackages dupPackages =
177183
B.byteString headerHelp
178184
<> B.byteString "\n\n"
179185
<> F.foldMap (goComment o) comments
186+
#if MIN_VERSION_aeson(2,0,0)
187+
<> goOthers (o `KeyMap.difference` KeyMap.fromList comments)
188+
#else
180189
<> goOthers (o `HM.difference` HM.fromList comments)
190+
#endif
181191
<> B.byteString footerHelp
182192
<> "\n"
183193

184194
goComment o (name, comment) =
195+
#if MIN_VERSION_aeson(2,0,0)
196+
case (convert <$> KeyMap.lookup name o) <|> nonPresentValue name of
197+
#else
185198
case (convert <$> HM.lookup name o) <|> nonPresentValue name of
199+
#endif
186200
Nothing -> assert (name == "user-message") mempty
187201
Just v ->
188202
B.byteString comment <>
@@ -226,7 +240,11 @@ renderStackYaml p ignoredPackages dupPackages =
226240
| otherwise = ""
227241

228242
goOthers o
243+
#if MIN_VERSION_aeson(2,0,0)
244+
| KeyMap.null o = mempty
245+
#else
229246
| HM.null o = mempty
247+
#endif
230248
| otherwise = assert False $ B.byteString $ Yaml.encode o
231249

232250
-- Per Section Help
@@ -394,9 +412,9 @@ getWorkingResolverPlan initOpts pkgDirs0 snapCandidate snapLoc = do
394412
Right (f, edeps)-> return (snapLoc, f, edeps, pkgDirs)
395413
Left ignored
396414
| Map.null available -> do
397-
logWarn "*** Could not find a working plan for any of \
398-
\the user packages.\nProceeding to create a \
399-
\config anyway."
415+
logWarn $ "*** Could not find a working plan for any of " <>
416+
"the user packages.\nProceeding to create a " <>
417+
"config anyway."
400418
return (snapLoc, Map.empty, Map.empty, Map.empty)
401419
| otherwise -> do
402420
when (Map.size available == Map.size pkgDirs) $
@@ -537,9 +555,9 @@ cabalPackagesCheck cabaldirs dupErrMsg = do
537555

538556
when (nameMismatchPkgs /= []) $ do
539557
rels <- mapM prettyPath nameMismatchPkgs
540-
error $ "Package name as defined in the .cabal file must match the \
541-
\.cabal file name.\n\
542-
\Please fix the following packages and try again:\n"
558+
error $ "Package name as defined in the .cabal file must match the " <>
559+
".cabal file name.\n" <>
560+
"Please fix the following packages and try again:\n"
543561
<> T.unpack (utf8BuilderToText (formatGroup rels))
544562

545563
let dupGroups = filter ((> 1) . length)

src/Stack/New.hs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE ConstraintKinds #-}
3+
{-# LANGUAGE CPP #-}
34
{-# LANGUAGE ScopedTypeVariables #-}
45
{-# LANGUAGE DeriveDataTypeable #-}
56
{-# LANGUAGE FlexibleContexts #-}
@@ -18,6 +19,9 @@ module Stack.New
1819
import Stack.Prelude
1920
import Control.Monad.Trans.Writer.Strict
2021
import Data.Aeson as A
22+
#if MIN_VERSION_aeson(2,0,0)
23+
import qualified Data.Aeson.KeyMap as KeyMap
24+
#endif
2125
import qualified Data.ByteString.Base64 as B64
2226
import Data.ByteString.Builder (lazyByteString)
2327
import qualified Data.ByteString.Lazy as LB
@@ -40,7 +44,9 @@ import Stack.Constants
4044
import Stack.Constants.Config
4145
import Stack.Types.Config
4246
import Stack.Types.TemplateName
47+
#if !MIN_VERSION_aeson(2,0,0)
4348
import qualified RIO.HashMap as HM
49+
#endif
4450
import RIO.Process
4551
import qualified Text.Mustache as Mustache
4652
import qualified Text.Mustache.Render as Mustache
@@ -139,7 +145,7 @@ loadTemplate name logIt = do
139145
RepoPath rtp -> do
140146
let settings = settingsFromRepoTemplatePath rtp
141147
downloadFromUrl settings templateDir
142-
148+
143149
where
144150
loadLocalFile :: Path b File -> (ByteString -> Either String Text) -> RIO env Text
145151
loadLocalFile path extract = do
@@ -209,7 +215,11 @@ settingsFromRepoTemplatePath (RepoTemplatePath Github user name) =
209215
, tplExtract = \bs -> do
210216
decodedJson <- eitherDecode (LB.fromStrict bs)
211217
case decodedJson of
218+
#if MIN_VERSION_aeson(2,0,0)
219+
Object o | Just (String content) <- KeyMap.lookup "content" o -> do
220+
#else
212221
Object o | Just (String content) <- HM.lookup "content" o -> do
222+
#endif
213223
let noNewlines = T.filter (/= '\n')
214224
bsContent <- B64.decode $ T.encodeUtf8 (noNewlines content)
215225
mapLeft show $ decodeUtf8' bsContent
@@ -258,8 +268,8 @@ applyTemplate project template nonceParams dir templateText = do
258268

259269
let isPkgSpec f = ".cabal" `isSuffixOf` f || f == "package.yaml"
260270
unless (any isPkgSpec . M.keys $ files) $
261-
throwM (InvalidTemplate template "Template does not contain a .cabal \
262-
\or package.yaml file")
271+
throwM (InvalidTemplate template
272+
"Template does not contain a .cabal or package.yaml file")
263273

264274
-- Apply Mustache templating to a single file within the project
265275
-- template.

0 commit comments

Comments
 (0)