Skip to content

Commit 21be704

Browse files
committed
Re #6542 Take a direct approach to initialBuildSteps
1 parent 4975d5b commit 21be704

File tree

2 files changed

+105
-42
lines changed

2 files changed

+105
-42
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Behaviour changes:
1212
version of GHC. Stack no longer supports such Cabal versions before 2.2, which
1313
came with versions of GHC before 8.4. Consequently, the `init` command will
1414
not try LTS Haskell before 12.0.
15+
* Stack's `StackSetupShim` executable, when called with `repl` and
16+
`stack-initial-build-steps`, no longer uses Cabal's `replHook` to apply
17+
`initialBuildSteps` but takes a more direct approach.
1518
* The `init` command initialises `stack.yaml` with a `snapshot` key rather than
1619
a `resolver` key.
1720
* After installing GHC or another tool, Stack deletes the archive file which

src/setup-shim/StackSetupShim.hs

Lines changed: 102 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,120 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE PackageImports #-}
3+
34
module StackSetupShim where
4-
import Main
5-
#if defined(MIN_VERSION_Cabal)
5+
6+
import Data.List ( stripPrefix )
7+
-- | Stack no longer supports Cabal < 2.2 and, consequently, GHC versions before
8+
-- GHC 8.4 or base < 4.11.0.0. Consequently, we do not need to test for the
9+
-- existence of the MIN_VERSION_Cabal macro (provided from GHC 8.0).
10+
#if MIN_VERSION_Cabal(3,0,0)
611
#if MIN_VERSION_Cabal(3,8,1)
7-
import Distribution.PackageDescription
8-
( PackageDescription, emptyHookedBuildInfo )
12+
import Distribution.Parsec ( eitherParsec )
913
#else
10-
import "Cabal" Distribution.PackageDescription
11-
( PackageDescription, emptyHookedBuildInfo )
14+
-- Avoid confusion with Cabal-syntax module of same name
15+
import "Cabal" Distribution.Parsec ( eitherParsec )
1216
#endif
1317
#else
14-
import Distribution.PackageDescription
15-
( PackageDescription, emptyHookedBuildInfo )
18+
import Distribution.Parsec.Class ( eitherParsec)
19+
#endif
20+
import Distribution.ReadE ( ReadE (..) )
21+
import Distribution.Simple.Configure ( getPersistBuildConfig )
22+
-- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
23+
#if MIN_VERSION_Cabal(3,11,0)
24+
import Distribution.Simple.Build ( writeBuiltinAutogenFiles )
25+
#else
26+
import Distribution.Simple.Build ( initialBuildSteps )
27+
#endif
28+
#if MIN_VERSION_Cabal(3,11,0)
29+
import Distribution.Simple.Errors ( exceptionMessage )
1630
#endif
17-
import Distribution.Simple
18-
import Distribution.Simple.Build
19-
import Distribution.Simple.Setup
20-
( ReplFlags, fromFlag, replDistPref, replVerbosity )
21-
import Distribution.Simple.LocalBuildInfo ( LocalBuildInfo )
2231
-- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
23-
#if defined(MIN_VERSION_Cabal)
2432
#if MIN_VERSION_Cabal(3,11,0)
2533
import Distribution.Simple.LocalBuildInfo
26-
( ComponentLocalBuildInfo, componentBuildDir
27-
, withAllComponentsInBuildOrder
28-
)
29-
import Distribution.Simple.Utils ( createDirectoryIfMissingVerbose )
30-
import Distribution.Verbosity ( Verbosity )
34+
( componentBuildDir, withAllComponentsInBuildOrder )
35+
#endif
36+
#if MIN_VERSION_Cabal(3,8,1)
37+
import Distribution.Simple.PackageDescription ( readGenericPackageDescription )
38+
#else
39+
-- Avoid confusion with Cabal-syntax module of same name
40+
import "Cabal" Distribution.PackageDescription.Parsec
41+
( readGenericPackageDescription )
42+
#endif
43+
import Distribution.Simple.Utils
44+
( createDirectoryIfMissingVerbose, findPackageDesc )
45+
#if MIN_VERSION_Cabal(3,8,1)
46+
import Distribution.Types.GenericPackageDescription
47+
( GenericPackageDescription (..) )
48+
#else
49+
-- Avoid confusion with Cabal-syntax module of same name
50+
import "Cabal" Distribution.Types.GenericPackageDescription
51+
( GenericPackageDescription (..) )
3152
#endif
53+
-- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
54+
#if MIN_VERSION_Cabal(3,11,0)
55+
import Distribution.Types.ComponentLocalBuildInfo ( ComponentLocalBuildInfo )
56+
import Distribution.Types.LocalBuildInfo ( LocalBuildInfo )
57+
import Distribution.Types.PackageDescription ( PackageDescription )
58+
import Distribution.Verbosity ( Verbosity )
3259
#endif
60+
import Distribution.Verbosity ( flagToVerbosity )
61+
import Main
3362
import System.Environment ( getArgs )
3463

