Build.hs 8.19 KB
Newer Older
1 2
-----------------------------------------------------------------------------
-- |
3
-- Module      :  Distribution.Simple.Build
4 5 6
-- Copyright   :  Isaac Jones 2003-2005,
--                Ross Paterson 2006,
--                Duncan Coutts 2007-2008
7
--
Duncan Coutts's avatar
Duncan Coutts committed
8
-- Maintainer  :  cabal-devel@haskell.org
ijones's avatar
ijones committed
9
-- Portability :  portable
10
--
Duncan Coutts's avatar
Duncan Coutts committed
11 12 13 14 15
-- This is the entry point to actually building the modules in a package. It
-- doesn't actually do much itself, most of the work is delegated to
-- compiler-specific actions. It does do some non-compiler specific bits like
-- running pre-processors.
--
16

17
{- Copyright (c) 2003-2005, Isaac Jones
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 47
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. -}

48
module Distribution.Simple.Build (
49 50 51 52
    build,

    initialBuildSteps,
    writeAutogenFiles,
53
  ) where
54

55 56
import qualified Distribution.Simple.GHC  as GHC
import qualified Distribution.Simple.JHC  as JHC
David Himmelstrup's avatar
David Himmelstrup committed
57
import qualified Distribution.Simple.LHC  as LHC
58 59 60 61 62 63 64 65
import qualified Distribution.Simple.NHC  as NHC
import qualified Distribution.Simple.Hugs as Hugs

import qualified Distribution.Simple.Build.Macros      as Build.Macros
import qualified Distribution.Simple.Build.PathsModule as Build.PathsModule

import Distribution.Package
         ( Package(..) )
66
import Distribution.Simple.Compiler
67
         ( CompilerFlavor(..), compilerFlavor, PackageDB(..) )
68
import Distribution.PackageDescription
69
         ( PackageDescription(..), BuildInfo(..)
70
         , Library(..), Executable(..) )
71
import qualified Distribution.ModuleName as ModuleName
72 73

import Distribution.Simple.Setup
74
         ( BuildFlags(..), fromFlag )
75 76
import Distribution.Simple.PreProcess
         ( preprocessSources, PPSuffixHandler )
77
import Distribution.Simple.LocalBuildInfo
78
         ( LocalBuildInfo(compiler, buildDir, withPackageDB)
79
         , ComponentLocalBuildInfo, withLibLBI, withExeLBI )
80
import Distribution.Simple.BuildPaths
81
         ( autogenModulesDir, autogenModuleName, cppHeaderName )
82
import Distribution.Simple.Register
83
         ( registerPackage, generateRegistrationInfo )
84
import Distribution.Simple.Utils
85 86
         ( createDirectoryIfMissingVerbose, rewriteFile
         , die, info, setupMessage )
87

88
import Distribution.Verbosity
89
         ( Verbosity )
90 91
import Distribution.Text
         ( display )
simonmar's avatar
simonmar committed
92

93 94 95
import Data.Maybe
         ( maybeToList )
import Control.Monad
96
         ( unless )
97 98 99
import System.FilePath
         ( (</>), (<.>) )

simonmar's avatar
simonmar committed
100
-- -----------------------------------------------------------------------------
ijones's avatar
ijones committed
101
-- |Build the libraries and executables in this package.
102

Simon Marlow's avatar
Simon Marlow committed
103
build    :: PackageDescription  -- ^mostly information from the .cabal file
ijones's avatar
ijones committed
104 105 106
         -> LocalBuildInfo -- ^Configuration information
         -> BuildFlags -- ^Flags that the user passed to build
         -> [ PPSuffixHandler ] -- ^preprocessors to run before compiling
ijones's avatar
ijones committed
107
         -> IO ()
108
build pkg_descr lbi flags suffixes = do
109 110 111
  let distPref  = fromFlag (buildDistPref flags)
      verbosity = fromFlag (buildVerbosity flags)
  initialBuildSteps distPref pkg_descr lbi verbosity suffixes
112
  setupMessage verbosity "Building" (packageId pkg_descr)
Simon Marlow's avatar
Simon Marlow committed
113

114 115
  internalPackageDB <- createInternalPackageDB distPref

116
  withLibLBI pkg_descr lbi $ \lib clbi -> do
117
    info verbosity "Building library..."
118
    buildLib verbosity pkg_descr lbi lib clbi
119

120 121 122
    installedPkgInfo <- generateRegistrationInfo verbosity pkg_descr lib
                               lbi clbi True{-inplace-} distPref

123 124 125
    -- Register the library in-place, so exes can depend
    -- on internally defined libraries.
    registerPackage verbosity
