1
1
{-# LANGUAGE CPP #-}
2
2
{-# LANGUAGE PackageImports #-}
3
+
3
4
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)
6
11
#if MIN_VERSION_Cabal(3,8,1)
7
- import Distribution.PackageDescription
8
- ( PackageDescription , emptyHookedBuildInfo )
12
+ import Distribution.Parsec ( eitherParsec )
9
13
#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 )
12
16
#endif
13
17
#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 )
16
30
#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 )
22
31
-- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
23
- #if defined(MIN_VERSION_Cabal)
24
32
#if MIN_VERSION_Cabal(3,11,0)
25
33
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 (.. ) )
31
52
#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 )
32
59
#endif
60
+ import Distribution.Verbosity ( flagToVerbosity )
61
+ import Main
33
62
import System.Environment ( getArgs )
34
63
35
64
mainOverride :: IO ()
36
65
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
46
70
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
57
115
58
116
-- | 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.
60
118
#if MIN_VERSION_Cabal(3,11,0)
61
119
-- | Runs 'componentInitialBuildSteps' on every configured component.
62
120
initialBuildSteps ::
@@ -66,8 +124,8 @@ initialBuildSteps ::
66
124
-> Verbosity -- ^ The verbosity to use
67
125
-> IO ()
68
126
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
71
129
72
130
-- | Creates the autogenerated files for a particular configured component.
73
131
componentInitialBuildSteps ::
@@ -79,6 +137,8 @@ componentInitialBuildSteps ::
79
137
-> IO ()
80
138
componentInitialBuildSteps _distPref pkg_descr lbi clbi verbosity = do
81
139
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.
82
143
writeBuiltinAutogenFiles verbosity pkg_descr lbi clbi
83
144
#endif
84
- #endif
0 commit comments