Register.hs 23.8 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
    internalPackageDBPath,
34
    createInternalPackageDB,
35

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

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
59
60
61
62

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
63
import qualified Distribution.Simple.HaskellSuite as HaskellSuite
64

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

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

85
import Data.List (partition)
86
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
87

simonmar's avatar
simonmar committed
88
89
90
-- -----------------------------------------------------------------------------
-- Registration

ijones's avatar
ijones committed
91
register :: PackageDescription -> LocalBuildInfo
ijones's avatar
ijones committed
92
         -> RegisterFlags -- ^Install in the user's database?; verbose
ijones's avatar
ijones committed
93
         -> IO ()
94
95
96
97
98
99
register pkg_descr lbi flags =
   -- 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
100
101
 where
  doRegister = do
102
    targets <- readTargetInfos verbosity pkg_descr lbi (regArgs flags)
103
104
105

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

    ipis <-
      if fromFlag (regAssumeDepsUpToDate flags)
        then
115
116
117
          case targets of
            [target] -> do
                mb_ipi <- maybeGenerateOne target
118
119
120
121
122
123
                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
124
           . traverse maybeGenerateOne
125
           $ neededTargetsInBuildOrder' pkg_descr lbi (map nodeKey targets)
126
127
128
129
    registerAll pkg_descr lbi flags ipis
    return ()
   where
    verbosity = fromFlag (regVerbosity flags)
130
131
132
133
134

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

153
154
155
156
157
registerAll :: PackageDescription -> LocalBuildInfo -> RegisterFlags
            -> [InstalledPackageInfo]
            -> IO ()
registerAll pkg lbi regFlags ipis
  = do
158
    when (fromFlag (regPrintId regFlags)) $ do
159
      for_ ipis $ \installedPkgInfo ->
160
161
162
        -- Only print the public library's IPI
        when (IPI.sourcePackageId installedPkgInfo == packageId pkg) $
          putStrLn (display (IPI.installedUnitId installedPkgInfo))
163

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

  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
181
182
183
184
185
    -- 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
186
187
    verbosity = fromFlag (regVerbosity regFlags)

188
189
190
191
192
    writeRegistrationFileOrDirectory = do
      -- Handles overwriting both directory and file
      deletePackageDB regFile
      case ipis of
        [installedPkgInfo] -> do
193
          info verbosity ("Creating package registration file: " ++ regFile)
194
195
          writeUTF8File regFile (IPI.showInstalledPackageInfo installedPkgInfo)
        _ -> do
196
          info verbosity ("Creating package registration directory: " ++ regFile)
197
198
199
200
201
          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)
202
          for_ (zip ([1..] :: [Int]) ipis) $ \(i, installedPkgInfo) ->
203
204
205
206
            writeUTF8File (regFile </> (number i ++ "-" ++ display (IPI.installedUnitId installedPkgInfo)))
                          (IPI.showInstalledPackageInfo installedPkgInfo)

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


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

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

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

255

256
  return installedPkgInfo{ IPI.abiHash = abi_hash }
257

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

274
initPackageDB :: Verbosity -> Compiler -> ProgramDb -> FilePath -> IO ()
275
initPackageDB verbosity comp progdb dbPath =
276
    createPackageDB verbosity comp progdb False dbPath
277

278
-- | Create an empty package DB at the specified location.
279
createPackageDB :: Verbosity -> Compiler -> ProgramDb -> Bool
280
281
282
283
284
285
286
287
288
289
290
                -> 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"

291
doesPackageDBExist :: FilePath -> NoCallStackIO Bool
292
293
294
295
296
297
298
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

299
deletePackageDB :: FilePath -> NoCallStackIO ()
300
301
302
303
304
305
306
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
307
308
309

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

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

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

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

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

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

369

370
371
372
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

Duncan Coutts's avatar
Duncan Coutts committed
373
374
375
376
377
378
379
-- | 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
380
  -> AbiHash
Duncan Coutts's avatar
Duncan Coutts committed
381
  -> Library
382
  -> LocalBuildInfo
Duncan Coutts's avatar
Duncan Coutts committed
383
384
385
  -> ComponentLocalBuildInfo
  -> InstallDirs FilePath
  -> InstalledPackageInfo
