diff --git a/.github/workflows/quick-jobs.yml b/.github/workflows/quick-jobs.yml index c30aec6b95b..afaa63e646b 100644 --- a/.github/workflows/quick-jobs.yml +++ b/.github/workflows/quick-jobs.yml @@ -42,6 +42,7 @@ jobs: make lexer make gen-extra-source-files make spdx + make templates make github-actions - name: Check that diff is clean run: | diff --git a/Cabal/Cabal.cabal b/Cabal/Cabal.cabal index ac5aca39b4a..2d5fc23832b 100644 --- a/Cabal/Cabal.cabal +++ b/Cabal/Cabal.cabal @@ -559,10 +559,12 @@ library Distribution.GetOpt Distribution.Lex Distribution.Utils.String + Distribution.Simple.Build.Macros.Z Distribution.Simple.GHC.EnvironmentParser Distribution.Simple.GHC.Internal Distribution.Simple.GHC.ImplInfo Distribution.Simple.Utils.Json + Distribution.ZinzaPrelude Paths_Cabal if flag(bundled-binary-generic) diff --git a/Cabal/Distribution/Simple/Build/Macros.hs b/Cabal/Distribution/Simple/Build/Macros.hs index 15c5b3d2fe4..139b8e954fa 100644 --- a/Cabal/Distribution/Simple/Build/Macros.hs +++ b/Cabal/Distribution/Simple/Build/Macros.hs @@ -35,126 +35,78 @@ import Distribution.Simple.Program.Types import Distribution.Types.MungedPackageId import Distribution.Types.MungedPackageName import Distribution.Types.PackageId +import Distribution.Types.PackageName (unPackageName) import Distribution.Pretty --- ------------------------------------------------------------ --- * Generate cabal_macros.h --- ------------------------------------------------------------ - --- Invariant: HeaderLines always has a trailing newline -type HeaderLines = String - -line :: String -> HeaderLines -line str = str ++ "\n" - -ifndef :: String -> HeaderLines -> HeaderLines -ifndef macro body = - line ("#ifndef " ++ macro) ++ - body ++ - line ("#endif /* " ++ macro ++ " */") - -define :: String -> Maybe [String] -> String -> HeaderLines -define macro params val = - line ("#define " ++ macro ++ f params ++ " " ++ val) - where - f Nothing = "" - f (Just xs) = "(" ++ intercalate "," xs ++ ")" - -defineStr :: String -> String -> HeaderLines -defineStr macro str = define macro Nothing (show str) - -ifndefDefine :: String -> Maybe [String] -> String -> HeaderLines -ifndefDefine macro params str = - ifndef macro (define macro params str) - -ifndefDefineStr :: String -> String -> HeaderLines -ifndefDefineStr macro str = - ifndef macro (defineStr macro str) +import qualified Distribution.Simple.Build.Macros.Z as Z -- | The contents of the @cabal_macros.h@ for the given configured package. -- generateCabalMacrosHeader :: PackageDescription -> LocalBuildInfo -> ComponentLocalBuildInfo -> String -generateCabalMacrosHeader pkg_descr lbi clbi = - "/* DO NOT EDIT: This file is automatically generated by Cabal */\n\n" ++ - generatePackageVersionMacros - (package pkg_descr : map getPid (componentPackageDeps clbi)) ++ - generateToolVersionMacros (configuredPrograms . withPrograms $ lbi) ++ - generateComponentIdMacro lbi clbi ++ - generateCurrentPackageVersion pkg_descr - where - getPid (_, MungedPackageId (MungedPackageName pn _) v) = - -- NB: Drop the library name! We're just reporting package versions. - -- This would have to be revisited if you are allowed to depend - -- on different versions of the same package - PackageIdentifier pn v +generateCabalMacrosHeader pkg_descr lbi clbi = Z.render Z.Z + { Z.zPackages = map mkZPackage $ package pkg_descr : map getPid (componentPackageDeps clbi) + , Z.zTools = + [ Z.ZTool + { Z.ztoolName = programId prog + , Z.ztoolVersion = ver + , Z.ztoolX = major1 + , Z.ztoolY = major2 + , Z.ztoolZ = minor + } + | prog <- configuredPrograms $ withPrograms lbi + , ver <- maybeToList (programVersion prog) + , let (major1,major2,minor) = majorMinor ver + ] + , Z.zPackageKey = case clbi of + LibComponentLocalBuildInfo{} -> componentCompatPackageKey clbi + _ -> "" + , Z.zComponentId = prettyShow (componentComponentId clbi) + , Z.zPackageVersion = pkgVersion (package pkg_descr) + , Z.zNotNull = not . null + , Z.zManglePkgName = map fixchar . unPackageName + , Z.zMangleStr = map fixchar + } + where + getPid (_, MungedPackageId (MungedPackageName pn _) v) = + -- NB: Drop the library name! We're just reporting package versions. + -- This would have to be revisited if you are allowed to depend + -- on different versions of the same package + PackageIdentifier pn v -- | Helper function that generates just the @VERSION_pkg@ and @MIN_VERSION_pkg@ -- macros for a list of package ids (usually used with the specific deps of -- a configured package). -- -generatePackageVersionMacros :: [PackageId] -> String -generatePackageVersionMacros pkgids = concat - [ line ("/* package " ++ prettyShow pkgid ++ " */") - ++ generateMacros "" pkgname version - | pkgid@(PackageIdentifier name version) <- pkgids - , let pkgname = map fixchar (prettyShow name) - ] - --- | Helper function that generates just the @TOOL_VERSION_pkg@ and --- @MIN_TOOL_VERSION_pkg@ macros for a list of configured programs. --- -generateToolVersionMacros :: [ConfiguredProgram] -> String -generateToolVersionMacros progs = concat - [ line ("/* tool " ++ progid ++ " */") - ++ generateMacros "TOOL_" progname version - | prog <- progs - , isJust . programVersion $ prog - , let progid = programId prog ++ "-" ++ prettyShow version - progname = map fixchar (programId prog) - version = fromMaybe version0 (programVersion prog) - ] - --- | Common implementation of 'generatePackageVersionMacros' and --- 'generateToolVersionMacros'. --- -generateMacros :: String -> String -> Version -> String -generateMacros macro_prefix name version = - concat - [ifndefDefineStr (macro_prefix ++ "VERSION_" ++ name) (prettyShow version) - ,ifndefDefine ("MIN_" ++ macro_prefix ++ "VERSION_" ++ name) - (Just ["major1","major2","minor"]) - $ concat [ - "(\\\n" - ," (major1) < ",major1," || \\\n" - ," (major1) == ",major1," && (major2) < ",major2," || \\\n" - ," (major1) == ",major1," && (major2) == ",major2," && (minor) <= ",minor,")" - ] - ,"\n"] +generatePackageVersionMacros :: Version -> [PackageId] -> String +generatePackageVersionMacros ver pkgids = Z.render Z.Z + { Z.zPackages = map mkZPackage pkgids + , Z.zTools = [] + , Z.zPackageKey = "" + , Z.zComponentId = "" + , Z.zPackageVersion = ver + , Z.zNotNull = not . null + , Z.zManglePkgName = map fixchar . unPackageName + , Z.zMangleStr = map fixchar + } + +mkZPackage :: PackageId -> Z.ZPackage +mkZPackage (PackageIdentifier name ver) = Z.ZPackage + { Z.zpkgName = name + , Z.zpkgVersion = ver + , Z.zpkgX = major1 + , Z.zpkgY = major2 + , Z.zpkgZ = minor + } where - (major1,major2,minor) = case map show (versionNumbers version) of + (major1,major2,minor) = majorMinor ver + +majorMinor :: Version -> (String, String, String) +majorMinor ver = case map show (versionNumbers ver) of [] -> ("0", "0", "0") [x] -> (x, "0", "0") [x,y] -> (x, y, "0") (x:y:z:_) -> (x, y, z) --- | Generate the @CURRENT_COMPONENT_ID@ definition for the component ID --- of the current package. -generateComponentIdMacro :: LocalBuildInfo -> ComponentLocalBuildInfo -> String -generateComponentIdMacro _lbi clbi = - concat $ - [case clbi of - LibComponentLocalBuildInfo{} -> - ifndefDefineStr "CURRENT_PACKAGE_KEY" (componentCompatPackageKey clbi) - _ -> "" - ,ifndefDefineStr "CURRENT_COMPONENT_ID" (prettyShow (componentComponentId clbi)) - ] - --- | Generate the @CURRENT_PACKAGE_VERSION@ definition for the declared version --- of the current package. -generateCurrentPackageVersion :: PackageDescription -> String -generateCurrentPackageVersion pd = - ifndefDefineStr "CURRENT_PACKAGE_VERSION" (prettyShow (pkgVersion (package pd))) - fixchar :: Char -> Char fixchar '-' = '_' fixchar c = c diff --git a/Cabal/Distribution/Simple/Build/Macros/Z.hs b/Cabal/Distribution/Simple/Build/Macros/Z.hs new file mode 100644 index 00000000000..641d395d15e --- /dev/null +++ b/Cabal/Distribution/Simple/Build/Macros/Z.hs @@ -0,0 +1,140 @@ +{-# LANGUAGE DeriveGeneric #-} +module Distribution.Simple.Build.Macros.Z (render, Z(..), ZPackage (..), ZTool (..)) where +import Distribution.ZinzaPrelude +data Z + = Z {zPackages :: ([ZPackage]), + zTools :: ([ZTool]), + zPackageKey :: String, + zComponentId :: String, + zPackageVersion :: Version, + zNotNull :: (String -> Bool), + zManglePkgName :: (PackageName -> String), + zMangleStr :: (String -> String)} + deriving Generic +data ZPackage + = ZPackage {zpkgName :: PackageName, + zpkgVersion :: Version, + zpkgX :: String, + zpkgY :: String, + zpkgZ :: String} + deriving Generic +data ZTool + = ZTool {ztoolName :: String, + ztoolVersion :: Version, + ztoolX :: String, + ztoolY :: String, + ztoolZ :: String} + deriving Generic +render :: Z -> String +render z_root = execWriter $ do + tell "/* DO NOT EDIT: This file is automatically generated by Cabal */\n" + tell "\n" + forM_ (zPackages z_root) $ \z_var0_pkg -> do + tell "/* package " + tell (prettyShow (zpkgName z_var0_pkg)) + tell "-" + tell (prettyShow (zpkgVersion z_var0_pkg)) + tell " */\n" + tell "#ifndef VERSION_" + tell (zManglePkgName z_root (zpkgName z_var0_pkg)) + tell "\n" + tell "#define VERSION_" + tell (zManglePkgName z_root (zpkgName z_var0_pkg)) + tell " \"" + tell (prettyShow (zpkgVersion z_var0_pkg)) + tell "\"\n" + tell "#endif /* VERSION_" + tell (zManglePkgName z_root (zpkgName z_var0_pkg)) + tell " */\n" + tell "#ifndef MIN_VERSION_" + tell (zManglePkgName z_root (zpkgName z_var0_pkg)) + tell "\n" + tell "#define MIN_VERSION_" + tell (zManglePkgName z_root (zpkgName z_var0_pkg)) + tell "(major1,major2,minor) (\\\n" + tell " (major1) < " + tell (zpkgX z_var0_pkg) + tell " || \\\n" + tell " (major1) == " + tell (zpkgX z_var0_pkg) + tell " && (major2) < " + tell (zpkgY z_var0_pkg) + tell " || \\\n" + tell " (major1) == " + tell (zpkgX z_var0_pkg) + tell " && (major2) == " + tell (zpkgY z_var0_pkg) + tell " && (minor) <= " + tell (zpkgZ z_var0_pkg) + tell ")\n" + tell "#endif /* MIN_VERSION_" + tell (zManglePkgName z_root (zpkgName z_var0_pkg)) + tell " */\n" + tell "\n" + forM_ (zTools z_root) $ \z_var1_tool -> do + tell "/* package " + tell (ztoolName z_var1_tool) + tell "-" + tell (prettyShow (ztoolVersion z_var1_tool)) + tell " */\n" + tell "#ifndef TOOL_VERSION_" + tell (zMangleStr z_root (ztoolName z_var1_tool)) + tell "\n" + tell "#define TOOL_VERSION_" + tell (zMangleStr z_root (ztoolName z_var1_tool)) + tell " \"" + tell (prettyShow (ztoolVersion z_var1_tool)) + tell "\"\n" + tell "#endif /* VERSION_" + tell (zMangleStr z_root (ztoolName z_var1_tool)) + tell " */\n" + tell "#ifndef MIN_TOOL_VERSION_" + tell (zMangleStr z_root (ztoolName z_var1_tool)) + tell "\n" + tell "#define MIN_TOOL_VERSION_" + tell (zMangleStr z_root (ztoolName z_var1_tool)) + tell "(major1,major2,minor) (\\\n" + tell " (major1) < " + tell (ztoolX z_var1_tool) + tell " || \\\n" + tell " (major1) == " + tell (ztoolX z_var1_tool) + tell " && (major2) < " + tell (ztoolY z_var1_tool) + tell " || \\\n" + tell " (major1) == " + tell (ztoolX z_var1_tool) + tell " && (major2) == " + tell (ztoolY z_var1_tool) + tell " && (minor) <= " + tell (ztoolZ z_var1_tool) + tell ")\n" + tell "#endif /* MIN_VERSION_" + tell (zMangleStr z_root (ztoolName z_var1_tool)) + tell " */\n" + tell "\n" + if (zNotNull z_root (zPackageKey z_root)) + then do + tell "#ifndef CURRENT_packageKey\n" + tell "#define CURRENT_packageKey \"" + tell (zPackageKey z_root) + tell "\"\n" + tell "#endif /* CURRENT_packageKey */\n" + return () + else do + return () + if (zNotNull z_root (zComponentId z_root)) + then do + tell "#ifndef CURRENT_COMPONENT_ID\n" + tell "#define CURRENT_COMPONENT_ID \"" + tell (zComponentId z_root) + tell "\"\n" + tell "#endif /* CURRENT_COMPONENT_ID */\n" + return () + else do + return () + tell "#ifndef CURRENT_PACKAGE_VERSION\n" + tell "#define CURRENT_PACKAGE_VERSION \"" + tell (prettyShow (zPackageVersion z_root)) + tell "\"\n" + tell "#endif /* CURRENT_PACKAGE_VERSION */\n" diff --git a/Cabal/Distribution/Simple/Utils.hs b/Cabal/Distribution/Simple/Utils.hs index 01f81c1969e..96c8406b7d2 100644 --- a/Cabal/Distribution/Simple/Utils.hs +++ b/Cabal/Distribution/Simple/Utils.hs @@ -250,7 +250,7 @@ cabalVersion = mkVersion' Paths_Cabal.version #elif defined(CABAL_VERSION) cabalVersion = mkVersion [CABAL_VERSION] #else -cabalVersion = mkVersion [1,9999] --used when bootstrapping +cabalVersion = mkVersion [3,0] --used when bootstrapping #endif -- ---------------------------------------------------------------------------- diff --git a/Cabal/Distribution/ZinzaPrelude.hs b/Cabal/Distribution/ZinzaPrelude.hs new file mode 100644 index 00000000000..61e8295617b --- /dev/null +++ b/Cabal/Distribution/ZinzaPrelude.hs @@ -0,0 +1,43 @@ +-- | A small prelude used in @zinza@ generated +-- template modules. +module Distribution.ZinzaPrelude ( + Writer, + execWriter, + tell, + -- * Re-exports + forM_, + Generic, + PackageName, + Version, + prettyShow + ) where + +import Distribution.Compat.Prelude +import Prelude () + +import Control.Monad (forM_) +import Distribution.Pretty (prettyShow) +import Distribution.Types.PackageName (PackageName) +import Distribution.Types.Version (Version) + +newtype Writer a = W { unW :: ShowS -> (ShowS, a) } + +instance Functor Writer where + fmap = liftM + +instance Applicative Writer where + pure x = W $ \ss -> (ss, x) + (<*>) = ap + +instance Monad Writer where + return = pure + m >>= k = W $ \s1 -> + let (s2, x) = unW m s1 + in unW (k x) s2 + {-# INLINE (>>=) #-} + +execWriter :: Writer a -> String +execWriter w = fst (unW w id) "" + +tell :: String -> Writer () +tell s = W $ \s' -> (s' . showString s, ()) diff --git a/Makefile b/Makefile index c835e0bf422..f91f223f142 100644 --- a/Makefile +++ b/Makefile @@ -2,10 +2,6 @@ .PHONY : gen-extra-source-files gen-extra-source-files-lib gen-extra-source-files-cli .PHONY : cabal-install-dev cabal-install-prod -LEXER_HS:=Cabal/Distribution/Fields/Lexer.hs -SPDX_LICENSE_HS:=Cabal/Distribution/SPDX/LicenseId.hs -SPDX_EXCEPTION_HS:=Cabal/Distribution/SPDX/LicenseExceptionId.hs - CABALBUILD := cabal v2-build CABALRUN := cabal v2-run @@ -25,6 +21,8 @@ lib-ghc-7.6 : # source generation: Lexer +LEXER_HS:=Cabal/Distribution/Fields/Lexer.hs + lexer : $(LEXER_HS) $(LEXER_HS) : boot/Lexer.x @@ -34,6 +32,9 @@ $(LEXER_HS) : boot/Lexer.x # source generation: SPDX +SPDX_LICENSE_HS:=Cabal/Distribution/SPDX/LicenseId.hs +SPDX_EXCEPTION_HS:=Cabal/Distribution/SPDX/LicenseExceptionId.hs + spdx : $(SPDX_LICENSE_HS) $(SPDX_EXCEPTION_HS) $(SPDX_LICENSE_HS) : boot/SPDX.LicenseId.template.hs cabal-dev-scripts/src/GenUtils.hs cabal-dev-scripts/src/GenSPDX.hs license-list-data/licenses-3.0.json license-list-data/licenses-3.2.json @@ -42,6 +43,15 @@ $(SPDX_LICENSE_HS) : boot/SPDX.LicenseId.template.hs cabal-dev-scripts/src/GenUt $(SPDX_EXCEPTION_HS) : boot/SPDX.LicenseExceptionId.template.hs cabal-dev-scripts/src/GenUtils.hs cabal-dev-scripts/src/GenSPDXExc.hs license-list-data/exceptions-3.0.json license-list-data/exceptions-3.2.json cabal v2-run --builddir=dist-newstyle-meta --project-file=cabal.project.meta gen-spdx-exc -- boot/SPDX.LicenseExceptionId.template.hs license-list-data/exceptions-3.0.json license-list-data/exceptions-3.2.json license-list-data/exceptions-3.6.json $(SPDX_EXCEPTION_HS) +# source generation: templates + +TEMPLATE_MACROS:=Cabal/Distribution/Simple/Build/Macros/Z.hs + +templates : $(TEMPLATE_MACROS) + +$(TEMPLATE_MACROS) : boot/cabal_macros.template.h cabal-dev-scripts/src/GenCabalMacros.hs + cabal v2-run --builddir=dist-newstyle-meta --project-file=cabal.project.meta gen-cabal-macros -- $< $@ + # cabal-install.cabal file generation cabal-install-prod : cabal-install/cabal-install.cabal.pp diff --git a/boot/cabal_macros.template.h b/boot/cabal_macros.template.h new file mode 100644 index 00000000000..4cd5956bc2f --- /dev/null +++ b/boot/cabal_macros.template.h @@ -0,0 +1,41 @@ +/* DO NOT EDIT: This file is automatically generated by Cabal */ + +{% for pkg in packages %} +/* package {{ pkg.name }}-{{ pkg.version }} */ +#ifndef VERSION_{{ manglePkgName pkg.name }} +#define VERSION_{{ manglePkgName pkg.name }} "{{ pkg.version }}" +#endif /* VERSION_{{ manglePkgName pkg.name }} */ +#ifndef MIN_VERSION_{{ manglePkgName pkg.name }} +#define MIN_VERSION_{{ manglePkgName pkg.name }}(major1,major2,minor) (\ + (major1) < {{ pkg.x }} || \ + (major1) == {{ pkg.x }} && (major2) < {{ pkg.y }} || \ + (major1) == {{ pkg.x }} && (major2) == {{ pkg.y }} && (minor) <= {{ pkg.z }}) +#endif /* MIN_VERSION_{{ manglePkgName pkg.name }} */ +{% endfor %} + +{% for tool in tools %} +/* package {{ tool.name }}-{{ tool.version }} */ +#ifndef TOOL_VERSION_{{ mangleStr tool.name }} +#define TOOL_VERSION_{{ mangleStr tool.name }} "{{ tool.version }}" +#endif /* VERSION_{{ mangleStr tool.name }} */ +#ifndef MIN_TOOL_VERSION_{{ mangleStr tool.name }} +#define MIN_TOOL_VERSION_{{ mangleStr tool.name }}(major1,major2,minor) (\ + (major1) < {{ tool.x }} || \ + (major1) == {{ tool.x }} && (major2) < {{ tool.y }} || \ + (major1) == {{ tool.x }} && (major2) == {{ tool.y }} && (minor) <= {{ tool.z }}) +#endif /* MIN_VERSION_{{ mangleStr tool.name }} */ +{% endfor %} + +{% if notNull packageKey %} +#ifndef CURRENT_packageKey +#define CURRENT_packageKey "{{ packageKey }}" +#endif /* CURRENT_packageKey */ +{% endif %} +{% if notNull componentId %} +#ifndef CURRENT_COMPONENT_ID +#define CURRENT_COMPONENT_ID "{{ componentId }}" +#endif /* CURRENT_COMPONENT_ID */ +{% endif %} +#ifndef CURRENT_PACKAGE_VERSION +#define CURRENT_PACKAGE_VERSION "{{ packageVersion }}" +#endif /* CURRENT_PACKAGE_VERSION */ diff --git a/boot/ci-quick-jobs.template.yml b/boot/ci-quick-jobs.template.yml index c30aec6b95b..afaa63e646b 100644 --- a/boot/ci-quick-jobs.template.yml +++ b/boot/ci-quick-jobs.template.yml @@ -42,6 +42,7 @@ jobs: make lexer make gen-extra-source-files make spdx + make templates make github-actions - name: Check that diff is clean run: | diff --git a/cabal-dev-scripts/cabal-dev-scripts.cabal b/cabal-dev-scripts/cabal-dev-scripts.cabal index 1c13b9242c6..a03ec40d743 100644 --- a/cabal-dev-scripts/cabal-dev-scripts.cabal +++ b/cabal-dev-scripts/cabal-dev-scripts.cabal @@ -66,3 +66,17 @@ executable gen-validate , bytestring , HsYAML ^>=0.2.1.0 , zinza ^>=0.2 + +executable gen-cabal-macros + default-language: Haskell2010 + main-is: GenCabalMacros.hs + other-modules: Capture + hs-source-dirs: src + ghc-options: -Wall + build-depends: + , base + , bytestring + , Cabal + , syb ^>=0.7.1 + , template-haskell + , zinza ^>=0.2 diff --git a/cabal-dev-scripts/src/Capture.hs b/cabal-dev-scripts/src/Capture.hs new file mode 100644 index 00000000000..886fb035023 --- /dev/null +++ b/cabal-dev-scripts/src/Capture.hs @@ -0,0 +1,31 @@ +module Capture (capture) where + +import Language.Haskell.TH +import Language.Haskell.TH.Syntax (NameFlavour (..), Name (..)) +import Control.Monad.IO.Class + +import Data.Generics as SYB + +-- | Capture the source code of declarations in the variable +capture + :: String -- ^ variable name + -> Q [Dec] -- ^ definitions + -> Q [Dec] +capture name decls = do + decls1 <- decls + + -- mangle all names to drop unique suffixes and module prefixes + let decls2 = SYB.everywhere (SYB.mkT mangleName) decls1 + let declsStr = pprint decls2 + -- liftIO (putStrLn declsStr) + + let nameTyDecl :: Dec + nameTyDecl = SigD (mkName name) (ConT (mkName "String")) + + nameDecl :: Dec + nameDecl = ValD (VarP $ mkName name) (NormalB (LitE (StringL declsStr))) [] + + return $ nameTyDecl : nameDecl : decls1 + where + mangleName :: Name -> Name + mangleName (Name occ _) = Name occ NameS diff --git a/cabal-dev-scripts/src/GenCabalMacros.hs b/cabal-dev-scripts/src/GenCabalMacros.hs new file mode 100644 index 00000000000..d4679b8a426 --- /dev/null +++ b/cabal-dev-scripts/src/GenCabalMacros.hs @@ -0,0 +1,119 @@ +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TemplateHaskell #-} +{-# OPTIONS_GHC -Wno-orphans #-} +module Main (main) where + +import Control.Exception (SomeException (..), catch, displayException) +import Distribution.Types.PackageName (PackageName) +import Distribution.Types.Version (Version) +import GHC.Generics (Generic) +import System.Environment (getArgs) +import System.Exit (exitFailure) +import Zinza + (ModuleConfig (..), Ty (..), Zinza (..), genericFromValueSFP, genericToTypeSFP, + genericToValueSFP, parseAndCompileModuleIO) + +import Capture + +------------------------------------------------------------------------------- +-- Inputs +------------------------------------------------------------------------------- + +$(capture "decls" [d| + data Z = Z + { zPackages :: [ZPackage] + , zTools :: [ZTool] + , zPackageKey :: String + , zComponentId :: String + , zPackageVersion :: Version + , zNotNull :: String -> Bool + , zManglePkgName :: PackageName -> String + , zMangleStr :: String -> String + } + deriving (Generic) + + data ZPackage = ZPackage + { zpkgName :: PackageName + , zpkgVersion :: Version + , zpkgX :: String + , zpkgY :: String + , zpkgZ :: String + } + deriving (Generic) + + data ZTool = ZTool + { ztoolName :: String + , ztoolVersion :: Version + , ztoolX :: String + , ztoolY :: String + , ztoolZ :: String + } + deriving (Generic) + |]) + +------------------------------------------------------------------------------- +-- Main +------------------------------------------------------------------------------- + +withIO :: (FilePath -> FilePath -> IO a) -> IO a +withIO k = do + args <- getArgs + case args of + [src,tgt] -> k src tgt `catch` \(SomeException e) -> do + putStrLn $ "Exception: " ++ displayException e + exitFailure + _ -> do + putStrLn "Usage cabal v2-run ... source.temeplate.ext target.ext" + exitFailure + +main :: IO () +main = withIO $ \src tgt -> do + mdl <- parseAndCompileModuleIO config src + writeFile tgt mdl + +config :: ModuleConfig Z +config = ModuleConfig + { mcRender = "render" + , mcHeader = + [ "{-# LANGUAGE DeriveGeneric #-}" + , "module Distribution.Simple.Build.Macros.Z (render, Z(..), ZPackage (..), ZTool (..)) where" + , "import Distribution.ZinzaPrelude" + , decls + , "render :: Z -> String" + ] + } + +------------------------------------------------------------------------------- +-- Zinza instances +------------------------------------------------------------------------------- + +instance Zinza Z where + toType = genericToTypeSFP + toValue = genericToValueSFP + fromValue = genericFromValueSFP + +instance Zinza ZPackage where + toType = genericToTypeSFP + toValue = genericToValueSFP + fromValue = genericFromValueSFP + +instance Zinza ZTool where + toType = genericToTypeSFP + toValue = genericToValueSFP + fromValue = genericFromValueSFP + +------------------------------------------------------------------------------- +-- Orphans +------------------------------------------------------------------------------- + +instance Zinza PackageName where + toType _ = TyString (Just "prettyShow") + toValue _ = error "not needed" + fromValue _ = error "not needed" + +instance Zinza Version where + toType _ = TyString (Just "prettyShow") + toValue _ = error "not needed" + fromValue _ = error "not needed" diff --git a/cabal-install/Distribution/Client/SetupWrapper.hs b/cabal-install/Distribution/Client/SetupWrapper.hs index 79dccc6679a..5a8dac7def1 100644 --- a/cabal-install/Distribution/Client/SetupWrapper.hs +++ b/cabal-install/Distribution/Client/SetupWrapper.hs @@ -903,7 +903,7 @@ getExternalSetupMethod verbosity options pkg bt = do let ghcCmdLine = renderGhcOptions compiler platform ghcOptions when (useVersionMacros options') $ rewriteFileEx verbosity cppMacrosFile - (generatePackageVersionMacros (map snd selectedDeps)) + (generatePackageVersionMacros (pkgVersion $ package pkg) (map snd selectedDeps)) case useLoggingHandle options of Nothing -> runDbProgram verbosity program progdb ghcCmdLine