Commit 68d43d61 authored by Mikhail Glushenkov's avatar Mikhail Glushenkov
Browse files

Merge pull request #1549 from 23Skidoo/tool-version

Add TOOL_VERSION_* macros to 'cabal_macros.h'.
parents 1d177fd2 6329f8d1
......@@ -22,6 +22,8 @@ module Distribution.Simple.Build.Macros (
generatePackageVersionMacros,
) where
import Data.Maybe
( isJust )
import Distribution.Package
( PackageIdentifier(PackageIdentifier) )
import Distribution.Version
......@@ -29,7 +31,11 @@ import Distribution.Version
import Distribution.PackageDescription
( PackageDescription )
import Distribution.Simple.LocalBuildInfo
( LocalBuildInfo, externalPackageDeps )
( LocalBuildInfo(withPrograms), externalPackageDeps )
import Distribution.Simple.Program.Db
( configuredPrograms )
import Distribution.Simple.Program.Types
( ConfiguredProgram(programId, programVersion) )
import Distribution.Text
( display )
......@@ -42,8 +48,8 @@ import Distribution.Text
generate :: PackageDescription -> LocalBuildInfo -> String
generate _pkg_descr lbi =
"/* DO NOT EDIT: This file is automatically generated by Cabal */\n\n" ++
generatePackageVersionMacros (map snd (externalPackageDeps lbi))
generatePackageVersionMacros (map snd (externalPackageDeps lbi)) ++
generateToolVersionMacros (configuredPrograms . withPrograms $ lbi)
-- | 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
......@@ -51,19 +57,42 @@ generate _pkg_descr lbi =
--
generatePackageVersionMacros :: [PackageIdentifier] -> String
generatePackageVersionMacros pkgids = concat
[ concat
["/* package ",display pkgid," */\n"
,"#define VERSION_",pkgname," ",show (display version),"\n"
,"#define MIN_VERSION_",pkgname,"(major1,major2,minor) (\\\n"
," (major1) < ",major1," || \\\n"
," (major1) == ",major1," && (major2) < ",major2," || \\\n"
," (major1) == ",major1," && (major2) == ",major2," && (minor) <= ",minor,")"
,"\n\n"
]
[ "/* package " ++ display pkgid ++ " */\n"
++ generateMacros "" pkgname version
| pkgid@(PackageIdentifier name version) <- pkgids
, let (major1:major2:minor:_) = map show (versionBranch version ++ repeat 0)
pkgname = map fixchar (display name)
, let pkgname = map fixchar (display 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
[ "/* tool " ++ progid ++ " */\n"
++ generateMacros "TOOL_" progname version
| prog <- progs
, isJust . programVersion $ prog
, let progid = programId prog ++ "-" ++ display version
progname = map fixchar (programId prog)
Just version = programVersion prog
]
-- | Common implementation of 'generatePackageVersionMacros' and
-- 'generateToolVersionMacros'.
--
generateMacros :: String -> String -> Version -> String
generateMacros prefix name version =
concat
["#define ", prefix, "VERSION_",name," ",show (display version),"\n"
,"#define MIN_", prefix, "VERSION_",name,"(major1,major2,minor) (\\\n"
," (major1) < ",major1," || \\\n"
," (major1) == ",major1," && (major2) < ",major2," || \\\n"
," (major1) == ",major1," && (major2) == ",major2," && (minor) <= ",minor,")"
,"\n\n"
]
where fixchar '-' = '_'
fixchar c = c
where
(major1:major2:minor:_) = map show (versionBranch version ++ repeat 0)
fixchar :: Char -> Char
fixchar '-' = '_'
fixchar c = c
......@@ -42,6 +42,7 @@ module Distribution.Simple.Program.Db (
userSpecifiedArgs,
lookupProgram,
updateProgram,
configuredPrograms,
-- ** Query and manipulate the program db
configureProgram,
......@@ -268,6 +269,10 @@ updateProgram prog = updateConfiguredProgs $
Map.insert (programId prog) prog
-- | List all configured programs.
configuredPrograms :: ProgramDb -> [ConfiguredProgram]
configuredPrograms = Map.elems . configuredProgs
-- ---------------------------
-- Configuring known programs
......
......@@ -1953,6 +1953,10 @@ the actual version of the package in use is greater than or equal to
lexicographic on the sequence, but numeric on each component, so for
example 1.2.0 is greater than 1.0.3).
Since version 1.20, there is also the `MIN_TOOL_VERSION_`_`tool`_ family of
macros for conditioning on the version of build tools used to build the program
(e.g. `hsc2hs`).
Cabal places the definitions of these macros into an
automatically-generated header file, which is included when
preprocessing Haskell source code by passing options to the C
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment