Register.hs 17.8 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
48
49

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
50
import qualified Distribution.Simple.HaskellSuite as HaskellSuite
51

52
import Distribution.Simple.Compiler
53
         ( compilerVersion, Compiler, CompilerFlavor(..), compilerFlavor
54
         , PackageDBStack, registrationPackageDB )
Duncan Coutts's avatar
Duncan Coutts committed
55
import Distribution.Simple.Program
56
         ( ProgramConfiguration, runProgramInvocation )
Duncan Coutts's avatar
Duncan Coutts committed
57
58
import Distribution.Simple.Program.Script
         ( invocationAsSystemScript )
59
import           Distribution.Simple.Program.HcPkg (HcPkgInfo)
Duncan Coutts's avatar
Duncan Coutts committed
60
import qualified Distribution.Simple.Program.HcPkg as HcPkg
61
62
63
import Distribution.Simple.Setup
         ( RegisterFlags(..), CopyDest(..)
         , fromFlag, fromFlagOrDefault, flagToMaybe )
Duncan Coutts's avatar
Duncan Coutts committed
64
import Distribution.PackageDescription
65
         ( PackageDescription(..), Library(..), BuildInfo(..), libModules )
66
import Distribution.Package
67
         ( Package(..), packageName, InstalledPackageId(..) )
68
import Distribution.InstalledPackageInfo
69
         ( InstalledPackageInfo, InstalledPackageInfo_(InstalledPackageInfo)
70
         , showInstalledPackageInfo )
71
import qualified Distribution.InstalledPackageInfo as IPI
72
import Distribution.Simple.Utils
73
         ( writeUTF8File, writeFileAtomic, setFileExecutable
Duncan Coutts's avatar
Duncan Coutts committed
74
         , die, notice, setupMessage )
75
import Distribution.System
76
         ( OS(..), buildOS )
77
78
import Distribution.Text
         ( display )
79
import Distribution.Version ( Version(..) )
Duncan Coutts's avatar
Duncan Coutts committed
80
81
import Distribution.Verbosity as Verbosity
         ( Verbosity, normal )
82

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

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

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

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

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

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

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

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

136
    writeRegisterScript installedPkgInfo =
Duncan Coutts's avatar
Duncan Coutts committed
137
      case compilerFlavor (compiler lbi) of
138
139
140
141
142
143
        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)
               (writeHcPkgRegisterScript verbosity installedPkgInfo packageDbs)
Duncan Coutts's avatar
Duncan Coutts committed
144
145
146
147
148
149

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


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

162
163
  --TODO: the method of setting the InstalledPackageId is compiler specific
  --      this aspect should be delegated to a per-compiler helper.
164
  let comp = compiler lbi
165
166
167
168
169
  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))
170
171
172
     GHCJS -> do
            s <- GHCJS.libAbiHash verbosity pkg lbi lib clbi
            return (InstalledPackageId (display (packageId pkg) ++ '-':s))
173
174
     _other -> do
            return (InstalledPackageId (display (packageId pkg)))
175

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

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


185
186
187
188
-- | Create an empty package DB at the specified location.
initPackageDB :: Verbosity -> Compiler -> ProgramConfiguration -> FilePath
                 -> IO ()
initPackageDB verbosity comp conf dbPath =
189
  case compilerFlavor comp of
190
    HaskellSuite {} -> HaskellSuite.initPackageDB verbosity conf dbPath
191
192
193
    _               -> withHcPkg "Distribution.Simple.Register.initPackageDB: \
                                 \not implemented for this compiler" comp conf
                                 (\hpi -> HcPkg.init hpi verbosity dbPath)
refold's avatar
refold committed
194
195
196
197
198
199

-- | 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 =
200
201
202
203
204
205
206
207
208
209
210
211
  withHcPkg "invokeHcPkg" comp conf
    (\hpi -> HcPkg.invoke hpi verbosity dbStack extraArgs)

withHcPkg :: String -> Compiler -> ProgramConfiguration
          -> (HcPkgInfo -> IO a) -> IO a
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")
212

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

writeHcPkgRegisterScript :: Verbosity
236
                         -> InstalledPackageInfo
237
                         -> PackageDBStack
238
                         -> HcPkgInfo
Duncan Coutts's avatar
Duncan Coutts committed
239
                         -> IO ()
240
241
writeHcPkgRegisterScript verbosity installedPkgInfo packageDbs hpi = do
  let invocation  = HcPkg.reregisterInvocation hpi Verbosity.normal
242
                      packageDbs (Right installedPkgInfo)
243
      regScript   = invocationAsSystemScript buildOS invocation
Duncan Coutts's avatar
Duncan Coutts committed
244
245

  notice verbosity ("Creating package registration script: " ++ regScriptFileName)
246
  writeUTF8File regScriptFileName regScript
Duncan Coutts's avatar
Duncan Coutts committed
247
248
249
250
251
252
  setFileExecutable regScriptFileName

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

