Register.hs 19.7 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,
31 32 33 34
    doesPackageDBExist,
    createPackageDB,
    deletePackageDB,

refold's avatar
refold committed
35
    invokeHcPkg,
Duncan Coutts's avatar
Duncan Coutts committed
36
    registerPackage,
37
    generateRegistrationInfo,
Duncan Coutts's avatar
Duncan Coutts committed
38 39 40
    inplaceInstalledPackageInfo,
    absoluteInstalledPackageInfo,
    generalInstalledPackageInfo,
41
  ) where
42

43
import Distribution.Simple.LocalBuildInfo
44
import Distribution.Simple.BuildPaths
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
Duncan Coutts's avatar
Duncan Coutts committed
53 54 55
import Distribution.Simple.Program
import Distribution.Simple.Program.Script
import qualified Distribution.Simple.Program.HcPkg as HcPkg
56
import Distribution.Simple.Setup
Duncan Coutts's avatar
Duncan Coutts committed
57
import Distribution.PackageDescription
58
import Distribution.Package
59
import qualified Distribution.InstalledPackageInfo as IPI
60
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
61
import Distribution.Simple.Utils
62
import Distribution.System
63
import Distribution.Text
Duncan Coutts's avatar
Duncan Coutts committed
64
import Distribution.Verbosity as Verbosity
65

66
import System.FilePath ((</>), (<.>), isAbsolute)
Duncan Coutts's avatar
Duncan Coutts committed
67
import System.Directory
68 69
         ( getCurrentDirectory, removeDirectoryRecursive, removeFile
         , doesDirectoryExist, doesFileExist )
ijones's avatar
ijones committed
70

71
import Data.Version
72
import Control.Monad (when)
Duncan Coutts's avatar
Duncan Coutts committed
73
import Data.Maybe
74
         ( isJust, fromMaybe, maybeToList )
Duncan Coutts's avatar
Duncan Coutts committed
75 76
import Data.List
         ( partition, nub )
77
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
78

simonmar's avatar
simonmar committed
79 80 81
-- -----------------------------------------------------------------------------
-- Registration

ijones's avatar
ijones committed
82
register :: PackageDescription -> LocalBuildInfo
ijones's avatar
ijones committed
83
         -> RegisterFlags -- ^Install in the user's database?; verbose
ijones's avatar
ijones committed
84
         -> IO ()
