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

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

30
    initPackageDB,
refold's avatar
refold committed
31
    invokeHcPkg,
Duncan Coutts's avatar
Duncan Coutts committed
32
    registerPackage,
33
    generateRegistrationInfo,
Duncan Coutts's avatar
Duncan Coutts committed
34
35
36
    inplaceInstalledPackageInfo,
    absoluteInstalledPackageInfo,
    generalInstalledPackageInfo,
37
  ) where
38

39
40
import Distribution.Simple.LocalBuildInfo
         ( LocalBuildInfo(..), ComponentLocalBuildInfo(..)
41
         , ComponentName(..), getComponentLocalBuildInfo
42
         , LibraryName(..)
43
         , InstallDirs(..), absoluteInstallDirs )
44
import Distribution.Simple.BuildPaths (haddockName)
45
46
47
import qualified Distribution.Simple.GHC  as GHC
import qualified Distribution.Simple.LHC  as LHC
import qualified Distribution.Simple.Hugs as Hugs
48
import qualified Distribution.Simple.UHC  as UHC
49
import qualified Distribution.Simple.HaskellSuite as HaskellSuite
50
import Distribution.Simple.Compiler
51
         ( compilerVersion, Compiler, CompilerFlavor(..), compilerFlavor
52
         , PackageDBStack, registrationPackageDB )
Duncan Coutts's avatar
Duncan Coutts committed
53
import Distribution.Simple.Program
54
55
56
         ( ProgramConfiguration, ConfiguredProgram
         , runProgramInvocation, requireProgram, lookupProgram
         , ghcPkgProgram, lhcPkgProgram )
Duncan Coutts's avatar
Duncan Coutts committed
57
58
59
import Distribution.Simple.Program.Script
         ( invocationAsSystemScript )
import qualified Distribution.Simple.Program.HcPkg as HcPkg
60
61
62
import Distribution.Simple.Setup
         ( RegisterFlags(..), CopyDest(..)
         , fromFlag, fromFlagOrDefault, flagToMaybe )
Duncan Coutts's avatar
Duncan Coutts committed
63
64
import Distribution.PackageDescription
         ( PackageDescription(..), Library(..), BuildInfo(..), hcOptions )
65
import Distribution.Package
66
         ( Package(..), packageName, InstalledPackageId(..) )
67
import Distribution.InstalledPackageInfo
68
         ( InstalledPackageInfo, InstalledPackageInfo_(InstalledPackageInfo)
69
         , showInstalledPackageInfo, ModuleReexport(..) )
70
import qualified Distribution.InstalledPackageInfo as IPI
71
import Distribution.Simple.Utils
72
         ( writeUTF8File, writeFileAtomic, setFileExecutable
Duncan Coutts's avatar
Duncan Coutts committed
73
         , die, notice, setupMessage )
74
import Distribution.System
75
         ( OS(..), buildOS )
76
77
import Distribution.Text
         ( display )
78
import Distribution.Version ( Version(..) )
Duncan Coutts's avatar
Duncan Coutts committed
79
80
import Distribution.Verbosity as Verbosity
         ( Verbosity, normal )
81
82
import Distribution.Compat.Exception
         ( tryIO )
83

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

88
import Control.Monad (when)
Duncan Coutts's avatar
Duncan Coutts committed
89
import Data.Maybe
90
         ( isJust, fromMaybe, maybeToList )
Duncan Coutts's avatar
Duncan Coutts committed
91
92
import Data.List
         ( partition, nub )
93
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
94

simonmar's avatar
simonmar committed
95
96
97
-- -----------------------------------------------------------------------------
-- Registration

ijones's avatar
ijones committed
98
register :: PackageDescription -> LocalBuildInfo
ijones's avatar
ijones committed
99
         -> RegisterFlags -- ^Install in the user's database?; verbose
ijones's avatar
ijones committed
100
         -> IO ()
101
register pkg@PackageDescription { library       = Just lib  } lbi regFlags
102
  = do
103
    let clbi = getComponentLocalBuildInfo lbi CLibName