254

255
256
257
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

Duncan Coutts's avatar
Duncan Coutts committed
258
259
260
261
262
263
264
-- | 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
265
  -> InstalledPackageId
Duncan Coutts's avatar
Duncan Coutts committed
266
  -> Library
267
  -> LocalBuildInfo
Duncan Coutts's avatar
Duncan Coutts committed
268
269
270
  -> ComponentLocalBuildInfo
  -> InstallDirs FilePath
  -> InstalledPackageInfo
271
generalInstalledPackageInfo adjustRelIncDirs pkg ipid lib lbi clbi installDirs =
Duncan Coutts's avatar
Duncan Coutts committed
272
  InstalledPackageInfo {
273
    IPI.installedPackageId = ipid,
274
    IPI.sourcePackageId    = packageId   pkg,
275
    IPI.packageKey         = pkgKey lbi,
Duncan Coutts's avatar
Duncan Coutts committed
276
277
278
279
280
281
282
    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,
283
    IPI.synopsis           = synopsis    pkg,
Duncan Coutts's avatar
Duncan Coutts committed
284
285
286
    IPI.description        = description pkg,
    IPI.category           = category    pkg,
    IPI.exposed            = libExposed  lib,
287
    IPI.exposedModules     = map fixupSelf (componentExposedModules clbi),
Duncan Coutts's avatar
Duncan Coutts committed
288
    IPI.hiddenModules      = otherModules bi,
289
290
291
    IPI.instantiatedWith   = map (\(k,(p,n)) ->
                                   (k,IPI.OriginalModule (IPI.installedPackageId p) n))
                                 (instantiatedWith lbi),
292
    IPI.trusted            = IPI.trusted IPI.emptyInstalledPackageInfo,
Duncan Coutts's avatar
Duncan Coutts committed
293
    IPI.importDirs         = [ libdir installDirs | hasModules ],
294
295
    -- Note. the libsubdir and datasubdir templates have already been expanded
    -- into libdir and datadir.
Duncan Coutts's avatar
Duncan Coutts committed
296
297
298
    IPI.libraryDirs        = if hasLibrary
                               then libdir installDirs : extraLibDirs bi
                               else                      extraLibDirs bi,
299
    IPI.dataDir            = datadir installDirs,
refold's avatar
refold committed
300
    IPI.hsLibraries        = [ libname
301
302
                             | LibraryName libname <- componentLibraries clbi
                             , hasLibrary ],
Duncan Coutts's avatar
Duncan Coutts committed
303
    IPI.extraLibraries     = extraLibs bi,
304
    IPI.extraGHCiLibraries = extraGHCiLibs bi,
Duncan Coutts's avatar
Duncan Coutts committed
305
306
    IPI.includeDirs        = absinc ++ adjustRelIncDirs relinc,
    IPI.includes           = includes bi,
307
    IPI.depends            = map fst (componentPackageDeps clbi),
Duncan Coutts's avatar
Duncan Coutts committed
308
309
310
311
312
313
314
315
316
317
318
319
    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)
320
    hasModules = not $ null (libModules lib)
Duncan Coutts's avatar
Duncan Coutts committed
321
    hasLibrary = hasModules || not (null (cSources bi))
322
323
324
                            || (not (null (jsSources bi)) &&
                                compilerFlavor (compiler lbi) == GHCJS)

325
326
    -- 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
327
    -- of modules defined within this package, so we used an empty one that
328
329
    -- 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.
330
331
332
333
334
335
    fixupSelf (IPI.ExposedModule n o o') =
        IPI.ExposedModule n (fmap fixupOriginalModule o)
                            (fmap fixupOriginalModule o')
    fixupOriginalModule (IPI.OriginalModule i m) = IPI.OriginalModule (fixupIpid i) m
    fixupIpid (InstalledPackageId []) = ipid
    fixupIpid x = x
Duncan Coutts's avatar
Duncan Coutts committed
336

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

simonmar's avatar
simonmar committed
390
391
-- -----------------------------------------------------------------------------
-- Unregistration
392

ijones's avatar
ijones committed
393
unregister :: PackageDescription -> LocalBuildInfo -> RegisterFlags -> IO ()
Duncan Coutts's avatar
Duncan Coutts committed
394
395
396
unregister pkg lbi regFlags = do
  let pkgid     = packageId pkg
      genScript = fromFlag (regGenScript regFlags)
397
      verbosity = fromFlag (regVerbosity regFlags)
Duncan Coutts's avatar
Duncan Coutts committed
398
      packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
399
                                    (regPackageDB regFlags)
400
401
402
403
404
405
406
      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
407
  setupMessage verbosity "Unregistering" pkgid
408
409
  withHcPkg "unregistering is only implemented for GHC and GHCJS"
    (compiler lbi) (withPrograms lbi) unreg
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"