Register.hs 17.3 KB
Newer Older
1 2
-----------------------------------------------------------------------------
-- |
ijones's avatar
ijones committed
3
-- Module      :  Distribution.Simple.Register
4
-- 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
8
--
Duncan Coutts's avatar
Duncan Coutts committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
-- This module deals with registering and unregistering packages. There are a
-- couple ways it can do this, one is to do it directly. Another is to generate
-- a script that can be run later to do it. The idea here being that the user
-- is shielded from the details of what command to use for package registration
-- for a particular compiler. In practice this aspect was not especially
-- popular so we also provide a way to simply generate the package registration
-- file which then must be manually passed to @ghc-pkg@. It is possible to
-- generate registration information for where the package is to be installed,
-- or alternatively to register the package inplace in the build tree. The
-- latter is occasionally handy, and will become more important when we try to
-- build multi-package systems.
--
-- This module does not delegate anything to the per-compiler modules but just
-- mixes it all in in this module, which is rather unsatisfactory. The script
-- generation and the unregister feature are not well used or tested.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

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

56
module Distribution.Simple.Register (
Duncan Coutts's avatar
Duncan Coutts committed
57 58 59
    register,
    unregister,

60
    initPackageDB,
Duncan Coutts's avatar
Duncan Coutts committed
61
    registerPackage,
62
    generateRegistrationInfo,
Duncan Coutts's avatar
Duncan Coutts committed
63 64 65
    inplaceInstalledPackageInfo,
    absoluteInstalledPackageInfo,
    generalInstalledPackageInfo,
66
  ) where
67

68 69 70
import Distribution.Simple.LocalBuildInfo
         ( LocalBuildInfo(..), ComponentLocalBuildInfo(..)
         , InstallDirs(..), absoluteInstallDirs )
71
import Distribution.Simple.BuildPaths (haddockName)
72 73 74
import qualified Distribution.Simple.GHC  as GHC
import qualified Distribution.Simple.LHC  as LHC
import qualified Distribution.Simple.Hugs as Hugs
75
import qualified Distribution.Simple.UHC  as UHC
76
import Distribution.Simple.Compiler
77
         ( compilerVersion, Compiler, CompilerFlavor(..), compilerFlavor
78
         , PackageDBStack, registrationPackageDB )
Duncan Coutts's avatar
Duncan Coutts committed
79
import Distribution.Simple.Program
80 81 82
         ( ProgramConfiguration, ConfiguredProgram
         , runProgramInvocation, requireProgram, lookupProgram
         , ghcPkgProgram, lhcPkgProgram )
Duncan Coutts's avatar
Duncan Coutts committed
83 84 85
import Distribution.Simple.Program.Script
         ( invocationAsSystemScript )
import qualified Distribution.Simple.Program.HcPkg as HcPkg
86 87 88
import Distribution.Simple.Setup
         ( RegisterFlags(..), CopyDest(..)
         , fromFlag, fromFlagOrDefault, flagToMaybe )
Duncan Coutts's avatar
Duncan Coutts committed
89 90
import Distribution.PackageDescription
         ( PackageDescription(..), Library(..), BuildInfo(..), hcOptions )
91
import Distribution.Package
92
         ( Package(..), packageName, InstalledPackageId(..) )
93
import Distribution.InstalledPackageInfo
94 95
         ( InstalledPackageInfo, InstalledPackageInfo_(InstalledPackageInfo)
         , showInstalledPackageInfo )
96
import qualified Distribution.InstalledPackageInfo as IPI
97
import Distribution.Simple.Utils
98
         ( writeUTF8File, writeFileAtomic, setFileExecutable
Duncan Coutts's avatar
Duncan Coutts committed
99
         , die, notice, setupMessage )
100
import Distribution.System
101
         ( OS(..), buildOS )
102 103
import Distribution.Text
         ( display )
104
import Distribution.Version ( Version(..) )
Duncan Coutts's avatar
Duncan Coutts committed
105 106
import Distribution.Verbosity as Verbosity
         ( Verbosity, normal )
107 108
import Distribution.Compat.Exception
         ( tryIO )
109

