-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Simple.LocalBuildInfo
-- Copyright   :  Isaac Jones 2003-2004
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- Once a package has been configured we have resolved conditionals and
-- dependencies, configured the compiler and other needed external programs.
-- The 'LocalBuildInfo' is used to hold all this information. It holds the
-- install dirs, the compiler, the exact package dependencies, the configured
-- programs, the package database to use and a bunch of miscellaneous configure
-- flags. It gets saved and reloaded from a file (@dist\/setup-config@). It gets
-- passed in to very many subsequent build actions.

{- All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

module Distribution.Simple.LocalBuildInfo (
        LocalBuildInfo(..),
        externalPackageDeps,
        inplacePackageId,
        withLibLBI,
        withExeLBI,
        withComponentsLBI,
        withTestLBI,
        ComponentLocalBuildInfo(..),
        -- * Installation directories
        module Distribution.Simple.InstallDirs,
        absoluteInstallDirs, prefixRelativeInstallDirs,
        substPathTemplate
  ) where


import Distribution.Simple.InstallDirs hiding (absoluteInstallDirs,
                                               prefixRelativeInstallDirs,
                                               substPathTemplate, )
import qualified Distribution.Simple.InstallDirs as InstallDirs
import Distribution.Simple.Program (ProgramConfiguration)
import Distribution.PackageDescription
         ( PackageDescription(..), withLib, Library, withExe
         , Executable(exeName), withTest, TestSuite(..)
         , Component(..) )
import Distribution.Package
         ( PackageId, Package(..), InstalledPackageId(..) )
import Distribution.Simple.Compiler
         ( Compiler(..), PackageDBStack, OptimisationLevel )
import Distribution.Simple.PackageIndex
         ( PackageIndex )
import Distribution.Simple.Utils
         ( die )
import Distribution.Simple.Setup
         ( ConfigFlags )
import Distribution.Text
         ( display )

import Data.List (nub)

-- | Data cached after configuration step.  See also
-- 'Distribution.Simple.Setup.ConfigFlags'.
data executableConfigs :: [(String, ComponentLocalBuildInfo)]LocalBuildInfo = LocalBuildInfo {
        configFlags   :: ConfigFlags,
        -- ^ Options passed to the configuration step.
        -- Needed to re-run configuration when .cabal is out of date
        extraConfigArgs     :: [String],
        -- ^ Extra args on the command line for the configuration step.
        -- Needed to re-run configuration when .cabal is out of date
        installDirTemplates :: InstallDirTemplates,
                -- ^ The installation directories for the various differnt
                -- kinds of files
        --TODO: inplaceDirTemplates :: InstallDirs FilePath
        compiler      :: Compiler,
                -- ^ The compiler we're building with
        buildDir      :: FilePath,
                -- ^ Where to build the package.
        --TODO: eliminate hugs's scratchDir, use builddir
        scratchDir    :: FilePath,
                -- ^ Where to put the result of the Hugs build.
        libraryConfig       :: Maybe ComponentLocalBuildInfo,
        executableConfigs   :: [(String, ComponentLocalBuildInfo)],
        compBuildOrder :: [Component],
                -- ^ All the components to build, ordered by topological sort
                -- over the intrapackage dependency graph
        testSuiteConfigs    :: [(String, ComponentLocalBuildInfo)],
        installedPkgs :: PackageIndex,
                -- ^ All the info about the installed packages that the
                -- current package depends on (directly or indirectly).
        pkgDescrFile  :: Maybe FilePath,
                -- ^ the filename containing the .cabal file, if available
        localPkgDescr :: PackageDescription,
                -- ^ The resolved package description, that does not contain
                -- any conditionals.
        withPrograms  :: ProgramConfiguration, -- ^Location and args for all programs
        withPackageDB :: PackageDBStack,  -- ^What package database to use, global\/user
        withVanillaLib:: Bool,  -- ^Whether to build normal libs.
        withProfLib   :: Bool,  -- ^Whether to build profiling versions of libs.
        withSharedLib :: Bool,  -- ^Whether to build shared versions of libs.
        withDynExe    :: Bool,  -- ^Whether to link executables dynamically
        withProfExe   :: Bool,  -- ^Whether to build executables for profiling.
        withOptimization :: OptimisationLevel, -- ^Whether to build with optimization (if available).
        withGHCiLib   :: Bool,  -- ^Whether to build libs suitable for use with GHCi.
        splitObjs     :: Bool,  -- ^Use -split-objs with GHC, if available
        stripExes     :: Bool,  -- ^Whether to strip executables during install
        progPrefix    :: PathTemplate, -- ^Prefix to be prepended to installed executables
        progSuffix    :: PathTemplate -- ^Suffix to be appended to installed executables
  } deriving (D:Read ::
  (Int -> ReadS a)
  -> ReadS [a]
  -> ReadPrec a
  -> ReadPrec [a]
  -> T:Read aRead, D:Show ::
  (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> T:Show aShow)

data componentPackageDeps :: [(InstalledPackageId, PackageId)]ComponentLocalBuildInfo = ComponentLocalBuildInfo {
    -- | Resolved internal and external package dependencies for this component.
    -- The 'BuildInfo' specifies a set of build dependencies that must be
    -- satisfied in terms of version ranges. This field fixes those dependencies
    -- to the specific versions available on this machine for this compiler.
    componentPackageDeps :: [(InstalledPackageId, PackageId)]
  }
  deriving (D:Read ::
  (Int -> ReadS a)
  -> ReadS [a]
  -> ReadPrec a
  -> ReadPrec [a]
  -> T:Read aRead, D:Show ::
  (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> T:Show aShow)

-- | External package dependencies for the package as a whole, the union of the
-- individual 'targetPackageDeps'.
externalPackageDeps :: LocalBuildInfo -> [(InstalledPackageId, PackageId)]
externalPackageDeps lbi = nub :: Eq a => [a] -> [a]nub ($) :: (a -> b) -> a -> b$
  -- TODO:  what about non-buildable components?
     maybe :: b -> (a -> b) -> Maybe a -> bmaybe [] :: [a][] componentPackageDeps ::
  ComponentLocalBuildInfo -> [(InstalledPackageId, PackageId)]componentPackageDeps (libraryConfig :: LocalBuildInfo -> Maybe ComponentLocalBuildInfolibraryConfig lbi :: LocalBuildInfolbi)
  (++) :: [a] -> [a] -> [a]++ concatMap :: (a -> [b]) -> [a] -> [b]concatMap (componentPackageDeps ::
  ComponentLocalBuildInfo -> [(InstalledPackageId, PackageId)]componentPackageDeps (.) :: (b -> c) -> (a -> b) -> a -> c. snd :: (a, b) -> bsnd) (executableConfigs ::
  LocalBuildInfo -> [(String, ComponentLocalBuildInfo)]executableConfigs lbi :: LocalBuildInfolbi)

-- | The installed package Id we use for local packages registered in the local
-- package db. This is what is used for intra-package deps between components.
--
inplacePackageId :: PackageId -> InstalledPackageId
inplacePackageId pkgid = InstalledPackageId :: String -> InstalledPackageIdInstalledPackageId (display :: Text a => a -> Stringdisplay pkgid :: PackageIdpkgid (++) :: [a] -> [a] -> [a]++ "-inplace")

-- |If the package description has a library section, call the given
--  function with the library build info as argument.  Extended version of
-- 'withLib' that also gives corresponding build info.
withLibLBI :: PackageDescription -> LocalBuildInfo
           -> (Library -> ComponentLocalBuildInfo -> IO ()) -> IO ()
withLibLBI pkg_descr lbi f = withLib :: PackageDescription -> (Library -> IO ()) -> IO ()withLib pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$ \lib ->
  case libraryConfig :: LocalBuildInfo -> Maybe ComponentLocalBuildInfolibraryConfig lbi :: LocalBuildInfolbi of
    Just clbi -> f :: Library -> ComponentLocalBuildInfo -> IO ()f lib :: Librarylib clbi :: ComponentLocalBuildInfoclbi
    Nothing   -> die :: String -> IO adie ($) :: (a -> b) -> a -> b$ "internal error: the package contains a library "
                    (++) :: [a] -> [a] -> [a]++ "but there is no corresponding configuration data"

-- | Perform the action on each buildable 'Executable' in the package
-- description.  Extended version of 'withExe' that also gives corresponding
-- build info.
withExeLBI :: PackageDescription -> LocalBuildInfo
           -> (Executable -> ComponentLocalBuildInfo -> IO ()) -> IO ()
withExeLBI pkg_descr lbi f = withExe :: PackageDescription -> (Executable -> IO ()) -> IO ()withExe pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$ \exe ->
  case lookup :: Eq a => a -> [(a, b)] -> Maybe blookup (exeName :: Executable -> StringexeName exe :: Executableexe) (executableConfigs ::
  LocalBuildInfo -> [(String, ComponentLocalBuildInfo)]executableConfigs lbi :: LocalBuildInfolbi) of
    Just clbi -> f :: Library -> ComponentLocalBuildInfo -> IO ()f exe :: Executableexe clbi :: ComponentLocalBuildInfoclbi
    Nothing   -> die :: String -> IO adie ($) :: (a -> b) -> a -> b$ "internal error: the package contains an executable "
                    (++) :: [a] -> [a] -> [a]++ exeName :: Executable -> StringexeName exe :: Executableexe (++) :: [a] -> [a] -> [a]++ " but there is no corresponding "
                    (++) :: [a] -> [a] -> [a]++ "configuration data"

-- | Perform the action on each buildable 'Library' or 'Executable' (Component)
-- in the PackageDescription, subject to the build order specified by the
-- 'compBuildOrder' field of the given 'LocalBuildInfo'
withComponentsLBI :: LocalBuildInfo
                  -> (Component -> ComponentLocalBuildInfo -> IO ())
                  -> IO ()
withComponentsLBI lbi f = mapM_ :: Monad m => (a -> m b) -> [a] -> m ()mapM_ compF :: Component -> IO ()compF (compBuildOrder :: LocalBuildInfo -> [Component]compBuildOrder lbi :: LocalBuildInfolbi)
  where
    compF l@(CLib _lib) =
      case libraryConfig :: LocalBuildInfo -> Maybe ComponentLocalBuildInfolibraryConfig lbi :: LocalBuildInfolbi of
        Just clbi -> f :: Library -> ComponentLocalBuildInfo -> IO ()f l :: Componentl clbi :: ComponentLocalBuildInfoclbi
        Nothing   -> die :: String -> IO adie ($) :: (a -> b) -> a -> b$ "internal error: the package contains a library "
                        (++) :: [a] -> [a] -> [a]++ "but there is no corresponding configuration data"
    compF r@(CExe exe) =
      case lookup :: Eq a => a -> [(a, b)] -> Maybe blookup (exeName :: Executable -> StringexeName exe :: Executableexe) (executableConfigs ::
  LocalBuildInfo -> [(String, ComponentLocalBuildInfo)]executableConfigs lbi :: LocalBuildInfolbi) of
        Just clbi -> f :: Library -> ComponentLocalBuildInfo -> IO ()f r :: Componentr clbi :: ComponentLocalBuildInfoclbi
        Nothing   -> die :: String -> IO adie ($) :: (a -> b) -> a -> b$ "internal error: the package contains an executable "
                        (++) :: [a] -> [a] -> [a]++ exeName :: Executable -> StringexeName exe :: Executableexe (++) :: [a] -> [a] -> [a]++ " but there is no corresponding "
                        (++) :: [a] -> [a] -> [a]++ "configuration data"
    compF t@(CTst test) =
      case lookup :: Eq a => a -> [(a, b)] -> Maybe blookup (testName :: TestSuite -> StringtestName test :: TestSuitetest) (testSuiteConfigs ::
  LocalBuildInfo -> [(String, ComponentLocalBuildInfo)]testSuiteConfigs lbi :: LocalBuildInfolbi) of
        Just clbi -> f :: Library -> ComponentLocalBuildInfo -> IO ()f t :: Componentt clbi :: ComponentLocalBuildInfoclbi
        Nothing -> die :: String -> IO adie ($) :: (a -> b) -> a -> b$ "internal error: the package contains a test suite "
                        (++) :: [a] -> [a] -> [a]++ testName :: TestSuite -> StringtestName test :: TestSuitetest (++) :: [a] -> [a] -> [a]++ " but there is no corresponding "
                        (++) :: [a] -> [a] -> [a]++ "configuration data"

withTestLBI :: PackageDescription -> LocalBuildInfo
            -> (TestSuite -> ComponentLocalBuildInfo -> IO ()) -> IO ()
withTestLBI pkg_descr lbi f =
    let wrapper test = case lookup :: Eq a => a -> [(a, b)] -> Maybe blookup (testName :: TestSuite -> StringtestName test :: TestSuitetest) (testSuiteConfigs ::
  LocalBuildInfo -> [(String, ComponentLocalBuildInfo)]testSuiteConfigs lbi :: LocalBuildInfolbi) of
            Just clbi -> f :: Library -> ComponentLocalBuildInfo -> IO ()f test :: TestSuitetest clbi :: ComponentLocalBuildInfoclbi
            Nothing -> die :: String -> IO adie ($) :: (a -> b) -> a -> b$ "internal error: the package contains a test suite "
                            (++) :: [a] -> [a] -> [a]++ testName :: TestSuite -> StringtestName test :: TestSuitetest (++) :: [a] -> [a] -> [a]++ " but there is no corresponding "
                            (++) :: [a] -> [a] -> [a]++ "configuration data"
    in withTest :: PackageDescription -> (TestSuite -> IO ()) -> IO ()withTest pkg_descr :: PackageDescriptionpkg_descr wrapper :: TestSuite -> IO ()wrapper

-- -----------------------------------------------------------------------------
-- Wrappers for a couple functions from InstallDirs

-- |See 'InstallDirs.absoluteInstallDirs'
absoluteInstallDirs :: PackageDescription -> LocalBuildInfo -> CopyDest
                    -> InstallDirs FilePath
absoluteInstallDirs pkg lbi copydest =
  absoluteInstallDirs ::
  PackageIdentifier
  -> CompilerId
  -> CopyDest
  -> InstallDirs PathTemplate
  -> InstallDirs FilePathInstallDirs.absoluteInstallDirs
    (packageId :: Package pkg => pkg -> PackageIdentifierpackageId pkg :: PackageDescriptionpkg)
    (compilerId :: Compiler -> CompilerIdcompilerId (compiler :: LocalBuildInfo -> Compilercompiler lbi :: LocalBuildInfolbi))
    copydest :: CopyDestcopydest
    (installDirTemplates :: LocalBuildInfo -> InstallDirTemplatesinstallDirTemplates lbi :: LocalBuildInfolbi)

-- |See 'InstallDirs.prefixRelativeInstallDirs'
prefixRelativeInstallDirs :: PackageId -> LocalBuildInfo
                          -> InstallDirs (Maybe FilePath)
prefixRelativeInstallDirs pkg_descr lbi =
  prefixRelativeInstallDirs ::
  PackageIdentifier
  -> CompilerId
  -> InstallDirTemplates
  -> InstallDirs (Maybe FilePath)InstallDirs.prefixRelativeInstallDirs
    (packageId :: Package pkg => pkg -> PackageIdentifierpackageId pkg_descr :: PackageDescriptionpkg_descr)
    (compilerId :: Compiler -> CompilerIdcompilerId (compiler :: LocalBuildInfo -> Compilercompiler lbi :: LocalBuildInfolbi))
    (installDirTemplates :: LocalBuildInfo -> InstallDirTemplatesinstallDirTemplates lbi :: LocalBuildInfolbi)

substPathTemplate :: PackageId -> LocalBuildInfo
                  -> PathTemplate -> FilePath
substPathTemplate pkgid lbi = fromPathTemplate :: PathTemplate -> FilePathfromPathTemplate
                                (.) :: (b -> c) -> (a -> b) -> a -> c. ( substPathTemplate ::
  PathTemplateEnv -> PathTemplate -> PathTemplateInstallDirs.substPathTemplate env :: PathTemplateEnvenv )
    where env = initialPathTemplateEnv ::
  PackageIdentifier -> CompilerId -> PathTemplateEnvinitialPathTemplateEnv
                   pkgid :: PackageIdpkgid
                   (compilerId :: Compiler -> CompilerIdcompilerId (compiler :: LocalBuildInfo -> Compilercompiler lbi :: LocalBuildInfolbi))