Skip to content

Commit d68cb50

Browse files
committed
configureCompiler: separate compiler vs ProgramDb
This commit splits up the logic in configureCompiler into two parts: 1. Configuring the compiler proper, e.g. finding the location and version of GHC. 2. Creating a program database of attendant programs such as ghc-pkg, haddock, and build tools such as ar, ld. This is done using information about the compiler, such as its location on the filesystem and toolchain information from its settings file.
1 parent ffaa46b commit d68cb50

File tree

5 files changed

+243
-122
lines changed

5 files changed

+243
-122
lines changed

Cabal/src/Distribution/Simple/Configure.hs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ module Distribution.Simple.Configure
4848
, getInstalledPackagesMonitorFiles
4949
, getInstalledPackagesById
5050
, getPackageDBContents
51+
, configCompiler
5152
, configCompilerEx
5253
, configCompilerAuxEx
54+
, configCompilerProgDb
5355
, computeEffectiveProfiling
5456
, ccLdOptionsBuildInfo
5557
, checkForeignDeps
@@ -2484,10 +2486,12 @@ configCompilerAuxEx cfg = do
24842486
programDb
24852487
verbosity
24862488

2489+
-- | Configure the compiler and associated programs such as @hc-pkg@, @haddock@
2490+
-- and toolchain program such as @ar@, @ld@.
24872491
configCompilerEx
24882492
:: Maybe CompilerFlavor
2489-
-> Maybe FilePath
2490-
-> Maybe FilePath
2493+
-> Maybe FilePath -- ^ user-specified @hc@ path (optional)
2494+
-> Maybe FilePath -- ^ user-specified @hc-pkg@ path (optional)
24912495
-> ProgramDb
24922496
-> Verbosity
24932497
-> IO (Compiler, Platform, ProgramDb)
@@ -2496,10 +2500,45 @@ configCompilerEx (Just hcFlavor) hcPath hcPkg progdb verbosity = do
24962500
(comp, maybePlatform, programDb) <- case hcFlavor of
24972501
GHC -> GHC.configure verbosity hcPath hcPkg progdb
24982502
GHCJS -> GHCJS.configure verbosity hcPath hcPkg progdb
2499-
UHC -> UHC.configure verbosity hcPath hcPkg progdb
2503+
UHC -> UHC.configure verbosity hcPath progdb
25002504
_ -> dieWithException verbosity UnknownCompilerException
25012505
return (comp, fromMaybe buildPlatform maybePlatform, programDb)
25022506

2507+
-- | Configure the compiler ONLY.
2508+
configCompiler
2509+
:: Maybe CompilerFlavor
2510+
-> Maybe FilePath -- ^ user-specified @hc@ path (optional)
2511+
-> ProgramDb
2512+
-> Verbosity
2513+
-> IO (Compiler, Platform, ProgramDb)
2514+
configCompiler mbFlavor hcPath progdb verbosity = do
2515+
(comp, maybePlatform, programDb) <-
2516+
case mbFlavor of
2517+
Nothing -> dieWithException verbosity UnknownCompilerException
2518+
Just hcFlavor ->
2519+
case hcFlavor of
2520+
GHC -> GHC.configureCompiler verbosity hcPath progdb
2521+
GHCJS -> GHCJS.configureCompiler verbosity hcPath progdb
2522+
UHC -> UHC.configure verbosity hcPath progdb
2523+
_ -> dieWithException verbosity UnknownCompilerException
2524+
return (comp, fromMaybe buildPlatform maybePlatform, programDb)
2525+
2526+
-- | Configure programs associated to the compiler, such as @hc-pkg@, @haddock@
2527+
-- and toolchain program such as @ar@, @ld@.
2528+
configCompilerProgDb
2529+
:: Verbosity
2530+
-> Compiler
2531+
-> ProgramDb
2532+
-- ^ program database containing the compiler
2533+
-> Maybe FilePath
2534+
-- ^ user-specified @hc-pkg@ path (optional)
2535+
-> IO ProgramDb
2536+
configCompilerProgDb verbosity comp hcProgDb hcPkgPath = do
2537+
case compilerFlavor comp of
2538+
GHC -> GHC.compilerProgramDb verbosity comp hcProgDb hcPkgPath
2539+
GHCJS -> GHCJS.compilerProgramDb verbosity comp hcProgDb hcPkgPath
2540+
_ -> return hcProgDb
2541+
25032542
-- -----------------------------------------------------------------------------
25042543
-- Testing C lib and header dependencies
25052544

