Register.hs 24.1 KB
Newer Older
1 2 3
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}

4 5
-----------------------------------------------------------------------------
-- |
ijones's avatar
ijones committed
6
-- Module      :  Distribution.Simple.Register
7
-- Copyright   :  Isaac Jones 2003-2004
8
-- License     :  BSD3
9
--
Duncan Coutts's avatar
Duncan Coutts committed
10
-- Maintainer  :  cabal-devel@haskell.org
ijones's avatar
ijones committed
11
-- Portability :  portable
12
--
Duncan Coutts's avatar
Duncan Coutts committed
13 14 15 16 17 18 19 20
-- 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,
Ian D. Bollinger's avatar
Ian D. Bollinger committed
21
-- or alternatively to register the package in place in the build tree. The
Duncan Coutts's avatar
Duncan Coutts committed
22 23 24 25 26 27
-- 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.
28

29
module Distribution.Simple.Register (
Duncan Coutts's avatar
Duncan Coutts committed
30 31 32
    register,
    unregister,

33 34
    internalPackageDBPath,

35
    initPackageDB,
36 37 38 39
    doesPackageDBExist,
    createPackageDB,
    deletePackageDB,

40
    abiHash,
refold's avatar
refold committed
41
    invokeHcPkg,
Duncan Coutts's avatar
Duncan Coutts committed
42
    registerPackage,
43
    generateRegistrationInfo,
Duncan Coutts's avatar
Duncan Coutts committed
44 45 46
    inplaceInstalledPackageInfo,
    absoluteInstalledPackageInfo,
    generalInstalledPackageInfo,
47
  ) where
48

49 50 51
import Prelude ()
import Distribution.Compat.Prelude

52 53
import Distribution.Types.TargetInfo
import Distribution.Types.LocalBuildInfo
54
import Distribution.Types.ComponentLocalBuildInfo
55

56
import Distribution.Simple.LocalBuildInfo
57
import Distribution.Simple.BuildPaths
58
import Distribution.Simple.BuildTarget
59 60 61 62 63

import qualified Distribution.Simple.GHC   as GHC
import qualified Distribution.Simple.GHCJS as GHCJS
import qualified Distribution.Simple.LHC   as LHC
import qualified Distribution.Simple.UHC   as UHC
64
import qualified Distribution.Simple.HaskellSuite as HaskellSuite
65
import qualified Distribution.Simple.PackageIndex as Index
66

67
import Distribution.Simple.Compiler
Duncan Coutts's avatar
Duncan Coutts committed
68 69 70
import Distribution.Simple.Program
import Distribution.Simple.Program.Script
import qualified Distribution.Simple.Program.HcPkg as HcPkg
71
import Distribution.Simple.Setup
Duncan Coutts's avatar
Duncan Coutts committed
72
import Distribution.PackageDescription
73
import Distribution.Package
74
import qualified Distribution.InstalledPackageInfo as IPI
75
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
76
import Distribution.Simple.Utils
77
import Distribution.Utils.MapAccum
78
import Distribution.System
79
import Distribution.Text
Duncan Coutts's avatar
Duncan Coutts committed
80
import Distribution.Verbosity as Verbosity
81
import Distribution.Version
82
import Distribution.Compat.Graph (IsNode(nodeKey))
83

84
import System.FilePath ((</>), (<.>), isAbsolute)
Duncan Coutts's avatar
Duncan Coutts committed
85
import System.Directory
ijones's avatar
ijones committed
86

87
import Data.List (partition)
88
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
89

simonmar's avatar
simonmar committed
90 91 92
-- -----------------------------------------------------------------------------
-- Registration

ijones's avatar
ijones committed
93
register :: PackageDescription -> LocalBuildInfo
ijones's avatar
ijones committed
94
         -> RegisterFlags -- ^Install in the user's database?; verbose
ijones's avatar
ijones committed
95
         -> IO ()
96
register pkg_descr lbi0 flags =
97 98 99 100 101
   -- Duncan originally asked for us to not register/install files
   -- when there was no public library.  But with per-component
   -- configure, we legitimately need to install internal libraries
   -- so that we can get them.  So just unconditionally install.
   doRegister
