@@ -17,19 +17,20 @@ import Distribution.Helper (Package, projectPackages, pUnits,
17
17
import Distribution.Helper.Discover (findProjects , getDefaultDistDir )
18
18
import Data.Char (toLower )
19
19
import Data.Function ((&) )
20
- import Data.List (isPrefixOf , isInfixOf )
20
+ import Data.List (isPrefixOf , isInfixOf , sortOn , find )
21
21
import qualified Data.List.NonEmpty as NonEmpty
22
22
import Data.List.NonEmpty (NonEmpty )
23
23
import qualified Data.Map as M
24
- import Data.List (sortOn , find )
25
24
import Data.Maybe (listToMaybe , mapMaybe , isJust )
26
25
import Data.Ord (Down (.. ))
27
26
import Data.String (IsString (.. ))
27
+ import qualified Data.Text as T
28
28
import Data.Foldable (toList )
29
- import Control.Exception (IOException , try )
29
+ import Control.Exception (IOException , try , catch )
30
30
import System.FilePath
31
31
import System.Directory (getCurrentDirectory , canonicalizePath , findExecutable )
32
32
import System.Exit
33
+ import System.Process (readCreateProcess , shell )
33
34
34
35
-- | Find the cradle that the given File belongs to.
35
36
--
@@ -57,6 +58,54 @@ isStackCradle = (`elem` ["stack", "Cabal-Helper-Stack", "Cabal-Helper-Stack-None
57
58
. BIOS. actionName
58
59
. BIOS. cradleOptsProg
59
60
61
+ -- | Check if the given cradle is a cabal cradle.
62
+ -- This might be used to determine the GHC version to use on the project.
63
+ -- If it is a stack-cradle, we have to use `stack path --compiler-exe`
64
+ -- otherwise we may ask `ghc` directly what version it is.
65
+ isCabalCradle :: Cradle -> Bool
66
+ isCabalCradle =
67
+ (`elem`
68
+ [" cabal"
69
+ , " Cabal-Helper-Cabal-V1"
70
+ , " Cabal-Helper-Cabal-V2"
71
+ , " Cabal-Helper-Cabal-V1-Dir"
72
+ , " Cabal-Helper-Cabal-V2-Dir"
73
+ , " Cabal-Helper-Cabal-None"
74
+ ]
75
+ )
76
+ . BIOS. actionName
77
+ . BIOS. cradleOptsProg
78
+
79
+
80
+ getProjectGhcPath :: Cradle -> IO (Maybe FilePath )
81
+ getProjectGhcPath crdl = do
82
+ isStackInstalled <- isJust <$> findExecutable " stack"
83
+ isCabalInstalled <- isJust <$> findExecutable " cabal"
84
+ if isStackCradle crdl && isStackInstalled
85
+ then
86
+ catch (Just <$> tryCommand " stack path --compiler-exe" ) $ \ (_ :: IOException ) ->
87
+ return Nothing
88
+ else if isCabalCradle crdl && isCabalInstalled then do
89
+ Just ghcCabalVersion <- catch (Just <$> tryCommand " cabal v2-exec ghc -- --numeric-version" ) $ \ (_ :: IOException ) ->
90
+ return Nothing
91
+ findExecutable (" ghc-" ++ ghcCabalVersion)
92
+ else
93
+ findExecutable " ghc"
94
+
95
+ tryCommand :: String -> IO String
96
+ tryCommand cmd =
97
+ T. unpack . T. strip . T. pack <$> readCreateProcess (shell cmd) " "
98
+
99
+ getProjectGhcLibDir :: Cradle -> IO (Maybe FilePath )
100
+ getProjectGhcLibDir crdl = do
101
+ mGhcPath <- getProjectGhcPath crdl
102
+ case mGhcPath of
103
+ Nothing -> return Nothing
104
+ Just ghcPath -> catch (Just <$> tryCommand (ghcPath ++ " --print-libdir" )) $ \ (_ :: IOException ) -> return Nothing
105
+
106
+ -- ---------------------------------------------------------------------
107
+
108
+
60
109
{- | Finds a Cabal v2-project, Cabal v1-project or a Stack project
61
110
relative to the given FilePath.
62
111
Cabal v2-project and Stack have priority over Cabal v1-project.
0 commit comments