Register.hs 23.5 KB
Newer Older
1
{-# LANGUAGE PatternGuards #-}
2 3
-----------------------------------------------------------------------------
-- |
ijones's avatar
ijones committed
4
-- Module      :  Distribution.Simple.Register
5
-- Copyright   :  Isaac Jones 2003-2004
6
-- License     :  BSD3
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 16 17 18
-- 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
19
-- or alternatively to register the package in place in the build tree. The
Duncan Coutts's avatar
Duncan Coutts committed
20 21 22 23 24 25
-- 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.
26

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

31
    internalPackageDBPath,
32
    createInternalPackageDB,
33

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

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

47 48 49
import Distribution.Types.TargetInfo
import Distribution.Types.LocalBuildInfo

50
import Distribution.Simple.LocalBuildInfo
51
import Distribution.Simple.BuildPaths
52 53 54 55 56

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
57
import qualified Distribution.Simple.HaskellSuite as HaskellSuite
58

59
import Distribution.Simple.BuildTarget
60
import Distribution.Simple.Compiler
Duncan Coutts's avatar
Duncan Coutts committed
61 62 63
import Distribution.Simple.Program
import Distribution.Simple.Program.Script
import qualified Distribution.Simple.Program.HcPkg as HcPkg
64
import Distribution.Simple.Setup
Duncan Coutts's avatar
Duncan Coutts committed
65
import Distribution.PackageDescription
66
import Distribution.Package
67
import qualified Distribution.InstalledPackageInfo as IPI
68
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
69
import Distribution.Simple.Utils
70
import Distribution.System
71
import Distribution.Text
Duncan Coutts's avatar
Duncan Coutts committed
72
import Distribution.Verbosity as Verbosity
73
import Distribution.Compat.Graph (IsNode(nodeKey))
74

75
import System.FilePath ((</>), (<.>), isAbsolute)
Duncan Coutts's avatar
Duncan Coutts committed
76
import System.Directory
ijones's avatar
ijones committed
77

78
import Data.Version
79
import Control.Monad
Duncan Coutts's avatar
Duncan Coutts committed
80
import Data.Maybe
Duncan Coutts's avatar
Duncan Coutts committed
81
import Data.List
82
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
83

simonmar's avatar
simonmar committed
84 85 86
-- -----------------------------------------------------------------------------
-- Registration

ijones's avatar
ijones committed
87
register :: PackageDescription -> LocalBuildInfo
ijones's avatar
ijones committed
88
         -> RegisterFlags -- ^Install in the user's database?; verbose
ijones's avatar
ijones committed
89
         -> IO ()
90 91 92 93 94 95 96
register pkg_descr lbi flags = when (hasPublicLib pkg_descr) doRegister
 where
  -- We do NOT register libraries outside of the inplace database
  -- if there is no public library, since no one else can use it
  -- usefully (they're not public.)  If we start supporting scoped
  -- packages, we'll have to relax this.
  doRegister = do
97
    targets <- readTargetInfos verbosity lbi (regArgs flags)
98 99 100

    -- It's important to register in build order, because ghc-pkg
    -- will complain if a dependency is not registered.
101 102
    let maybeGenerateOne target
            | CLib lib <- targetComponent target
103 104
            = fmap Just (generateOne pkg_descr lib lbi clbi flags)
            | otherwise = return Nothing
105
          where clbi = targetCLBI target
106 107 108 109

    ipis <-
      if fromFlag (regAssumeDepsUpToDate flags)
        then
110 111 112
          case targets of
            [target] -> do
                mb_ipi <- maybeGenerateOne target
113 114 115 116 117 118 119
                case mb_ipi of
                    Nothing -> die "Cannot --assume-deps-up-to-date register non-library target"
                    Just ipi -> return [ipi]
            [] -> die "In --assume-deps-up-to-date mode you must specify a target"
            _ -> die "In --assume-deps-up-to-date mode you can only register a single target"
        else fmap catMaybes
           . mapM maybeGenerateOne
120
           $ neededTargetsInBuildOrder lbi (map nodeKey targets)
121 122 123 124
    registerAll pkg_descr lbi flags ipis
    return ()
   where
    verbosity = fromFlag (regVerbosity flags)
125 126 127 128 129

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

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

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

  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
176 177 178 179 180
    -- 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
181 182
    verbosity = fromFlag (regVerbosity regFlags)

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    writeRegistrationFileOrDirectory = do
      -- Handles overwriting both directory and file
      deletePackageDB regFile
      case ipis of
        [installedPkgInfo] -> do
          notice verbosity ("Creating package registration file: " ++ regFile)
          writeUTF8File regFile (IPI.showInstalledPackageInfo installedPkgInfo)
        _ -> do
          notice verbosity ("Creating package registration directory: " ++ regFile)
          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)
          forM_ (zip ([1..] :: [Int]) ipis) $ \(i, installedPkgInfo) ->
            -- TODO: This will need a hashUnitId when Backpack comes.
            writeUTF8File (regFile </> (number i ++ "-" ++ display (IPI.installedUnitId installedPkgInfo)))
                          (IPI.showInstalledPackageInfo installedPkgInfo)

    writeRegisterScript =
Duncan Coutts's avatar
Duncan Coutts committed
203
      case compilerFlavor (compiler lbi) of
204 205 206 207 208
        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)