102 103
 where
  doRegister = do
104
    targets <- readTargetInfos verbosity pkg_descr lbi0 (regArgs flags)
105 106 107

    -- It's important to register in build order, because ghc-pkg
    -- will complain if a dependency is not registered.
108 109 110 111 112 113 114 115 116 117 118 119 120 121
    let componentsToRegister
            = neededTargetsInBuildOrder' pkg_descr lbi0 (map nodeKey targets)

    (_, ipi_mbs) <-
        mapAccumM `flip` installedPkgs lbi0 `flip` componentsToRegister $ \index tgt ->
            case targetComponent tgt of
                CLib lib -> do
                    let clbi = targetCLBI tgt
                        lbi = lbi0 { installedPkgs = index }
                    ipi <- generateOne pkg_descr lib lbi clbi flags
                    return (Index.insert ipi index, Just ipi)
                _   -> return (index, Nothing)

    registerAll pkg_descr lbi0 flags (catMaybes ipi_mbs)
122 123
   where
    verbosity = fromFlag (regVerbosity flags)
124 125 126 127 128

generateOne :: PackageDescription -> Library -> LocalBuildInfo -> ComponentLocalBuildInfo
            -> RegisterFlags
            -> IO InstalledPackageInfo
generateOne pkg lib lbi clbi regFlags
129
  = do
130
    absPackageDBs    <- absolutePackageDBPaths packageDbs
131
    installedPkgInfo <- generateRegistrationInfo
132
                           verbosity pkg lib lbi clbi inplace reloc distPref
133
                           (registrationPackageDB absPackageDBs)
134
    info verbosity (IPI.showInstalledPackageInfo installedPkgInfo)
135 136 137 138 139 140 141 142 143 144 145
    return installedPkgInfo
  where
    inplace   = fromFlag (regInPlace regFlags)
    reloc     = relocatable lbi
    -- 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))
    distPref  = fromFlag (regDistPref regFlags)
    verbosity = fromFlag (regVerbosity regFlags)
146

147 148 149 150 151
registerAll :: PackageDescription -> LocalBuildInfo -> RegisterFlags
            -> [InstalledPackageInfo]
            -> IO ()
registerAll pkg lbi regFlags ipis
  = do
152
    when (fromFlag (regPrintId regFlags)) $ do
153
      for_ ipis $ \installedPkgInfo ->
154 155 156
        -- Only print the public library's IPI
        when (IPI.sourcePackageId installedPkgInfo == packageId pkg) $
          putStrLn (display (IPI.installedUnitId installedPkgInfo))
157

158 159
     -- Three different modes:
    case () of
160 161
     _ | modeGenerateRegFile   -> writeRegistrationFileOrDirectory
       | modeGenerateRegScript -> writeRegisterScript
162 163
       | otherwise             -> do
           setupMessage verbosity "Registering" (packageId pkg)
164
           for_ ipis $ \installedPkgInfo ->
165 166
               registerPackage verbosity (compiler lbi) (withPrograms lbi)
                               HcPkg.NoMultiInstance packageDbs installedPkgInfo
Duncan Coutts's avatar
Duncan Coutts committed
167 168 169 170 171 172 173 174

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

    modeGenerateRegScript = fromFlag (regGenScript regFlags)

Duncan Coutts's avatar
Duncan Coutts committed
175 176 177 178 179
    -- 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
180 181
    verbosity = fromFlag (regVerbosity regFlags)

182 183 184 185 186
    writeRegistrationFileOrDirectory = do
      -- Handles overwriting both directory and file
      deletePackageDB regFile
      case ipis of
        [installedPkgInfo] -> do
187
          info verbosity ("Creating package registration file: " ++ regFile)
188 189
          writeUTF8File regFile (IPI.showInstalledPackageInfo installedPkgInfo)
        _ -> do
190
          info verbosity ("Creating package registration directory: " ++ regFile)