386
generalInstalledPackageInfo adjustRelIncDirs pkg abi_hash lib lbi clbi installDirs =
387
  IPI.InstalledPackageInfo {
388
389
390
    IPI.sourcePackageId    = (packageId   pkg) {
                                pkgName = componentCompatPackageName clbi
                             },
391
    IPI.installedUnitId    = componentUnitId clbi,
392
    IPI.installedComponentId_ = componentComponentId clbi,
393
    IPI.instantiatedWith   = componentInstantiatedWith clbi,
394
    IPI.compatPackageKey   = componentCompatPackageKey clbi,
Duncan Coutts's avatar
Duncan Coutts committed
395
396
397
398
399
400
401
    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,
402
    IPI.synopsis           = synopsis    pkg,
Duncan Coutts's avatar
Duncan Coutts committed
403
404
    IPI.description        = description pkg,
    IPI.category           = category    pkg,
405
    IPI.abiHash            = abi_hash,
406
    IPI.indefinite         = componentIsIndefinite clbi,
Duncan Coutts's avatar
Duncan Coutts committed
407
    IPI.exposed            = libExposed  lib,
408
    IPI.exposedModules     = componentExposedModules clbi,
Duncan Coutts's avatar
Duncan Coutts committed
409
    IPI.hiddenModules      = otherModules bi,
410
    IPI.trusted            = IPI.trusted IPI.emptyInstalledPackageInfo,
Duncan Coutts's avatar
Duncan Coutts committed
411
    IPI.importDirs         = [ libdir installDirs | hasModules ],
412
413
    -- Note. the libsubdir and datasubdir templates have already been expanded
    -- into libdir and datadir.
Duncan Coutts's avatar
Duncan Coutts committed
414
415
416
    IPI.libraryDirs        = if hasLibrary
                               then libdir installDirs : extraLibDirs bi
                               else                      extraLibDirs bi,
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
427
                             --TODO: unclear what the root cause of the
                             -- duplication is, but we nub it here for now:
    IPI.depends            = ordNub $ map fst (componentPackageDeps clbi),
Duncan Coutts's avatar
Duncan Coutts committed
428
429
430
431
432
    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,
433
    IPI.frameworkDirs      = extraFrameworkDirs bi,
Duncan Coutts's avatar
Duncan Coutts committed
434
    IPI.haddockInterfaces  = [haddockdir installDirs </> haddockName pkg],
435
436
    IPI.haddockHTMLs       = [htmldir installDirs],
    IPI.pkgRoot            = Nothing
Duncan Coutts's avatar
Duncan Coutts committed
437
438
439
440
  }
  where
    bi = libBuildInfo lib
    (absinc, relinc) = partition isAbsolute (includeDirs bi)
441
    hasModules = not $ null (allLibModules lib clbi)
Duncan Coutts's avatar
Duncan Coutts committed
442
    hasLibrary = hasModules || not (null (cSources bi))
443
444
445
                            || (not (null (jsSources bi)) &&
                                compilerFlavor (compiler lbi) == GHCJS)

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

500
501

relocatableInstalledPackageInfo :: PackageDescription
502
                                -> AbiHash
503
504
505
                                -> Library
                                -> LocalBuildInfo
                                -> ComponentLocalBuildInfo
506
                                -> FilePath
507
                                -> InstalledPackageInfo
508
relocatableInstalledPackageInfo pkg abi_hash lib lbi clbi pkgroot =
509
    generalInstalledPackageInfo adjustReativeIncludeDirs
510
                                pkg abi_hash lib lbi clbi installDirs
511
512
513
514
515
516
517
  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
518
519

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

simonmar's avatar
simonmar committed
522
523
-- -----------------------------------------------------------------------------
-- Unregistration
524

ijones's avatar
ijones committed
525
unregister :: PackageDescription -> LocalBuildInfo -> RegisterFlags -> IO ()
Duncan Coutts's avatar
Duncan Coutts committed
526
527
528
unregister pkg lbi regFlags = do
  let pkgid     = packageId pkg
      genScript = fromFlag (regGenScript regFlags)
529
      verbosity = fromFlag (regVerbosity regFlags)
Duncan Coutts's avatar
Duncan Coutts committed
530
      packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
531
                                    (regPackageDB regFlags)
532
533
534
535
536
537
538
      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
539
  setupMessage verbosity "Unregistering" pkgid
540
541
  withHcPkg "unregistering is only implemented for GHC and GHCJS"
    (compiler lbi) (withPrograms lbi) unreg
ijones's avatar
ijones committed
542

Duncan Coutts's avatar
Duncan Coutts committed
543
544
545
546
unregScriptFileName :: FilePath
unregScriptFileName = case buildOS of
                          Windows -> "unregister.bat"
                          _       -> "unregister.sh"
547
548
549
550
551
552

internalPackageDBPath :: LocalBuildInfo -> FilePath -> FilePath
internalPackageDBPath lbi distPref =
      case compilerFlavor (compiler lbi) of
        UHC -> UHC.inplacePackageDbPath lbi
        _   -> distPref </> "package.conf.inplace"
553
554
555
556
557
558
559
560
561
562
563
564

-- | 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