110
import System.FilePath ((</>), (<.>), isAbsolute)
Duncan Coutts's avatar
Duncan Coutts committed
111 112
import System.Directory
         ( getCurrentDirectory, removeDirectoryRecursive )
ijones's avatar
ijones committed
113

Duncan Coutts's avatar
Duncan Coutts committed
114
import Data.Maybe
115
         ( isJust, fromMaybe, maybeToList )
Duncan Coutts's avatar
Duncan Coutts committed
116 117
import Data.List
         ( partition, nub )
118
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
119

simonmar's avatar
simonmar committed
120 121 122
-- -----------------------------------------------------------------------------
-- Registration

ijones's avatar
ijones committed
123
register :: PackageDescription -> LocalBuildInfo
ijones's avatar
ijones committed
124
         -> RegisterFlags -- ^Install in the user's database?; verbose
ijones's avatar
ijones committed
125
         -> IO ()
Duncan Coutts's avatar
Duncan Coutts committed
126 127
register pkg@PackageDescription { library       = Just lib  }
         lbi@LocalBuildInfo     { libraryConfig = Just clbi } regFlags
128 129 130 131 132 133 134 135 136 137
  = do

    installedPkgInfo <- generateRegistrationInfo
                           verbosity pkg lib lbi clbi inplace distPref

     -- Three different modes:
    case () of
     _ | modeGenerateRegFile   -> writeRegistrationFile installedPkgInfo
       | modeGenerateRegScript -> writeRegisterScript   installedPkgInfo
       | otherwise             -> registerPackage verbosity
138
                                    installedPkgInfo pkg lbi inplace packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
139 140 141 142 143 144 145 146 147

  where
    modeGenerateRegFile = isJust (flagToMaybe (regGenPkgConf regFlags))
    regFile             = fromMaybe (display (packageId pkg) <.> "conf")
                                    (fromFlag (regGenPkgConf regFlags))

    modeGenerateRegScript = fromFlag (regGenScript regFlags)

    inplace   = fromFlag (regInPlace regFlags)
Duncan Coutts's avatar
Duncan Coutts committed
148 149 150 151 152
    -- FIXME: there's really no guarantee this will work.
    -- registering into a totally different db stack can
    -- fail if dependencies cannot be satisfied.
    packageDbs = nub $ withPackageDB lbi
                    ++ maybeToList (flagToMaybe  (regPackageDB regFlags))
Duncan Coutts's avatar
Duncan Coutts committed
153 154 155
    distPref  = fromFlag (regDistPref regFlags)
    verbosity = fromFlag (regVerbosity regFlags)

156
    writeRegistrationFile installedPkgInfo = do
Duncan Coutts's avatar
Duncan Coutts committed
157
      notice verbosity ("Creating package registration file: " ++ regFile)
158
      writeUTF8File regFile (showInstalledPackageInfo installedPkgInfo)
Duncan Coutts's avatar
Duncan Coutts committed
159

160
    writeRegisterScript installedPkgInfo =
Duncan Coutts's avatar
Duncan Coutts committed
161 162
      case compilerFlavor (compiler lbi) of
        GHC  -> do (ghcPkg, _) <- requireProgram verbosity ghcPkgProgram (withPrograms lbi)
163
                   writeHcPkgRegisterScript verbosity installedPkgInfo ghcPkg packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
164
        LHC  -> do (lhcPkg, _) <- requireProgram verbosity lhcPkgProgram (withPrograms lbi)
165
                   writeHcPkgRegisterScript verbosity installedPkgInfo lhcPkg packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
166 167 168
        Hugs -> notice verbosity "Registration scripts not needed for hugs"
        JHC  -> notice verbosity "Registration scripts not needed for jhc"
        NHC  -> notice verbosity "Registration scripts not needed for nhc98"
169
        UHC  -> notice verbosity "Registration scripts not needed for uhc"
Duncan Coutts's avatar
Duncan Coutts committed
170 171 172 173 174 175 176
        _    -> die "Registration scripts are not implemented for this compiler"

register _ _ regFlags = notice verbosity "No package to register"
  where
    verbosity = fromFlag (regVerbosity regFlags)


177 178
generateRegistrationInfo :: Verbosity
                         -> PackageDescription