191 192 193 194 195
          createDirectory regFile
          let num_ipis = length ipis
              lpad m xs = replicate (m - length ys) '0' ++ ys
                  where ys = take m xs
              number i = lpad (length (show num_ipis)) (show i)
196
          for_ (zip ([1..] :: [Int]) ipis) $ \(i, installedPkgInfo) ->
197 198 199 200
            writeUTF8File (regFile </> (number i ++ "-" ++ display (IPI.installedUnitId installedPkgInfo)))
                          (IPI.showInstalledPackageInfo installedPkgInfo)

    writeRegisterScript =
Duncan Coutts's avatar
Duncan Coutts committed
201
      case compilerFlavor (compiler lbi) of
202 203 204 205 206
        JHC -> notice verbosity "Registration scripts not needed for jhc"
        UHC -> notice verbosity "Registration scripts not needed for uhc"
        _   -> withHcPkg
               "Registration scripts are not implemented for this compiler"
               (compiler lbi) (withPrograms lbi)
207
               (writeHcPkgRegisterScript verbosity ipis packageDbs)
Duncan Coutts's avatar
Duncan Coutts committed
208 209


210 211
generateRegistrationInfo :: Verbosity
                         -> PackageDescription
Duncan Coutts's avatar
Duncan Coutts committed
212 213 214 215
                         -> Library
                         -> LocalBuildInfo
                         -> ComponentLocalBuildInfo
                         -> Bool
216
                         -> Bool
Duncan Coutts's avatar
Duncan Coutts committed
217
                         -> FilePath
218
                         -> PackageDB
Duncan Coutts's avatar
Duncan Coutts committed
219
                         -> IO InstalledPackageInfo
220
generateRegistrationInfo verbosity pkg lib lbi clbi inplace reloc distPref packageDb = do
Duncan Coutts's avatar
Duncan Coutts committed
221 222
  --TODO: eliminate pwd!
  pwd <- getCurrentDirectory
223

224
  abi_hash <- abiHash verbosity pkg distPref lbi lib clbi
225

Christiaan Baaij's avatar
Christiaan Baaij committed
226 227 228
  installedPkgInfo <-
    if inplace
      then return (inplaceInstalledPackageInfo pwd distPref
229
                     pkg abi_hash lib lbi clbi)
Christiaan Baaij's avatar
Christiaan Baaij committed
230 231
    else if reloc
      then relocRegistrationInfo verbosity
232
                     pkg lib lbi clbi abi_hash packageDb
Christiaan Baaij's avatar
Christiaan Baaij committed
233
      else return (absoluteInstalledPackageInfo
234
                     pkg abi_hash lib lbi clbi)
235

236

237
  return installedPkgInfo{ IPI.abiHash = abi_hash }
238

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
-- | Compute the 'AbiHash' of a library that we built inplace.
abiHash :: Verbosity
        -> PackageDescription
        -> FilePath
        -> LocalBuildInfo
        -> Library
        -> ComponentLocalBuildInfo
        -> IO AbiHash
abiHash verbosity pkg distPref lbi lib clbi =
    case compilerFlavor comp of
     GHC | compilerVersion comp >= mkVersion [6,11] -> do
            fmap mkAbiHash $ GHC.libAbiHash verbosity pkg lbi' lib clbi
     GHCJS -> do
            fmap mkAbiHash $ GHCJS.libAbiHash verbosity pkg lbi' lib clbi
     _ -> return (mkAbiHash "")
  where
    comp = compiler lbi
    lbi' = lbi {
              withPackageDB = withPackageDB lbi
                  ++ [SpecificPackageDB (internalPackageDBPath lbi distPref)]
           }

261 262 263 264 265
relocRegistrationInfo :: Verbosity
                      -> PackageDescription
                      -> Library
                      -> LocalBuildInfo
                      -> ComponentLocalBuildInfo
266
                      -> AbiHash
267 268
                      -> PackageDB
                      -> IO InstalledPackageInfo
269
relocRegistrationInfo verbosity pkg lib lbi clbi abi_hash packageDb =
270
  case (compilerFlavor (compiler lbi)) of