104
105
106
    installedPkgInfo <- generateRegistrationInfo
                           verbosity pkg lib lbi clbi inplace distPref

107
108
109
    when (fromFlag (regPrintId regFlags)) $ do
      putStrLn (display (IPI.installedPackageId installedPkgInfo))

110
111
112
113
114
     -- Three different modes:
    case () of
     _ | modeGenerateRegFile   -> writeRegistrationFile installedPkgInfo
       | modeGenerateRegScript -> writeRegisterScript   installedPkgInfo
       | otherwise             -> registerPackage verbosity
115
                                    installedPkgInfo pkg lbi inplace packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
116
117
118
119
120
121
122
123
124

  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
125
126
127
128
129
    -- 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
130
131
132
    distPref  = fromFlag (regDistPref regFlags)
    verbosity = fromFlag (regVerbosity regFlags)

133
    writeRegistrationFile installedPkgInfo = do
Duncan Coutts's avatar
Duncan Coutts committed
134
      notice verbosity ("Creating package registration file: " ++ regFile)
135
      writeUTF8File regFile (showInstalledPackageInfo installedPkgInfo)
Duncan Coutts's avatar
Duncan Coutts committed
136

137
    writeRegisterScript installedPkgInfo =
Duncan Coutts's avatar
Duncan Coutts committed
138
139
      case compilerFlavor (compiler lbi) of
        GHC  -> do (ghcPkg, _) <- requireProgram verbosity ghcPkgProgram (withPrograms lbi)
140
                   writeHcPkgRegisterScript verbosity installedPkgInfo ghcPkg packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
141
        LHC  -> do (lhcPkg, _) <- requireProgram verbosity lhcPkgProgram (withPrograms lbi)
142
                   writeHcPkgRegisterScript verbosity installedPkgInfo lhcPkg packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
143
144
145
        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"
146
        UHC  -> notice verbosity "Registration scripts not needed for uhc"
Duncan Coutts's avatar
Duncan Coutts committed
147
148
149
150
151
152
153
        _    -> die "Registration scripts are not implemented for this compiler"

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


154
155
generateRegistrationInfo :: Verbosity
                         -> PackageDescription
Duncan Coutts's avatar
Duncan Coutts committed
156
157
158
159
160
161
                         -> Library
                         -> LocalBuildInfo
                         -> ComponentLocalBuildInfo
                         -> Bool
                         -> FilePath
                         -> IO InstalledPackageInfo
162
generateRegistrationInfo verbosity pkg lib lbi clbi inplace distPref = do
Duncan Coutts's avatar
Duncan Coutts committed
163
164
  --TODO: eliminate pwd!
  pwd <- getCurrentDirectory
165

166
167
  --TODO: the method of setting the InstalledPackageId is compiler specific
  --      this aspect should be delegated to a per-compiler helper.
168
  let comp = compiler lbi
169
170
171
172
173
174
175
  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)))
176

Duncan Coutts's avatar
Duncan Coutts committed
177
178
  let installedPkgInfo
        | inplace   = inplaceInstalledPackageInfo pwd distPref
179
                        pkg ipid lib lbi clbi
Duncan Coutts's avatar
Duncan Coutts committed
180
        | otherwise = absoluteInstalledPackageInfo
181
                        pkg ipid lib lbi clbi
182

183
184
185
  return installedPkgInfo{ IPI.installedPackageId = ipid }


186
187
188
189
190
191
-- | 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
192
    HaskellSuite {} -> HaskellSuite.initPackageDB verbosity conf dbPath
refold's avatar
refold committed
193
194
195
196
197
198
199
200
201
202
203
204
    _   -> die "Distribution.Simple.Register.initPackageDB: \
               \not implemented for this compiler"

-- | 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 =
    case (compilerFlavor comp) of
      GHC -> GHC.invokeHcPkg verbosity conf dbStack extraArgs
      _   -> die "Distribution.Simple.Register.invokeHcPkg: \
                 \not implemented for this compiler"
205