209
               (writeHcPkgRegisterScript verbosity ipis packageDbs)
Duncan Coutts's avatar
Duncan Coutts committed
210 211


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

226
  --TODO: the method of setting the UnitId is compiler specific
227
  --      this aspect should be delegated to a per-compiler helper.
228
  let comp = compiler lbi
229 230 231 232
      lbi' = lbi {
                withPackageDB = withPackageDB lbi
                    ++ [SpecificPackageDB (internalPackageDBPath lbi distPref)]
             }
233
  abi_hash <-
234 235
    case compilerFlavor comp of
     GHC | compilerVersion comp >= Version [6,11] [] -> do
236
            fmap AbiHash $ GHC.libAbiHash verbosity pkg lbi' lib clbi
237
     GHCJS -> do
238
            fmap AbiHash $ GHCJS.libAbiHash verbosity pkg lbi' lib clbi
239
     _ -> return (AbiHash "")
240

Christiaan Baaij's avatar
Christiaan Baaij committed
241 242 243
  installedPkgInfo <-
    if inplace
      then return (inplaceInstalledPackageInfo pwd distPref
244
                     pkg abi_hash lib lbi clbi)
Christiaan Baaij's avatar
Christiaan Baaij committed
245 246
    else if reloc
      then relocRegistrationInfo verbosity
247
                     pkg lib lbi clbi abi_hash packageDb
Christiaan Baaij's avatar
Christiaan Baaij committed
248
      else return (absoluteInstalledPackageInfo
249
                     pkg abi_hash lib lbi clbi)
250

251

252
  return installedPkgInfo{ IPI.abiHash = abi_hash }
253

254 255 256 257 258
relocRegistrationInfo :: Verbosity
                      -> PackageDescription
                      -> Library
                      -> LocalBuildInfo
                      -> ComponentLocalBuildInfo
259
                      -> AbiHash
260 261
                      -> PackageDB
                      -> IO InstalledPackageInfo
262
relocRegistrationInfo verbosity pkg lib lbi clbi abi_hash packageDb =
263
  case (compilerFlavor (compiler lbi)) of
264 265
    GHC -> do fs <- GHC.pkgRoot verbosity lbi packageDb
              return (relocatableInstalledPackageInfo
266
                        pkg abi_hash lib lbi clbi fs)
267 268
    _   -> die "Distribution.Simple.Register.relocRegistrationInfo: \
               \not implemented for this compiler"
269

270 271
initPackageDB :: Verbosity -> Compiler -> ProgramConfiguration -> FilePath -> IO ()
initPackageDB verbosity comp progdb dbPath =
272
    createPackageDB verbosity comp progdb False dbPath
273

274
-- | Create an empty package DB at the specified location.
275 276 277 278 279 280 281 282 283 284 285 286
createPackageDB :: Verbosity -> Compiler -> ProgramConfiguration -> Bool
                -> 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"

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
doesPackageDBExist :: FilePath -> IO Bool
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

deletePackageDB :: FilePath -> IO ()
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
303 304 305 306 307 308

-- | Run @hc-pkg@ using a given package DB stack, directly forwarding the
-- provided command-line arguments to it.
invokeHcPkg :: Verbosity -> Compiler -> ProgramConfiguration -> PackageDBStack
                -> [String] -> IO ()
invokeHcPkg verbosity comp conf dbStack extraArgs =
309 310 311 312
  withHcPkg "invokeHcPkg" comp conf
    (\hpi -> HcPkg.invoke hpi verbosity dbStack extraArgs)

withHcPkg :: String -> Compiler -> ProgramConfiguration
313
          -> (HcPkg.HcPkgInfo -> IO a) -> IO a
314 315 316 317 318 319 320
withHcPkg name comp conf f =
  case compilerFlavor comp of
    GHC   -> f (GHC.hcPkgInfo conf)
    GHCJS -> f (GHCJS.hcPkgInfo conf)
    LHC   -> f (LHC.hcPkgInfo conf)
    _     -> die ("Distribution.Simple.Register." ++ name ++ ":\
                  \not implemented for this compiler")
321

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

