Make “sublibrary” standard terminology in docs
[cabal.git] / Cabal / src / Distribution / Simple / Build / Macros.hs
blob3dbce8616fc5b59c91ab708848c8ff13f6cabb7b
1 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Distribution.Simple.Build.Macros
5 -- Copyright : Simon Marlow 2008
6 --
7 -- Maintainer : cabal-devel@haskell.org
8 -- Portability : portable
9 --
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
19 -- numbers.
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
26 ) where
28 import Distribution.Compat.Prelude
29 import 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 =
45 Z.render
46 Z.Z
47 { Z.zPackages = map mkZPackage $ package pkg_descr : map getPid (componentPackageDeps clbi)
48 , Z.zTools =
49 [ Z.ZTool
50 { Z.ztoolName = programId prog
51 , Z.ztoolVersion = ver
52 , Z.ztoolX = major1
53 , Z.ztoolY = major2
54 , Z.ztoolZ = minor
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 clbi
62 _ -> ""
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
69 where
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 =
81 Z.render
82 Z.Z
83 { Z.zPackages = map mkZPackage pkgids
84 , Z.zTools = []
85 , Z.zPackageKey = ""
86 , Z.zComponentId = ""
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) =
95 Z.ZPackage
96 { Z.zpkgName = name
97 , Z.zpkgVersion = ver
98 , Z.zpkgX = major1
99 , Z.zpkgY = major2
100 , Z.zpkgZ = minor
102 where
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")
108 [x] -> (x, "0", "0")
109 [x, y] -> (x, y, "0")
110 (x : y : z : _) -> (x, y, z)
112 fixchar :: Char -> Char
113 fixchar '-' = '_'
114 fixchar c = c