271 272
    GHC -> do fs <- GHC.pkgRoot verbosity lbi packageDb
              return (relocatableInstalledPackageInfo
273
                        pkg abi_hash lib lbi clbi fs)
274 275
    _   -> die "Distribution.Simple.Register.relocRegistrationInfo: \
               \not implemented for this compiler"
276

277
initPackageDB :: Verbosity -> Compiler -> ProgramDb -> FilePath -> IO ()
278
initPackageDB verbosity comp progdb dbPath =
279
    createPackageDB verbosity comp progdb False dbPath
280

281
-- | Create an empty package DB at the specified location.
282
createPackageDB :: Verbosity -> Compiler -> ProgramDb -> Bool
283 284 285 286 287 288 289 290 291 292 293
                -> FilePath -> IO ()
createPackageDB verbosity comp progdb preferCompat dbPath =
    case compilerFlavor comp of
      GHC   -> HcPkg.init (GHC.hcPkgInfo   progdb) verbosity preferCompat dbPath
      GHCJS -> HcPkg.init (GHCJS.hcPkgInfo progdb) verbosity False dbPath
      LHC   -> HcPkg.init (LHC.hcPkgInfo   progdb) verbosity False dbPath
      UHC   -> return ()
      HaskellSuite _ -> HaskellSuite.initPackageDB verbosity progdb dbPath
      _              -> die $ "Distribution.Simple.Register.createPackageDB: "
                           ++ "not implemented for this compiler"

294
doesPackageDBExist :: FilePath -> NoCallStackIO Bool
295 296 297 298 299 300 301
doesPackageDBExist dbPath = do
    -- currently one impl for all compiler flavours, but could change if needed
    dir_exists <- doesDirectoryExist dbPath
    if dir_exists
        then return True
        else doesFileExist dbPath

302
deletePackageDB :: FilePath -> NoCallStackIO ()
303 304 305 306 307 308 309
deletePackageDB dbPath = do
    -- currently one impl for all compiler flavours, but could change if needed
    dir_exists <- doesDirectoryExist dbPath
    if dir_exists
        then removeDirectoryRecursive dbPath
        else do file_exists <- doesFileExist dbPath
                when file_exists $ removeFile dbPath
refold's avatar
refold committed
310 311 312

-- | Run @hc-pkg@ using a given package DB stack, directly forwarding the
-- provided command-line arguments to it.
313
invokeHcPkg :: Verbosity -> Compiler -> ProgramDb -> PackageDBStack
refold's avatar
refold committed
314
                -> [String] -> IO ()
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
315 316
invokeHcPkg verbosity comp progdb dbStack extraArgs =
  withHcPkg "invokeHcPkg" comp progdb
317 318
    (\hpi -> HcPkg.invoke hpi verbosity dbStack extraArgs)

319
withHcPkg :: String -> Compiler -> ProgramDb
320
          -> (HcPkg.HcPkgInfo -> IO a) -> IO a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
321
withHcPkg name comp progdb f =
322
  case compilerFlavor comp of
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
323 324 325
    GHC   -> f (GHC.hcPkgInfo progdb)
    GHCJS -> f (GHCJS.hcPkgInfo progdb)
    LHC   -> f (LHC.hcPkgInfo progdb)
326 327
    _     -> die ("Distribution.Simple.Register." ++ name ++ ":\
                  \not implemented for this compiler")
328

Duncan Coutts's avatar
Duncan Coutts committed
329
registerPackage :: Verbosity
330
                -> Compiler
331
                -> ProgramDb
332
                -> HcPkg.MultiInstance
333
                -> PackageDBStack
334
                -> InstalledPackageInfo
Duncan Coutts's avatar
Duncan Coutts committed
335
                -> IO ()
336
registerPackage verbosity comp progdb multiInstance packageDbs installedPkgInfo =
337
  case compilerFlavor comp of
338 339
    GHC   -> GHC.registerPackage   verbosity progdb multiInstance packageDbs installedPkgInfo
    GHCJS -> GHCJS.registerPackage verbosity progdb multiInstance packageDbs installedPkgInfo