3564
mainOverride :: IO ()
3665
mainOverride = do
37-
args <- getArgs
38-
if "repl" `elem` args && "stack-initial-build-steps" `elem` args
39-
then do
40-
defaultMainWithHooks simpleUserHooks
41-
{ preRepl = \_ _ -> pure emptyHookedBuildInfo
42-
, replHook = stackReplHook
43-
, postRepl = \_ _ _ _ -> pure ()
44-
}
45-
else main
66+
args <- getArgs
67+
case args of
68+
[arg1, arg2, "repl", "stack-initial-build-steps"] -> stackReplHook arg1 arg2
69+
_ -> main
4670

47-
stackReplHook :: PackageDescription -> LocalBuildInfo -> UserHooks -> ReplFlags -> [String] -> IO ()
48-
stackReplHook pkg_descr lbi hooks flags args = do
49-
let distPref = fromFlag (replDistPref flags)
50-
verbosity = fromFlag (replVerbosity flags)
51-
case args of
52-
("stack-initial-build-steps":rest)
53-
| null rest -> initialBuildSteps distPref pkg_descr lbi verbosity
54-
| otherwise ->
55-
fail "Misuse of running Setup.hs with stack-initial-build-steps, expected no arguments"
56-
_ -> replHook simpleUserHooks pkg_descr lbi hooks flags args
71+
-- | The name of the function is a mismomer, but is kept for historical reasons.
72+
-- This function relies on Stack calling the 'setup' executable with:
73+
--
74+
-- --verbose=<Cabal_verbosity>
75+
-- --builddir=<path_to_dist_prefix>
76+
-- repl
77+
-- stack-initial-build-steps
78+
stackReplHook :: String -> String -> IO ()
79+
stackReplHook arg1 arg2 = do
80+
let mRawVerbosity = stripPrefix "--verbose=" arg1
81+
mRawBuildDir = stripPrefix "--builddir=" arg2
82+
case (mRawVerbosity, mRawBuildDir) of
83+
(Nothing, _) -> fail $
84+
"Misuse of running Setup.hs with stack-initial-build-steps, expected " <>
85+
"first argument to start --verbose="
86+
(_, Nothing) -> fail $
87+
"Misuse of running Setup.hs with stack-initial-build-steps, expected" <>
88+
"second argument to start --builddir="
89+
(Just rawVerbosity, Just rawBuildDir) -> do
90+
let eVerbosity = runReadE flagToVerbosity rawVerbosity
91+
case eVerbosity of
92+
Left msg1 -> fail $
93+
"Unexpected happened running Setup.hs with " <>
94+
"stack-initial-build-steps, expected to parse Cabal verbosity: " <>
95+
msg1
96+
Right verbosity -> do
97+
eFp <- findPackageDesc ""
98+
case eFp of
99+
Left err -> fail $
100+
"Unexpected happened running Setup.hs with " <>
101+
"stack-initial-build-steps, expected to find a Cabal file: " <>
102+
msg2
103+
where
104+
#if MIN_VERSION_Cabal(3,11,0)
105+
-- The type of findPackageDesc changed in Cabal-3.11.0.0.
106+
msg2 = exceptionMessage err
107+
#else
108+
msg2 = err
109+
#endif
110+
Right fp -> do
111+
gpd <- readGenericPackageDescription verbosity fp
112+
let pd = packageDescription gpd
113+
lbi <- getPersistBuildConfig rawBuildDir
114+
initialBuildSteps rawBuildDir pd lbi verbosity
57115

58116
-- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
59-
#if defined(MIN_VERSION_Cabal)
117+
-- Based on the functions of the same name provided by Cabal-3.10.3.0.
60118
#if MIN_VERSION_Cabal(3,11,0)
61119
-- | Runs 'componentInitialBuildSteps' on every configured component.
62120
initialBuildSteps ::
@@ -66,8 +124,8 @@ initialBuildSteps ::
66124
-> Verbosity -- ^The verbosity to use
67125
-> IO ()
68126
initialBuildSteps distPref pkg_descr lbi verbosity =
69-
withAllComponentsInBuildOrder pkg_descr lbi $ \_comp clbi ->
70-
componentInitialBuildSteps distPref pkg_descr lbi clbi verbosity
127+
withAllComponentsInBuildOrder pkg_descr lbi $ \_comp clbi ->
128+
componentInitialBuildSteps distPref pkg_descr lbi clbi verbosity
71129

72130
-- | Creates the autogenerated files for a particular configured component.
73131
componentInitialBuildSteps ::
@@ -79,6 +137,8 @@ componentInitialBuildSteps ::
79137
-> IO ()
80138
componentInitialBuildSteps _distPref pkg_descr lbi clbi verbosity = do
81139
createDirectoryIfMissingVerbose verbosity True (componentBuildDir lbi clbi)
140+
-- Cabal-3.10.3.0 used writeAutogenFiles, that generated and wrote out the
141+
-- Paths_<pkg>.hs, PackageInfo_<pkg>.hs, and cabal_macros.h files. This
142+
-- appears to be the equivalent function for Cabal-3.11.0.0.
82143
writeBuiltinAutogenFiles verbosity pkg_descr lbi clbi
83144
#endif
84-
#endif

0 commit comments

Comments
 (0)