Register.hs 17 KB
Newer Older
1
2
-----------------------------------------------------------------------------
-- |
ijones's avatar
ijones committed
3
-- Module      :  Distribution.Simple.Register
4
-- Copyright   :  Isaac Jones 2003-2004
5
--
Duncan Coutts's avatar
Duncan Coutts committed
6
-- Maintainer  :  cabal-devel@haskell.org
ijones's avatar
ijones committed
7
-- Portability :  portable
8
--
Duncan Coutts's avatar
Duncan Coutts committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- 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,
-- or alternatively to register the package inplace in the build tree. The
-- 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.
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

{- Copyright (c) 2003-2004, Isaac Jones
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

56
module Distribution.Simple.Register (
Duncan Coutts's avatar
Duncan Coutts committed
57
58
59
60
61
62
63
    register,
    unregister,

    registerPackage,
    inplaceInstalledPackageInfo,
    absoluteInstalledPackageInfo,
    generalInstalledPackageInfo,
64
  ) where
65

66
67
68
import Distribution.Simple.LocalBuildInfo
         ( LocalBuildInfo(..), ComponentLocalBuildInfo(..)
         , InstallDirs(..), absoluteInstallDirs )
69
import Distribution.Simple.BuildPaths (haddockName)
70
import Distribution.Simple.Compiler
71
72
         ( CompilerFlavor(..), compilerFlavor
         , PackageDB(..), registrationPackageDB )
Duncan Coutts's avatar
Duncan Coutts committed
73
74
75
76
77
78
import Distribution.Simple.Program
         ( ConfiguredProgram, runProgramInvocation
         , requireProgram, lookupProgram, ghcPkgProgram, lhcPkgProgram )
import Distribution.Simple.Program.Script
         ( invocationAsSystemScript )
import qualified Distribution.Simple.Program.HcPkg as HcPkg
79
80
81
import Distribution.Simple.Setup
         ( RegisterFlags(..), CopyDest(..)
         , fromFlag, fromFlagOrDefault, flagToMaybe )
Duncan Coutts's avatar
Duncan Coutts committed
82
83
import Distribution.PackageDescription
         ( PackageDescription(..), Library(..), BuildInfo(..), hcOptions )
84
import Distribution.Package
85
         ( Package(..), packageName )
86
import Distribution.InstalledPackageInfo
87
88
         ( InstalledPackageInfo, InstalledPackageInfo_(InstalledPackageInfo)
         , showInstalledPackageInfo )
89
import qualified Distribution.InstalledPackageInfo as IPI
90
import Distribution.Simple.Utils
Duncan Coutts's avatar
Duncan Coutts committed
91
92
         ( createDirectoryIfMissingVerbose, writeFileAtomic
         , die, notice, setupMessage )
93
import Distribution.System
94
         ( OS(..), buildOS )
95
96
import Distribution.Text
         ( display )
Duncan Coutts's avatar
Duncan Coutts committed
97
98
99
100
import Distribution.Verbosity as Verbosity
         ( Verbosity, normal )
import Distribution.Compat.CopyFile
         ( setFileExecutable )
101

102
import System.FilePath ((</>), (<.>), isAbsolute)
Duncan Coutts's avatar
Duncan Coutts committed
103
104
import System.Directory
         ( getCurrentDirectory, removeDirectoryRecursive )
105
import System.IO.Error (try)
ijones's avatar
ijones committed
106

107
import Control.Monad (when)
Duncan Coutts's avatar
Duncan Coutts committed
108
109
import Data.Maybe
         ( isJust, fromMaybe )
110
import Data.List (partition)
simonmar's avatar
simonmar committed
111

112

simonmar's avatar
simonmar committed
113
114
115
-- -----------------------------------------------------------------------------
-- Registration

ijones's avatar
ijones committed
116
register :: PackageDescription -> LocalBuildInfo
ijones's avatar
ijones committed
117
         -> RegisterFlags -- ^Install in the user's database?; verbose
ijones's avatar
ijones committed
118
         -> IO ()
Duncan Coutts's avatar
Duncan Coutts committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
register pkg@PackageDescription { library       = Just lib  }
         lbi@LocalBuildInfo     { libraryConfig = Just clbi } regFlags
  -- Three different modes:
  | modeGenerateRegFile   = writeRegistrationFile
  | modeGenerateRegScript = writeRegisterScript
  | otherwise             = registerPackage verbosity
                              pkg lib lbi clbi distPref inplace packageDb

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

    modeGenerateRegScript = fromFlag (regGenScript regFlags)

    inplace   = fromFlag (regInPlace regFlags)
    packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
                                  (regPackageDB regFlags)
    distPref  = fromFlag (regDistPref regFlags)
    verbosity = fromFlag (regVerbosity regFlags)

    writeRegistrationFile = do
      installedPkgInfo <- generateRegistrationInfo
                            pkg lib lbi clbi inplace distPref
      notice verbosity ("Creating package registration file: " ++ regFile)
      writeFileAtomic regFile (showInstalledPackageInfo installedPkgInfo ++ "\n")

    writeRegisterScript =
      case compilerFlavor (compiler lbi) of
        GHC  -> do (ghcPkg, _) <- requireProgram verbosity ghcPkgProgram (withPrograms lbi)
                   writeHcPkgRegisterScript verbosity ghcPkg pkg lib lbi clbi distPref inplace packageDb
        LHC  -> do (lhcPkg, _) <- requireProgram verbosity lhcPkgProgram (withPrograms lbi)
                   writeHcPkgRegisterScript verbosity lhcPkg pkg lib lbi clbi distPref inplace packageDb
        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"
        _    -> die "Registration scripts are not implemented for this compiler"

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


generateRegistrationInfo :: PackageDescription
                         -> Library
                         -> LocalBuildInfo
                         -> ComponentLocalBuildInfo
                         -> Bool
                         -> FilePath
                         -> IO InstalledPackageInfo
generateRegistrationInfo pkg lib lbi clbi inplace distPref = do
  --TODO: eliminate pwd!
  pwd <- getCurrentDirectory
  let installedPkgInfo
        | inplace   = inplaceInstalledPackageInfo pwd distPref
                        pkg lib lbi clbi
        | otherwise = absoluteInstalledPackageInfo
                        pkg lib lbi clbi
  return installedPkgInfo


registerPackage :: Verbosity
                -> PackageDescription
                -> Library
                -> LocalBuildInfo
                -> ComponentLocalBuildInfo
                -> FilePath
                -> Bool
                -> PackageDB
                -> IO ()
registerPackage verbosity pkg lib lbi clbi distPref inplace packageDb = do
  setupMessage verbosity "Registering" (packageId pkg)
  case compilerFlavor (compiler lbi) of
    GHC  -> registerPackageGHC  verbosity pkg lib lbi clbi distPref inplace packageDb
    LHC  -> registerPackageLHC  verbosity pkg lib lbi clbi distPref inplace packageDb
    Hugs -> registerPackageHugs verbosity pkg lib lbi clbi distPref inplace packageDb
    JHC  -> notice verbosity "Registering for jhc (nothing to do)"
    NHC  -> notice verbosity "Registering for nhc98 (nothing to do)"
    _    -> die "Registering is not implemented for this compiler"


registerPackageGHC, registerPackageLHC, registerPackageHugs
  :: Verbosity
  -> PackageDescription
  -> Library
  -> LocalBuildInfo
  -> ComponentLocalBuildInfo
  -> FilePath
  -> Bool
  -> PackageDB
  -> IO ()
registerPackageGHC verbosity pkg lib lbi clbi distPref inplace packageDb = do
  installedPkgInfo <- generateRegistrationInfo
                        pkg lib lbi clbi inplace distPref
  let Just ghcPkg = lookupProgram ghcPkgProgram (withPrograms lbi)
  HcPkg.reregister verbosity ghcPkg packageDb (Right installedPkgInfo)


registerPackageLHC verbosity pkg lib lbi clbi distPref inplace packageDb = do
  installedPkgInfo <- generateRegistrationInfo
                        pkg lib lbi clbi inplace distPref
  let Just lhcPkg = lookupProgram lhcPkgProgram (withPrograms lbi)
  HcPkg.reregister verbosity lhcPkg packageDb (Right installedPkgInfo)


registerPackageHugs verbosity pkg lib lbi clbi distPref inplace _packageDb = do
  when inplace $ die "--inplace is not supported with Hugs"
  installedPkgInfo <- generateRegistrationInfo
                        pkg lib lbi clbi inplace distPref
228
  let installDirs = absoluteInstallDirs pkg lbi NoCopyDest
Duncan Coutts's avatar
Duncan Coutts committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  createDirectoryIfMissingVerbose verbosity True (libdir installDirs)
  writeFileAtomic (libdir installDirs </> "package.conf")
                  (showInstalledPackageInfo installedPkgInfo ++ "\n")


writeHcPkgRegisterScript :: Verbosity
                         -> ConfiguredProgram
                         -> PackageDescription
                         -> Library
                         -> LocalBuildInfo
                         -> ComponentLocalBuildInfo
                         -> FilePath
                         -> Bool
                         -> PackageDB
                         -> IO ()
writeHcPkgRegisterScript verbosity hcPkg pkg lib lbi clbi distPref inplace packageDb = do
  installedPkgInfo <- generateRegistrationInfo
                        pkg lib lbi clbi inplace distPref

  let invocation  = HcPkg.reregisterInvocation hcPkg Verbosity.normal
                      packageDb (Right installedPkgInfo)
      regScript   = invocationAsSystemScript buildOS   invocation

  notice verbosity ("Creating package registration script: " ++ regScriptFileName)
  writeFileAtomic regScriptFileName regScript
  setFileExecutable regScriptFileName

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

261

262
263
264
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

Duncan Coutts's avatar
Duncan Coutts committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
-- | 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
  -> Library
  -> ComponentLocalBuildInfo
  -> InstallDirs FilePath
  -> InstalledPackageInfo
generalInstalledPackageInfo adjustRelIncDirs pkg lib clbi installDirs =
  InstalledPackageInfo {
    IPI.package            = packageId   pkg,
    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,
    IPI.description        = description pkg,
    IPI.category           = category    pkg,
    IPI.exposed            = libExposed  lib,
    IPI.exposedModules     = exposedModules lib,
    IPI.hiddenModules      = otherModules bi,
    IPI.importDirs         = [ libdir installDirs | hasModules ],
    IPI.libraryDirs        = if hasLibrary
                               then libdir installDirs : extraLibDirs bi
                               else                      extraLibDirs bi,
    IPI.hsLibraries        = [ "HS" ++ display (packageId pkg) | hasLibrary ],
    IPI.extraLibraries     = extraLibs bi,
    IPI.extraGHCiLibraries = [],
    IPI.includeDirs        = absinc ++ adjustRelIncDirs relinc,
    IPI.includes           = includes bi,
    IPI.depends            = componentPackageDeps clbi,
    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))


