Install.hs 9.56 KB
Newer Older
1 2
-----------------------------------------------------------------------------
-- |
3
-- Module      :  Distribution.Simple.Install
4
-- Copyright   :  Isaac Jones 2003-2004
5
-- License     :  BSD3
6
--
Duncan Coutts's avatar
Duncan Coutts committed
7
-- Maintainer  :  cabal-devel@haskell.org
ijones's avatar
ijones committed
8
-- Portability :  portable
9
--
Duncan Coutts's avatar
Duncan Coutts committed
10 11 12 13
-- This is the entry point into installing a built package. Performs the
-- \"@.\/setup install@\" and \"@.\/setup copy@\" actions. It moves files into
-- place based on the prefix argument. It does the generic bits and then calls
-- compiler-specific functions to do the rest.
14

simonmar's avatar
simonmar committed
15
module Distribution.Simple.Install (
16
        install,
simonmar's avatar
simonmar committed
17
  ) where
18

19
import Distribution.PackageDescription
20
import Distribution.Package (Package(..))
21
import Distribution.Simple.LocalBuildInfo
22
import Distribution.Simple.BuildPaths (haddockName, haddockPref)
23
import Distribution.Simple.Utils
24 25 26
         ( createDirectoryIfMissingVerbose
         , installDirectoryContents, installOrdinaryFile, isInSearchPath
         , die, info, notice, warn, matchDirFileGlob )
27 28
import Distribution.Simple.Compiler
         ( CompilerFlavor(..), compilerFlavor )
29
import Distribution.Simple.Setup (CopyFlags(..), fromFlag)
30

31 32 33 34 35
import qualified Distribution.Simple.GHC   as GHC
import qualified Distribution.Simple.GHCJS as GHCJS
import qualified Distribution.Simple.JHC   as JHC
import qualified Distribution.Simple.LHC   as LHC
import qualified Distribution.Simple.UHC   as UHC
36
import qualified Distribution.Simple.HaskellSuite as HaskellSuite
37

38
import Control.Monad (when, unless)
39 40
import System.Directory
         ( doesDirectoryExist, doesFileExist )
41 42
import System.FilePath
         ( takeFileName, takeDirectory, (</>), isAbsolute )
simonmar's avatar
simonmar committed
43

44
import Distribution.Verbosity
45 46
import Distribution.Text
         ( display )
47

ijones's avatar
ijones committed
48
-- |Perform the \"@.\/setup install@\" and \"@.\/setup copy@\"
49
-- actions.  Move files into place based on the prefix argument.
50 51 52
--
-- This does NOT register libraries, you should call 'register'
-- to do that.
ijones's avatar
ijones committed
53 54 55 56

install :: PackageDescription -- ^information from the .cabal file
        -> LocalBuildInfo -- ^information from the configure step
        -> CopyFlags -- ^flags sent to copy or install
ijones's avatar
ijones committed
57
        -> IO ()
58
install pkg_descr lbi flags = do
59 60
  let distPref  = fromFlag (copyDistPref flags)
      verbosity = fromFlag (copyVerbosity flags)
61
      copydest  = fromFlag (copyDest flags)
62 63 64
      -- This is a bit of a hack, to handle files which are not
      -- per-component (data files and Haddock files.)
      InstallDirs {
65
         datadir    = dataPref,
66 67 68 69 70 71 72 73
         -- NB: The situation with Haddock is a bit delicate.  On the
         -- one hand, the easiest to understand Haddock documentation
         -- path is pkgname-0.1, which means it's per-package (not
         -- per-component).  But this means that it's impossible to
         -- install Haddock documentation for internal libraries.  We'll
         -- keep this constraint for now; this means you can't use
         -- Cabal to Haddock internal libraries.  This does not seem
         -- like a big problem.
74
         docdir     = docPref,
75
         htmldir    = htmlPref,
76
         haddockdir = interfacePref}
77 78 79 80
             -- Notice use of 'absoluteInstallDirs' (not the
             -- per-component variant).  This means for non-library
             -- packages we'll just pick a nondescriptive foo-0.1
             = absoluteInstallDirs pkg_descr lbi copydest
81

82 83
  unless (hasLibs pkg_descr || hasExes pkg_descr) $
      die "No executables and no library found. Nothing to do."
84 85 86 87 88

  -- Install (package-global) data files
  installDataFiles verbosity pkg_descr dataPref

  -- Install (package-global) Haddock files
89 90
  docExists <- doesDirectoryExist $ haddockPref distPref pkg_descr
  info verbosity ("directory " ++ haddockPref distPref pkg_descr ++
91
                  " does exist: " ++ show docExists)
92

93
  when docExists $ do
94
      createDirectoryIfMissingVerbose verbosity True htmlPref
95
      installDirectoryContents verbosity
96
          (haddockPref distPref pkg_descr) htmlPref
97
      -- setPermissionsRecursive [Read] htmlPref
98 99 100 101
      -- The haddock interface file actually already got installed
      -- in the recursive copy, but now we install it where we actually
      -- want it to be (normally the same place). We could remove the
      -- copy in htmlPref first.
102 103 104 105 106 107 108 109
      let haddockInterfaceFileSrc  = haddockPref distPref pkg_descr
                                                   </> haddockName pkg_descr
          haddockInterfaceFileDest = interfacePref </> haddockName pkg_descr
      -- We only generate the haddock interface file for libs, So if the
      -- package consists only of executables there will not be one:
      exists <- doesFileExist haddockInterfaceFileSrc
      when exists $ do
        createDirectoryIfMissingVerbose verbosity True interfacePref
