1
1
{-# LANGUAGE CPP #-}
2
2
{-# LANGUAGE PackageImports #-}
3
3
module StackSetupShim where
4
+
4
5
import Main
5
- #if defined(MIN_VERSION_Cabal)
6
- #if MIN_VERSION_Cabal(3,8,1)
7
- import Distribution.PackageDescription
8
- ( PackageDescription , emptyHookedBuildInfo )
9
- #else
10
- import "Cabal " Distribution.PackageDescription
11
- ( PackageDescription , emptyHookedBuildInfo )
12
- #endif
13
- #else
14
- import Distribution.PackageDescription
15
- ( PackageDescription , emptyHookedBuildInfo )
16
- #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
- -- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
23
- #if defined(MIN_VERSION_Cabal)
6
+ import System.Environment ( getArgs )
7
+
8
+ -- | We do not need to test for the existence of the MIN_VERSION_Cabal macro, as
9
+ -- Stack no longer supports GHC versions before GHC 8.0.
24
10
#if MIN_VERSION_Cabal(3,11,0)
11
+
12
+ import Data.List ( stripPrefix )
13
+ import Distribution.Parsec ( eitherParsec )
14
+ import Distribution.Simple.Configure ( getPersistBuildConfig )
15
+ import Distribution.Simple.Build ( writeBuiltinAutogenFiles )
16
+ import Distribution.Simple.Errors ( exceptionMessage )
25
17
import Distribution.Simple.LocalBuildInfo
26
- ( ComponentLocalBuildInfo , componentBuildDir
18
+ ( ComponentLocalBuildInfo , LocalBuildInfo , componentBuildDir
27
19
, withAllComponentsInBuildOrder
28
20
)
29
- import Distribution.Simple.Utils ( createDirectoryIfMissingVerbose )
21
+ import Distribution.Simple.PackageDescription ( readGenericPackageDescription )
22
+ import Distribution.Simple.Utils
23
+ ( createDirectoryIfMissingVerbose , findPackageDesc )
24
+ import Distribution.Types.GenericPackageDescription
25
+ ( GenericPackageDescription (.. ) )
26
+ import Distribution.Types.PackageDescription ( PackageDescription )
30
27
import Distribution.Verbosity ( Verbosity )
31
- #endif
32
- #endif
33
- import System.Environment ( getArgs )
34
28
35
29
mainOverride :: IO ()
36
30
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
31
+ args <- getArgs
32
+ case args of
33
+ arg1: arg2: " repl" : " stack-initial-build-steps" : [] ->
34
+ stackReplHook arg1 arg2
35
+ _ -> main
46
36
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
37
+ -- | The name of the function is a mismomer, but is kept for historical reasons.
38
+ stackReplHook :: String -> String -> IO ()
39
+ stackReplHook arg1 arg2 = do
40
+ let mRawVerbosity = stripPrefix " --verbose=" arg1
41
+ mRawBuildDir = stripPrefix " --builddir=" arg2
42
+ case (mRawVerbosity, mRawBuildDir) of
43
+ (Nothing , _) -> fail $
44
+ " Misuse of running Setup.hs with stack-initial-build-steps, expected " <>
45
+ " first argument to start --verbose="
46
+ (_, Nothing ) -> fail $
47
+ " Misuse of running Setup.hs with stack-initial-build-steps, expected" <>
48
+ " second argument to start --builddir="
49
+ (Just rawVerbosity, Just rawBuildDir) -> case eitherParsec rawVerbosity of
50
+ Left msg1 -> fail $
51
+ " Unexpected happened running Setup.hs with " <>
52
+ " stack-initial-build-steps, expected to parse Cabal verbosity: " <> msg1
53
+ Right verbosity -> do
54
+ eFp <- findPackageDesc " "
55
+ case eFp of
56
+ Left err -> fail $
57
+ " Unexpected happened running Setup.hs with " <>
58
+ " stack-initial-build-steps, expected to find a Cabal file: " <>
59
+ exceptionMessage err
60
+ Right fp -> do
61
+ gpd <- readGenericPackageDescription verbosity fp
62
+ let pd = packageDescription gpd
63
+ lbi <- getPersistBuildConfig rawBuildDir
64
+ initialBuildSteps rawBuildDir pd lbi verbosity
57
65
58
66
-- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
59
- #if defined(MIN_VERSION_Cabal)
60
- #if MIN_VERSION_Cabal(3,11,0)
67
+
61
68
-- | Runs 'componentInitialBuildSteps' on every configured component.
62
69
initialBuildSteps ::
63
70
FilePath -- ^ "dist" prefix
@@ -80,5 +87,50 @@ componentInitialBuildSteps ::
80
87
componentInitialBuildSteps _distPref pkg_descr lbi clbi verbosity = do
81
88
createDirectoryIfMissingVerbose verbosity True (componentBuildDir lbi clbi)
82
89
writeBuiltinAutogenFiles verbosity pkg_descr lbi clbi
90
+
91
+ #else
92
+
93
+ #if MIN_VERSION_Cabal(3,8,1)
94
+ import Distribution.PackageDescription
95
+ ( PackageDescription , emptyHookedBuildInfo )
96
+ #else
97
+ import "Cabal " Distribution.PackageDescription
98
+ ( PackageDescription , emptyHookedBuildInfo )
83
99
#endif
100
+ import Distribution.Simple
101
+ import Distribution.Simple.Build
102
+ import Distribution.Simple.Setup
103
+ ( ReplFlags , fromFlag , replDistPref , replVerbosity )
104
+ import Distribution.Simple.LocalBuildInfo ( LocalBuildInfo )
105
+
106
+ mainOverride :: IO ()
107
+ mainOverride = do
108
+ args <- getArgs
109
+ if " repl" `elem` args && " stack-initial-build-steps" `elem` args
110
+ then do
111
+ defaultMainWithHooks simpleUserHooks
112
+ { preRepl = \ _ _ -> pure emptyHookedBuildInfo
113
+ , replHook = stackReplHook
114
+ , postRepl = \ _ _ _ _ -> pure ()
115
+ }
116
+ else main
117
+
118
+ stackReplHook ::
119
+ PackageDescription
120
+ -> LocalBuildInfo
121
+ -> UserHooks
122
+ -> ReplFlags
123
+ -> [String ]
124
+ -> IO ()
125
+ stackReplHook pkg_descr lbi hooks flags args = do
126
+ let distPref = fromFlag (replDistPref flags)
127
+ verbosity = fromFlag (replVerbosity flags)
128
+ case args of
129
+ (" stack-initial-build-steps" : rest)
130
+ | null rest -> initialBuildSteps distPref pkg_descr lbi verbosity
131
+ | otherwise -> fail $
132
+ " Misuse of running Setup.hs with stack-initial-build-steps, " <>
133
+ " expected no arguments"
134
+ _ -> replHook simpleUserHooks pkg_descr lbi hooks flags args
135
+
84
136
#endif
0 commit comments