JHC.hs 7.85 KB
Newer Older
1 2 3 4 5 6 7 8 9
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Simple.JHC
-- Copyright   :  Isaac Jones 2003-2006
-- 
-- Maintainer  :  Isaac Jones <ijones@syntaxpolice.org>
-- Stability   :  alpha
-- Portability :  portable
--
ijones's avatar
ijones committed
10
-- Build and install functionality for the JHC compiler.
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

{- Copyright (c) 2003-2005, 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. -}

module Distribution.Simple.JHC (
44
	configure, getInstalledPackages, build, installLib, installExe
45 46
 ) where

47
import Distribution.PackageDescription as PD
48 49 50 51
				( PackageDescription(..), BuildInfo(..),
				  withLib,
				  Executable(..), withExe, Library(..),
				  libModules, hcOptions )
52
import Distribution.InstalledPackageInfo
53
				( InstalledPackageInfo, emptyInstalledPackageInfo )
54 55
import qualified Distribution.InstalledPackageInfo as InstalledPackageInfo
				( InstalledPackageInfo_(package) )
56 57
import Distribution.Simple.PackageIndex (PackageIndex)
import qualified Distribution.Simple.PackageIndex as PackageIndex
58
import Distribution.Simple.LocalBuildInfo
59 60 61
				( LocalBuildInfo(..) )
import Distribution.Simple.BuildPaths
				( autogenModulesDir, exeExtension )
62 63 64
import Distribution.Simple.Compiler
         ( CompilerFlavor(..), CompilerId(..), Compiler(..)
         , PackageDB, Flag, extensionsToFlags )
65
import Language.Haskell.Extension (Extension(..))
66
import Distribution.Simple.Program     ( ConfiguredProgram(..), jhcProgram,
67
                                  ProgramConfiguration, userMaybeSpecifyPath,
68 69
                                  requireProgram, lookupProgram,
                                  rawSystemProgram, rawSystemProgramStdoutConf )
70
import Distribution.Version	( VersionRange(AnyVersion) )
71
import Distribution.Package
72
         ( Package(..) )
73
import Distribution.Simple.Utils
74
        ( createDirectoryIfMissingVerbose, copyFileVerbose
75
        , die, info, intercalate )
76
import System.FilePath          ( (</>) )
77
import Distribution.Verbosity
78 79
import Distribution.Text
         ( parse, display )
80 81
import Distribution.Compat.ReadP
    ( readP_to_S, many, skipSpaces )
82

83
import Data.List		( nub )
84 85
import Data.Char		( isSpace )

86 87


88 89 90
-- -----------------------------------------------------------------------------
-- Configuring

91 92 93
configure :: Verbosity -> Maybe FilePath -> Maybe FilePath
          -> ProgramConfiguration -> IO (Compiler, ProgramConfiguration)
configure verbosity hcPath _hcPkgPath conf = do
94