85 86 87 88 89 90 91
register pkg lbi regFlags =
    -- We do NOT register libraries outside of the inplace database
    -- if there is no public library, since no one else can use it
    -- usefully (they're not public.)  If we start supporting scoped
    -- packages, we'll have to relax this.
    when (hasPublicLib pkg) $
        withLib pkg (registerOne pkg lbi regFlags)
92 93 94 95 96

registerOne :: PackageDescription -> LocalBuildInfo -> RegisterFlags
            -> Library
            -> IO ()
registerOne pkg lbi regFlags lib
97
  = do
98
    let clbi = getComponentLocalBuildInfo lbi (CLibName (libName lib))
99

100
    absPackageDBs    <- absolutePackageDBPaths packageDbs
101
    -- TODO: registration info named base on LIBNAME!!!
102
    installedPkgInfo <- generateRegistrationInfo
103
                           verbosity pkg lib lbi clbi inplace reloc distPref
104
                           (registrationPackageDB absPackageDBs)
105

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

109 110 111 112
     -- Three different modes:
    case () of
     _ | modeGenerateRegFile   -> writeRegistrationFile installedPkgInfo
       | modeGenerateRegScript -> writeRegisterScript   installedPkgInfo
113 114
       | otherwise             -> do
           setupMessage verbosity "Registering" (packageId pkg)
115
           registerPackage verbosity (compiler lbi) (withPrograms lbi) False
116
                           packageDbs installedPkgInfo
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 (IPI.showInstalledPackageInfo installedPkgInfo)
Duncan Coutts's avatar
Duncan Coutts committed
138

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


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

163
  --TODO: the method of setting the UnitId is compiler specific
164
  --      this aspect should be delegated to a per-compiler helper.
165
  let comp = compiler lbi
166
  abi_hash <-
167 168
    case compilerFlavor comp of
     GHC | compilerVersion comp >= Version [6,11] [] -> do
169
            fmap AbiHash $ GHC.libAbiHash verbosity pkg lbi lib clbi
170
     GHCJS -> do
171 172
            fmap AbiHash $ GHCJS.libAbiHash verbosity pkg lbi lib clbi
     _ -> return (AbiHash "")
173

Christiaan Baaij's avatar
Christiaan Baaij committed
174 175 176
  installedPkgInfo <-
    if inplace
      then return (inplaceInstalledPackageInfo pwd distPref
177
                     pkg abi_hash lib lbi clbi)
Christiaan Baaij's avatar
Christiaan Baaij committed
178 179
    else if reloc
      then relocRegistrationInfo verbosity
180
                     pkg lib lbi clbi abi_hash packageDb
Christiaan Baaij's avatar
Christiaan Baaij committed
181
      else return (absoluteInstalledPackageInfo
182
                     pkg abi_hash lib lbi clbi)
183

184

185
  return installedPkgInfo{ IPI.abiHash = abi_hash }
186

187 188 189 190 191
relocRegistrationInfo :: Verbosity
                      -> PackageDescription
                      -> Library
                      -> LocalBuildInfo
                      -> ComponentLocalBuildInfo
192
                      -> AbiHash
193 194
                      -> PackageDB
                      -> IO InstalledPackageInfo
195
relocRegistrationInfo verbosity pkg lib lbi clbi abi_hash packageDb =
196
  case (compilerFlavor (compiler lbi)) of
197 198
    GHC -> do fs <- GHC.pkgRoot verbosity lbi packageDb
              return (relocatableInstalledPackageInfo
199
                        pkg abi_hash lib lbi clbi fs)
200 201
    _   -> die "Distribution.Simple.Register.relocRegistrationInfo: \
               \not implemented for this compiler"
202

203 204
initPackageDB :: Verbosity -> Compiler -> ProgramConfiguration -> FilePath -> IO ()
initPackageDB verbosity comp progdb dbPath =
205
    createPackageDB verbosity comp progdb False dbPath
206

207
-- | Create an empty package DB at the specified location.
208 209 210 211 212 213 214 215 216 217 218 219
createPackageDB :: Verbosity -> Compiler -> ProgramConfiguration -> Bool
                -> FilePath -> IO ()
createPackageDB verbosity comp progdb preferCompat dbPath =
    case compilerFlavor comp of
      GHC   -> HcPkg.init (GHC.hcPkgInfo   progdb) verbosity preferCompat dbPath
      GHCJS -> HcPkg.init (GHCJS.hcPkgInfo progdb) verbosity False dbPath
      LHC   -> HcPkg.init (LHC.hcPkgInfo   progdb) verbosity False dbPath
      UHC   -> return ()
      HaskellSuite _ -> HaskellSuite.initPackageDB verbosity progdb dbPath
      _              -> die $ "Distribution.Simple.Register.createPackageDB: "
                           ++ "not implemented for this compiler"

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
doesPackageDBExist :: FilePath -> IO Bool
doesPackageDBExist dbPath = do
    -- currently one impl for all compiler flavours, but could change if needed
    dir_exists <- doesDirectoryExist dbPath
    if dir_exists
        then return True
        else doesFileExist dbPath

deletePackageDB :: FilePath -> IO ()
deletePackageDB dbPath = do
    -- currently one impl for all compiler flavours, but could change if needed
    dir_exists <- doesDirectoryExist dbPath
    if dir_exists
        then removeDirectoryRecursive dbPath
        else do file_exists <- doesFileExist dbPath
                when file_exists $ removeFile dbPath
refold's avatar
refold committed
236 237 238 239 240 241

-- | 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 =
242 243 244 245
  withHcPkg "invokeHcPkg" comp conf
    (\hpi -> HcPkg.invoke hpi verbosity dbStack extraArgs)

withHcPkg :: String -> Compiler -> ProgramConfiguration
246
          -> (HcPkg.HcPkgInfo -> IO a) -> IO a
247 248 249 250 251 252 253
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")
254

Duncan Coutts's avatar
Duncan Coutts committed
255
registerPackage :: Verbosity
256 257
                -> Compiler
                -> ProgramConfiguration
258
                -> Bool
259
                -> PackageDBStack
260
                -> InstalledPackageInfo
Duncan Coutts's avatar
Duncan Coutts committed
261
                -> IO ()
262
registerPackage verbosity comp progdb multiInstance packageDbs installedPkgInfo =
263
  case compilerFlavor comp of
264 265 266 267
    GHC   -> GHC.registerPackage   verbosity progdb multiInstance packageDbs installedPkgInfo
    GHCJS -> GHCJS.registerPackage verbosity progdb multiInstance packageDbs installedPkgInfo
    _ | multiInstance
          -> die "Registering multiple package instances is not yet supported for this compiler"
268 269
    LHC   -> LHC.registerPackage   verbosity      progdb packageDbs installedPkgInfo
    UHC   -> UHC.registerPackage   verbosity comp progdb packageDbs installedPkgInfo
270
    JHC   -> notice verbosity "Registering for jhc (nothing to do)"
271
    HaskellSuite {} ->
272
      HaskellSuite.registerPackage verbosity      progdb packageDbs installedPkgInfo
Duncan Coutts's avatar
Duncan Coutts committed
273 274 275
    _    -> die "Registering is not implemented for this compiler"

writeHcPkgRegisterScript :: Verbosity
276
                         -> InstalledPackageInfo
277
                         -> PackageDBStack
278
                         -> HcPkg.HcPkgInfo
Duncan Coutts's avatar
Duncan Coutts committed
279
                         -> IO ()
280 281
writeHcPkgRegisterScript verbosity installedPkgInfo packageDbs hpi = do
  let invocation  = HcPkg.reregisterInvocation hpi Verbosity.normal
282
                      packageDbs (Right installedPkgInfo)
283
      regScript   = invocationAsSystemScript buildOS invocation
Duncan Coutts's avatar
Duncan Coutts committed
284 285

  notice verbosity ("Creating package registration script: " ++ regScriptFileName)
286
  writeUTF8File regScriptFileName regScript
Duncan Coutts's avatar
Duncan Coutts committed
287 288 289 290 291 292
  setFileExecutable regScriptFileName

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

294

295 296 297
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

Duncan Coutts's avatar
Duncan Coutts committed
298 299 300 301 302 303 304
-- | 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
305
  -> AbiHash
Duncan Coutts's avatar
Duncan Coutts committed
306
  -> Library
307
  -> LocalBuildInfo
Duncan Coutts's avatar
Duncan Coutts committed
308 309 310
  -> ComponentLocalBuildInfo
  -> InstallDirs FilePath
  -> InstalledPackageInfo
311
generalInstalledPackageInfo adjustRelIncDirs pkg abi_hash lib lbi clbi installDirs =
312
  IPI.InstalledPackageInfo {
313 314 315
    IPI.sourcePackageId    = (packageId   pkg) {
                                pkgName = componentCompatPackageName clbi
                             },
316
    IPI.installedUnitId    = componentUnitId clbi,
317
    IPI.compatPackageKey   = componentCompatPackageKey clbi,
Duncan Coutts's avatar
Duncan Coutts committed
318 319 320 321 322 323 324
    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,
325
    IPI.synopsis           = synopsis    pkg,
Duncan Coutts's avatar
Duncan Coutts committed
326 327
    IPI.description        = description pkg,
    IPI.category           = category    pkg,
328
    IPI.abiHash            = abi_hash,
Duncan Coutts's avatar
Duncan Coutts committed
329
    IPI.exposed            = libExposed  lib,
330
    IPI.exposedModules     = componentExposedModules clbi,
Duncan Coutts's avatar
Duncan Coutts committed
331
    IPI.hiddenModules      = otherModules bi,
332
    IPI.trusted            = IPI.trusted IPI.emptyInstalledPackageInfo,
Duncan Coutts's avatar
Duncan Coutts committed
333
    IPI.importDirs         = [ libdir installDirs | hasModules ],
334 335
    -- Note. the libsubdir and datasubdir templates have already been expanded
    -- into libdir and datadir.
Duncan Coutts's avatar
Duncan Coutts committed
336 337 338
    IPI.libraryDirs        = if hasLibrary
                               then libdir installDirs : extraLibDirs bi
                               else                      extraLibDirs bi,
339
    IPI.dataDir            = datadir installDirs,
340
    IPI.hsLibraries        = if hasLibrary
341
                               then [getHSLibraryName (componentUnitId clbi)]
342
                               else [],
Duncan Coutts's avatar
Duncan Coutts committed
343
    IPI.extraLibraries     = extraLibs bi,
344
    IPI.extraGHCiLibraries = extraGHCiLibs bi,
Duncan Coutts's avatar
Duncan Coutts committed
345 346
    IPI.includeDirs        = absinc ++ adjustRelIncDirs relinc,
    IPI.includes           = includes bi,
347
    IPI.depends            = map fst (componentPackageDeps clbi),
Duncan Coutts's avatar
Duncan Coutts committed
348 349 350 351 352
    IPI.ccOptions          = [], -- Note. NOT ccOptions bi!
                                 -- We don't want cc-options to be propagated
                                 -- to C compilations in other packages.
    IPI.ldOptions          = ldOptions bi,
    IPI.frameworks         = frameworks bi,
353
    IPI.frameworkDirs      = extraFrameworkDirs bi,
Duncan Coutts's avatar
Duncan Coutts committed
354
    IPI.haddockInterfaces  = [haddockdir installDirs </> haddockName pkg],
355 356
    IPI.haddockHTMLs       = [htmldir installDirs],
    IPI.pkgRoot            = Nothing
Duncan Coutts's avatar
Duncan Coutts committed
357 358 359 360
  }
  where
    bi = libBuildInfo lib
    (absinc, relinc) = partition isAbsolute (includeDirs bi)
361
    hasModules = not $ null (libModules lib)
Duncan Coutts's avatar
Duncan Coutts committed
362
    hasLibrary = hasModules || not (null (cSources bi))
363 364 365
                            || (not (null (jsSources bi)) &&
                                compilerFlavor (compiler lbi) == GHCJS)

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

420 421

relocatableInstalledPackageInfo :: PackageDescription
422
                                -> AbiHash
423 424 425
                                -> Library
                                -> LocalBuildInfo
                                -> ComponentLocalBuildInfo
426
                                -> FilePath
427
                                -> InstalledPackageInfo
428
relocatableInstalledPackageInfo pkg abi_hash lib lbi clbi pkgroot =
429
    generalInstalledPackageInfo adjustReativeIncludeDirs
430
                                pkg abi_hash lib lbi clbi installDirs
431 432 433 434 435 436 437
  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
438 439

    installDirs = fmap (("${pkgroot}" </>) . shortRelativePath pkgroot)
440
                $ absoluteInstallDirs pkg lbi (componentUnitId clbi) NoCopyDest
441

simonmar's avatar
simonmar committed
442 443
-- -----------------------------------------------------------------------------
-- Unregistration
444

ijones's avatar
ijones committed
445
unregister :: PackageDescription -> LocalBuildInfo -> RegisterFlags -> IO ()
Duncan Coutts's avatar
Duncan Coutts committed
446 447 448
unregister pkg lbi regFlags = do
  let pkgid     = packageId pkg
      genScript = fromFlag (regGenScript regFlags)
449
      verbosity = fromFlag (regVerbosity regFlags)
Duncan Coutts's avatar
Duncan Coutts committed
450
      packageDb = fromFlagOrDefault (registrationPackageDB (withPackageDB lbi))
451
                                    (regPackageDB regFlags)
452 453 454 455 456 457 458
      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
459
  setupMessage verbosity "Unregistering" pkgid
460 461
  withHcPkg "unregistering is only implemented for GHC and GHCJS"
    (compiler lbi) (withPrograms lbi) unreg
ijones's avatar
ijones committed
462

Duncan Coutts's avatar
Duncan Coutts committed
463 464 465 466
unregScriptFileName :: FilePath
unregScriptFileName = case buildOS of
                          Windows -> "unregister.bat"
                          _       -> "unregister.sh"