110 111
        installOrdinaryFile verbosity haddockInterfaceFileSrc
                                      haddockInterfaceFileDest
Duncan Coutts's avatar
Duncan Coutts committed
112

Duncan Coutts's avatar
Duncan Coutts committed
113 114
  let lfiles = licenseFiles pkg_descr
  unless (null lfiles) $ do
115
    createDirectoryIfMissingVerbose verbosity True docPref
Duncan Coutts's avatar
Duncan Coutts committed
116 117 118
    sequence_
      [ installOrdinaryFile verbosity lfile (docPref </> takeFileName lfile)
      | lfile <- lfiles ]
Duncan Coutts's avatar
Duncan Coutts committed
119

120 121 122 123
  withLibLBI pkg_descr lbi $ \lib clbi -> do
    let InstallDirs{
            libdir = libPref,
            includedir = incPref
124
            } = absoluteComponentInstallDirs pkg_descr lbi (componentUnitId clbi) copydest
125
        buildPref = componentBuildDir lbi clbi
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    -- TODO: decide if we need the user to be able to control the libdir
    -- for shared libs independently of the one for static libs. If so
    -- it should also have a flag in the command line UI
    -- For the moment use dynlibdir = libdir
        dynlibPref = libPref

    if componentUnitId clbi == localUnitId lbi
        then notice verbosity ("Installing library in " ++ libPref)
        else notice verbosity ("Installing internal library " ++ libName lib ++ " in " ++ libPref)

    -- install include files for all compilers - they may be needed to compile
    -- haskell files (using the CPP extension)
    installIncludeFiles verbosity lib incPref

    case compilerFlavor (compiler lbi) of
      GHC   -> GHC.installLib   verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
      GHCJS -> GHCJS.installLib verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
      LHC   -> LHC.installLib   verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
      JHC   -> JHC.installLib   verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
      UHC   -> UHC.installLib   verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
      HaskellSuite _ -> HaskellSuite.installLib
                                verbosity lbi libPref dynlibPref buildPref pkg_descr lib clbi
      _ -> die $ "installing with "
              ++ display (compilerFlavor (compiler lbi))
              ++ " is not implemented"

  withExeLBI pkg_descr lbi $ \exe clbi -> do
    let installDirs@InstallDirs {
            bindir = binPref
155
            } = absoluteComponentInstallDirs pkg_descr lbi (componentUnitId clbi) copydest
156 157 158 159 160 161 162
        -- the installers know how to find the actual location of the
        -- binaries
        buildPref = buildDir lbi
        uid = componentUnitId clbi
        progPrefixPref = substPathTemplate (packageId pkg_descr) lbi uid (progPrefix lbi)
        progSuffixPref = substPathTemplate (packageId pkg_descr) lbi uid (progSuffix lbi)
    notice verbosity ("Installing executable " ++ exeName exe ++ " in " ++ binPref)
163 164 165 166
    inPath <- isInSearchPath binPref
    when (not inPath) $
      warn verbosity ("The directory " ++ binPref
                      ++ " is not in the system search path.")
167
    case compilerFlavor (compiler lbi) of
168 169 170 171 172 173 174 175 176
      GHC   -> GHC.installExe   verbosity lbi installDirs buildPref (progPrefixPref, progSuffixPref) pkg_descr exe
      GHCJS -> GHCJS.installExe verbosity lbi installDirs buildPref (progPrefixPref, progSuffixPref) pkg_descr exe
      LHC   -> LHC.installExe   verbosity lbi installDirs buildPref (progPrefixPref, progSuffixPref) pkg_descr exe
      JHC   -> JHC.installExe   verbosity binPref buildPref (progPrefixPref, progSuffixPref) pkg_descr exe
      UHC   -> return ()
      HaskellSuite {} -> return ()
      _ -> die $ "installing with "
              ++ display (compilerFlavor (compiler lbi))
              ++ " is not implemented"
simonmar's avatar
simonmar committed
177

178 179 180 181 182 183 184 185 186
-- | Install the files listed in data-files
--
installDataFiles :: Verbosity -> PackageDescription -> FilePath -> IO ()
installDataFiles verbosity pkg_descr destDataDir =
  flip mapM_ (dataFiles pkg_descr) $ \ file -> do
    let srcDataDir = dataDir pkg_descr
    files <- matchDirFileGlob srcDataDir file
    let dir = takeDirectory file
    createDirectoryIfMissingVerbose verbosity True (destDataDir </> dir)
187 188
    sequence_ [ installOrdinaryFile verbosity (srcDataDir  </> file')
                                              (destDataDir </> file')
189 190
              | file' <- files ]

191
-- | Install the files listed in install-includes for a library
192
--
193 194
installIncludeFiles :: Verbosity -> Library -> FilePath -> IO ()
installIncludeFiles verbosity lib destIncludeDir = do
195 196 197 198 199 200 201 202 203
    let relincdirs = "." : filter (not.isAbsolute) (includeDirs lbi)
        lbi = libBuildInfo lib
    incs <- mapM (findInc relincdirs) (installIncludes lbi)
    sequence_
      [ do createDirectoryIfMissingVerbose verbosity True destDir
           installOrdinaryFile verbosity srcFile destFile
      | (relFile, srcFile) <- incs
      , let destFile = destIncludeDir </> relFile
            destDir  = takeDirectory destFile ]
204 205
  where

206 207 208 209 210
   findInc []         file = die ("can't find include file " ++ file)
   findInc (dir:dirs) file = do
     let path = dir </> file
     exists <- doesFileExist path
     if exists then return (file, path) else findInc dirs file