95 96
  (jhcProg, conf')  <- requireProgram verbosity jhcProgram AnyVersion
                         (userMaybeSpecifyPath "jhc" hcPath conf)
97 98

  let Just version = programVersion jhcProg
99
      comp = Compiler {
100
        compilerId             = CompilerId JHC version,
101
        compilerExtensions     = jhcLanguageExtensions
102 103
      }
  return (comp, conf')
104

105 106 107 108 109 110 111 112 113
-- | The flags for the supported extensions
jhcLanguageExtensions :: [(Extension, Flag)]
jhcLanguageExtensions =
    [(TypeSynonymInstances       , "")
    ,(ForeignFunctionInterface   , "")
    ,(NoImplicitPrelude          , "--noprelude")
    ,(CPP                        , "-fcpp")
    ]

114
getInstalledPackages :: Verbosity -> PackageDB -> ProgramConfiguration
115
                    -> IO (PackageIndex InstalledPackageInfo)
116 117
getInstalledPackages verbosity _packagedb conf = do
   str <- rawSystemProgramStdoutConf verbosity jhcProgram conf ["--list-libraries"]
118
   case pCheck (readP_to_S (many (skipSpaces >> parse)) str) of
119
     [ps] -> return $ PackageIndex.fromList
120
                    [ emptyInstalledPackageInfo {
121 122 123
                        InstalledPackageInfo.package = p
                      }
                    | p <- ps ]
124 125 126 127 128
     _    -> die "cannot parse package list"
  where
    pCheck :: [(a, [Char])] -> [a]
    pCheck rs = [ r | (r,s) <- rs, all isSpace s ]

129 130 131 132 133
-- -----------------------------------------------------------------------------
-- Building

-- | Building a package for JHC.
-- Currently C source files are not supported.
134 135
build :: PackageDescription -> LocalBuildInfo -> Verbosity -> IO ()
build pkg_descr lbi verbosity = do
136
  let Just jhcProg = lookupProgram jhcProgram (withPrograms lbi)
137
  withLib pkg_descr () $ \lib -> do
138
      info verbosity "Building library..."
139
      let libBi = libBuildInfo lib
140
      let args  = constructJHCCmdLine lbi libBi (buildDir lbi) verbosity
141
      rawSystemProgram verbosity jhcProg (["-c"] ++ args ++ libModules pkg_descr)
142
      let pkgid = display (packageId pkg_descr)
143 144
          pfile = buildDir lbi </> "jhc-pkg.conf"
          hlfile= buildDir lbi </> (pkgid ++ ".hl")
145
      writeFile pfile $ jhcPkgConf pkg_descr
146
      rawSystemProgram verbosity jhcProg ["--build-hl="++pfile, "-o", hlfile]
147
  withExe pkg_descr $ \exe -> do
148
      info verbosity ("Building executable "++exeName exe)
149
      let exeBi = buildInfo exe
150
      let out   = buildDir lbi </> exeName exe
151
      let args  = constructJHCCmdLine lbi exeBi (buildDir lbi) verbosity
152
      rawSystemProgram verbosity jhcProg (["-o",out] ++ args ++ [modulePath exe])
153

154 155 156
constructJHCCmdLine :: LocalBuildInfo -> BuildInfo -> FilePath -> Verbosity -> [String]
constructJHCCmdLine lbi bi _odir verbosity =
        (if verbosity >= deafening then ["-v"] else [])
157
     ++ extensionsToFlags (compiler lbi) (extensions bi)
158
     ++ hcOptions JHC bi
159 160 161
     ++ ["--noauto","-i-"]
     ++ ["-i", autogenModulesDir lbi]
     ++ concat [["-i", l] | l <- nub (hsSourceDirs bi)]
162
     ++ ["-optc" ++ opt | opt <- PD.ccOptions bi]
163
     ++ (concat [ ["-p", display pkg] | pkg <- packageDeps lbi ])
164 165 166 167 168

jhcPkgConf :: PackageDescription -> String
jhcPkgConf pd =
  let sline name sel = name ++ ": "++sel pd
      Just lib = library pd
169
      comma = intercalate ","
170
  in unlines [sline "name" (display . packageId)
171
             ,"exposed-modules: " ++ (comma (PD.exposedModules lib))
172
             ,"hidden-modules: " ++ (comma (otherModules $ libBuildInfo lib))
173
             ]
174 175

installLib :: Verbosity -> FilePath -> FilePath -> PackageDescription -> Library -> IO ()
Ian Lynagh's avatar
Ian Lynagh committed
176
installLib verb dest build_dir pkg_descr _ = do
177
    let p = display (packageId pkg_descr)++".hl"
178
    createDirectoryIfMissingVerbose verb True dest
179
    copyFileVerbose verb (build_dir </> p) (dest </> p)
180

181 182 183 184 185
installExe :: Verbosity -> FilePath -> FilePath -> (FilePath,FilePath) -> PackageDescription -> Executable -> IO ()
installExe verb dest build_dir (progprefix,progsuffix) _ exe = do
    let exe_name = exeName exe
        src = exe_name </> exeExtension
        out   = (progprefix ++ exe_name ++ progsuffix) </> exeExtension
186
    createDirectoryIfMissingVerbose verb True dest
187
    copyFileVerbose verb (build_dir </> src) (dest </> out)
Ian Lynagh's avatar
Ian Lynagh committed
188