340
    _ | HcPkg.MultiInstance == multiInstance
341
          -> die "Registering multiple package instances is not yet supported for this compiler"
342 343
    LHC   -> LHC.registerPackage   verbosity      progdb packageDbs installedPkgInfo
    UHC   -> UHC.registerPackage   verbosity comp progdb packageDbs installedPkgInfo
344
    JHC   -> notice verbosity "Registering for jhc (nothing to do)"
345
    HaskellSuite {} ->
346
      HaskellSuite.registerPackage verbosity      progdb packageDbs installedPkgInfo
Duncan Coutts's avatar
Duncan Coutts committed
347 348 349
    _    -> die "Registering is not implemented for this compiler"

writeHcPkgRegisterScript :: Verbosity
350
                         -> [InstalledPackageInfo]
351
                         -> PackageDBStack
352
                         -> HcPkg.HcPkgInfo
Duncan Coutts's avatar
Duncan Coutts committed
353
                         -> IO ()
354 355 356 357 358 359 360 361
writeHcPkgRegisterScript verbosity ipis packageDbs hpi = do
  let genScript installedPkgInfo =
          let invocation  = HcPkg.reregisterInvocation hpi Verbosity.normal
                              packageDbs (Right installedPkgInfo)
          in invocationAsSystemScript buildOS invocation
      scripts = map genScript ipis
      -- TODO: Do something more robust here
      regScript = unlines scripts
Duncan Coutts's avatar
Duncan Coutts committed
362

363
  info verbosity ("Creating package registration script: " ++ regScriptFileName)
364
  writeUTF8File regScriptFileName regScript
Duncan Coutts's avatar
Duncan Coutts committed
365 366 367 368 369 370
  setFileExecutable regScriptFileName

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

372

373 374 375
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

Duncan Coutts's avatar
Duncan Coutts committed
376 377 378 379 380 381 382
-- | 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
383
  -> AbiHash
Duncan Coutts's avatar
Duncan Coutts committed
384
  -> Library
385
  -> LocalBuildInfo
Duncan Coutts's avatar
Duncan Coutts committed
386 387 388
  -> ComponentLocalBuildInfo
  -> InstallDirs FilePath
  -> InstalledPackageInfo
389
generalInstalledPackageInfo adjustRelIncDirs pkg abi_hash lib lbi clbi installDirs =
390
  IPI.InstalledPackageInfo {
391 392 393
    IPI.sourcePackageId    = (packageId   pkg) {
                                pkgName = componentCompatPackageName clbi
                             },
394
    IPI.installedUnitId    = componentUnitId clbi,
395
    IPI.installedComponentId_ = componentComponentId clbi,
396
    IPI.instantiatedWith   = componentInstantiatedWith clbi,
397
    IPI.compatPackageKey   = componentCompatPackageKey clbi,
Duncan Coutts's avatar
Duncan Coutts committed
398 399 400 401 402 403 404
    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,
405
    IPI.synopsis           = synopsis    pkg,
Duncan Coutts's avatar
Duncan Coutts committed
406 407
    IPI.description        = description pkg,
    IPI.category           = category    pkg,
408
    IPI.abiHash            = abi_hash,
409
    IPI.indefinite         = componentIsIndefinite clbi,
Duncan Coutts's avatar
Duncan Coutts committed
410
    IPI.exposed            = libExposed  lib,
411
    IPI.exposedModules     = componentExposedModules clbi,
Duncan Coutts's avatar
Duncan Coutts committed
412
    IPI.hiddenModules      = otherModules bi,
413
    IPI.trusted            = IPI.trusted IPI.emptyInstalledPackageInfo,
Duncan Coutts's avatar
Duncan Coutts committed
414
    IPI.importDirs         = [ libdir installDirs | hasModules ],
Christiaan Baaij's avatar
Christiaan Baaij committed
415 416
    IPI.libraryDirs        = libdirs,
    IPI.libraryDynDirs     = dynlibdirs,
417
    IPI.dataDir            = datadir installDirs,
418
    IPI.hsLibraries        = if hasLibrary
419
                               then [getHSLibraryName (componentUnitId clbi)]
420
                               else [],
Duncan Coutts's avatar
Duncan Coutts committed
421
    IPI.extraLibraries     = extraLibs bi,
422
    IPI.extraGHCiLibraries = extraGHCiLibs bi,
Duncan Coutts's avatar
Duncan Coutts committed
423 424
    IPI.includeDirs        = absinc ++ adjustRelIncDirs relinc,
    IPI.includes           = includes bi,
425 426
    IPI.depends            = depends,
    IPI.abiDepends         = abi_depends,
Duncan Coutts's avatar
Duncan Coutts committed
427 428 429 430 431
    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.frameworks         = frameworks bi,
432
    IPI.frameworkDirs      = extraFrameworkDirs bi,
Duncan Coutts's avatar
Duncan Coutts committed
433
    IPI.haddockInterfaces  = [haddockdir installDirs </> haddockName pkg],
434 435
    IPI.haddockHTMLs       = [htmldir installDirs],
    IPI.pkgRoot            = Nothing
Duncan Coutts's avatar
Duncan Coutts committed
436 437 438
  }
  where
    bi = libBuildInfo lib