Duncan Coutts's avatar
Duncan Coutts committed
206
registerPackage :: Verbosity
207
                -> InstalledPackageInfo
Duncan Coutts's avatar
Duncan Coutts committed
208
209
210
                -> PackageDescription
                -> LocalBuildInfo
                -> Bool
211
                -> PackageDBStack
Duncan Coutts's avatar
Duncan Coutts committed
212
                -> IO ()
213
registerPackage verbosity installedPkgInfo pkg lbi inplace packageDbs = do
refold's avatar
refold committed
214
215
216
217
  let msg = if inplace
            then "In-place registering"
            else "Registering"
  setupMessage verbosity msg (packageId pkg)
Duncan Coutts's avatar
Duncan Coutts committed
218
  case compilerFlavor (compiler lbi) of
219
220
221
    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
222
    UHC  -> UHC.registerPackage  verbosity installedPkgInfo pkg lbi inplace packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
223
224
    JHC  -> notice verbosity "Registering for jhc (nothing to do)"
    NHC  -> notice verbosity "Registering for nhc98 (nothing to do)"
225
226
    HaskellSuite {} ->
      HaskellSuite.registerPackage verbosity installedPkgInfo pkg lbi inplace packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
227
228
229
230
    _    -> die "Registering is not implemented for this compiler"


writeHcPkgRegisterScript :: Verbosity
231
                         -> InstalledPackageInfo
Duncan Coutts's avatar
Duncan Coutts committed
232
                         -> ConfiguredProgram
233
                         -> PackageDBStack
Duncan Coutts's avatar
Duncan Coutts committed
234
                         -> IO ()
235
writeHcPkgRegisterScript verbosity installedPkgInfo hcPkg packageDbs = do
Duncan Coutts's avatar
Duncan Coutts committed
236
  let invocation  = HcPkg.reregisterInvocation hcPkg Verbosity.normal
237
                      packageDbs (Right installedPkgInfo)
Duncan Coutts's avatar
Duncan Coutts committed
238
239
240
      regScript   = invocationAsSystemScript buildOS   invocation

  notice verbosity ("Creating package registration script: " ++ regScriptFileName)
241
  writeUTF8File regScriptFileName regScript
Duncan Coutts's avatar
Duncan Coutts committed
242
243
244
245
246
247
  setFileExecutable regScriptFileName

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

249

250
251
252
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

Duncan Coutts's avatar
Duncan Coutts committed
253
254
255
256
257
258
259
-- | 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
260
  -> InstalledPackageId
Duncan Coutts's avatar
Duncan Coutts committed
261
  -> Library
262
  -> LocalBuildInfo
Duncan Coutts's avatar
Duncan Coutts committed
263
264
265
  -> ComponentLocalBuildInfo
  -> InstallDirs FilePath
  -> InstalledPackageInfo
266
generalInstalledPackageInfo adjustRelIncDirs pkg ipid lib lbi clbi installDirs =
Duncan Coutts's avatar
Duncan Coutts committed
267
  InstalledPackageInfo {
268
    IPI.installedPackageId = ipid,
269
    IPI.sourcePackageId    = packageId   pkg,
270
    IPI.packageKey         = pkgKey lbi,
Duncan Coutts's avatar
Duncan Coutts committed
271
272
273
274
275
276
277
    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,
278
    IPI.synopsis           = synopsis    pkg,
Duncan Coutts's avatar
Duncan Coutts committed
279
280
281
282
    IPI.description        = description pkg,
    IPI.category           = category    pkg,
    IPI.exposed            = libExposed  lib,
    IPI.exposedModules     = exposedModules lib,
283
    IPI.reexportedModules  = map fixupSelfReexport (componentModuleReexports clbi),
Duncan Coutts's avatar
Duncan Coutts committed
284
    IPI.hiddenModules      = otherModules bi,
285
    IPI.trusted            = IPI.trusted IPI.emptyInstalledPackageInfo,
Duncan Coutts's avatar
Duncan Coutts committed
286
287
288
289
    IPI.importDirs         = [ libdir installDirs | hasModules ],
    IPI.libraryDirs        = if hasLibrary
                               then libdir installDirs : extraLibDirs bi
                               else                      extraLibDirs bi,
refold's avatar
refold committed
290
    IPI.hsLibraries        = [ libname
291
292
                             | LibraryName libname <- componentLibraries clbi
                             , hasLibrary ],
Duncan Coutts's avatar
Duncan Coutts committed
293
294
295
296
    IPI.extraLibraries     = extraLibs bi,
    IPI.extraGHCiLibraries = [],
    IPI.includeDirs        = absinc ++ adjustRelIncDirs relinc,
    IPI.includes           = includes bi,
297
    IPI.depends            = map fst (componentPackageDeps clbi),
Duncan Coutts's avatar
Duncan Coutts committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
    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))
