-
Notifications
You must be signed in to change notification settings - Fork 710
Implement show-build-info
for cabal-install
#7478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0bdf9e5
Add show-build-info command
bgamari 956b329
Rebase work of cfraz89 and bgamari
fendor 8193561
Improve s-b-i frontend command and add tests
fendor decae3b
Rework show-build-info command to avoid wrapper
fendor a524314
Generate autogen files
fendor dfb6f2b
Rework show-build-info to use ProjectPlanning/Building infrastructure
fendor 9794067
Port JSON module to Text based implementation
fendor b79e4a1
Extend s-b-i information in Cabal
fendor 8bd4431
Cache show-build-info results in cache directory
fendor 8ce27f2
Add verbose output marker for show-build-info
fendor 2b51b5e
Re-design test-cases for show-build-info
fendor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
-- This module defines a simple JSON-based format for exporting basic | ||
-- information about a Cabal package and the compiler configuration Cabal | ||
-- would use to build it. This can be produced with the | ||
-- @cabal new-show-build-info@ command. | ||
-- @cabal show-build-info@ command. | ||
-- | ||
-- | ||
-- This format is intended for consumption by external tooling and should | ||
|
@@ -54,7 +54,12 @@ | |
-- Note: At the moment this is only supported when using the GHC compiler. | ||
-- | ||
|
||
module Distribution.Simple.ShowBuildInfo (mkBuildInfo) where | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Distribution.Simple.ShowBuildInfo | ||
( mkBuildInfo, mkBuildInfo', mkCompilerInfo, mkComponentInfo ) where | ||
|
||
import qualified Data.Text as T | ||
|
||
import Distribution.Compat.Prelude | ||
import Prelude () | ||
|
@@ -70,85 +75,112 @@ import Distribution.Simple.LocalBuildInfo | |
import Distribution.Simple.Program | ||
import Distribution.Simple.Setup | ||
import Distribution.Simple.Utils (cabalVersion) | ||
import Distribution.Simple.Utils.Json | ||
import Distribution.Utils.Json | ||
import Distribution.Types.TargetInfo | ||
import Distribution.Text | ||
import Distribution.Pretty | ||
import Distribution.Utils.Path | ||
|
||
import System.FilePath (addTrailingPathSeparator) | ||
|
||
-- | Construct a JSON document describing the build information for a | ||
-- package. | ||
mkBuildInfo | ||
:: PackageDescription -- ^ Mostly information from the .cabal file | ||
:: FilePath -- ^ The source directory of the package | ||
-> PackageDescription -- ^ Mostly information from the .cabal file | ||
-> LocalBuildInfo -- ^ Configuration information | ||
-> BuildFlags -- ^ Flags that the user passed to build | ||
-> [TargetInfo] | ||
-> Json | ||
mkBuildInfo pkg_descr lbi _flags targetsToBuild = info | ||
where | ||
targetToNameAndLBI target = | ||
(componentLocalName $ targetCLBI target, targetCLBI target) | ||
componentsToBuild = map targetToNameAndLBI targetsToBuild | ||
(.=) :: String -> Json -> (String, Json) | ||
k .= v = (k, v) | ||
mkBuildInfo wdir pkg_descr lbi _flags targetsToBuild = | ||
JsonObject $ | ||
mkBuildInfo' (mkCompilerInfo (withPrograms lbi) (compiler lbi)) | ||
(map (mkComponentInfo wdir pkg_descr lbi . targetCLBI) targetsToBuild) | ||
|
||
info = JsonObject | ||
[ "cabal-version" .= JsonString (display cabalVersion) | ||
, "compiler" .= mkCompilerInfo | ||
, "components" .= JsonArray (map mkComponentInfo componentsToBuild) | ||
] | ||
-- | A variant of 'mkBuildInfo' if you need to call 'mkCompilerInfo' and | ||
-- 'mkComponentInfo' yourself. | ||
mkBuildInfo' | ||
:: Json -- ^ The 'Json' from 'mkCompilerInfo' | ||
-> [Json] -- ^ The 'Json' from 'mkComponentInfo' | ||
-> [(T.Text, Json)] | ||
mkBuildInfo' cmplrInfo componentInfos = | ||
[ "cabal-version" .= JsonString (T.pack (display cabalVersion)) | ||
, "compiler" .= cmplrInfo | ||
, "components" .= JsonArray componentInfos | ||
] | ||
|
||
mkCompilerInfo = JsonObject | ||
[ "flavour" .= JsonString (prettyShow $ compilerFlavor $ compiler lbi) | ||
, "compiler-id" .= JsonString (showCompilerId $ compiler lbi) | ||
, "path" .= path | ||
] | ||
where | ||
path = maybe JsonNull (JsonString . programPath) | ||
$ (flavorToProgram . compilerFlavor $ compiler lbi) | ||
>>= flip lookupProgram (withPrograms lbi) | ||
mkCompilerInfo :: ProgramDb -> Compiler -> Json | ||
mkCompilerInfo programDb cmplr = JsonObject | ||
[ "flavour" .= JsonString (T.pack (prettyShow $ compilerFlavor cmplr)) | ||
, "compiler-id" .= JsonString (T.pack (showCompilerId cmplr)) | ||
, "path" .= path | ||
] | ||
where | ||
path = maybe JsonNull (JsonString . T.pack . programPath) | ||
$ (flavorToProgram . compilerFlavor $ cmplr) | ||
>>= flip lookupProgram programDb | ||
|
||
flavorToProgram :: CompilerFlavor -> Maybe Program | ||
flavorToProgram GHC = Just ghcProgram | ||
flavorToProgram GHCJS = Just ghcjsProgram | ||
flavorToProgram UHC = Just uhcProgram | ||
flavorToProgram JHC = Just jhcProgram | ||
flavorToProgram _ = Nothing | ||
flavorToProgram :: CompilerFlavor -> Maybe Program | ||
flavorToProgram GHC = Just ghcProgram | ||
flavorToProgram GHCJS = Just ghcjsProgram | ||
flavorToProgram UHC = Just uhcProgram | ||
flavorToProgram JHC = Just jhcProgram | ||
flavorToProgram _ = Nothing | ||
|
||
mkComponentInfo (name, clbi) = JsonObject | ||
[ "type" .= JsonString compType | ||
, "name" .= JsonString (prettyShow name) | ||
, "unit-id" .= JsonString (prettyShow $ componentUnitId clbi) | ||
, "compiler-args" .= JsonArray (map JsonString $ getCompilerArgs bi lbi clbi) | ||
, "modules" .= JsonArray (map (JsonString . display) modules) | ||
, "src-files" .= JsonArray (map JsonString sourceFiles) | ||
, "src-dirs" .= JsonArray (map JsonString $ map getSymbolicPath $ hsSourceDirs bi) | ||
] | ||
where | ||
bi = componentBuildInfo comp | ||
comp = fromMaybe (error $ "mkBuildInfo: no component " ++ prettyShow name) $ lookupComponent pkg_descr name | ||
compType = case comp of | ||
CLib _ -> "lib" | ||
CExe _ -> "exe" | ||
CTest _ -> "test" | ||
CBench _ -> "bench" | ||
CFLib _ -> "flib" | ||
modules = case comp of | ||
CLib lib -> explicitLibModules lib | ||
CExe exe -> exeModules exe | ||
_ -> [] | ||
sourceFiles = case comp of | ||
CLib _ -> [] | ||
CExe exe -> [modulePath exe] | ||
_ -> [] | ||
mkComponentInfo :: FilePath -> PackageDescription -> LocalBuildInfo -> ComponentLocalBuildInfo -> Json | ||
mkComponentInfo wdir pkg_descr lbi clbi = JsonObject $ | ||
[ "type" .= JsonString compType | ||
, "name" .= JsonString (T.pack $ prettyShow name) | ||
, "unit-id" .= JsonString (T.pack $ prettyShow $ componentUnitId clbi) | ||
, "compiler-args" .= JsonArray (map JsonString $ getCompilerArgs bi lbi clbi) | ||
, "modules" .= JsonArray (map (JsonString . T.pack . display) modules) | ||
, "src-files" .= JsonArray (map (JsonString . T.pack) sourceFiles) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not ideal as |
||
, "hs-src-dirs" .= JsonArray (map (JsonString . T.pack . prettyShow) $ hsSourceDirs bi) | ||
, "src-dir" .= JsonString (T.pack $ addTrailingPathSeparator wdir) | ||
] <> cabalFile | ||
where | ||
name = componentLocalName clbi | ||
bi = componentBuildInfo comp | ||
comp = fromMaybe (error $ "mkBuildInfo: no component " ++ prettyShow name) $ lookupComponent pkg_descr name | ||
compType = case comp of | ||
CLib _ -> "lib" | ||
CExe _ -> "exe" | ||
CTest _ -> "test" | ||
CBench _ -> "bench" | ||
CFLib _ -> "flib" | ||
modules = case comp of | ||
CLib lib -> explicitLibModules lib | ||
CExe exe -> exeModules exe | ||
CTest test -> | ||
case testInterface test of | ||
TestSuiteExeV10 _ _ -> [] | ||
TestSuiteLibV09 _ modName -> [modName] | ||
TestSuiteUnsupported _ -> [] | ||
CBench bench -> benchmarkModules bench | ||
CFLib flib -> foreignLibModules flib | ||
sourceFiles = case comp of | ||
CLib _ -> [] | ||
CExe exe -> [modulePath exe] | ||
CTest test -> | ||
case testInterface test of | ||
TestSuiteExeV10 _ fp -> [fp] | ||
TestSuiteLibV09 _ _ -> [] | ||
TestSuiteUnsupported _ -> [] | ||
CBench bench -> case benchmarkInterface bench of | ||
BenchmarkExeV10 _ fp -> [fp] | ||
BenchmarkUnsupported _ -> [] | ||
|
||
CFLib _ -> [] | ||
cabalFile | ||
| Just fp <- pkgDescrFile lbi = [("cabal-file", JsonString (T.pack fp))] | ||
| otherwise = [] | ||
|
||
-- | Get the command-line arguments that would be passed | ||
-- to the compiler to build the given component. | ||
getCompilerArgs | ||
:: BuildInfo | ||
-> LocalBuildInfo | ||
-> ComponentLocalBuildInfo | ||
-> [String] | ||
-> [T.Text] | ||
getCompilerArgs bi lbi clbi = | ||
case compilerFlavor $ compiler lbi of | ||
GHC -> ghc | ||
|
@@ -157,6 +189,7 @@ getCompilerArgs bi lbi clbi = | |
"build arguments for compiler "++show c | ||
where | ||
-- This is absolutely awful | ||
ghc = GHC.renderGhcOptions (compiler lbi) (hostPlatform lbi) baseOpts | ||
ghc = T.pack <$> | ||
GHC.renderGhcOptions (compiler lbi) (hostPlatform lbi) baseOpts | ||
where | ||
baseOpts = GHC.componentGhcOptions normal lbi bi clbi (buildDir lbi) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