Duncan Coutts's avatar
Duncan Coutts committed
179 180 181 182 183 184
                         -> Library
                         -> LocalBuildInfo
                         -> ComponentLocalBuildInfo
                         -> Bool
                         -> FilePath
                         -> IO InstalledPackageInfo
185
generateRegistrationInfo verbosity pkg lib lbi clbi inplace distPref = do
Duncan Coutts's avatar
Duncan Coutts committed
186 187
  --TODO: eliminate pwd!
  pwd <- getCurrentDirectory
188

189 190
  --TODO: the method of setting the InstalledPackageId is compiler specific
  --      this aspect should be delegated to a per-compiler helper.
191
  let comp = compiler lbi
192 193 194 195 196 197 198
  ipid <-
    case compilerFlavor comp of
     GHC | compilerVersion comp >= Version [6,11] [] -> do
            s <- GHC.libAbiHash verbosity pkg lbi lib clbi
            return (InstalledPackageId (display (packageId pkg) ++ '-':s))
     _other -> do
            return (InstalledPackageId (display (packageId pkg)))
199

Duncan Coutts's avatar
Duncan Coutts committed
200 201 202 203 204
  let installedPkgInfo
        | inplace   = inplaceInstalledPackageInfo pwd distPref
                        pkg lib lbi clbi
        | otherwise = absoluteInstalledPackageInfo
                        pkg lib lbi clbi
205

206 207 208
  return installedPkgInfo{ IPI.installedPackageId = ipid }


209 210 211 212 213 214 215 216
-- | Create an empty package DB at the specified location.
initPackageDB :: Verbosity -> Compiler -> ProgramConfiguration -> FilePath
                 -> IO ()
initPackageDB verbosity comp conf dbPath =
  case (compilerFlavor comp) of
    GHC -> GHC.initPackageDB verbosity conf dbPath
    _   -> die "initPackageDB is not implemented for this compiler"

Duncan Coutts's avatar
Duncan Coutts committed
217
registerPackage :: Verbosity
218
                -> InstalledPackageInfo
Duncan Coutts's avatar
Duncan Coutts committed
219 220 221
                -> PackageDescription
                -> LocalBuildInfo
                -> Bool
222
                -> PackageDBStack
Duncan Coutts's avatar
Duncan Coutts committed
223
                -> IO ()
224
registerPackage verbosity installedPkgInfo pkg lbi inplace packageDbs = do
refold's avatar
refold committed
225 226 227 228
  let msg = if inplace
            then "In-place registering"
            else "Registering"
  setupMessage verbosity msg (packageId pkg)
Duncan Coutts's avatar
Duncan Coutts committed
229
  case compilerFlavor (compiler lbi) of
230 231 232
    GHC  -> GHC.registerPackage  verbosity installedPkgInfo pkg lbi inplace packageDbs
    LHC  -> LHC.registerPackage  verbosity installedPkgInfo pkg lbi inplace packageDbs
    Hugs -> Hugs.registerPackage verbosity installedPkgInfo pkg lbi inplace packageDbs
233
    UHC  -> UHC.registerPackage  verbosity installedPkgInfo pkg lbi inplace packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
234 235 236 237 238 239
    JHC  -> notice verbosity "Registering for jhc (nothing to do)"
    NHC  -> notice verbosity "Registering for nhc98 (nothing to do)"
    _    -> die "Registering is not implemented for this compiler"


writeHcPkgRegisterScript :: Verbosity
240
                         -> InstalledPackageInfo
Duncan Coutts's avatar
Duncan Coutts committed
241
                         -> ConfiguredProgram
242
                         -> PackageDBStack
Duncan Coutts's avatar
Duncan Coutts committed
243
                         -> IO ()
244
writeHcPkgRegisterScript verbosity installedPkgInfo hcPkg packageDbs = do
Duncan Coutts's avatar
Duncan Coutts committed
245
  let invocation  = HcPkg.reregisterInvocation hcPkg Verbosity.normal
246
                      packageDbs (Right installedPkgInfo)
Duncan Coutts's avatar
Duncan Coutts committed
247 248 249
      regScript   = invocationAsSystemScript buildOS   invocation

  notice verbosity ("Creating package registration script: " ++ regScriptFileName)