-- | Construct 'InstalledPackageInfo' for a library that is inplace in the
-- build tree.
--
-- This function knows about the layout of inplace packages.
--
inplaceInstalledPackageInfo :: FilePath -- ^ top of the build tree
                            -> FilePath -- ^ location of the dist tree
                            -> PackageDescription
                            -> Library
                            -> LocalBuildInfo
                            -> ComponentLocalBuildInfo
                            -> InstalledPackageInfo
inplaceInstalledPackageInfo inplaceDir distPref pkg lib lbi clbi =
    generalInstalledPackageInfo adjustReativeIncludeDirs pkg lib clbi installDirs
  where
    adjustReativeIncludeDirs = map (inplaceDir </>)
    installDirs =
336
      (absoluteInstallDirs pkg lbi NoCopyDest) {
Duncan Coutts's avatar
Duncan Coutts committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
        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
                             -> Library
                             -> LocalBuildInfo
                             -> ComponentLocalBuildInfo
                             -> InstalledPackageInfo
absoluteInstalledPackageInfo pkg lib lbi clbi =
    generalInstalledPackageInfo adjustReativeIncludeDirs pkg lib clbi installDirs
  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
367
    installDirs = absoluteInstallDirs pkg lbi NoCopyDest
Duncan Coutts's avatar
Duncan Coutts committed
368

369

simonmar's avatar
simonmar committed
370
371
-- -----------------------------------------------------------------------------
-- Unregistration
372

ijones's avatar
ijones committed
373
unregister :: PackageDescription -> LocalBuildInfo -> RegisterFlags -> IO ()
Duncan Coutts's avatar
Duncan Coutts committed
374
375
376
unregister pkg lbi regFlags = do
  let pkgid     = packageId pkg
      genScript = fromFlag (regGenScript regFlags)
377
      verbosity = fromFlag (regVerbosity regFlags)
Duncan Coutts's avatar
Duncan Coutts committed
378
      packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
379
                                    (regPackageDB regFlags)
380
      installDirs = absoluteInstallDirs pkg lbi NoCopyDest
Duncan Coutts's avatar
Duncan Coutts committed
381
  setupMessage verbosity "Unregistering" pkgid
382
  case compilerFlavor (compiler lbi) of
Duncan Coutts's avatar
Duncan Coutts committed
383
384
385
386
387
388
389
390
    GHC ->
      let Just ghcPkg = lookupProgram ghcPkgProgram (withPrograms lbi)
          invocation = HcPkg.unregisterInvocation ghcPkg Verbosity.normal
                         packageDb pkgid
      in if genScript
           then writeFileAtomic unregScriptFileName
                  (invocationAsSystemScript buildOS invocation)
            else runProgramInvocation verbosity invocation
ijones's avatar
ijones committed
391
    Hugs -> do
392
        try $ removeDirectoryRecursive (libdir installDirs)
393
        return ()
394
    NHC -> do
395
        try $ removeDirectoryRecursive (libdir installDirs)
396
        return ()
ijones's avatar
ijones committed
397
    _ ->
398
        die ("only unregistering with GHC and Hugs is implemented")
ijones's avatar
ijones committed
399

Duncan Coutts's avatar
Duncan Coutts committed
400
401
402
403
unregScriptFileName :: FilePath
unregScriptFileName = case buildOS of
                          Windows -> "unregister.bat"
                          _       -> "unregister.sh"