Configure.hs 7.04 KB
Newer Older
1
{-# OPTIONS -cpp -DDEBUG #-}
2 3
-----------------------------------------------------------------------------
-- |
ijones's avatar
ijones committed
4
-- Module      :  Distribution.Simple.Configure
5 6 7 8 9 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
-- Copyright   :  Isaac Jones 2003-2004
-- 
-- Maintainer  :  Isaac Jones <ijones@syntaxpolice.org>
-- Stability   :  alpha
-- Portability :  GHC
--
-- 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. -}

ijones's avatar
ijones committed
45 46
module Distribution.Simple.Configure (writePersistBuildConfig,
                                      getPersistBuildConfig,
47 48
                                      LocalBuildInfo(..),
 			  	      configure)
ijones's avatar
ijones committed
49
    where
50

51
import Distribution.Setup(ConfigFlags,CompilerFlavor(..), Compiler(..))
52
import Distribution.Package(PackageDescription(..))
53
import Distribution.Simple.Utils (splitFilenameDir, die, split)
54

55
import System.IO hiding (catch)
56 57 58 59
import System.Exit
import System.Directory
import System.Environment	( getEnv )
import Control.Monad		( when )
60 61
import Control.Exception	( catch, evaluate )
import Prelude hiding (catch)
62 63 64 65

#ifdef DEBUG
import HUnit
#endif
66

ijones's avatar
ijones committed
67 68 69
-- |Data cached after configuration step.
data LocalBuildInfo = LocalBuildInfo {prefix :: String,
                                      compiler :: Compiler}
70
     deriving (Show, Read, Eq)
71

ijones's avatar
ijones committed
72 73
emptyLocalBuildInfo :: LocalBuildInfo
emptyLocalBuildInfo = undefined
74

ijones's avatar
ijones committed
75
getPersistBuildConfig :: IO LocalBuildInfo
76 77
getPersistBuildConfig = do
  str <- readFile localBuildInfoFile
78 79 80 81
  let bi = read str
  evaluate bi `catch` \e -> 
	die "error reading .setup-config; perhaps run ./Setup.lhs configure?"
  return bi
ijones's avatar
ijones committed
82 83

writePersistBuildConfig :: LocalBuildInfo -> IO ()
84 85 86 87 88
writePersistBuildConfig lbi = do
  writeFile localBuildInfoFile (show lbi)

localBuildInfoFile :: FilePath
localBuildInfoFile = "./.setup-config"
ijones's avatar
ijones committed
89

90 91 92
-- -----------------------------------------------------------------------------
-- Configuration

93
configure :: PackageDescription -> ConfigFlags -> IO LocalBuildInfo
94 95 96 97 98 99 100
configure pkgconfig (maybe_hc_flavor, maybe_hc_path, maybe_prefix)
  = do
	-- prefix
	let prefix = case maybe_prefix of
			Just path -> path
			Nothing   -> system_default_prefix pkgconfig

ijones's avatar
ijones committed
101
        message $ "Using build prefix: " ++ prefix
102
	-- detect compiler
