Skip to content

Commit 7a9ad97

Browse files
authored
Merge pull request #10993 from mpickering/wip/separateProgDb
configureCompiler: separate compiler vs ProgramDb
2 parents 8f5ec4e + 72e2a58 commit 7a9ad97

File tree

5 files changed

+248
-116
lines changed

5 files changed

+248
-116
lines changed

Cabal/src/Distribution/Simple/Configure.hs

Lines changed: 43 additions & 1 deletion
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,14 @@ 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
24892493
-> Maybe FilePath
2494+
-- ^ user-specified @hc@ path (optional)
24902495
-> Maybe FilePath
2496+
-- ^ user-specified @hc-pkg@ path (optional)
24912497
-> ProgramDb
24922498
-> Verbosity
24932499
-> IO (Compiler, Platform, ProgramDb)
@@ -2496,10 +2502,46 @@ configCompilerEx (Just hcFlavor) hcPath hcPkg progdb verbosity = do
24962502
(comp, maybePlatform, programDb) <- case hcFlavor of
24972503
GHC -> GHC.configure verbosity hcPath hcPkg progdb
24982504
GHCJS -> GHCJS.configure verbosity hcPath hcPkg progdb
2499-
UHC -> UHC.configure verbosity hcPath hcPkg progdb
2505+
UHC -> UHC.configure verbosity hcPath progdb
25002506
_ -> dieWithException verbosity UnknownCompilerException
25012507
return (comp, fromMaybe buildPlatform maybePlatform, programDb)
25022508

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

Cabal/src/Distribution/Simple/GHC.hs

Lines changed: 95 additions & 43 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
@@ -86,6 +88,7 @@ import Prelude ()
8688
import Control.Arrow ((***))
8789
import Control.Monad (forM_)
8890
import qualified Data.Map as Map
91+
import Data.Maybe (fromJust)
8992
import Distribution.CabalSpecVersion
9093
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
9194
import qualified Distribution.InstalledPackageInfo as InstalledPackageInfo
@@ -152,20 +155,35 @@ import Distribution.Simple.Setup.Build
152155
-- -----------------------------------------------------------------------------
153156
-- Configuring
154157

158+
-- | Configure GHC, and then auxiliary programs such as @ghc-pkg@, @haddock@
159+
-- as well as toolchain programs such as @ar@, @ld.
155160
configure
156161
:: Verbosity
157162
-> Maybe FilePath
163+
-- ^ user-specified @ghc@ path (optional)
158164
-> Maybe FilePath
165+
-- ^ user-specified @ghc-pkg@ path (optional)
159166
-> ProgramDb
160167
-> IO (Compiler, Maybe Platform, ProgramDb)
161168
configure verbosity hcPath hcPkgPath conf0 = do
169+
(comp, compPlatform, progdb1) <- configureCompiler verbosity hcPath conf0
170+
compProgDb <- compilerProgramDb verbosity comp progdb1 hcPkgPath
171+
return (comp, compPlatform, compProgDb)
172+
173+
-- | Configure GHC.
174+
configureCompiler
175+
:: Verbosity
176+
-> Maybe FilePath
177+
-- ^ user-specified @ghc@ path (optional)
178+
-> ProgramDb
179+
-> IO (Compiler, Maybe Platform, ProgramDb)
180+
configureCompiler verbosity hcPath conf0 = do
162181
(ghcProg, ghcVersion, progdb1) <-
163182
requireProgramVersion
164183
verbosity
165184
ghcProgram
166185
(orLaterVersion (mkVersion [7, 0, 1]))
167186
(userMaybeSpecifyPath "ghc" hcPath conf0)
168-
let implInfo = ghcVersionImplInfo ghcVersion
169187

170188
-- Cabal currently supports GHC less than `maxGhcVersion`
171189
let maxGhcVersion = mkVersion [9, 14]
@@ -181,48 +199,12 @@ configure verbosity hcPath hcPkgPath conf0 = do
181199
++ " is version "
182200
++ prettyShow ghcVersion
183201

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

225206
ghcInfo <- Internal.getGhcInfo verbosity implInfo ghcProg
207+
226208
let ghcInfoMap = Map.fromList ghcInfo
227209
filterJS = if ghcVersion < mkVersion [9, 8] then filterExt JavaScriptFFI else id
228210
extensions =
@@ -254,7 +236,13 @@ configure verbosity hcPath hcPkgPath conf0 = do
254236
-- So, we need to be careful to only strip the /common/ prefix.
255237
-- In this example, @AbiTag@ is "inplace".
256238
compilerAbiTag :: AbiTag
257-
compilerAbiTag = maybe NoAbiTag AbiTag (dropWhile (== '-') . stripCommonPrefix (prettyShow compilerId) <$> Map.lookup "Project Unit Id" ghcInfoMap)
239+
compilerAbiTag =
240+
maybe
241+
NoAbiTag
242+
AbiTag
243+
( dropWhile (== '-') . stripCommonPrefix (prettyShow compilerId)
244+
<$> Map.lookup "Project Unit Id" ghcInfoMap
245+
)
258246

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

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

Cabal/src/Distribution/Simple/GHCJS.hs

Lines changed: 57 additions & 19 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
@@ -87,6 +89,7 @@ import Control.Arrow ((***))
8789
import Control.Monad (msum)
8890
import Data.Char (isLower)
8991
import qualified Data.Map as Map
92+
import Data.Maybe (fromJust)
9093
import System.Directory
9194
( canonicalizePath
9295
, createDirectoryIfMissing
@@ -106,13 +109,29 @@ 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
111116
-> Maybe FilePath
117+
-- ^ user-specified @ghcjs@ path (optional)
112118
-> Maybe FilePath
119+
-- ^ user-specified @ghcjs-pkg@ path (optional)
113120
-> ProgramDb
114121
-> IO (Compiler, Maybe Platform, ProgramDb)
115122
configure verbosity hcPath hcPkgPath conf0 = do
123+
(comp, compPlatform, progdb1) <- configureCompiler verbosity hcPath conf0
124+
compProgDb <- compilerProgramDb verbosity comp progdb1 hcPkgPath
125+
return (comp, compPlatform, compProgDb)
126+
127+
-- | Configure GHCJS.
128+
configureCompiler
129+
:: Verbosity
130+
-> Maybe FilePath
131+
-- ^ user-specified @ghc@ path (optional)
132+
-> ProgramDb
133+
-> IO (Compiler, Maybe Platform, ProgramDb)
134+
configureCompiler verbosity hcPath conf0 = do
116135
(ghcjsProg, ghcjsVersion, progdb1) <-
117136
requireProgramVersion
118137
verbosity
@@ -133,6 +152,43 @@ configure verbosity hcPath hcPkgPath conf0 = do
133152

134153
let implInfo = ghcjsVersionImplInfo ghcjsVersion ghcjsGhcVersion
135154

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

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)
246+
return progdb3
209247

210248
guessGhcjsPkgFromGhcjsPath
211249
:: ConfiguredProgram

0 commit comments

Comments
 (0)