-
Notifications
You must be signed in to change notification settings - Fork 733
Add PackageMeta_ autogenerated module (#11755) #11756
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
Open
angerman
wants to merge
4
commits into
haskell:master
Choose a base branch
from
angerman:angerman/add-package-meta-module
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cefb43c
Add PackageMeta_ autogenerated module (#11755)
angerman 105b17c
Add changelog entry for PackageMeta_ module
angerman 286fc8c
Fix changelog format: remove YAML frontmatter separators
angerman bf5886f
Fix import: compilerId is in Distribution.Simple.Compiler
angerman 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| ----------------------------------------------------------------------------- | ||
|
|
||
| -- | | ||
| -- Module : Distribution.Simple.Build.PackageMetaModule | ||
| -- Copyright : Moritz Angermann <moritz.angermann@iohk.io> | ||
| -- | ||
| -- Maintainer : cabal-devel@haskell.org | ||
| -- Portability : portable | ||
| -- | ||
| -- Generating the PackageMeta_pkgname module. | ||
| -- | ||
| -- This is a module that Cabal generates for the benefit of packages. It | ||
| -- enables them to find build-environment metadata such as compiler info, | ||
| -- platform, cabal flags, and git revision. | ||
| module Distribution.Simple.Build.PackageMetaModule | ||
| ( generatePackageMetaModule | ||
| ) where | ||
|
|
||
| import Distribution.Compat.Prelude | ||
| import Prelude () | ||
|
|
||
| import Distribution.Compiler (CompilerId (..)) | ||
| import Distribution.Package | ||
| ( PackageName | ||
| , packageName | ||
| , unPackageName | ||
| ) | ||
| import Distribution.Pretty (prettyShow) | ||
| import Distribution.Simple.Compiler (compilerId) | ||
| import Distribution.System (Platform (..)) | ||
| import Distribution.Types.Flag (FlagName, unFlagAssignment, unFlagName) | ||
| import Distribution.Types.LocalBuildInfo (LocalBuildInfo (..)) | ||
| import Distribution.Types.PackageDescription (PackageDescription) | ||
| import Distribution.Types.Version (versionNumbers) | ||
|
|
||
| import qualified Distribution.Simple.Build.PackageMetaModule.Z as Z | ||
|
|
||
| -- ------------------------------------------------------------ | ||
|
|
||
| -- * Building PackageMeta_<pkg>.hs | ||
|
|
||
| -- ------------------------------------------------------------ | ||
|
|
||
| -- | Generate the source code for the @PackageMeta_<pkgname>@ module. | ||
| -- | ||
| -- The @gitRev@ and @gitDirty@ parameters are passed in because computing | ||
| -- them requires IO (running @git@ commands), and this function is pure. | ||
| generatePackageMetaModule | ||
| :: PackageDescription | ||
| -> LocalBuildInfo | ||
| -> String | ||
| -- ^ Git revision hash (empty string if unavailable) | ||
| -> Bool | ||
| -- ^ Whether the working tree has uncommitted changes | ||
| -> String | ||
| generatePackageMetaModule pkg_descr lbi gitRev gitDirty = | ||
| Z.render | ||
| Z.Z | ||
| { Z.zPackageName = showPkgName $ packageName pkg_descr | ||
| , Z.zCompilerFlavour = prettyShow flavour | ||
| , Z.zCompilerName = prettyShow cid | ||
| , Z.zCompilerVersionDigits = show $ versionNumbers ver | ||
| , Z.zOs = prettyShow os | ||
| , Z.zArch = prettyShow arch | ||
| , Z.zGitRevision = gitRev | ||
| , Z.zGitDirty = gitDirty | ||
| , Z.zFlags = map toFlagZ $ unFlagAssignment $ flagAssignment lbi | ||
| } | ||
| where | ||
| cid@(CompilerId flavour ver) = compilerId $ compiler lbi | ||
| Platform arch os = hostPlatform lbi | ||
|
|
||
| toFlagZ :: (FlagName, Bool) -> Z.FlagZ | ||
| toFlagZ (fn, val) = | ||
| Z.FlagZ | ||
| { Z.zFlagName = unFlagName fn | ||
| , Z.zFlagHaskellName = "flag_" ++ map fixchar (unFlagName fn) | ||
| , Z.zFlagValue = val | ||
| } | ||
|
|
||
| showPkgName :: PackageName -> String | ||
| showPkgName = map fixchar . unPackageName | ||
|
|
||
| fixchar :: Char -> Char | ||
| fixchar '-' = '_' | ||
| fixchar c = c |
124 changes: 124 additions & 0 deletions
124
Cabal/src/Distribution/Simple/Build/PackageMetaModule/Z.hs
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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| {-# LANGUAGE DeriveGeneric #-} | ||
|
|
||
| -- | | ||
| -- Module : Distribution.Simple.Build.PackageMetaModule.Z | ||
| -- Copyright : Moritz Angermann <moritz.angermann@iohk.io> | ||
| -- | ||
| -- Maintainer : cabal-devel@haskell.org | ||
| -- Portability : portable | ||
| -- | ||
| -- Zinza-style renderer for the @PackageMeta_pkgname@ module. | ||
| module Distribution.Simple.Build.PackageMetaModule.Z (render, Z (..), FlagZ (..)) where | ||
|
|
||
| import Distribution.ZinzaPrelude (Generic, execWriter, forM_, tell) | ||
|
|
||
| -- | A single cabal flag entry. | ||
| data FlagZ = FlagZ | ||
| { zFlagName :: String | ||
| -- ^ Original flag name (e.g. "debug") | ||
| , zFlagHaskellName :: String | ||
| -- ^ Haskell-safe name (e.g. "flag_debug") | ||
| , zFlagValue :: Bool | ||
| -- ^ Resolved value | ||
| } | ||
| deriving (Generic) | ||
|
|
||
| -- | Template data for rendering the PackageMeta module. | ||
| data Z = Z | ||
| { zPackageName :: String | ||
| , zCompilerFlavour :: String | ||
| , zCompilerName :: String | ||
| , zCompilerVersionDigits :: String | ||
| , zOs :: String | ||
| , zArch :: String | ||
| , zGitRevision :: String | ||
| , zGitDirty :: Bool | ||
|
Collaborator
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. Not really convinced of the git parts, for several reasons:
|
||
| , zFlags :: [FlagZ] | ||
| } | ||
| deriving (Generic) | ||
|
|
||
| render :: Z -> String | ||
| render z = execWriter $ do | ||
| tell "{-# LANGUAGE NoRebindableSyntax #-}\n" | ||
| tell "{-# OPTIONS_GHC -w #-}\n" | ||
| tell "\n" | ||
| tell "{-|\n" | ||
| tell "Module : PackageMeta_" | ||
| tell (zPackageName z) | ||
| tell "\n" | ||
| tell "\n" | ||
| tell "Autogenerated module providing build-environment metadata.\n" | ||
| tell "-}\n" | ||
| tell "module PackageMeta_" | ||
| tell (zPackageName z) | ||
| tell " (\n" | ||
| tell " compiler,\n" | ||
| tell " compilerVersion,\n" | ||
| tell " os,\n" | ||
| tell " arch,\n" | ||
| tell " gitRevision,\n" | ||
| tell " gitDirty,\n" | ||
| -- Export flag names | ||
| forM_ (zFlags z) $ \f -> do | ||
| tell " " | ||
| tell (zFlagHaskellName f) | ||
| tell ",\n" | ||
| tell " ) where\n" | ||
| tell "\n" | ||
| tell "import Data.Version (Version(..))\n" | ||
| tell "import Prelude\n" | ||
| tell "\n" | ||
| -- Compiler info | ||
| tell "-- | The Haskell compiler used to build this package (e.g. @\"ghc\"@).\n" | ||
| tell "compiler :: String\n" | ||
| tell "compiler = " | ||
| tell (show (zCompilerFlavour z)) | ||
| tell "\n" | ||
| tell "\n" | ||
| tell "-- | The version of the compiler used to build this package.\n" | ||
| tell "compilerVersion :: Version\n" | ||
| tell "compilerVersion = Version " | ||
| tell (zCompilerVersionDigits z) | ||
| tell " []\n" | ||
| tell "\n" | ||
| -- Platform info | ||
| tell "-- | The operating system this package was built on (e.g. @\"linux\"@, @\"darwin\"@).\n" | ||
| tell "os :: String\n" | ||
| tell "os = " | ||
| tell (show (zOs z)) | ||
| tell "\n" | ||
| tell "\n" | ||
| tell "-- | The CPU architecture this package was built for (e.g. @\"x86_64\"@, @\"aarch64\"@).\n" | ||
| tell "arch :: String\n" | ||
| tell "arch = " | ||
| tell (show (zArch z)) | ||
| tell "\n" | ||
| tell "\n" | ||
| -- Git info | ||
| tell "-- | The VCS revision at build time, or @\"\"@ if unavailable.\n" | ||
| tell "gitRevision :: String\n" | ||
| tell "gitRevision = " | ||
| tell (show (zGitRevision z)) | ||
| tell "\n" | ||
| tell "\n" | ||
| tell "-- | Whether the working tree had uncommitted changes at build time.\n" | ||
| tell "gitDirty :: Bool\n" | ||
| tell "gitDirty = " | ||
| if zGitDirty z | ||
| then tell "True" | ||
| else tell "False" | ||
| tell "\n" | ||
| -- Flag values | ||
| forM_ (zFlags z) $ \f -> do | ||
| tell "\n" | ||
| tell "-- | The value of the @" | ||
| tell (zFlagName f) | ||
| tell "@ cabal flag.\n" | ||
| tell (zFlagHaskellName f) | ||
| tell " :: Bool\n" | ||
| tell (zFlagHaskellName f) | ||
| tell " = " | ||
| if zFlagValue f | ||
| then tell "True" | ||
| else tell "False" | ||
| tell "\n" | ||
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
12 changes: 12 additions & 0 deletions
12
cabal-testsuite/PackageTests/PackageMetaModule/Executable/Main.hs
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| module Main where | ||
|
|
||
| import PackageMeta_PackageMetaModule (compiler, compilerVersion, os, arch, gitRevision, gitDirty) | ||
|
|
||
| main :: IO () | ||
| main = do | ||
| putStrLn compiler | ||
| print compilerVersion | ||
| putStrLn os | ||
| putStrLn arch | ||
| putStrLn gitRevision | ||
| print gitDirty |
18 changes: 18 additions & 0 deletions
18
cabal-testsuite/PackageTests/PackageMetaModule/Executable/my.cabal
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| Cabal-version: 3.12 | ||
| name: PackageMetaModule | ||
| version: 0.1 | ||
| license: BSD-3-Clause | ||
| author: Moritz Angermann | ||
| stability: stable | ||
| category: PackageTests | ||
| build-type: Simple | ||
|
|
||
| description: | ||
| Check that the generated package meta module compiles. | ||
|
|
||
| Executable TestPackageMetaModule | ||
| main-is: Main.hs | ||
| other-modules: | ||
| PackageMeta_PackageMetaModule | ||
| Paths_PackageMetaModule | ||
| build-depends: base |
5 changes: 5 additions & 0 deletions
5
cabal-testsuite/PackageTests/PackageMetaModule/Executable/setup.cabal.out
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Setup configure | ||
| Configuring PackageMetaModule-0.1... | ||
| # Setup build | ||
| Preprocessing executable 'TestPackageMetaModule' for PackageMetaModule-0.1... | ||
| Building executable 'TestPackageMetaModule' for PackageMetaModule-0.1... |
5 changes: 5 additions & 0 deletions
5
cabal-testsuite/PackageTests/PackageMetaModule/Executable/setup.out
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Setup configure | ||
| Configuring PackageMetaModule-0.1... | ||
| # Setup build | ||
| Preprocessing executable 'TestPackageMetaModule' for PackageMetaModule-0.1... | ||
| Building executable 'TestPackageMetaModule' for PackageMetaModule-0.1... |
3 changes: 3 additions & 0 deletions
3
cabal-testsuite/PackageTests/PackageMetaModule/Executable/setup.test.hs
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import Test.Cabal.Prelude | ||
| -- Test that PackageMeta module is generated and available for executables. | ||
| main = setupAndCabalTest $ setup_build [] |
36 changes: 36 additions & 0 deletions
36
cabal-testsuite/PackageTests/PackageMetaModule/Library/my.cabal
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| Cabal-version: 3.12 | ||
| name: PackageMetaModule | ||
| version: 0.1 | ||
| license: BSD-3-Clause | ||
| author: Moritz Angermann | ||
| stability: stable | ||
| category: PackageTests | ||
| build-type: Simple | ||
|
|
||
| description: | ||
| Check that the generated package meta module compiles. | ||
|
|
||
| Library | ||
| exposed-modules: | ||
| PackageMeta_PackageMetaModule | ||
| Paths_PackageMetaModule | ||
| build-depends: base | ||
| default-language: Haskell2010 | ||
| default-extensions: | ||
| -- This is a non-exhaustive list of extensions that can cause code to | ||
| -- not compile when it would if the extension was disabled. This ensures | ||
| -- that autogen modules are compatible with default extensions. | ||
| NoImplicitPrelude | ||
| CPP | ||
| TemplateHaskell | ||
| QuasiQuotes | ||
| Arrows | ||
| OverloadedStrings | ||
| if impl(ghc >= 6.12) | ||
| default-extensions: MonoLocalBinds | ||
| if impl(ghc >= 7.0.1) | ||
| default-extensions: RebindableSyntax | ||
| if impl(ghc >= 7.4.1) | ||
| default-extensions: NoTraditionalRecordSyntax | ||
| if impl(ghc >= 7.8.1) | ||
| default-extensions: OverloadedLists |
Oops, something went wrong.
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.
Why do we need to leave a todo here? It seems like everything can be solved right here.
builtinAutogenFilesis only used inwriteBuiltinAutogenFiles :: ... -> IO ()