126
      installedPkgInfo pkg_descr lbi True{-inplace-} internalPackageDB
127 128 129 130 131

  -- Use the internal package db for the exes.
  let lbi' = lbi { withPackageDB = withPackageDB lbi ++ [internalPackageDB] }

  withExeLBI pkg_descr lbi' $ \exe clbi -> do
132
    info verbosity $ "Building executable " ++ exeName exe ++ "..."
133 134 135 136 137 138 139 140 141 142
    buildExe verbosity pkg_descr lbi' exe clbi

-- | Initialize a new package db file for libraries defined
-- internally to the package.
createInternalPackageDB :: FilePath -> IO PackageDB
createInternalPackageDB distPref = do
    let dbFile = distPref </> "package.conf.inplace"
        packageDB = SpecificPackageDB dbFile
    writeFile dbFile "[]"
    return packageDB
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

buildLib :: Verbosity -> PackageDescription -> LocalBuildInfo
                      -> Library            -> ComponentLocalBuildInfo -> IO ()
buildLib verbosity pkg_descr lbi lib clbi =
  case compilerFlavor (compiler lbi) of
    GHC  -> GHC.buildLib  verbosity pkg_descr lbi lib clbi
    JHC  -> JHC.buildLib  verbosity pkg_descr lbi lib clbi
    LHC  -> LHC.buildLib  verbosity pkg_descr lbi lib clbi
    Hugs -> Hugs.buildLib verbosity pkg_descr lbi lib clbi
    NHC  -> NHC.buildLib  verbosity pkg_descr lbi lib clbi
    _    -> die "Building is not supported with this compiler."

buildExe :: Verbosity -> PackageDescription -> LocalBuildInfo
                      -> Executable         -> ComponentLocalBuildInfo -> IO ()
buildExe verbosity pkg_descr lbi exe clbi =
  case compilerFlavor (compiler lbi) of
    GHC  -> GHC.buildExe  verbosity pkg_descr lbi exe clbi
    JHC  -> JHC.buildExe  verbosity pkg_descr lbi exe clbi
    LHC  -> LHC.buildExe  verbosity pkg_descr lbi exe clbi
    Hugs -> Hugs.buildExe verbosity pkg_descr lbi exe clbi
    NHC  -> NHC.buildExe  verbosity pkg_descr lbi exe clbi
    _    -> die "Building is not supported with this compiler."
Simon Marlow's avatar
Simon Marlow committed
165

166 167
initialBuildSteps :: FilePath -- ^"dist" prefix
                  -> PackageDescription  -- ^mostly information from the .cabal file
Ian Lynagh's avatar
Ian Lynagh committed
168
                  -> LocalBuildInfo -- ^Configuration information
169
                  -> Verbosity -- ^The verbosity to use
Ian Lynagh's avatar
Ian Lynagh committed
170 171
                  -> [ PPSuffixHandler ] -- ^preprocessors to run before compiling
                  -> IO ()
172
initialBuildSteps _distPref pkg_descr lbi verbosity suffixes = do
ijones's avatar
ijones committed
173 174 175 176 177
  -- check that there's something to build
  let buildInfos =
          map libBuildInfo (maybeToList (library pkg_descr)) ++
          map buildInfo (executables pkg_descr)
  unless (any buildable buildInfos) $ do
178
    let name = display (packageId pkg_descr)
ijones's avatar
ijones committed
179 180
    die ("Package " ++ name ++ " can't be built on this system.")

181
  createDirectoryIfMissingVerbose verbosity True (buildDir lbi)
182

183
  writeAutogenFiles verbosity pkg_descr lbi
184

185
  preprocessSources pkg_descr lbi False verbosity suffixes
ijones's avatar
ijones committed
186

187 188 189 190 191 192 193 194
-- | Generate and write out the Paths_<pkg>.hs and cabal_macros.h files
--
writeAutogenFiles :: Verbosity
                  -> PackageDescription
                  -> LocalBuildInfo
                  -> IO ()
writeAutogenFiles verbosity pkg lbi = do
  createDirectoryIfMissingVerbose verbosity True (autogenModulesDir lbi)
195

196 197 198
  let pathsModulePath = autogenModulesDir lbi
                    </> ModuleName.toFilePath (autogenModuleName pkg) <.> "hs"
  rewriteFile pathsModulePath (Build.PathsModule.generate pkg lbi)
199

200 201
  let cppHeaderPath = autogenModulesDir lbi </> cppHeaderName
  rewriteFile cppHeaderPath (Build.Macros.generate pkg lbi)