writeHcPkgRegisterScript :: Verbosity
343
                         -> [InstalledPackageInfo]
344
                         -> PackageDBStack
345
                         -> HcPkg.HcPkgInfo
Duncan Coutts's avatar
Duncan Coutts committed
346
                         -> IO ()
347 348 349 350 351 352 353 354
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
355 356

  notice verbosity ("Creating package registration script: " ++ regScriptFileName)
357
  writeUTF8File regScriptFileName regScript
Duncan Coutts's avatar
Duncan Coutts committed
358 359 360 361 362 363
  setFileExecutable regScriptFileName

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

365

366 367 368
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

Duncan Coutts's avatar
Duncan Coutts committed
369 370 371 372 373 374 375
-- | 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
376
  -> AbiHash
Duncan Coutts's avatar
Duncan Coutts committed
377
  -> Library
378
  -> LocalBuildInfo
Duncan Coutts's avatar
Duncan Coutts committed
379 380 381
  -> ComponentLocalBuildInfo
  -> InstallDirs FilePath
  -> InstalledPackageInfo
382
generalInstalledPackageInfo adjustRelIncDirs pkg abi_hash lib lbi clbi installDirs =
383
  IPI.InstalledPackageInfo {
384 385 386
    IPI.sourcePackageId    = (packageId   pkg) {
                                pkgName = componentCompatPackageName clbi
                             },
387
    IPI.installedUnitId    = componentUnitId clbi,
388
    IPI.compatPackageKey   = componentCompatPackageKey clbi,
Duncan Coutts's avatar
Duncan Coutts committed
389 390 391 392 393 394 395
    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,
396
    IPI.synopsis           = synopsis    pkg,
Duncan Coutts's avatar
Duncan Coutts committed
397 398
    IPI.description        = description pkg,
    IPI.category           = category    pkg,
399
    IPI.abiHash            = abi_hash,
Duncan Coutts's avatar
Duncan Coutts committed
400
    IPI.exposed            = libExposed  lib,
401
    IPI.exposedModules     = componentExposedModules clbi,
Duncan Coutts's avatar
Duncan Coutts committed
402
    IPI.hiddenModules      = otherModules bi,
403
    IPI.trusted            = IPI.trusted IPI.emptyInstalledPackageInfo,
Duncan Coutts's avatar
Duncan Coutts committed
404
    IPI.importDirs         = [ libdir installDirs | hasModules ],
405 406
    -- Note. the libsubdir and datasubdir templates have already been expanded
    -- into libdir and datadir.
Duncan Coutts's avatar
Duncan Coutts committed
407 408 409
    IPI.libraryDirs        = if hasLibrary
                               then libdir installDirs : extraLibDirs bi
                               else                      extraLibDirs bi,
410
    IPI.dataDir            = datadir installDirs,
411
    IPI.hsLibraries        = if hasLibrary
412
                               then [getHSLibraryName (componentUnitId clbi)]
413
                               else [],
Duncan Coutts's avatar
Duncan Coutts committed
414
    IPI.extraLibraries     = extraLibs bi,
415
    IPI.extraGHCiLibraries = extraGHCiLibs bi,
Duncan Coutts's avatar
Duncan Coutts committed
416 417
    IPI.includeDirs        = absinc ++ adjustRelIncDirs relinc,
    IPI.includes           = includes bi,
418
    IPI.depends            = map fst (componentPackageDeps clbi),
Duncan Coutts's avatar
Duncan Coutts committed
419 420 421 422 423
    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,
424
    IPI.frameworkDirs      = extraFrameworkDirs bi,
Duncan Coutts's avatar
Duncan Coutts committed
425
    IPI.haddockInterfaces  = [haddockdir installDirs </> haddockName pkg],
426 427
    IPI.haddockHTMLs       = [htmldir installDirs],
    IPI.pkgRoot            = Nothing
Duncan Coutts's avatar
Duncan Coutts committed
428 429 430 431
  }
  where
    bi = libBuildInfo lib
    (absinc, relinc) = partition isAbsolute (includeDirs bi)
432
    hasModules = not $ null (libModules lib)
Duncan Coutts's avatar
Duncan Coutts committed
433
    hasLibrary = hasModules || not (null (cSources bi))
434 435 436
                            || (not (null (jsSources bi)) &&
                                compilerFlavor (compiler lbi) == GHCJS)

Ian D. Bollinger's avatar
Ian D. Bollinger committed
437
-- | Construct 'InstalledPackageInfo' for a library that is in place in the
Duncan Coutts's avatar
Duncan Coutts committed
438 439
-- build tree.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
440
-- This function knows about the layout of in place packages.
Duncan Coutts's avatar
Duncan Coutts committed
441 442 443 444
--
inplaceInstalledPackageInfo :: FilePath -- ^ top of the build tree
                            -> FilePath -- ^ location of the dist tree
                            -> PackageDescription
