Register.hs 19.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,
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 107
    info verbosity (IPI.showInstalledPackageInfo installedPkgInfo)

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

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

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

    modeGenerateRegScript = fromFlag (regGenScript regFlags)

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

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

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


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

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

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

186

187
  return installedPkgInfo{ IPI.abiHash = abi_hash }
188

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

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

209
-- | Create an empty package DB at the specified location.
210 211 212 213 214 215 216 217 218 219 220 221
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"

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
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
238 239 240 241 242 243

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

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

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

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

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

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

296

297 298 299
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

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

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

422 423

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

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

simonmar's avatar
simonmar committed
444 445
-- -----------------------------------------------------------------------------
-- Unregistration
446

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

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