250
  writeUTF8File regScriptFileName regScript
Duncan Coutts's avatar
Duncan Coutts committed
251 252 253 254 255 256
  setFileExecutable regScriptFileName

regScriptFileName :: FilePath
regScriptFileName = case buildOS of
                        Windows -> "register.bat"
                        _       -> "register.sh"
simonmar's avatar
simonmar committed
257

258

259 260 261
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

Duncan Coutts's avatar
Duncan Coutts committed
262 263 264 265 266 267 268 269 270 271 272 273 274
-- | Construct 'InstalledPackageInfo' for a library in a package, given a set
-- of installation directories.
--
generalInstalledPackageInfo
  :: ([FilePath] -> [FilePath]) -- ^ Translate relative include dir paths to
                                -- absolute paths.
  -> PackageDescription
  -> Library
  -> ComponentLocalBuildInfo
  -> InstallDirs FilePath
  -> InstalledPackageInfo
generalInstalledPackageInfo adjustRelIncDirs pkg lib clbi installDirs =
  InstalledPackageInfo {
275
    --TODO: do not open-code this conversion from PackageId to InstalledPackageId
276
    IPI.installedPackageId = InstalledPackageId (display (packageId pkg)),
277
    IPI.sourcePackageId    = packageId   pkg,
Duncan Coutts's avatar
Duncan Coutts committed
278 279 280 281 282 283 284
    IPI.license            = license     pkg,
    IPI.copyright          = copyright   pkg,
    IPI.maintainer         = maintainer  pkg,
    IPI.author             = author      pkg,
    IPI.stability          = stability   pkg,
    IPI.homepage           = homepage    pkg,
    IPI.pkgUrl             = pkgUrl      pkg,
285
    IPI.synopsis           = synopsis    pkg,
Duncan Coutts's avatar
Duncan Coutts committed
286 287 288 289 290
    IPI.description        = description pkg,
    IPI.category           = category    pkg,
    IPI.exposed            = libExposed  lib,
    IPI.exposedModules     = exposedModules lib,
    IPI.hiddenModules      = otherModules bi,
291
    IPI.trusted            = IPI.trusted IPI.emptyInstalledPackageInfo,
Duncan Coutts's avatar
Duncan Coutts committed
292 293 294 295 296 297 298 299 300
    IPI.importDirs         = [ libdir installDirs | hasModules ],
    IPI.libraryDirs        = if hasLibrary
                               then libdir installDirs : extraLibDirs bi
                               else                      extraLibDirs bi,
    IPI.hsLibraries        = [ "HS" ++ display (packageId pkg) | hasLibrary ],
    IPI.extraLibraries     = extraLibs bi,
    IPI.extraGHCiLibraries = [],
    IPI.includeDirs        = absinc ++ adjustRelIncDirs relinc,
    IPI.includes           = includes bi,
301
    IPI.depends            = map fst (componentPackageDeps clbi),
Duncan Coutts's avatar
Duncan Coutts committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    IPI.hugsOptions        = hcOptions Hugs bi,
    IPI.ccOptions          = [], -- Note. NOT ccOptions bi!
                                 -- We don't want cc-options to be propagated
                                 -- to C compilations in other packages.
    IPI.ldOptions          = ldOptions bi,
    IPI.frameworkDirs      = [],
    IPI.frameworks         = frameworks bi,
    IPI.haddockInterfaces  = [haddockdir installDirs </> haddockName pkg],
    IPI.haddockHTMLs       = [htmldir installDirs]
  }
  where
    bi = libBuildInfo lib
    (absinc, relinc) = partition isAbsolute (includeDirs bi)
    hasModules = not $ null (exposedModules lib)
                    && null (otherModules bi)
    hasLibrary = hasModules || not (null (cSources bi))


-- | Construct 'InstalledPackageInfo' for a library that is inplace in the
-- build tree.
--
-- This function knows about the layout of inplace packages.
--
inplaceInstalledPackageInfo :: FilePath -- ^ top of the build tree
                            -> FilePath -- ^ location of the dist tree
                            -> PackageDescription
                            -> Library
                            -> LocalBuildInfo
                            -> ComponentLocalBuildInfo
                            -> InstalledPackageInfo
