11{-# LANGUAGE CPP #-}
22{-# LANGUAGE PackageImports #-}
3+
34module StackSetupShim where
4- import Main
5- #if defined(MIN_VERSION_Cabal)
5+
6+ -- | Stack no longer supports Cabal < 2.2 and, consequently, GHC versions before
7+ -- GHC 8.4 or base < 4.11.0.0. Consequently, we do not need to test for the
8+ -- existence of the MIN_VERSION_Cabal macro (provided from GHC 8.0).
9+
10+ import Data.List ( stripPrefix )
11+ import Distribution.ReadE ( ReadE (.. ) )
12+ import Distribution.Simple.Configure ( getPersistBuildConfig )
13+ -- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
14+ #if MIN_VERSION_Cabal(3,11,0)
15+ import Distribution.Simple.Build ( writeBuiltinAutogenFiles )
16+ #else
17+ import Distribution.Simple.Build ( initialBuildSteps )
18+ #endif
19+ #if MIN_VERSION_Cabal(3,11,0)
20+ import Distribution.Simple.Errors ( exceptionMessage )
21+ #endif
22+ -- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
23+ #if MIN_VERSION_Cabal(3,11,0)
24+ import Distribution.Simple.LocalBuildInfo
25+ ( componentBuildDir , withAllComponentsInBuildOrder )
26+ #endif
627#if MIN_VERSION_Cabal(3,8,1)
7- import Distribution.PackageDescription
8- ( PackageDescription , emptyHookedBuildInfo )
28+ import Distribution.Simple.PackageDescription ( readGenericPackageDescription )
929#else
10- import "Cabal " Distribution.PackageDescription
11- ( PackageDescription , emptyHookedBuildInfo )
30+ -- Avoid confusion with Cabal-syntax module of same name
31+ import "Cabal " Distribution.PackageDescription.Parsec
32+ ( readGenericPackageDescription )
1233#endif
34+ import Distribution.Simple.Utils
35+ ( createDirectoryIfMissingVerbose , findPackageDesc )
36+ #if MIN_VERSION_Cabal(3,8,1)
37+ import Distribution.Types.GenericPackageDescription
38+ ( GenericPackageDescription (.. ) )
1339#else
14- import Distribution.PackageDescription
15- ( PackageDescription , emptyHookedBuildInfo )
40+ -- Avoid confusion with Cabal-syntax module of same name
41+ import "Cabal " Distribution.Types.GenericPackageDescription
42+ ( GenericPackageDescription (.. ) )
1643#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 )
2244-- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
23- #if defined(MIN_VERSION_Cabal)
2445#if MIN_VERSION_Cabal(3,11,0)
25- import Distribution.Simple.LocalBuildInfo
26- ( ComponentLocalBuildInfo , componentBuildDir
27- , withAllComponentsInBuildOrder
28- )
29- import Distribution.Simple.Utils ( createDirectoryIfMissingVerbose )
46+ import Distribution.Types.ComponentLocalBuildInfo ( ComponentLocalBuildInfo )
47+ import Distribution.Types.LocalBuildInfo ( LocalBuildInfo )
48+ import Distribution.Types.PackageDescription ( PackageDescription )
3049import Distribution.Verbosity ( Verbosity )
3150#endif
32- #endif
51+ import Distribution.Verbosity ( flagToVerbosity )
52+ import Main
3353import System.Environment ( getArgs )
3454
3555mainOverride :: IO ()
3656mainOverride = 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
57+ args <- getArgs
58+ case args of
59+ [arg1, arg2, " repl" , " stack-initial-build-steps" ] -> stackReplHook arg1 arg2
60+ _ -> main
4661
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
62+ -- | The name of the function is a mismomer, but is kept for historical reasons.
63+ -- This function relies on Stack calling the 'setup' executable with:
64+ --
65+ -- --verbose=<Cabal_verbosity>
66+ -- --builddir=<path_to_dist_prefix>
67+ -- repl
68+ -- stack-initial-build-steps
69+ stackReplHook :: String -> String -> IO ()
70+ stackReplHook arg1 arg2 = do
71+ let mRawVerbosity = stripPrefix " --verbose=" arg1
72+ mRawBuildDir = stripPrefix " --builddir=" arg2
73+ case (mRawVerbosity, mRawBuildDir) of
74+ (Nothing , _) -> fail $
75+ " Misuse of running Setup.hs with stack-initial-build-steps, expected " <>
76+ " first argument to start --verbose="
77+ (_, Nothing ) -> fail $
78+ " Misuse of running Setup.hs with stack-initial-build-steps, expected" <>
79+ " second argument to start --builddir="
80+ (Just rawVerbosity, Just rawBuildDir) -> do
81+ let eVerbosity = runReadE flagToVerbosity rawVerbosity
82+ case eVerbosity of
83+ Left msg1 -> fail $
84+ " Unexpected happened running Setup.hs with " <>
85+ " stack-initial-build-steps, expected to parse Cabal verbosity: " <>
86+ msg1
87+ Right verbosity -> do
88+ eFp <- findPackageDesc " "
89+ case eFp of
90+ Left err -> fail $
91+ " Unexpected happened running Setup.hs with " <>
92+ " stack-initial-build-steps, expected to find a Cabal file: " <>
93+ msg2
94+ where
95+ #if MIN_VERSION_Cabal(3,11,0)
96+ -- The type of findPackageDesc changed in Cabal-3.11.0.0.
97+ msg2 = exceptionMessage err
98+ #else
99+ msg2 = err
100+ #endif
101+ Right fp -> do
102+ gpd <- readGenericPackageDescription verbosity fp
103+ let pd = packageDescription gpd
104+ lbi <- getPersistBuildConfig rawBuildDir
105+ initialBuildSteps rawBuildDir pd lbi verbosity
57106
58107-- | Temporary, can be removed if initialBuildSteps restored to Cabal's API.
59- #if defined(MIN_VERSION_Cabal)
108+ -- Based on the functions of the same name provided by Cabal-3.10.3.0.
60109#if MIN_VERSION_Cabal(3,11,0)
61110-- | Runs 'componentInitialBuildSteps' on every configured component.
62111initialBuildSteps ::
@@ -66,8 +115,8 @@ initialBuildSteps ::
66115 -> Verbosity -- ^ The verbosity to use
67116 -> IO ()
68117initialBuildSteps distPref pkg_descr lbi verbosity =
69- withAllComponentsInBuildOrder pkg_descr lbi $ \ _comp clbi ->
70- componentInitialBuildSteps distPref pkg_descr lbi clbi verbosity
118+ withAllComponentsInBuildOrder pkg_descr lbi $ \ _comp clbi ->
119+ componentInitialBuildSteps distPref pkg_descr lbi clbi verbosity
71120
72121-- | Creates the autogenerated files for a particular configured component.
73122componentInitialBuildSteps ::
@@ -79,6 +128,8 @@ componentInitialBuildSteps ::
79128 -> IO ()
80129componentInitialBuildSteps _distPref pkg_descr lbi clbi verbosity = do
81130 createDirectoryIfMissingVerbose verbosity True (componentBuildDir lbi clbi)
131+ -- Cabal-3.10.3.0 used writeAutogenFiles, that generated and wrote out the
132+ -- Paths_<pkg>.hs, PackageInfo_<pkg>.hs, and cabal_macros.h files. This
133+ -- appears to be the equivalent function for Cabal-3.11.0.0.
82134 writeBuiltinAutogenFiles verbosity pkg_descr lbi clbi
83135#endif
84- #endif
0 commit comments