Cabal/src/Distribution/Simple/GHC.hs

Lines changed: 92 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
module Distribution.Simple.GHC
4242
( getGhcInfo
4343
, configure
44+
, configureCompiler
45+
, compilerProgramDb
4446
, getInstalledPackages
4547
, getInstalledPackagesMonitorFiles
4648
, getPackageDBContents
@@ -124,6 +126,7 @@ import Distribution.Utils.Path
124126
import Distribution.Verbosity
125127
import Distribution.Version
126128
import Language.Haskell.Extension
129+
import Data.Maybe (fromJust)
127130
import System.FilePath
128131
( isRelative
129132
, takeDirectory
@@ -153,20 +156,33 @@ import Distribution.Simple.Setup.Build
153156
-- -----------------------------------------------------------------------------
154157
-- Configuring
155158

159+
-- | Configure GHC, and then auxiliary programs such as @ghc-pkg@, @haddock@
160+
-- as well as toolchain programs such as @ar@, @ld.
156161
configure
157162
:: Verbosity
158-
-> Maybe FilePath
159-
-> Maybe FilePath
163+
-> Maybe FilePath -- ^ user-specified @ghc@ path (optional)
164+
-> Maybe FilePath -- ^ user-specified @ghc-pkg@ path (optional)
160165
-> ProgramDb
161166
-> IO (Compiler, Maybe Platform, ProgramDb)
162167
configure verbosity hcPath hcPkgPath conf0 = do
168+
(comp, compPlatform, progdb1) <- configureCompiler verbosity hcPath conf0
169+
compProgDb <- compilerProgramDb verbosity comp progdb1 hcPkgPath
170+
return (comp, compPlatform, compProgDb)
171+
172+
-- | Configure GHC.
173+
configureCompiler
174+
:: Verbosity
175+
-> Maybe FilePath -- ^ user-specified @ghc@ path (optional)
176+
-> ProgramDb
177+
-> IO (Compiler, Maybe Platform, ProgramDb)
178+
configureCompiler verbosity hcPath conf0 = do
179+
163180
(ghcProg, ghcVersion, progdb1) <-
164181
requireProgramVersion
165182
verbosity
166183
ghcProgram
167184
(orLaterVersion (mkVersion [7, 0, 1]))
168185
(userMaybeSpecifyPath "ghc" hcPath conf0)
169-
let implInfo = ghcVersionImplInfo ghcVersion
170186

171187
-- Cabal currently supports GHC less than `maxGhcVersion`
172188
let maxGhcVersion = mkVersion [9, 14]
@@ -182,48 +198,12 @@ configure verbosity hcPath hcPkgPath conf0 = do
182198
++ " is version "
183199
++ prettyShow ghcVersion
184200

185-
-- This is slightly tricky, we have to configure ghc first, then we use the
186-
-- location of ghc to help find ghc-pkg in the case that the user did not
187-
-- specify the location of ghc-pkg directly:
188-
(ghcPkgProg, ghcPkgVersion, progdb2) <-
189-
requireProgramVersion
190-
verbosity
191-
ghcPkgProgram
192-
{ programFindLocation = guessGhcPkgFromGhcPath ghcProg
193-
}
194-
anyVersion
195-
(userMaybeSpecifyPath "ghc-pkg" hcPkgPath progdb1)
196-
197-
when (ghcVersion /= ghcPkgVersion) $
198-
dieWithException verbosity $
199-
VersionMismatchGHC (programPath ghcProg) ghcVersion (programPath ghcPkgProg) ghcPkgVersion
200-
-- Likewise we try to find the matching hsc2hs and haddock programs.
201-
let hsc2hsProgram' =
202-
hsc2hsProgram
203-
{ programFindLocation = guessHsc2hsFromGhcPath ghcProg
204-
}
205-
haddockProgram' =
206-
haddockProgram
207-
{ programFindLocation = guessHaddockFromGhcPath ghcProg
208-
}
209-
hpcProgram' =
210-
hpcProgram
211-
{ programFindLocation = guessHpcFromGhcPath ghcProg
212-
}
213-
runghcProgram' =
214-
runghcProgram
215-
{ programFindLocation = guessRunghcFromGhcPath ghcProg
216-
}
217-
progdb3 =
218-
addKnownProgram haddockProgram' $
219-
addKnownProgram hsc2hsProgram' $
220-
addKnownProgram hpcProgram' $
221-
addKnownProgram runghcProgram' progdb2
222-
201+
let implInfo = ghcVersionImplInfo ghcVersion
223202
languages <- Internal.getLanguages verbosity implInfo ghcProg
224203
extensions0 <- Internal.getExtensions verbosity implInfo ghcProg
225204

226205
ghcInfo <- Internal.getGhcInfo verbosity implInfo ghcProg
206+
227207
let ghcInfoMap = Map.fromList ghcInfo
228208
filterJS = if ghcVersion < mkVersion [9, 8] then filterExt JavaScriptFFI else id
229209
extensions =
@@ -249,7 +229,10 @@ configure verbosity hcPath hcPkgPath conf0 = do
249229
compilerId = CompilerId GHC ghcVersion
250230

251231
compilerAbiTag :: AbiTag
252-
compilerAbiTag = maybe NoAbiTag AbiTag (Map.lookup "Project Unit Id" ghcInfoMap >>= stripPrefix (prettyShow compilerId <> "-"))
232+
compilerAbiTag =
233+
maybe NoAbiTag AbiTag
234+
(Map.lookup "Project Unit Id" ghcInfoMap
235+
>>= stripPrefix (prettyShow compilerId <> "-"))
253236

254237
let comp =
255238
Compiler
@@ -261,9 +244,73 @@ configure verbosity hcPath hcPkgPath conf0 = do
261244
, compilerProperties = ghcInfoMap
262245
}
263246
compPlatform = Internal.targetPlatform ghcInfo
264-
-- configure gcc and ld
265-
progdb4 = Internal.configureToolchain implInfo ghcProg ghcInfoMap progdb3
266-
return (comp, compPlatform, progdb4)
247+
return (comp, compPlatform, progdb1)
248+
249+
-- | Given a configured @ghc@ program, configure auxiliary programs such
250+
-- as @ghc-pkg@ or @haddock@, as well as toolchain programs such as @ar@, @ld@,
251+
-- based on:
252+
--
253+
-- - the location of the @ghc@ executable,
254+
-- - toolchain information in the GHC settings file.
255+
compilerProgramDb
256+
:: Verbosity
257+
-> Compiler
258+
-> ProgramDb
259+
-> Maybe FilePath -- ^ user-specified @ghc-pkg@ path (optional)
260+
-> IO ProgramDb
261+
compilerProgramDb verbosity comp progdb1 hcPkgPath = do
262+
263+
let
264+
ghcProg = fromJust $ lookupProgram ghcProgram progdb1
265+
ghcVersion = compilerVersion comp
266+
267+
-- This is slightly tricky, we have to configure ghc first, then we use the
268+
-- location of ghc to help find ghc-pkg in the case that the user did not
269+
-- specify the location of ghc-pkg directly:
270+
(ghcPkgProg, ghcPkgVersion, progdb2) <-
271+
requireProgramVersion
272+
verbosity
273+
ghcPkgProgram
274+
{ programFindLocation = guessGhcPkgFromGhcPath ghcProg
275+
}
276+
anyVersion
277+
(userMaybeSpecifyPath "ghc-pkg" hcPkgPath progdb1)
278+
279+
when (ghcVersion /= ghcPkgVersion) $
280+
dieWithException verbosity $
281+
VersionMismatchGHC (programPath ghcProg) ghcVersion (programPath ghcPkgProg) ghcPkgVersion
282+
-- Likewise we try to find the matching hsc2hs and haddock programs.
283+
let hsc2hsProgram' =
284+
hsc2hsProgram
285+
{ programFindLocation = guessHsc2hsFromGhcPath ghcProg
286+
}
287+
haddockProgram' =
288+
haddockProgram
289+
{ programFindLocation = guessHaddockFromGhcPath ghcProg
290+
}
291+
hpcProgram' =
292+
hpcProgram
293+
{ programFindLocation = guessHpcFromGhcPath ghcProg
294+
}
295+
runghcProgram' =
296+
runghcProgram
297+
{ programFindLocation = guessRunghcFromGhcPath ghcProg
298+
}
299+
progdb3 =
300+
addKnownProgram haddockProgram' $
301+
addKnownProgram hsc2hsProgram' $
302+
addKnownProgram hpcProgram' $
303+
addKnownProgram runghcProgram' progdb2
304+
305+
-- configure gcc, ld, ar etc... based on the paths stored
306+
-- in the GHC settings file
307+
progdb4 =
308+
Internal.configureToolchain
309+
(ghcVersionImplInfo ghcVersion)
310+
ghcProg
311+
(compilerProperties comp)
312+
progdb3
313+
return progdb4
267314

268315
-- | Given something like /usr/local/bin/ghc-6.6.1(.exe) we try and find
269316
-- the corresponding tool; e.g. if the tool is ghc-pkg, we try looking

Cabal/src/Distribution/Simple/GHCJS.hs

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
module Distribution.Simple.GHCJS
66
( getGhcInfo
77
, configure
8+
, configureCompiler
9+
, compilerProgramDb
810
, getInstalledPackages
911
, getInstalledPackagesMonitorFiles
1012
, getPackageDBContents
@@ -86,6 +88,7 @@ import Distribution.Version
8688
import Control.Arrow ((***))
8789
import Control.Monad (msum)
8890
import Data.Char (isLower)
91+
import Data.Maybe (fromJust)
8992
import qualified Data.Map as Map
9093
import System.Directory
9194
( canonicalizePath
@@ -106,13 +109,26 @@ import qualified System.Info
106109
-- -----------------------------------------------------------------------------
107110
-- Configuring
108111

112+
-- | Configure GHCJS, and then auxiliary programs such as @ghc-pkg@, @haddock@
113+
-- as well as toolchain programs such as @ar@, @ld.
109114
configure
110115
:: Verbosity
111-
-> Maybe FilePath
112-
-> Maybe FilePath
116+
-> Maybe FilePath -- ^ user-specified @ghcjs@ path (optional)
117+
-> Maybe FilePath -- ^ user-specified @ghcjs-pkg@ path (optional)
113118
-> ProgramDb
114119
-> IO (Compiler, Maybe Platform, ProgramDb)
115120
configure verbosity hcPath hcPkgPath conf0 = do
121+
(comp, compPlatform, progdb1) <- configureCompiler verbosity hcPath conf0
122+
compProgDb <- compilerProgramDb verbosity comp progdb1 hcPkgPath
123+
return (comp, compPlatform, compProgDb)
124+
125+
-- | Configure GHCJS.
126+
configureCompiler
127+
:: Verbosity
128+
-> Maybe FilePath -- ^ user-specified @ghc@ path (optional)
129+
-> ProgramDb
130+
-> IO (Compiler, Maybe Platform, ProgramDb)
131+
configureCompiler verbosity hcPath conf0 = do
116132
(ghcjsProg, ghcjsVersion, progdb1) <-
117133
requireProgramVersion
118134
verbosity
@@ -133,6 +149,43 @@ configure verbosity hcPath hcPkgPath conf0 = do
133149

134150
let implInfo = ghcjsVersionImplInfo ghcjsVersion ghcjsGhcVersion
135151

152+
languages <- Internal.getLanguages verbosity implInfo ghcjsProg
153+
extensions <- Internal.getExtensions verbosity implInfo ghcjsProg
154+
155+
ghcjsInfo <- Internal.getGhcInfo verbosity implInfo ghcjsProg
156+
let ghcInfoMap = Map.fromList ghcjsInfo
157+
158+
let comp =
159+
Compiler
160+
{ compilerId = CompilerId GHCJS ghcjsVersion
161+
, compilerAbiTag =
162+
AbiTag $
163+
"ghc" ++ intercalate "_" (map show . versionNumbers $ ghcjsGhcVersion)
164+
, compilerCompat = [CompilerId GHC ghcjsGhcVersion]
165+
, compilerLanguages = languages
166+
, compilerExtensions = extensions
167+
, compilerProperties = ghcInfoMap
168+
}
169+
compPlatform = Internal.targetPlatform ghcjsInfo
170+
return (comp, compPlatform, progdb1)
171+
172+
-- | Given a configured @ghcjs@ program, configure auxiliary programs such
173+
-- as @ghcjs-pkg@ or @haddock@, based on the location of the @ghcjs@ executable.
174+
compilerProgramDb
175+
:: Verbosity
176+
-> Compiler
177+
-> ProgramDb
178+
-> Maybe FilePath -- ^ user-specified @ghc-pkg@ path (optional)
179+
-> IO ProgramDb
180+
compilerProgramDb verbosity comp progdb1 hcPkgPath = do
181+
182+
let
183+
ghcjsProg = fromJust $ lookupProgram ghcjsProgram progdb1
184+
ghcjsVersion = compilerVersion comp
185+
ghcjsGhcVersion = case compilerCompat comp of
186+
[CompilerId GHC ghcjsGhcVer] -> ghcjsGhcVer
187+
compat -> error $ "could not parse ghcjsGhcVersion:" ++ show compat
188+
136189
-- This is slightly tricky, we have to configure ghc first, then we use the
137190
-- location of ghc to help find ghc-pkg in the case that the user did not
138191
-- specify the location of ghc-pkg directly:
@@ -187,25 +240,7 @@ configure verbosity hcPath hcPkgPath conf0 = do
187240
addKnownProgram hpcProgram' $
188241
{- addKnownProgram runghcProgram' -} progdb2
189242

190-
languages <- Internal.getLanguages verbosity implInfo ghcjsProg
191-
extensions <- Internal.getExtensions verbosity implInfo ghcjsProg
192-
193-
ghcjsInfo <- Internal.getGhcInfo verbosity implInfo ghcjsProg
194-
let ghcInfoMap = Map.fromList ghcjsInfo
195-
196-
let comp =
197-
Compiler
198-
{ compilerId = CompilerId GHCJS ghcjsVersion
199-
, compilerAbiTag =
200-
AbiTag $
201-
"ghc" ++ intercalate "_" (map show . versionNumbers $ ghcjsGhcVersion)
202-
, compilerCompat = [CompilerId GHC ghcjsGhcVersion]
203-
, compilerLanguages = languages
204-
, compilerExtensions = extensions
205-
, compilerProperties = ghcInfoMap
206-
}
207-
compPlatform = Internal.targetPlatform ghcjsInfo
208-
return (comp, compPlatform, progdb3)
243+
return progdb3
209244

210245
guessGhcjsPkgFromGhcjsPath
211246
:: ConfiguredProgram

Cabal/src/Distribution/Simple/UHC.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ import System.FilePath (pathSeparator)
5959
configure
6060
:: Verbosity
6161
-> Maybe FilePath
62-
-> Maybe FilePath
6362
-> ProgramDb
6463
-> IO (Compiler, Maybe Platform, ProgramDb)
65-
configure verbosity hcPath _hcPkgPath progdb = do
64+
configure verbosity hcPath progdb = do
6665
(_uhcProg, uhcVersion, progdb') <-
6766
requireProgramVersion
6867
verbosity

0 commit comments

Comments
 (0)