439 440 441 442 443 444 445 446 447 448
    --TODO: unclear what the root cause of the
    -- duplication is, but we nub it here for now:
    depends = ordNub $ map fst (componentPackageDeps clbi)
    abi_depends = map add_abi depends
    add_abi uid = IPI.AbiDependency uid abi
      where
        abi = case Index.lookupUnitId (installedPkgs lbi) uid of
                Nothing -> error $
                  "generalInstalledPackageInfo: missing IPI for " ++ display uid
                Just ipi -> IPI.abiHash ipi
Duncan Coutts's avatar
Duncan Coutts committed
449
    (absinc, relinc) = partition isAbsolute (includeDirs bi)
450
    hasModules = not $ null (allLibModules lib clbi)
Christiaan Baaij's avatar
Christiaan Baaij committed
451
    comp = compiler lbi
Duncan Coutts's avatar
Duncan Coutts committed
452
    hasLibrary = hasModules || not (null (cSources bi))
453
                            || (not (null (jsSources bi)) &&
Christiaan Baaij's avatar
Christiaan Baaij committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
                                compilerFlavor comp == GHCJS)
    (libdirs, dynlibdirs)
      | not hasLibrary
      = (extraLibDirs bi, [])
      -- the dynamic-library-dirs defaults to the library-dirs if not specified,
      -- so this works whether the dynamic-library-dirs field is supported or not

      | libraryDynDirSupported comp
      = (libdir    installDirs : extraLibDirs bi,
         dynlibdir installDirs : extraLibDirs bi)

      | otherwise
      = (libdir installDirs : dynlibdir installDirs : extraLibDirs bi, [])
      -- the compiler doesn't understand the dynamic-library-dirs field so we
      -- add the dyn directory to the "normal" list in the library-dirs field
469

Ian D. Bollinger's avatar
Ian D. Bollinger committed
470
-- | Construct 'InstalledPackageInfo' for a library that is in place in the
Duncan Coutts's avatar
Duncan Coutts committed
471 472
-- build tree.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
473
-- This function knows about the layout of in place packages.
Duncan Coutts's avatar
Duncan Coutts committed
474 475 476 477
--
inplaceInstalledPackageInfo :: FilePath -- ^ top of the build tree
                            -> FilePath -- ^ location of the dist tree
                            -> PackageDescription
478
                            -> AbiHash
Duncan Coutts's avatar
Duncan Coutts committed
479 480 481 482
                            -> Library
                            -> LocalBuildInfo
                            -> ComponentLocalBuildInfo
                            -> InstalledPackageInfo
483
inplaceInstalledPackageInfo inplaceDir distPref pkg abi_hash lib lbi clbi =
484
    generalInstalledPackageInfo adjustRelativeIncludeDirs
485
                                pkg abi_hash lib lbi clbi installDirs
Duncan Coutts's avatar
Duncan Coutts committed
486
  where