445
                            -> AbiHash
Duncan Coutts's avatar
Duncan Coutts committed
446 447 448 449
                            -> Library
                            -> LocalBuildInfo
                            -> ComponentLocalBuildInfo
                            -> InstalledPackageInfo
450
inplaceInstalledPackageInfo inplaceDir distPref pkg abi_hash lib lbi clbi =
451
    generalInstalledPackageInfo adjustRelativeIncludeDirs
452
                                pkg abi_hash lib lbi clbi installDirs
Duncan Coutts's avatar
Duncan Coutts committed
453
  where
refold's avatar
refold committed
454
    adjustRelativeIncludeDirs = map (inplaceDir </>)
455
    libTargetDir = componentBuildDir lbi clbi
Duncan Coutts's avatar
Duncan Coutts committed
456
    installDirs =
457
      (absoluteComponentInstallDirs pkg lbi (componentUnitId clbi) NoCopyDest) {
458
        libdir     = inplaceDir </> libTargetDir,
459
        datadir    = inplaceDir </> dataDir pkg,
Duncan Coutts's avatar
Duncan Coutts committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473
        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
474
                             -> AbiHash
Duncan Coutts's avatar
Duncan Coutts committed
475 476 477 478
                             -> Library
                             -> LocalBuildInfo
                             -> ComponentLocalBuildInfo
                             -> InstalledPackageInfo
479
absoluteInstalledPackageInfo pkg abi_hash lib lbi clbi =
480
    generalInstalledPackageInfo adjustReativeIncludeDirs
481
                                pkg abi_hash lib lbi clbi installDirs
Duncan Coutts's avatar
Duncan Coutts committed
482 483 484 485 486 487 488
  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
489
    installDirs = absoluteComponentInstallDirs pkg lbi (componentUnitId clbi) NoCopyDest
Duncan Coutts's avatar
Duncan Coutts committed
490

491 492

relocatableInstalledPackageInfo :: PackageDescription
493
                                -> AbiHash
494 495 496
                                -> Library
                                -> LocalBuildInfo
                                -> ComponentLocalBuildInfo
497
                                -> FilePath
498
                                -> InstalledPackageInfo
499
relocatableInstalledPackageInfo pkg abi_hash lib lbi clbi pkgroot =
500
    generalInstalledPackageInfo adjustReativeIncludeDirs
501
                                pkg abi_hash lib lbi clbi installDirs
502 503 504 505 506 507 508
  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
509 510

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

simonmar's avatar
simonmar committed
513 514
-- -----------------------------------------------------------------------------
-- Unregistration
515

ijones's avatar
ijones committed
516
unregister :: PackageDescription -> LocalBuildInfo -> RegisterFlags -> IO ()
Duncan Coutts's avatar
Duncan Coutts committed
517 518 519
unregister pkg lbi regFlags = do
  let pkgid     = packageId pkg
      genScript = fromFlag (regGenScript regFlags)
520
      verbosity = fromFlag (regVerbosity regFlags)
Duncan Coutts's avatar
Duncan Coutts committed
521
      packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
522
                                    (regPackageDB regFlags)
523 524 525 526 527 528 529
      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
530
  setupMessage verbosity "Unregistering" pkgid
531 532
  withHcPkg "unregistering is only implemented for GHC and GHCJS"
    (compiler lbi) (withPrograms lbi) unreg
ijones's avatar
ijones committed
533

Duncan Coutts's avatar
Duncan Coutts committed
534 535 536 537
unregScriptFileName :: FilePath
unregScriptFileName = case buildOS of
                          Windows -> "unregister.bat"
                          _       -> "unregister.sh"
538 539 540 541 542 543

internalPackageDBPath :: LocalBuildInfo -> FilePath -> FilePath
internalPackageDBPath lbi distPref =
      case compilerFlavor (compiler lbi) of
        UHC -> UHC.inplacePackageDbPath lbi
        _   -> distPref </> "package.conf.inplace"
544 545 546 547 548 549 550 551 552 553 554 555

-- | Initialize a new package db file for libraries defined
-- internally to the package.
createInternalPackageDB :: Verbosity -> LocalBuildInfo -> FilePath
                        -> IO PackageDB
createInternalPackageDB verbosity lbi distPref = do
    existsAlready <- doesPackageDBExist dbPath
    when existsAlready $ deletePackageDB dbPath
    createPackageDB verbosity (compiler lbi) (withPrograms lbi) False dbPath
    return (SpecificPackageDB dbPath)
  where
    dbPath = internalPackageDBPath lbi distPref