1 -----------------------------------------------------------------------------
4 -- Module : Distribution.Simple.Build.Macros
5 -- Copyright : Simon Marlow 2008
7 -- Maintainer : cabal-devel@haskell.org
8 -- Portability : portable
10 -- Generate cabal_macros.h - CPP macros for package version testing
12 -- When using CPP you get
14 -- > VERSION_<package>
15 -- > MIN_VERSION_<package>(A,B,C)
17 -- for each /package/ in @build-depends@, which is true if the version of
18 -- /package/ in use is @>= A.B.C@, using the normal ordering on version
21 -- TODO Figure out what to do about backpack and internal libraries. It is very
22 -- suspicious that this stuff works with munged package identifiers
23 module Distribution
.Simple
.Build
.Macros
24 ( generateCabalMacrosHeader
25 , generatePackageVersionMacros
28 import Distribution
.Compat
.Prelude
31 import Distribution
.PackageDescription
32 import Distribution
.Pretty
33 import Distribution
.Simple
.LocalBuildInfo
34 import Distribution
.Simple
.Program
.Db
35 import Distribution
.Simple
.Program
.Types
36 import Distribution
.Types
.MungedPackageId
37 import Distribution
.Types
.MungedPackageName
38 import Distribution
.Version
40 import qualified Distribution
.Simple
.Build
.Macros
.Z
as Z
42 -- | The contents of the @cabal_macros.h@ for the given configured package.
43 generateCabalMacrosHeader
:: PackageDescription
-> LocalBuildInfo
-> ComponentLocalBuildInfo
-> String
44 generateCabalMacrosHeader pkg_descr lbi clbi
=
47 { Z
.zPackages
= map mkZPackage
$ package pkg_descr
: map getPid
(componentPackageDeps clbi
)
50 { Z
.ztoolName
= programId prog
51 , Z
.ztoolVersion
= ver
56 | prog
<- configuredPrograms
$ withPrograms lbi
57 , ver
<- maybeToList (programVersion prog
)
58 , let (major1
, major2
, minor
) = majorMinor ver
60 , Z
.zPackageKey
= case clbi
of
61 LibComponentLocalBuildInfo
{componentCompatPackageKey
= compatPackageKey
} -> compatPackageKey
63 , Z
.zComponentId
= prettyShow
(componentComponentId clbi
)
64 , Z
.zPackageVersion
= pkgVersion
(package pkg_descr
)
65 , Z
.zNotNull
= not . null
66 , Z
.zManglePkgName
= map fixchar
. unPackageName
67 , Z
.zMangleStr
= map fixchar
70 getPid
(_
, MungedPackageId
(MungedPackageName pn _
) v
) =
71 -- NB: Drop the library name! We're just reporting package versions.
72 -- This would have to be revisited if you are allowed to depend
73 -- on different versions of the same package
74 PackageIdentifier pn v
76 -- | Helper function that generates just the @VERSION_pkg@ and @MIN_VERSION_pkg@
77 -- macros for a list of package ids (usually used with the specific deps of
78 -- a configured package).
79 generatePackageVersionMacros
:: Version
-> [PackageId
] -> String
80 generatePackageVersionMacros ver pkgids
=
83 { Z
.zPackages
= map mkZPackage pkgids
87 , Z
.zPackageVersion
= ver
88 , Z
.zNotNull
= not . null
89 , Z
.zManglePkgName
= map fixchar
. unPackageName
90 , Z
.zMangleStr
= map fixchar
93 mkZPackage
:: PackageId
-> Z
.ZPackage
94 mkZPackage
(PackageIdentifier name ver
) =
103 (major1
, major2
, minor
) = majorMinor ver
105 majorMinor
:: Version
-> (String, String, String)
106 majorMinor ver
= case map show (versionNumbers ver
) of
107 [] -> ("0", "0", "0")
109 [x
, y
] -> (x
, y
, "0")
110 (x
: y
: z
: _
) -> (x
, y
, z
)
112 fixchar
:: Char -> Char