Register.hs 20.1 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 44
import Distribution.Simple.LocalBuildInfo
         ( LocalBuildInfo(..), ComponentLocalBuildInfo(..)
45
         , ComponentName(..), getComponentLocalBuildInfo
46
         , InstallDirs(..), absoluteInstallDirs )
47
import Distribution.Simple.BuildPaths (haddockName)
48 49 50 51 52

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
53
import qualified Distribution.Simple.HaskellSuite as HaskellSuite
54

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

87
import System.FilePath ((</>), (<.>), isAbsolute)
Duncan Coutts's avatar
Duncan Coutts committed
88
import System.Directory
89 90
         ( getCurrentDirectory, removeDirectoryRecursive, removeFile
         , doesDirectoryExist, doesFileExist )
ijones's avatar
ijones committed
91

92
import Data.Version
93
import Control.Monad (when)
Duncan Coutts's avatar
Duncan Coutts committed
94
import Data.Maybe
95
         ( isJust, fromMaybe, maybeToList )
Duncan Coutts's avatar
Duncan Coutts committed
96 97
import Data.List
         ( partition, nub )
98
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
99

simonmar's avatar
simonmar committed
100 101 102
-- -----------------------------------------------------------------------------
-- Registration

ijones's avatar
ijones committed
103
register :: PackageDescription -> LocalBuildInfo
ijones's avatar
ijones committed
104
         -> RegisterFlags -- ^Install in the user's database?; verbose
ijones's avatar
ijones committed
105
         -> IO ()
106
register pkg@PackageDescription { library       = Just lib  } lbi regFlags
107
  = do
108
    let clbi = getComponentLocalBuildInfo lbi CLibName
109

110
    absPackageDBs    <- absolutePackageDBPaths packageDbs
111
    installedPkgInfo <- generateRegistrationInfo
112
                           verbosity pkg lib lbi clbi inplace reloc distPref
113
                           (registrationPackageDB absPackageDBs)
114

115
    when (fromFlag (regPrintId regFlags)) $ do
116
      putStrLn (display (IPI.installedComponentId installedPkgInfo))
117

118 119 120 121
     -- Three different modes:
    case () of
     _ | modeGenerateRegFile   -> writeRegistrationFile installedPkgInfo
       | modeGenerateRegScript -> writeRegisterScript   installedPkgInfo
122 123
       | otherwise             -> do
           setupMessage verbosity "Registering" (packageId pkg)
124 125
           registerPackage verbosity (compiler lbi) (withPrograms lbi)
                           packageDbs installedPkgInfo
Duncan Coutts's avatar
Duncan Coutts committed
126 127 128 129 130 131 132 133 134

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

    modeGenerateRegScript = fromFlag (regGenScript regFlags)

    inplace   = fromFlag (regInPlace regFlags)
135
    reloc     = relocatable lbi
Duncan Coutts's avatar
Duncan Coutts committed
136 137 138 139 140
    -- 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
141 142 143
    distPref  = fromFlag (regDistPref regFlags)
    verbosity = fromFlag (regVerbosity regFlags)

144
    writeRegistrationFile installedPkgInfo = do
Duncan Coutts's avatar
Duncan Coutts committed
145
      notice verbosity ("Creating package registration file: " ++ regFile)
146
      writeUTF8File regFile (showInstalledPackageInfo installedPkgInfo)
Duncan Coutts's avatar
Duncan Coutts committed
147

148
    writeRegisterScript installedPkgInfo =
Duncan Coutts's avatar
Duncan Coutts committed
149
      case compilerFlavor (compiler lbi) of
150 151 152 153 154 155
        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
156 157 158 159 160 161

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


162 163
generateRegistrationInfo :: Verbosity
                         -> PackageDescription
Duncan Coutts's avatar
Duncan Coutts committed
164 165 166 167
                         -> Library
                         -> LocalBuildInfo
                         -> ComponentLocalBuildInfo
                         -> Bool
168
                         -> Bool
Duncan Coutts's avatar
Duncan Coutts committed
169
                         -> FilePath
170
                         -> PackageDB
Duncan Coutts's avatar
Duncan Coutts committed
171
                         -> IO InstalledPackageInfo
172
generateRegistrationInfo verbosity pkg lib lbi clbi inplace reloc distPref packageDb = do
Duncan Coutts's avatar
Duncan Coutts committed
173 174
  --TODO: eliminate pwd!
  pwd <- getCurrentDirectory
175

176
  --TODO: the method of setting the ComponentId is compiler specific
177
  --      this aspect should be delegated to a per-compiler helper.
178
  let comp = compiler lbi
179
  abi_hash <-
180 181
    case compilerFlavor comp of
     GHC | compilerVersion comp >= Version [6,11] [] -> do
182
            fmap AbiHash $ GHC.libAbiHash verbosity pkg lbi lib clbi
183
     GHCJS -> do
184 185
            fmap AbiHash $ GHCJS.libAbiHash verbosity pkg lbi lib clbi
     _ -> return (AbiHash "")
