module Distribution.Simple.Build.Macros (
generateCabalMacrosHeader,
generatePackageVersionMacros,
) where
import Prelude ()
import Distribution.Compat.Prelude
import Distribution.Version
import Distribution.PackageDescription
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Program.Db
import Distribution.Simple.Program.Types
import Distribution.Types.MungedPackageId
import Distribution.Types.MungedPackageName
import Distribution.Pretty
import qualified Distribution.Simple.Build.Macros.Z as Z
generateCabalMacrosHeader :: PackageDescription -> LocalBuildInfo -> ComponentLocalBuildInfo -> String
PackageDescription
pkg_descr LocalBuildInfo
lbi ComponentLocalBuildInfo
clbi = Z -> String
Z.render Z.Z
{ zPackages :: [ZPackage]
Z.zPackages = forall a b. (a -> b) -> [a] -> [b]
map PackageId -> ZPackage
mkZPackage forall a b. (a -> b) -> a -> b
$ PackageDescription -> PackageId
package PackageDescription
pkg_descr forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map forall {a}. (a, MungedPackageId) -> PackageId
getPid (ComponentLocalBuildInfo -> [(UnitId, MungedPackageId)]
componentPackageDeps ComponentLocalBuildInfo
clbi)
, zTools :: [ZTool]
Z.zTools =
[ Z.ZTool
{ ztoolName :: String
Z.ztoolName = ConfiguredProgram -> String
programId ConfiguredProgram
prog
, ztoolVersion :: Version
Z.ztoolVersion = Version
ver
, ztoolX :: String
Z.ztoolX = String
major1
, ztoolY :: String
Z.ztoolY = String
major2
, ztoolZ :: String
Z.ztoolZ = String
minor
}
| ConfiguredProgram
prog <- ProgramDb -> [ConfiguredProgram]
configuredPrograms forall a b. (a -> b) -> a -> b
$ LocalBuildInfo -> ProgramDb
withPrograms LocalBuildInfo
lbi
, Version
ver <- forall a. Maybe a -> [a]
maybeToList (ConfiguredProgram -> Maybe Version
programVersion ConfiguredProgram
prog)
, let (String
major1,String
major2,String
minor) = Version -> (String, String, String)
majorMinor Version
ver
]
, zPackageKey :: String
Z.zPackageKey = case ComponentLocalBuildInfo
clbi of
LibComponentLocalBuildInfo{} -> ComponentLocalBuildInfo -> String
componentCompatPackageKey ComponentLocalBuildInfo
clbi
ComponentLocalBuildInfo
_ -> String
""
, zComponentId :: String
Z.zComponentId = forall a. Pretty a => a -> String
prettyShow (ComponentLocalBuildInfo -> ComponentId
componentComponentId ComponentLocalBuildInfo
clbi)
, zPackageVersion :: Version
Z.zPackageVersion = PackageId -> Version
pkgVersion (PackageDescription -> PackageId
package PackageDescription
pkg_descr)
, zNotNull :: String -> Bool
Z.zNotNull = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null
, zManglePkgName :: PackageName -> String
Z.zManglePkgName = forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
fixchar forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageName -> String
unPackageName
, zMangleStr :: String -> String
Z.zMangleStr = forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
fixchar
}
where
getPid :: (a, MungedPackageId) -> PackageId
getPid (a
_, MungedPackageId (MungedPackageName PackageName
pn LibraryName
_) Version
v) =
PackageName -> Version -> PackageId
PackageIdentifier PackageName
pn Version
v
generatePackageVersionMacros :: Version -> [PackageId] -> String
generatePackageVersionMacros :: Version -> [PackageId] -> String
generatePackageVersionMacros Version
ver [PackageId]
pkgids = Z -> String
Z.render Z.Z
{ zPackages :: [ZPackage]
Z.zPackages = forall a b. (a -> b) -> [a] -> [b]
map PackageId -> ZPackage
mkZPackage [PackageId]
pkgids
, zTools :: [ZTool]
Z.zTools = []
, zPackageKey :: String
Z.zPackageKey = String
""
, zComponentId :: String
Z.zComponentId = String
""
, zPackageVersion :: Version
Z.zPackageVersion = Version
ver
, zNotNull :: String -> Bool
Z.zNotNull = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null
, zManglePkgName :: PackageName -> String
Z.zManglePkgName = forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
fixchar forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageName -> String
unPackageName
, zMangleStr :: String -> String
Z.zMangleStr = forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
fixchar
}
mkZPackage :: PackageId -> Z.ZPackage
mkZPackage :: PackageId -> ZPackage
mkZPackage (PackageIdentifier PackageName
name Version
ver) = Z.ZPackage
{ zpkgName :: PackageName
Z.zpkgName = PackageName
name
, zpkgVersion :: Version
Z.zpkgVersion = Version
ver
, zpkgX :: String
Z.zpkgX = String
major1
, zpkgY :: String
Z.zpkgY = String
major2
, zpkgZ :: String
Z.zpkgZ = String
minor
}
where
(String
major1,String
major2,String
minor) = Version -> (String, String, String)
majorMinor Version
ver
majorMinor :: Version -> (String, String, String)
majorMinor :: Version -> (String, String, String)
majorMinor Version
ver = case forall a b. (a -> b) -> [a] -> [b]
map forall a. Show a => a -> String
show (Version -> [Int]
versionNumbers Version
ver) of
[] -> (String
"0", String
"0", String
"0")
[String
x] -> (String
x, String
"0", String
"0")
[String
x,String
y] -> (String
x, String
y, String
"0")
(String
x:String
y:String
z:[String]
_) -> (String
x, String
y, String
z)
fixchar :: Char -> Char
fixchar :: Char -> Char
fixchar Char
'-' = Char
'_'
fixchar Char
c = Char
c