LocalBuildInfo.hs 8.74 KB
Newer Older
ijones's avatar
ijones committed
1
2
3
4
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Simple.LocalBuildInfo
-- Copyright   :  Isaac Jones 2003-2004
5
--
Duncan Coutts's avatar
Duncan Coutts committed
6
-- Maintainer  :  cabal-devel@haskell.org
ijones's avatar
ijones committed
7
-- Portability :  portable
ijones's avatar
ijones committed
8
--
Duncan Coutts's avatar
Duncan Coutts committed
9
10
11
12
13
-- 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
14
-- flags. It gets saved and reloaded from a file (@dist\/setup-config@). It gets
Duncan Coutts's avatar
Duncan Coutts committed
15
-- passed in to very many subsequent build actions.
ijones's avatar
ijones committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

{- 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. -}

47
48
module Distribution.Simple.LocalBuildInfo (
        LocalBuildInfo(..),
49
        externalPackageDeps,
50
51
        withLibLBI,
        withExeLBI,
52
        ComponentLocalBuildInfo(..),
53
        isInternalPackage,
54
55
        -- * Installation directories
        module Distribution.Simple.InstallDirs,
56
        absoluteInstallDirs, prefixRelativeInstallDirs,
57
        substPathTemplate
58
  ) where
ijones's avatar
ijones committed
59

ijones's avatar
ijones committed
60

61
import Distribution.Simple.InstallDirs hiding (absoluteInstallDirs,
62
63
                                               prefixRelativeInstallDirs,
                                               substPathTemplate, )
64
import qualified Distribution.Simple.InstallDirs as InstallDirs
65
66
import Distribution.Simple.Setup (CopyDest(..))
import Distribution.Simple.Program (ProgramConfiguration)
67
68
69
import Distribution.PackageDescription
         ( PackageDescription(..), withLib, Library, withExe
         , Executable(exeName) )
70
import Distribution.Package (PackageId, Package(..))
71
import Distribution.Simple.Compiler
72
         ( Compiler(..), PackageDBStack, OptimisationLevel )
73
import Distribution.Simple.PackageIndex (PackageIndex)
74
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
75
76
import Distribution.Simple.Utils
         ( die )
ijones's avatar
ijones committed
77

78
79
import Data.List (nub)

80
81
-- |Data cached after configuration step.  See also
-- 'Distribution.Setup.ConfigFlags'.
ijones's avatar
ijones committed
82
data LocalBuildInfo = LocalBuildInfo {
83
        installDirTemplates :: InstallDirTemplates,
84
85
86
87
88
89
90
91
                -- ^ The installation directories for the various differnt
                -- kinds of files
        compiler      :: Compiler,
                -- ^ The compiler we're building with
        buildDir      :: FilePath,
                -- ^ Where to build the package.
        scratchDir    :: FilePath,
                -- ^ Where to put the result of the Hugs build.
92
93
        libraryConfig       :: Maybe ComponentLocalBuildInfo,
        executableConfigs   :: [(String, ComponentLocalBuildInfo)],
94
        installedPkgs :: PackageIndex InstalledPackageInfo,
95
                -- ^ All the info about all installed packages.
96
97
        pkgDescrFile  :: Maybe FilePath,
                -- ^ the filename containing the .cabal file, if available
98
99
100
        localPkgDescr :: PackageDescription,
                -- ^ The resolved package description, that does not contain
                -- any conditionals.
Duncan Coutts's avatar
Duncan Coutts committed
101
        withPrograms  :: ProgramConfiguration, -- ^Location and args for all programs
102
        withPackageDB :: PackageDBStack,  -- ^What package database to use, global\/user
103
104
        withVanillaLib:: Bool,  -- ^Whether to build normal libs.
        withProfLib   :: Bool,  -- ^Whether to build profiling versions of libs.
105
        withSharedLib :: Bool,  -- ^Whether to build shared versions of libs.
106
        withProfExe   :: Bool,  -- ^Whether to build executables for profiling.
107
        withOptimization :: OptimisationLevel, -- ^Whether to build with optimization (if available).
108
        withGHCiLib   :: Bool,  -- ^Whether to build libs suitable for use with GHCi.
109
        splitObjs     :: Bool,  -- ^Use -split-objs with GHC, if available
110
        stripExes     :: Bool,  -- ^Whether to strip executables during install
111
112
        progPrefix    :: PathTemplate, -- ^Prefix to be prepended to installed executables
        progSuffix    :: PathTemplate -- ^Suffix to be appended to installed executables
113
  } deriving (Read, Show)
114

115
data ComponentLocalBuildInfo = ComponentLocalBuildInfo {
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    -- | 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 :: [PackageId]
  }
  deriving (Read, Show)

-- | External package dependencies for the package as a whole, the union of the
-- individual 'targetPackageDeps'.
externalPackageDeps :: LocalBuildInfo -> [PackageId]
externalPackageDeps lbi = nub $
  -- TODO:  what about non-buildable components?
     maybe [] componentPackageDeps (libraryConfig lbi)
  ++ concatMap (componentPackageDeps . snd) (executableConfigs lbi)
ijones's avatar
ijones committed
131

132
133
134
135
136
137
138
-- |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 pkg_descr $ \lib ->
  case libraryConfig lbi of
139
140
141
    Just clbi -> f lib clbi
    Nothing   -> die $ "internal error: the package contains a library "
                    ++ "but there is no corresponding configuration data"
142
143
144
145
146
147
148
149

-- | 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 pkg_descr $ \exe ->
  case lookup (exeName exe) (executableConfigs lbi) of
150
151
152
153
    Just clbi -> f exe clbi
    Nothing   -> die $ "internal error: the package contains an executable "
                    ++ exeName exe ++ " but there is no corresponding "
                    ++ "configuration data"
154

155
156
157
158
159
-- True if the specified package (or library) is defined internally
-- to the package.
isInternalPackage :: PackageDescription -> PackageId -> Bool
isInternalPackage pkg_descr pkgid = packageId pkg_descr == pkgid

160

161
-- -----------------------------------------------------------------------------
162
163
164
-- Wrappers for a couple functions from InstallDirs

-- |See 'InstallDirs.absoluteInstallDirs'
165
absoluteInstallDirs :: PackageDescription -> LocalBuildInfo -> CopyDest
166
                    -> InstallDirs FilePath
167
absoluteInstallDirs pkg lbi copydest =
168
  InstallDirs.absoluteInstallDirs
169
    (packageId pkg)
170
171
172
173
174
    (compilerId (compiler lbi))
    copydest
    (installDirTemplates lbi)

-- |See 'InstallDirs.prefixRelativeInstallDirs'
175
prefixRelativeInstallDirs :: PackageId -> LocalBuildInfo
176
177
178
                          -> InstallDirs (Maybe FilePath)
prefixRelativeInstallDirs pkg_descr lbi =
  InstallDirs.prefixRelativeInstallDirs
179
    (packageId pkg_descr)
180
181
    (compilerId (compiler lbi))
    (installDirTemplates lbi)
182

183
substPathTemplate :: PackageId -> LocalBuildInfo
184
                  -> PathTemplate -> FilePath
185
substPathTemplate pkgid lbi = fromPathTemplate
186
                                . ( InstallDirs.substPathTemplate env )
187
    where env = initialPathTemplateEnv
188
                   pkgid
189
                   (compilerId (compiler lbi))