ijones's avatar
ijones committed
103 104 105 106
	compiler@(Compiler f' p' pkg) <- configCompiler maybe_hc_flavor maybe_hc_path pkgconfig 
        message $ "Using compiler flavor: " ++ (show f')
        message $ "Using compiler: " ++ p'
        message $ "Using package tool: " ++ pkg
107 108
	return LocalBuildInfo{prefix=prefix, compiler=compiler}

109
system_default_prefix PackageDescription{package=package} = 
110 111 112 113 114 115 116 117 118
#ifdef mingw32_TARGET_OS
  "C:\Program Files\" ++ pkgName package
#else
  "/usr"
#endif

-- -----------------------------------------------------------------------------
-- Determining the compiler details

119
configCompiler :: Maybe CompilerFlavor -> Maybe FilePath -> PackageDescription
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  -> IO Compiler

configCompiler (Just flavor) (Just path) pkgconfig
  = do pkgtool <- guessPkgToolFromHCPath flavor path
       return (Compiler{compilerFlavor=flavor,
			compilerPath=path,
			compilerPkgTool=pkgtool})

configCompiler (Just flavor) Nothing pkgconfig
  = do path <- findCompiler flavor
       pkgtool <- guessPkgToolFromHCPath flavor path
       return (Compiler{compilerFlavor=flavor,
			compilerPath=path,
			compilerPkgTool=pkgtool})

configCompiler Nothing maybe_path pkgconfig
  = configCompiler (Just defaultCompilerFlavor) maybe_path pkgconfig

defaultCompilerFlavor =
#if defined(__GLASGOW_HASKELL__)
   GHC
#elif defined(__NHC__)
   NHC
#elif defined(__HUGS__)
   Hugs
#endif

findCompiler :: CompilerFlavor -> IO FilePath
findCompiler flavor = findBinary (compilerBinaryName flavor)
   -- ToDo: check that compiler works? check compiler version?

compilerBinaryName GHC  = "ghc"
compilerBinaryName NHC  = "nhc98"
compilerBinaryName Hugs = "hugs"

compilerPkgToolName GHC  = "ghc-pkg"
compilerPkgToolName NHC  = "nhc98-pkg"
ijones's avatar
ijones committed
157
compilerPkgToolName Hugs = "hugs-package"
158 159 160

guessPkgToolFromHCPath :: CompilerFlavor -> FilePath -> IO FilePath
guessPkgToolFromHCPath flavor path
ijones's avatar
ijones committed
161 162 163 164
  = do let pkgToolName = compilerPkgToolName flavor
           (dir,_) = splitFilenameDir path
           pkgtool = dir ++ '/':pkgToolName
       message $ "looking for package tool: " ++ pkgToolName ++ " near compiler in " ++ path
165 166 167
       exists <- doesFileExist pkgtool
       when (not exists) $
	  die ("Cannot find package tool: " ++ pkgtool)
ijones's avatar
ijones committed
168
       message $ "found package tool in " ++ pkgtool
169 170 171 172 173
       return pkgtool

findBinary :: String -> IO FilePath
findBinary binary = do
  path <- getEnv "PATH"
ijones's avatar
ijones committed
174
  message $ "searching for " ++ binary ++ " in path."
175 176 177 178 179 180 181
  search (parsePath path)
  where
    search :: [FilePath] -> IO FilePath
    search [] = die ("Cannot find compiler for " ++ binary)
    search (d:ds) = do
	let path = d ++ '/':binary
	b <- doesFileExist path
ijones's avatar
ijones committed
182 183
	if b then do message ("found " ++ binary ++ " at "++ path); return path
             else search ds
184 185 186 187 188 189 190 191 192 193

parsePath :: String -> [FilePath]
parsePath path = split pathSep path
  where
#ifdef mingw32_TARGET_OS
	pathSep = ';'
#else
	pathSep = ':'
#endif

ijones's avatar
ijones committed
194 195
message s = putStrLn $ "configure: " ++ s

196
-- -----------------------------------------------------------------------------
197
-- Tests
198

199 200 201 202
#ifdef DEBUG
hunitTests :: IO [Test]
hunitTests = do
   let simonMarGHCLoc = "/home/simonmar/fp/bin/i386-unknown-linux/ghc"
203
   simonMarGHC <- configure PackageDescription{} (Just GHC,
204 205 206 207 208 209 210 211 212
				       Just simonMarGHCLoc,
				       Nothing)
   return $ [TestLabel "Configure Testing" $ TestList [
	     "finding ghc, etc on simonMar's machine" ~: "failed" ~:
             (LocalBuildInfo "/usr" (Compiler GHC 
	                    simonMarGHCLoc
 			    (simonMarGHCLoc ++ "-pkg")))
             ~=? simonMarGHC]]
#endif