refold's avatar
refold committed
487
    adjustRelativeIncludeDirs = map (inplaceDir </>)
488
    libTargetDir = componentBuildDir lbi clbi
Duncan Coutts's avatar
Duncan Coutts committed
489
    installDirs =
490
      (absoluteComponentInstallDirs pkg lbi (componentUnitId clbi) NoCopyDest) {
491
        libdir     = inplaceDir </> libTargetDir,
Christiaan Baaij's avatar
Christiaan Baaij committed
492
        dynlibdir  = inplaceDir </> libTargetDir,
493
        datadir    = inplaceDir </> dataDir pkg,
Duncan Coutts's avatar
Duncan Coutts committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507
        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
508
                             -> AbiHash
Duncan Coutts's avatar
Duncan Coutts committed
509 510 511 512
                             -> Library
                             -> LocalBuildInfo
                             -> ComponentLocalBuildInfo
                             -> InstalledPackageInfo
513
absoluteInstalledPackageInfo pkg abi_hash lib lbi clbi =
514
    generalInstalledPackageInfo adjustReativeIncludeDirs
515
                                pkg abi_hash lib lbi clbi installDirs
Duncan Coutts's avatar
Duncan Coutts committed
516 517 518 519 520 521 522
  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
523
    installDirs = absoluteComponentInstallDirs pkg lbi (componentUnitId clbi) NoCopyDest
Duncan Coutts's avatar
Duncan Coutts committed
524

525 526

relocatableInstalledPackageInfo :: PackageDescription
527
                                -> AbiHash
528 529 530
                                -> Library
                                -> LocalBuildInfo
                                -> ComponentLocalBuildInfo
531
                                -> FilePath
532
                                -> InstalledPackageInfo
533
relocatableInstalledPackageInfo pkg abi_hash lib lbi clbi pkgroot =
534
    generalInstalledPackageInfo adjustReativeIncludeDirs
535
                                pkg abi_hash lib lbi clbi installDirs
536 537 538 539 540 541 542
  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
543 544

    installDirs = fmap (("${pkgroot}" </>) . shortRelativePath pkgroot)
545
                $ absoluteComponentInstallDirs pkg lbi (componentUnitId clbi) NoCopyDest
546

simonmar's avatar
simonmar committed
547 548
-- -----------------------------------------------------------------------------
-- Unregistration
549

ijones's avatar
ijones committed
550
unregister :: PackageDescription -> LocalBuildInfo -> RegisterFlags -> IO ()
Duncan Coutts's avatar
Duncan Coutts committed
551 552 553
unregister pkg lbi regFlags = do
  let pkgid     = packageId pkg
      genScript = fromFlag (regGenScript regFlags)
554
      verbosity = fromFlag (regVerbosity regFlags)
Duncan Coutts's avatar
Duncan Coutts committed
555
      packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
556
                                    (regPackageDB regFlags)
557 558 559 560 561 562 563
      unreg hpi =
        let invocation = HcPkg.unregisterInvocation
                           hpi Verbosity.normal packageDb pkgid
        in if genScript
             then writeFileAtomic unregScriptFileName
                    (BS.Char8.pack $ invocationAsSystemScript buildOS invocation)
             else runProgramInvocation verbosity invocation
Duncan Coutts's avatar
Duncan Coutts committed
564
  setupMessage verbosity "Unregistering" pkgid
565 566
  withHcPkg "unregistering is only implemented for GHC and GHCJS"
    (compiler lbi) (withPrograms lbi) unreg
ijones's avatar
ijones committed
567

Duncan Coutts's avatar
Duncan Coutts committed
568 569 570 571
unregScriptFileName :: FilePath
unregScriptFileName = case buildOS of
                          Windows -> "unregister.bat"
                          _       -> "unregister.sh"
572 573 574 575 576 577

internalPackageDBPath :: LocalBuildInfo -> FilePath -> FilePath
internalPackageDBPath lbi distPref =
      case compilerFlavor (compiler lbi) of
        UHC -> UHC.inplacePackageDbPath lbi
        _   -> distPref </> "package.conf.inplace"