314
315
316
317
318
319
320
321
322
323
324
325
    -- Since we currently don't decide the InstalledPackageId of our package
    -- until just before we register, we didn't have one for the re-exports
    -- of modules definied within this package, so we used an empty one that
    -- we fill in here now that we know what it is. It's a bit of a hack,
    -- we ought really to decide the InstalledPackageId ahead of time.
    fixupSelfReexport mre@ModuleReexport {
                        moduleReexportDefiningPackage = InstalledPackageId []
                      }
                    = mre {
                        moduleReexportDefiningPackage = ipid
                      }
    fixupSelfReexport mre = mre
Duncan Coutts's avatar
Duncan Coutts committed
326

Ian D. Bollinger's avatar
Ian D. Bollinger committed
327
-- | Construct 'InstalledPackageInfo' for a library that is in place in the
Duncan Coutts's avatar
Duncan Coutts committed
328
329
-- build tree.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
330
-- This function knows about the layout of in place packages.
Duncan Coutts's avatar
Duncan Coutts committed
331
332
333
334
--
inplaceInstalledPackageInfo :: FilePath -- ^ top of the build tree
                            -> FilePath -- ^ location of the dist tree
                            -> PackageDescription
335
                            -> InstalledPackageId
Duncan Coutts's avatar
Duncan Coutts committed
336
337
338
339
                            -> Library
                            -> LocalBuildInfo
                            -> ComponentLocalBuildInfo
                            -> InstalledPackageInfo
340
341
342
inplaceInstalledPackageInfo inplaceDir distPref pkg ipid lib lbi clbi =
    generalInstalledPackageInfo adjustRelativeIncludeDirs
                                pkg ipid lib lbi clbi installDirs
Duncan Coutts's avatar
Duncan Coutts committed
343
  where
refold's avatar
refold committed
344
    adjustRelativeIncludeDirs = map (inplaceDir </>)
Duncan Coutts's avatar
Duncan Coutts committed
345
    installDirs =
346
      (absoluteInstallDirs pkg lbi NoCopyDest) {
Duncan Coutts's avatar
Duncan Coutts committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
        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
364
                             -> InstalledPackageId
Duncan Coutts's avatar
Duncan Coutts committed
365
366
367
368
                             -> Library
                             -> LocalBuildInfo
                             -> ComponentLocalBuildInfo
                             -> InstalledPackageInfo
369
370
371
absoluteInstalledPackageInfo pkg ipid lib lbi clbi =
    generalInstalledPackageInfo adjustReativeIncludeDirs
                                pkg ipid lib lbi clbi installDirs
Duncan Coutts's avatar
Duncan Coutts committed
372
373
374
375
376
377
378
  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
379
    installDirs = absoluteInstallDirs pkg lbi NoCopyDest
Duncan Coutts's avatar
Duncan Coutts committed
380

simonmar's avatar
simonmar committed
381
382
-- -----------------------------------------------------------------------------
-- Unregistration
383

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

Duncan Coutts's avatar
Duncan Coutts committed
411
412
413
414
unregScriptFileName :: FilePath
unregScriptFileName = case buildOS of
                          Windows -> "unregister.bat"
                          _       -> "unregister.sh"