Register.hs 3.95 KB
Newer Older
ijones's avatar
ijones committed
1
{-# OPTIONS -cpp -DDEBUG #-}
2 3
-----------------------------------------------------------------------------
-- |
ijones's avatar
ijones committed
4
-- Module      :  Distribution.Simple.Register
5 6 7 8
-- Copyright   :  Isaac Jones 2003-2004
-- 
-- Maintainer  :  Isaac Jones <ijones@syntaxpolice.org>
-- Stability   :  alpha
ijones's avatar
ijones committed
9
-- Portability :  
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
--
-- Explanation: <FIX>
-- WHERE DOES THIS MODULE FIT IN AT A HIGH-LEVEL <FIX>

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

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

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

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

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

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

45 46 47
module Distribution.Simple.Register (
	register,
	unregister,
ijones's avatar
ijones committed
48 49 50
#ifdef DEBUG
        hunitTests
#endif
51
  ) where
52

ijones's avatar
cleanup  
ijones committed
53 54 55 56 57
import Distribution.Simple.Configure (LocalBuildInfo, compiler)
import Distribution.Setup (CompilerFlavor(..), Compiler(..))
import Distribution.Package (PackageDescription, package, showPackageId)
import Distribution.Simple.Utils (setupMessage, rawSystemExit, die)
import Distribution.Simple.GHCPackageConfig (mkGHCPackageConfig, showGHCPackageConfig)
58

ijones's avatar
ijones committed
59 60
import System(getEnv)

ijones's avatar
cleanup  
ijones committed
61
import Control.Monad (when)
simonmar's avatar
simonmar committed
62

ijones's avatar
ijones committed
63 64 65 66
#ifdef DEBUG
import HUnit (Test)
#endif

simonmar's avatar
simonmar committed
67 68 69
-- -----------------------------------------------------------------------------
-- Registration

ijones's avatar
ijones committed
70 71 72 73
register :: PackageDescription -> LocalBuildInfo
         -> Bool -- ^Install in the user's database? FIX: doesn't use this yet.
         -> IO ()
register pkg_descr lbi userInst = do
simonmar's avatar
simonmar committed
74
  setupMessage "Registering" pkg_descr
ijones's avatar
ijones committed
75

76 77 78
  case compilerFlavor (compiler lbi) of
   GHC -> do let pkg_config = mkGHCPackageConfig pkg_descr lbi
             writeFile installedPkgConfigFile (showGHCPackageConfig pkg_config)
ijones's avatar
ijones committed
79
             home <- getEnv "HOME"
80
             rawSystemExit (compilerPkgTool (compiler lbi))
ijones's avatar
ijones committed
81 82
	                     (["--add-package", "--input-file="++installedPkgConfigFile]
                              ++ (if userInst then ["--config-file=" ++ home ++ "/.ghc-packages"] else []))
83
   _   -> die ("only registering with GHC is implemented")
simonmar's avatar
simonmar committed
84

85
installedPkgConfigFile :: String
simonmar's avatar
simonmar committed
86 87 88 89
installedPkgConfigFile = "installed-pkg-config"

-- -----------------------------------------------------------------------------
-- Unregistration
90 91 92

unregister :: PackageDescription -> LocalBuildInfo -> IO ()
unregister pkg_descr lbi = do
simonmar's avatar
simonmar committed
93
  setupMessage "Unregistering" pkg_descr
simonmar's avatar
simonmar committed
94 95 96 97 98 99

  when (compilerFlavor (compiler lbi) /= GHC) $
	die ("only unregistering with GHC is implemented")
	
  rawSystemExit (compilerPkgTool (compiler lbi))
	["--remove-package=" ++ showPackageId (package pkg_descr)]
ijones's avatar
ijones committed
100 101 102 103 104 105 106 107 108 109


-- ------------------------------------------------------------
-- * Testing
-- ------------------------------------------------------------

#ifdef DEBUG
hunitTests :: [Test]
hunitTests = []
#endif