Commit 79b34ea4 authored by Mikhail Glushenkov's avatar Mikhail Glushenkov
Browse files

Add TOOL_VERSION_* macros to 'cabal_macros.h'.

Fixes #1547. Sample generated file: https://gist.github.com/23Skidoo/7026016
parent 282ebe6c
......@@ -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,43 @@ 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
]
where fixchar '-' = '_'
fixchar c = c
-- | 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
(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
......
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