186

Christiaan Baaij's avatar
Christiaan Baaij committed
187 188 189
  installedPkgInfo <-
    if inplace
      then return (inplaceInstalledPackageInfo pwd distPref
190
                     pkg abi_hash lib lbi clbi)
Christiaan Baaij's avatar
Christiaan Baaij committed
191 192
    else if reloc
      then relocRegistrationInfo verbosity
193
                     pkg lib lbi clbi abi_hash packageDb
Christiaan Baaij's avatar
Christiaan Baaij committed
194
      else return (absoluteInstalledPackageInfo
195
                     pkg abi_hash lib lbi clbi)
196

197

198
  return installedPkgInfo{ IPI.abiHash = abi_hash }
199

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

216 217 218 219
initPackageDB :: Verbosity -> Compiler -> ProgramConfiguration -> FilePath -> IO ()
initPackageDB verbosity comp progdb dbPath =
    createPackageDB verbosity comp progdb True dbPath

220
-- | Create an empty package DB at the specified location.
221 222 223 224 225 226 227 228 229 230 231 232
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"

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
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
249 250 251 252 253 254

-- | 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 =
255 256 257 258 259 260 261 262 263 264 265 266
  withHcPkg "invokeHcPkg" comp conf
    (\hpi -> HcPkg.invoke hpi verbosity dbStack extraArgs)

withHcPkg :: String -> Compiler -> ProgramConfiguration
          -> (HcPkgInfo -> IO a) -> IO a
withHcPkg name comp conf f =
  case compilerFlavor comp of
    GHC   -> f (GHC.hcPkgInfo conf)
    GHCJS -> f (GHCJS.hcPkgInfo conf)
    LHC   -> f (LHC.hcPkgInfo conf)
    _     -> die ("Distribution.Simple.Register." ++ name ++ ":\
                  \not implemented for this compiler")
267

Duncan Coutts's avatar
Duncan Coutts committed
268
registerPackage :: Verbosity
269 270
                -> Compiler
                -> ProgramConfiguration
271
                -> PackageDBStack
272
                -> InstalledPackageInfo
Duncan Coutts's avatar
Duncan Coutts committed
273
                -> IO ()
274
registerPackage verbosity comp progdb packageDbs installedPkgInfo = do
275 276 277 278 279
  case compilerFlavor comp of
    GHC   -> GHC.registerPackage   verbosity      progdb packageDbs installedPkgInfo
    GHCJS -> GHCJS.registerPackage verbosity      progdb packageDbs installedPkgInfo
    LHC   -> LHC.registerPackage   verbosity      progdb packageDbs installedPkgInfo
    UHC   -> UHC.registerPackage   verbosity comp progdb packageDbs installedPkgInfo
280
    JHC   -> notice verbosity "Registering for jhc (nothing to do)"
281
    HaskellSuite {} ->
282
      HaskellSuite.registerPackage verbosity      progdb packageDbs installedPkgInfo
Duncan Coutts's avatar
Duncan Coutts committed
283 284 285
    _    -> die "Registering is not implemented for this compiler"

writeHcPkgRegisterScript :: Verbosity
286
                         -> InstalledPackageInfo
287
                         -> PackageDBStack
288
                         -> HcPkgInfo
Duncan Coutts's avatar
Duncan Coutts committed
289
                         -> IO ()
290 291
writeHcPkgRegisterScript verbosity installedPkgInfo packageDbs hpi = do
  let invocation  = HcPkg.reregisterInvocation hpi Verbosity.normal
292
                      packageDbs (Right installedPkgInfo)
293
      regScript   = invocationAsSystemScript buildOS invocation
Duncan Coutts's avatar
Duncan Coutts committed
294 295

  notice verbosity ("Creating package registration script: " ++ regScriptFileName)
296
  writeUTF8File regScriptFileName regScript
Duncan Coutts's avatar
Duncan Coutts committed
297 298 299 300 301 302
  setFileExecutable regScriptFileName

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

304

305 306 307
-- -----------------------------------------------------------------------------
-- Making the InstalledPackageInfo

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

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

430 431

relocatableInstalledPackageInfo :: PackageDescription
432
                                -> AbiHash
433 434 435
                                -> Library
                                -> LocalBuildInfo
                                -> ComponentLocalBuildInfo
436
                                -> FilePath
437
                                -> InstalledPackageInfo
438
relocatableInstalledPackageInfo pkg abi_hash lib lbi clbi pkgroot =
439
    generalInstalledPackageInfo adjustReativeIncludeDirs
440
                                pkg abi_hash lib lbi clbi installDirs
441 442 443 444 445 446 447
  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
448 449 450

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

simonmar's avatar
simonmar committed
452 453
-- -----------------------------------------------------------------------------
-- Unregistration
454

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

Duncan Coutts's avatar
Duncan Coutts committed
473 474 475 476
unregScriptFileName :: FilePath
unregScriptFileName = case buildOS of
                          Windows -> "unregister.bat"
                          _       -> "unregister.sh"