inplaceInstalledPackageInfo inplaceDir distPref pkg lib lbi clbi =
refold's avatar
refold committed
333 334
    generalInstalledPackageInfo adjustRelativeIncludeDirs pkg lib clbi
    installDirs
Duncan Coutts's avatar
Duncan Coutts committed
335
  where
refold's avatar
refold committed
336
    adjustRelativeIncludeDirs = map (inplaceDir </>)
Duncan Coutts's avatar
Duncan Coutts committed
337
    installDirs =
338
      (absoluteInstallDirs pkg lbi NoCopyDest) {
Duncan Coutts's avatar
Duncan Coutts committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
        libdir     = inplaceDir </> buildDir lbi,
        datadir    = inplaceDir,
        datasubdir = distPref,
        docdir     = inplaceDocdir,
        htmldir    = inplaceHtmldir,
        haddockdir = inplaceHtmldir
      }
    inplaceDocdir  = inplaceDir </> distPref </> "doc"
    inplaceHtmldir = inplaceDocdir </> "html" </> display (packageName pkg)


-- | Construct 'InstalledPackageInfo' for the final install location of a
-- library package.
--
-- This function knows about the layout of installed packages.
--
absoluteInstalledPackageInfo :: PackageDescription
                             -> Library
                             -> LocalBuildInfo
                             -> ComponentLocalBuildInfo
                             -> InstalledPackageInfo
absoluteInstalledPackageInfo pkg lib lbi clbi =
    generalInstalledPackageInfo adjustReativeIncludeDirs pkg lib clbi installDirs
  where
    -- For installed packages we install all include files into one dir,
    -- whereas in the build tree they may live in multiple local dirs.
    adjustReativeIncludeDirs _
      | null (installIncludes bi) = []
      | otherwise                 = [includedir installDirs]
    bi = libBuildInfo lib
369
    installDirs = absoluteInstallDirs pkg lbi NoCopyDest
Duncan Coutts's avatar
Duncan Coutts committed
370

simonmar's avatar
simonmar committed
371 372
-- -----------------------------------------------------------------------------
-- Unregistration
373

ijones's avatar
ijones committed
374
unregister :: PackageDescription -> LocalBuildInfo -> RegisterFlags -> IO ()
Duncan Coutts's avatar
Duncan Coutts committed
375 376 377
unregister pkg lbi regFlags = do
  let pkgid     = packageId pkg
      genScript = fromFlag (regGenScript regFlags)
378
      verbosity = fromFlag (regVerbosity regFlags)
Duncan Coutts's avatar
Duncan Coutts committed
379
      packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
380
                                    (regPackageDB regFlags)
381
      installDirs = absoluteInstallDirs pkg lbi NoCopyDest
Duncan Coutts's avatar
Duncan Coutts committed
382
  setupMessage verbosity "Unregistering" pkgid
383
  case compilerFlavor (compiler lbi) of
Duncan Coutts's avatar
Duncan Coutts committed
384 385 386 387 388 389
    GHC ->
      let Just ghcPkg = lookupProgram ghcPkgProgram (withPrograms lbi)
          invocation = HcPkg.unregisterInvocation ghcPkg Verbosity.normal
                         packageDb pkgid
      in if genScript
           then writeFileAtomic unregScriptFileName
390
                  (BS.Char8.pack $ invocationAsSystemScript buildOS invocation)
Duncan Coutts's avatar
Duncan Coutts committed
391
            else runProgramInvocation verbosity invocation
ijones's avatar
ijones committed
392
    Hugs -> do
393
        _ <- tryIO $ removeDirectoryRecursive (libdir installDirs)
394
        return ()
395
    NHC -> do
396
        _ <- tryIO $ removeDirectoryRecursive (libdir installDirs)
397
        return ()
ijones's avatar
ijones committed
398
    _ ->
399
        die ("only unregistering with GHC and Hugs is implemented")
ijones's avatar
ijones committed
400

Duncan Coutts's avatar
Duncan Coutts committed
401 402 403 404
unregScriptFileName :: FilePath
unregScriptFileName = case buildOS of
                          Windows -> "unregister.bat"
                          _       -> "unregister.sh"