-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Simple.Build.Macros
-- Copyright   :  Simon Marlow 2008
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- Generate cabal_macros.h - CPP macros for package version testing
--
-- When using CPP you get
--
-- > VERSION_<package>
-- > MIN_VERSION_<package>(A,B,C)
--
-- for each /package/ in @build-depends@, which is true if the version of
-- /package/ in use is @>= A.B.C@, using the normal ordering on version
-- numbers.
--
module Distribution.Simple.Build.Macros (
    generate
  ) where

import Distribution.Package
         ( PackageIdentifier(PackageIdentifier) )
import Distribution.Version
         ( Version(versionBranch) )
import Distribution.PackageDescription
         ( PackageDescription )
import Distribution.Simple.LocalBuildInfo
        ( LocalBuildInfo, externalPackageDeps )
import Distribution.Text
         ( display )

-- ------------------------------------------------------------
-- * Generate cabal_macros.h
-- ------------------------------------------------------------

generate :: PackageDescription -> LocalBuildInfo -> String
generate _pkg_descr lbi = concat :: [[a]] -> [a]concat ($) :: (a -> b) -> a -> b$
  "/* DO NOT EDIT: This file is automatically generated by Cabal */\n\n" (:) :: a -> [a] -> [a]:
  [ concat :: [[a]] -> [a]concat
    ["/* package ",display :: Text a => a -> Stringdisplay pkgid :: PackageIdpkgid," */\n"
    ,"#define VERSION_",pkgname :: [Char]pkgname," ",show :: Show a => a -> Stringshow (display :: Text a => a -> Stringdisplay version :: Versionversion),"\n"
    ,"#define MIN_VERSION_",pkgname :: [Char]pkgname,"(major1,major2,minor) (\\\n"
    ,"  (major1) <  ",major1 :: Stringmajor1," || \\\n"
    ,"  (major1) == ",major1 :: Stringmajor1," && (major2) <  ",major2 :: Stringmajor2," || \\\n"
    ,"  (major1) == ",major1 :: Stringmajor1," && (major2) == ",major2 :: Stringmajor2," && (minor) <= ",minor :: Stringminor,")"
    ,"\n\n"
    ]
  | (_, pkgid@(PackageIdentifier name version)) <- externalPackageDeps ::
  LocalBuildInfo -> [(InstalledPackageId, PackageId)]externalPackageDeps lbi :: LocalBuildInfolbi
  , let (major1:major2:minor:_) = map :: (a -> b) -> [a] -> [b]map show :: Show a => a -> Stringshow (versionBranch :: Version -> [Int]versionBranch version :: Versionversion (++) :: [a] -> [a] -> [a]++ repeat :: a -> [a]repeat 0)
        pkgname = map :: (a -> b) -> [a] -> [b]map fixchar :: Char -> Charfixchar (display :: Text a => a -> Stringdisplay name :: PackageNamename)
  ]
  where fixchar '-' = '_'
        fixchar c   = c :: Charc