JHC.hs 7.96 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
				( emptyInstalledPackageInfo )
54
55
import qualified Distribution.InstalledPackageInfo as InstalledPackageInfo
				( InstalledPackageInfo_(package) )
56
57
import Distribution.Simple.InstalledPackageIndex (InstalledPackageIndex)
import qualified Distribution.Simple.InstalledPackageIndex as InstalledPackageIndex
58
import Distribution.Simple.LocalBuildInfo
59
60
61
				( LocalBuildInfo(..) )
import Distribution.Simple.BuildPaths
				( autogenModulesDir, exeExtension )
62
63
import Distribution.Simple.Compiler ( Compiler(..), CompilerFlavor(..), Flag,
                                  PackageDB, extensionsToFlags )
64
import Language.Haskell.Extension (Extension(..))
65
import Distribution.Simple.Program     ( ConfiguredProgram(..), jhcProgram,
66
                                  ProgramConfiguration, userMaybeSpecifyPath,
67
68
                                  requireProgram, lookupProgram,
                                  rawSystemProgram, rawSystemProgramStdoutConf )
69
import Distribution.Version	( VersionRange(AnyVersion) )
70
71
import Distribution.Package  	( PackageIdentifier(..), showPackageId,
                                  parsePackageId )
72
import Distribution.Simple.Utils( createDirectoryIfMissingVerbose,
73
                                  copyFileVerbose, die, info, intercalate )
74
import System.FilePath          ( (</>) )
75
import Distribution.Verbosity
76
77
import Distribution.Compat.ReadP
    ( readP_to_S, many, skipSpaces )
78

79
import Data.List		( nub )
80
81
import Data.Char		( isSpace )

82
83


84
85
86
-- -----------------------------------------------------------------------------
-- Configuring

87
88
89
configure :: Verbosity -> Maybe FilePath -> Maybe FilePath
          -> ProgramConfiguration -> IO (Compiler, ProgramConfiguration)
configure verbosity hcPath _hcPkgPath conf = do
90

91
92
  (jhcProg, conf')  <- requireProgram verbosity jhcProgram AnyVersion
                         (userMaybeSpecifyPath "jhc" hcPath conf)
93
94

  let Just version = programVersion jhcProg
95
      comp = Compiler {
96
97
        compilerFlavor         = JHC,
        compilerId             = PackageIdentifier "jhc" version,
98
        compilerExtensions     = jhcLanguageExtensions
99
100
      }
  return (comp, conf')
101

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

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

126
127
128
129
130
-- -----------------------------------------------------------------------------
-- Building

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

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

jhcPkgConf :: PackageDescription -> String
jhcPkgConf pd =
  let sline name sel = name ++ ": "++sel pd
      Just lib = library pd
166
      comma = intercalate ","
167
  in unlines [sline "name" (showPackageId . package)
168
             ,"exposed-modules: " ++ (comma (PD.exposedModules lib))
169
             ,"hidden-modules: " ++ (comma (otherModules $ libBuildInfo lib))
170
             ]
171
172

installLib :: Verbosity -> FilePath -> FilePath -> PackageDescription -> Library -> IO ()
Ian Lynagh's avatar
Ian Lynagh committed
173
installLib verb dest build_dir pkg_descr _ = do
174
    let p = showPackageId (package pkg_descr)++".hl"
175
    createDirectoryIfMissingVerbose verb True dest
176
    copyFileVerbose verb (build_dir </> p) (dest </> p)
177

178
179
180
181
182
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
183
    createDirectoryIfMissingVerbose verb True dest
184
    copyFileVerbose verb (build_dir </> src) (dest </> out)
Ian Lynagh's avatar
Ian Lynagh committed
185