module Distribution.Simple.Build.Macros (
generate,
generatePackageVersionMacros,
) where
import Data.Maybe
( isJust )
import Distribution.Package
( PackageIdentifier(PackageIdentifier) )
import Distribution.Version
( Version(versionBranch) )
import Distribution.PackageDescription
( PackageDescription ( package ) )
import Distribution.Simple.LocalBuildInfo
( LocalBuildInfo(withPrograms), externalPackageDeps
, localComponentId, localCompatPackageKey )
import Distribution.Simple.Program.Db
( configuredPrograms )
import Distribution.Simple.Program.Types
( ConfiguredProgram(programId, programVersion) )
import Distribution.Text
( display )
generate :: PackageDescription -> LocalBuildInfo -> String
generate pkg_descr lbi =
"/* DO NOT EDIT: This file is automatically generated by Cabal */\n\n" ++
generatePackageVersionMacros
(package pkg_descr : map snd (externalPackageDeps lbi)) ++
generateToolVersionMacros (configuredPrograms . withPrograms $ lbi) ++
generateComponentIdMacro lbi
generatePackageVersionMacros :: [PackageIdentifier] -> String
generatePackageVersionMacros pkgids = concat
[ "/* package " ++ display pkgid ++ " */\n"
++ generateMacros "" pkgname version
| pkgid@(PackageIdentifier name version) <- pkgids
, let pkgname = map fixchar (display name)
]
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
]
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)
generateComponentIdMacro :: LocalBuildInfo -> String
generateComponentIdMacro lbi =
concat
[ "#define CURRENT_COMPONENT_ID \"" ++ display (localComponentId lbi) ++ "\"\n\n"
, "#define CURRENT_PACKAGE_KEY \"" ++ localCompatPackageKey lbi ++ "\"\n\n"
]
fixchar :: Char -> Char
fixchar '-' = '_'
fixchar c = c