Skip to content

Commit c26077d

Browse files
committed
Make compiler path not nullable in dumped build-info
Refactor the API slightly s.t. a ConfiguredProgram for the Compiler is passed to build-info generation directly.
1 parent f94e8cc commit c26077d

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

Cabal/src/Distribution/Simple/Build.hs

+18-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import Distribution.Simple.BuildTarget
6969
import Distribution.Simple.BuildToolDepends
7070
import Distribution.Simple.PreProcess
7171
import Distribution.Simple.LocalBuildInfo
72+
import Distribution.Simple.Program.Builtin (ghcProgram, ghcjsProgram, uhcProgram, jhcProgram, haskellSuiteProgram)
7273
import Distribution.Simple.Program.Types
7374
import Distribution.Simple.Program.Db
7475
import Distribution.Simple.ShowBuildInfo
@@ -164,7 +165,13 @@ dumpBuildInfo verbosity distPref dumpBuildInfoFlag pkg_descr lbi flags = do
164165
(map (showComponentName . componentLocalName . targetCLBI)
165166
activeTargets)
166167
pwd <- getCurrentDirectory
167-
let (warns, json) = mkBuildInfo pwd pkg_descr lbi flags activeTargets
168+
169+
(compilerProg, _) <- case flavorToProgram (compilerFlavor (compiler lbi)) of
170+
Nothing -> die' verbosity $ "dumpBuildInfo: Unknown compiler flavor: "
171+
++ show (compilerFlavor (compiler lbi))
172+
Just program -> requireProgram verbosity program (withPrograms lbi)
173+
174+
let (warns, json) = mkBuildInfo pwd pkg_descr lbi flags (compilerProg, compiler lbi) activeTargets
168175
buildInfoText = renderJson json
169176
unless (null warns) $
170177
warn verbosity $ "Encountered warnings while dumping build-info:\n"
@@ -178,6 +185,16 @@ dumpBuildInfo verbosity distPref dumpBuildInfoFlag pkg_descr lbi flags = do
178185
where
179186
shouldDumpBuildInfo = fromFlagOrDefault NoDumpBuildInfo dumpBuildInfoFlag == DumpBuildInfo
180187

188+
-- | Given the flavor of the compiler, try to find out
189+
-- which program we need.
190+
flavorToProgram :: CompilerFlavor -> Maybe Program
191+
flavorToProgram GHC = Just ghcProgram
192+
flavorToProgram GHCJS = Just ghcjsProgram
193+
flavorToProgram UHC = Just uhcProgram
194+
flavorToProgram JHC = Just jhcProgram
195+
flavorToProgram HaskellSuite {} = Just haskellSuiteProgram
196+
flavorToProgram _ = Nothing
197+
181198

182199
repl :: PackageDescription -- ^ Mostly information from the .cabal file
183200
-> LocalBuildInfo -- ^ Configuration information

Cabal/src/Distribution/Simple/ShowBuildInfo.hs

+9-16
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,15 @@ mkBuildInfo
9393
-> PackageDescription -- ^ Mostly information from the .cabal file
9494
-> LocalBuildInfo -- ^ Configuration information
9595
-> BuildFlags -- ^ Flags that the user passed to build
96+
-> (ConfiguredProgram, Compiler)
97+
-- ^ Compiler information.
98+
-- Needs to be passed explicitly, as we can't extract that information here
99+
-- without some partial function.
96100
-> [TargetInfo]
97101
-> ([String], Json) -- ^ Json representation of buildinfo alongside generated warnings
98-
mkBuildInfo wdir pkg_descr lbi _flags targetsToBuild = (warnings, JsonObject buildInfoFields)
102+
mkBuildInfo wdir pkg_descr lbi _flags compilerInfo targetsToBuild = (warnings, JsonObject buildInfoFields)
99103
where
100-
buildInfoFields = mkBuildInfo' (mkCompilerInfo (withPrograms lbi) (compiler lbi)) componentInfos
104+
buildInfoFields = mkBuildInfo' (uncurry mkCompilerInfo compilerInfo) componentInfos
101105
componentInfosWithWarnings = map (mkComponentInfo wdir pkg_descr lbi . targetCLBI) targetsToBuild
102106
componentInfos = map snd componentInfosWithWarnings
103107
warnings = concatMap fst componentInfosWithWarnings
@@ -114,23 +118,12 @@ mkBuildInfo' compilerInfo componentInfos =
114118
, "components" .= JsonArray componentInfos
115119
]
116120

117-
mkCompilerInfo :: ProgramDb -> Compiler -> Json
118-
mkCompilerInfo programDb compilerInfo = JsonObject
121+
mkCompilerInfo :: ConfiguredProgram -> Compiler -> Json
122+
mkCompilerInfo compilerProgram compilerInfo = JsonObject
119123
[ "flavour" .= JsonString (prettyShow $ compilerFlavor compilerInfo)
120124
, "compiler-id" .= JsonString (showCompilerId compilerInfo)
121-
, "path" .= path
125+
, "path" .= JsonString (programPath compilerProgram)
122126
]
123-
where
124-
path = maybe JsonNull (JsonString . programPath)
125-
$ (flavorToProgram . compilerFlavor $ compilerInfo)
126-
>>= flip lookupProgram programDb
127-
128-
flavorToProgram :: CompilerFlavor -> Maybe Program
129-
flavorToProgram GHC = Just ghcProgram
130-
flavorToProgram GHCJS = Just ghcjsProgram
131-
flavorToProgram UHC = Just uhcProgram
132-
flavorToProgram JHC = Just jhcProgram
133-
flavorToProgram _ = Nothing
134127

135128
mkComponentInfo :: FilePath -> PackageDescription -> LocalBuildInfo -> ComponentLocalBuildInfo -> ([String], Json)
136129
mkComponentInfo wdir pkg_descr lbi clbi = (warnings, JsonObject $

cabal-install/src/Distribution/Client/ProjectPlanOutput.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,17 @@ encodePlanAsJson distDirLayout elaboratedInstallPlan elaboratedSharedConfig =
179179
where
180180
-- | Only add build-info file location if the Setup.hs CLI
181181
-- is recent enough to be able to generate build info files.
182-
-- Otherwise, do not add the expected file location.
182+
-- Otherwise, write 'null'.
183183
--
184-
-- Consumers of `plan.json` can use the absence of this file location
184+
-- Consumers of `plan.json` can use the nullability of this file location
185185
-- to indicate that the given component uses `build-type: Custom`
186186
-- with an old lib:Cabal version.
187187
buildInfoFileLocation :: J.Pair
188188
buildInfoFileLocation
189189
| elabSetupScriptCliVersion elab < mkVersion [3, 7, 0, 0]
190-
= ("build-info" J..= J.Null)
190+
= "build-info" J..= J.Null
191191
| otherwise
192-
= ("build-info" J..= J.String (buildInfoPref dist_dir))
192+
= "build-info" J..= J.String (buildInfoPref dist_dir)
193193

194194
packageLocationToJ :: PackageLocation (Maybe FilePath) -> J.Value
195195
packageLocationToJ pkgloc =

0 commit comments

Comments
 (0)