Register.hs 19.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
import qualified Distribution.Simple.GHC  as GHC
import qualified Distribution.Simple.LHC  as LHC
47
import qualified Distribution.Simple.UHC  as UHC
48
import qualified Distribution.Simple.HaskellSuite as HaskellSuite
49
import Distribution.Simple.Compiler
50
         ( compilerVersion, Compiler, CompilerFlavor(..), compilerFlavor
51 52
         , PackageDB, PackageDBStack, absolutePackageDBPaths
         , 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
import Distribution.PackageDescription
64
         ( PackageDescription(..), Library(..), BuildInfo(..), libModules )
65
import Distribution.Package
66
         ( Package(..), packageName, InstalledPackageId(..) )
67
import Distribution.InstalledPackageInfo
68
         ( InstalledPackageInfo, InstalledPackageInfo_(InstalledPackageInfo)
69
         , showInstalledPackageInfo )
70
import qualified Distribution.InstalledPackageInfo as IPI
71
import Distribution.Simple.Utils
72
         ( writeUTF8File, writeFileAtomic, setFileExecutable
73
         , die, notice, setupMessage, shortRelativePath )
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 System.FilePath ((</>), (<.>), isAbsolute)
Duncan Coutts's avatar
Duncan Coutts committed
83
import System.Directory
84
         ( getCurrentDirectory )
ijones's avatar
ijones committed
85

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

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

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

103
    absPackageDBs    <- absolutePackageDBPaths packageDbs
104
    installedPkgInfo <- generateRegistrationInfo
105
                           verbosity pkg lib lbi clbi inplace reloc distPref
106
                           (registrationPackageDB absPackageDBs)
107

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

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

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

    modeGenerateRegScript = fromFlag (regGenScript regFlags)

    inplace   = fromFlag (regInPlace regFlags)
126
    reloc     = relocatable lbi
Duncan Coutts's avatar
Duncan Coutts committed
127 128 129 130 131
    -- 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
132 133 134
    distPref  = fromFlag (regDistPref regFlags)
    verbosity = fromFlag (regVerbosity regFlags)

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

139
    writeRegisterScript installedPkgInfo =
Duncan Coutts's avatar
Duncan Coutts committed
140 141
      case compilerFlavor (compiler lbi) of
        GHC  -> do (ghcPkg, _) <- requireProgram verbosity ghcPkgProgram (withPrograms lbi)
142
                   writeHcPkgRegisterScript verbosity installedPkgInfo ghcPkg packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
143
        LHC  -> do (lhcPkg, _) <- requireProgram verbosity lhcPkgProgram (withPrograms lbi)
144
                   writeHcPkgRegisterScript verbosity installedPkgInfo lhcPkg packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
145
        JHC  -> notice verbosity "Registration scripts not needed for jhc"
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
                         -> Library
                         -> LocalBuildInfo
                         -> ComponentLocalBuildInfo
                         -> Bool
160
                         -> Bool
Duncan Coutts's avatar
Duncan Coutts committed
161
                         -> FilePath
162
                         -> PackageDB
Duncan Coutts's avatar
Duncan Coutts committed
163
                         -> IO InstalledPackageInfo
164
generateRegistrationInfo verbosity pkg lib lbi clbi inplace reloc distPref packageDb = do
Duncan Coutts's avatar
Duncan Coutts committed
165 166
  --TODO: eliminate pwd!
  pwd <- getCurrentDirectory
167

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

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  -- let installedPkgInfo
  --       | inplace   = inplaceInstalledPackageInfo pwd distPref
  --                       pkg ipid lib lbi clbi
  --       | reloc     = relocatableInstalledPackageInfo
  --                       pkg ipid lib lbi clbi undefined
  --       | otherwise = absoluteInstalledPackageInfo
  --                       pkg ipid lib lbi clbi

  installedPkgInfo <- if inplace then
                         return (inplaceInstalledPackageInfo pwd distPref
                                  pkg ipid lib lbi clbi)
                      else if reloc then
                         relocRegistrationInfo verbosity pkg lib lbi clbi ipid
                           packageDb
                      else
                         return (absoluteInstalledPackageInfo
                                   pkg ipid lib lbi clbi)

197

198 199
  return installedPkgInfo{ IPI.installedPackageId = ipid }

200 201 202 203 204 205 206 207 208 209
relocRegistrationInfo :: Verbosity
                      -> PackageDescription
                      -> Library
                      -> LocalBuildInfo
                      -> ComponentLocalBuildInfo
                      -> InstalledPackageId
                      -> PackageDB
                      -> IO InstalledPackageInfo
relocRegistrationInfo verbosity pkg lib lbi clbi ipid packageDb =
  case (compilerFlavor (compiler lbi)) of
210 211 212
    GHC -> do fs <- GHC.pkgRoot verbosity lbi packageDb
              return (relocatableInstalledPackageInfo
                        pkg ipid lib lbi clbi fs)
213 214
    _   -> die "Distribution.Simple.Register.relocRegistrationInfo: \
               \not implemented for this compiler"
215

216 217 218 219 220 221
-- | 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
222
    HaskellSuite {} -> HaskellSuite.initPackageDB verbosity conf dbPath
refold's avatar
refold committed
223 224 225 226 227 228 229 230 231 232 233 234
    _   -> 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"
235

Duncan Coutts's avatar
Duncan Coutts committed
236
registerPackage :: Verbosity
237
                -> InstalledPackageInfo
Duncan Coutts's avatar
Duncan Coutts committed
238 239 240
                -> PackageDescription
                -> LocalBuildInfo
                -> Bool
241
                -> PackageDBStack
Duncan Coutts's avatar
Duncan Coutts committed
242
                -> IO ()
243
registerPackage verbosity installedPkgInfo pkg lbi inplace packageDbs = do
refold's avatar
refold committed
244 245 246 247
  let msg = if inplace
            then "In-place registering"
            else "Registering"
  setupMessage verbosity msg (packageId pkg)
Duncan Coutts's avatar
Duncan Coutts committed
248
  case compilerFlavor (compiler lbi) of
249 250
    GHC  -> GHC.registerPackage  verbosity installedPkgInfo pkg lbi inplace packageDbs
    LHC  -> LHC.registerPackage  verbosity installedPkgInfo pkg lbi inplace packageDbs
251
    UHC  -> UHC.registerPackage  verbosity installedPkgInfo pkg lbi inplace packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
252
    JHC  -> notice verbosity "Registering for jhc (nothing to do)"
253 254
    HaskellSuite {} ->
      HaskellSuite.registerPackage verbosity installedPkgInfo pkg lbi inplace packageDbs
Duncan Coutts's avatar
Duncan Coutts committed
255 256 257 258
    _    -> die "Registering is not implemented for this compiler"


writeHcPkgRegisterScript :: Verbosity
259
                         -> InstalledPackageInfo
Duncan Coutts's avatar
Duncan Coutts committed
260
                         -> ConfiguredProgram
261
                         -> PackageDBStack
Duncan Coutts's avatar
Duncan Coutts committed
262
                         -> IO ()
263
writeHcPkgRegisterScript verbosity installedPkgInfo hcPkg packageDbs = do
Duncan Coutts's avatar
Duncan Coutts committed
264
  let invocation  = HcPkg.reregisterInvocation hcPkg Verbosity.normal
265
                      packageDbs (Right installedPkgInfo)
Duncan Coutts's avatar
Duncan Coutts committed
266 267 268
      regScript   = invocationAsSystemScript buildOS   invocation

  notice verbosity ("Creating package registration script: " ++ regScriptFileName)
269
  writeUTF8File regScriptFileName regScript
Duncan Coutts's avatar
Duncan Coutts committed
270 271 272 273 274 275
  setFileExecutable regScriptFileName

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

277

278 279 280
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

Duncan Coutts's avatar
Duncan Coutts committed
281 282 283 284 285 286 287
-- | 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
288
  -> InstalledPackageId
Duncan Coutts's avatar
Duncan Coutts committed
289
  -> Library
290
  -> LocalBuildInfo
Duncan Coutts's avatar
Duncan Coutts committed
291 292 293
  -> ComponentLocalBuildInfo
  -> InstallDirs FilePath
  -> InstalledPackageInfo
294
generalInstalledPackageInfo adjustRelIncDirs pkg ipid lib lbi clbi installDirs =
Duncan Coutts's avatar
Duncan Coutts committed
295
  InstalledPackageInfo {
296
    IPI.installedPackageId = ipid,
297
    IPI.sourcePackageId    = packageId   pkg,
298
    IPI.packageKey         = pkgKey lbi,
Duncan Coutts's avatar
Duncan Coutts committed
299 300 301 302 303 304 305
    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,
306
    IPI.synopsis           = synopsis    pkg,
Duncan Coutts's avatar
Duncan Coutts committed
307 308 309
    IPI.description        = description pkg,
    IPI.category           = category    pkg,
    IPI.exposed            = libExposed  lib,
310
    IPI.exposedModules     = map fixupSelf (componentExposedModules clbi),
Duncan Coutts's avatar
Duncan Coutts committed
311
    IPI.hiddenModules      = otherModules bi,
312 313 314
    IPI.instantiatedWith   = map (\(k,(p,n)) ->
                                   (k,IPI.OriginalModule (IPI.installedPackageId p) n))
                                 (instantiatedWith lbi),
315
    IPI.trusted            = IPI.trusted IPI.emptyInstalledPackageInfo,
Duncan Coutts's avatar
Duncan Coutts committed
316
    IPI.importDirs         = [ libdir installDirs | hasModules ],
317 318
    -- Note. the libsubdir and datasubdir templates have already been expanded
    -- into libdir and datadir.
Duncan Coutts's avatar
Duncan Coutts committed
319 320 321
    IPI.libraryDirs        = if hasLibrary
                               then libdir installDirs : extraLibDirs bi
                               else                      extraLibDirs bi,
322
    IPI.dataDir            = datadir installDirs,
refold's avatar
refold committed
323
    IPI.hsLibraries        = [ libname
324 325
                             | LibraryName libname <- componentLibraries clbi
                             , hasLibrary ],
Duncan Coutts's avatar
Duncan Coutts committed
326
    IPI.extraLibraries     = extraLibs bi,
327
    IPI.extraGHCiLibraries = extraGHCiLibs bi,
Duncan Coutts's avatar
Duncan Coutts committed
328 329
    IPI.includeDirs        = absinc ++ adjustRelIncDirs relinc,
    IPI.includes           = includes bi,
330
    IPI.depends            = map fst (componentPackageDeps clbi),
Duncan Coutts's avatar
Duncan Coutts committed
331 332 333 334 335 336 337 338 339 340 341 342
    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)
343
    hasModules = not $ null (libModules lib)
Duncan Coutts's avatar
Duncan Coutts committed
344
    hasLibrary = hasModules || not (null (cSources bi))
345 346
    -- 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
347
    -- of modules defined within this package, so we used an empty one that
348 349
    -- 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.
350 351 352 353 354 355
    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
356

Ian D. Bollinger's avatar
Ian D. Bollinger committed
357
-- | Construct 'InstalledPackageInfo' for a library that is in place in the
Duncan Coutts's avatar
Duncan Coutts committed
358 359
-- build tree.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
360
-- This function knows about the layout of in place packages.
Duncan Coutts's avatar
Duncan Coutts committed
361 362 363 364
--
inplaceInstalledPackageInfo :: FilePath -- ^ top of the build tree
                            -> FilePath -- ^ location of the dist tree
                            -> PackageDescription
365
                            -> InstalledPackageId
Duncan Coutts's avatar
Duncan Coutts committed
366 367 368 369
                            -> Library
                            -> LocalBuildInfo
                            -> ComponentLocalBuildInfo
                            -> InstalledPackageInfo
370 371 372
inplaceInstalledPackageInfo inplaceDir distPref pkg ipid lib lbi clbi =
    generalInstalledPackageInfo adjustRelativeIncludeDirs
                                pkg ipid lib lbi clbi installDirs
Duncan Coutts's avatar
Duncan Coutts committed
373
  where
refold's avatar
refold committed
374
    adjustRelativeIncludeDirs = map (inplaceDir </>)
Duncan Coutts's avatar
Duncan Coutts committed
375
    installDirs =
376
      (absoluteInstallDirs pkg lbi NoCopyDest) {
Duncan Coutts's avatar
Duncan Coutts committed
377
        libdir     = inplaceDir </> buildDir lbi,
378
        datadir    = inplaceDir </> dataDir pkg,
Duncan Coutts's avatar
Duncan Coutts committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392
        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
393
                             -> InstalledPackageId
Duncan Coutts's avatar
Duncan Coutts committed
394 395 396 397
                             -> Library
                             -> LocalBuildInfo
                             -> ComponentLocalBuildInfo
                             -> InstalledPackageInfo
398 399 400
absoluteInstalledPackageInfo pkg ipid lib lbi clbi =
    generalInstalledPackageInfo adjustReativeIncludeDirs
                                pkg ipid lib lbi clbi installDirs
Duncan Coutts's avatar
Duncan Coutts committed
401 402 403 404 405 406 407
  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
408
    installDirs = absoluteInstallDirs pkg lbi NoCopyDest
Duncan Coutts's avatar
Duncan Coutts committed
409

410 411 412 413 414 415

relocatableInstalledPackageInfo :: PackageDescription
                                -> InstalledPackageId
                                -> Library
                                -> LocalBuildInfo
                                -> ComponentLocalBuildInfo
416
                                -> FilePath
417
                                -> InstalledPackageInfo
418
relocatableInstalledPackageInfo pkg ipid lib lbi clbi pkgroot =
419 420 421 422 423 424 425 426 427
    generalInstalledPackageInfo adjustReativeIncludeDirs
                                pkg ipid lib lbi 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
428 429 430

    installDirs = fmap (("${pkgroot}" </>) . shortRelativePath pkgroot)
                $ absoluteInstallDirs pkg lbi NoCopyDest
431

simonmar's avatar
simonmar committed
432 433
-- -----------------------------------------------------------------------------
-- Unregistration
434

ijones's avatar
ijones committed
435
unregister :: PackageDescription -> LocalBuildInfo -> RegisterFlags -> IO ()
Duncan Coutts's avatar
Duncan Coutts committed
436 437 438
unregister pkg lbi regFlags = do
  let pkgid     = packageId pkg
      genScript = fromFlag (regGenScript regFlags)
439
      verbosity = fromFlag (regVerbosity regFlags)
Duncan Coutts's avatar
Duncan Coutts committed
440
      packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
441
                                    (regPackageDB regFlags)
Duncan Coutts's avatar
Duncan Coutts committed
442
  setupMessage verbosity "Unregistering" pkgid
443
  case compilerFlavor (compiler lbi) of
Duncan Coutts's avatar
Duncan Coutts committed
444 445 446 447 448 449
    GHC ->
      let Just ghcPkg = lookupProgram ghcPkgProgram (withPrograms lbi)
          invocation = HcPkg.unregisterInvocation ghcPkg Verbosity.normal
                         packageDb pkgid
      in if genScript
           then writeFileAtomic unregScriptFileName
450
                  (BS.Char8.pack $ invocationAsSystemScript buildOS invocation)
Duncan Coutts's avatar
Duncan Coutts committed
451
            else runProgramInvocation verbosity invocation
ijones's avatar
ijones committed
452
    _ ->
453
        die ("unregistering is only implemented for GHC")
ijones's avatar
ijones committed
454

Duncan Coutts's avatar
Duncan Coutts committed
455 456 457 458
unregScriptFileName :: FilePath
unregScriptFileName = case buildOS of
                          Windows -> "unregister.bat"
                          _       -> "unregister.sh"