Build.hs 23.8 KB
Newer Older
1
2
-----------------------------------------------------------------------------
-- |
3
-- Module      :  Distribution.Simple.Build
4
5
-- Copyright   :  Isaac Jones 2003-2005,
--                Ross Paterson 2006,
6
--                Duncan Coutts 2007-2008, 2012
7
-- License     :  BSD3
8
--
Duncan Coutts's avatar
Duncan Coutts committed
9
-- Maintainer  :  cabal-devel@haskell.org
ijones's avatar
ijones committed
10
-- Portability :  portable
11
--
Duncan Coutts's avatar
Duncan Coutts committed
12
13
14
15
16
-- This is the entry point to actually building the modules in a package. It
-- doesn't actually do much itself, most of the work is delegated to
-- compiler-specific actions. It does do some non-compiler specific bits like
-- running pre-processors.
--
17

18
module Distribution.Simple.Build (
19
    build, repl,
20
    startInterpreter,
21
22
23

    initialBuildSteps,
    writeAutogenFiles,
24
  ) where
25

26
import Distribution.Package
27
28
29
30
31
import qualified Distribution.Simple.GHC   as GHC
import qualified Distribution.Simple.GHCJS as GHCJS
import qualified Distribution.Simple.JHC   as JHC
import qualified Distribution.Simple.LHC   as LHC
import qualified Distribution.Simple.UHC   as UHC
32
import qualified Distribution.Simple.HaskellSuite as HaskellSuite
33
34
35

import qualified Distribution.Simple.Build.Macros      as Build.Macros
import qualified Distribution.Simple.Build.PathsModule as Build.PathsModule
36
import qualified Distribution.Simple.Program.HcPkg as HcPkg
37

38
39
import Distribution.Simple.Compiler hiding (Flag)
import Distribution.PackageDescription hiding (Flag)
40
import qualified Distribution.InstalledPackageInfo as IPI
41
import qualified Distribution.ModuleName as ModuleName
42
43

import Distribution.Simple.Setup
refold's avatar
refold committed
44
import Distribution.Simple.BuildTarget
45
import Distribution.Simple.PreProcess
46
import Distribution.Simple.LocalBuildInfo
47
48
import Distribution.Simple.Program.Types
import Distribution.Simple.Program.Db
49
import Distribution.Simple.BuildPaths
50
import Distribution.Simple.Configure
51
import Distribution.Simple.Register
52
import Distribution.Simple.Test.LibV09
53
import Distribution.Simple.Utils
54

55
import Distribution.System
56
import Distribution.Text
57
import Distribution.Verbosity
simonmar's avatar
simonmar committed
58

59
import qualified Data.Map as Map
60
import qualified Data.Set as Set
61
import Data.List
62
         ( intersect )
63
import Control.Monad
64
         ( when, unless )
65
66
import System.FilePath
         ( (</>), (<.>) )
67
import System.Directory
68
         ( getCurrentDirectory )
69

simonmar's avatar
simonmar committed
70
-- -----------------------------------------------------------------------------
ijones's avatar
ijones committed
71
-- |Build the libraries and executables in this package.
72

73
74
75
76
build    :: PackageDescription  -- ^ Mostly information from the .cabal file
         -> LocalBuildInfo      -- ^ Configuration information
         -> BuildFlags          -- ^ Flags that the user passed to build
         -> [ PPSuffixHandler ] -- ^ preprocessors to run before compiling
ijones's avatar
ijones committed
77
         -> IO ()
78
build pkg_descr lbi flags suffixes = do
79
80
  let distPref  = fromFlag (buildDistPref flags)
      verbosity = fromFlag (buildVerbosity flags)
81
82
83

  targets  <- readBuildTargets pkg_descr (buildArgs flags)
  targets' <- checkBuildTargets verbosity pkg_descr targets
84
  let componentsToBuild = componentsInBuildOrder lbi (map fst targets')
85
  info verbosity $ "Component build order: "
86
                ++ intercalate ", " (map (showComponentName . componentLocalName) componentsToBuild)
87

88
89
90
  when (null targets) $
    -- Only bother with this message if we're building the whole package
    setupMessage verbosity "Building" (packageId pkg_descr)
Simon Marlow's avatar
Simon Marlow committed
91

92
  internalPackageDB <- createInternalPackageDB verbosity lbi distPref
93

94
95
  -- TODO: we're computing this twice, do it once!
  withComponentsInBuildOrder pkg_descr lbi (map fst targets') $ \comp clbi -> do
96
    initialBuildSteps distPref pkg_descr lbi clbi verbosity
97
98
99
100
101
102
    let bi     = componentBuildInfo comp
        progs' = addInternalBuildTools pkg_descr lbi bi (withPrograms lbi)
        lbi'   = lbi {
                   withPrograms  = progs',
                   withPackageDB = withPackageDB lbi ++ [internalPackageDB]
                 }
103
104
    buildComponent verbosity (buildNumJobs flags) pkg_descr
                   lbi' suffixes comp clbi distPref
105
106


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
repl     :: PackageDescription  -- ^ Mostly information from the .cabal file
         -> LocalBuildInfo      -- ^ Configuration information
         -> ReplFlags           -- ^ Flags that the user passed to build
         -> [ PPSuffixHandler ] -- ^ preprocessors to run before compiling
         -> [String]
         -> IO ()
repl pkg_descr lbi flags suffixes args = do
  let distPref  = fromFlag (replDistPref flags)
      verbosity = fromFlag (replVerbosity flags)

  targets  <- readBuildTargets pkg_descr args
  targets' <- case targets of
    []       -> return $ take 1 [ componentName c
                                | c <- pkgEnabledComponents pkg_descr ]
    [target] -> fmap (map fst) (checkBuildTargets verbosity pkg_descr [target])
    _        -> die $ "The 'repl' command does not support multiple targets at once."
  let componentsToBuild = componentsInBuildOrder lbi targets'
      componentForRepl  = last componentsToBuild
  debug verbosity $ "Component build order: "
                 ++ intercalate ", "
127
                      [ showComponentName (componentLocalName clbi) | clbi <- componentsToBuild ]
128

129
130
  internalPackageDB <- createInternalPackageDB verbosity lbi distPref

131
132
133
134
135
136
137
138
139
  let lbiForComponent comp lbi' =
        lbi' {
          withPackageDB = withPackageDB lbi ++ [internalPackageDB],
          withPrograms  = addInternalBuildTools pkg_descr lbi'
                            (componentBuildInfo comp) (withPrograms lbi')
        }

  -- build any dependent components
  sequence_
140
141
    [ do let cname = componentLocalName clbi
             comp = getComponent pkg_descr cname
142
143
144
145
             lbi' = lbiForComponent comp lbi
         initialBuildSteps distPref pkg_descr lbi clbi verbosity
         buildComponent verbosity NoFlag
                        pkg_descr lbi' suffixes comp clbi distPref
146
    | clbi <- init componentsToBuild ]
147

Ian D. Bollinger's avatar
Ian D. Bollinger committed
148
  -- REPL for target components
149
150
  let clbi = componentForRepl
      cname = componentLocalName clbi
151
152
      comp = getComponent pkg_descr cname
      lbi' = lbiForComponent comp lbi
153
154
  initialBuildSteps distPref pkg_descr lbi clbi verbosity
  replComponent verbosity pkg_descr lbi' suffixes comp clbi distPref
155
156


157
-- | Start an interpreter without loading any package files.
158
159
160
startInterpreter :: Verbosity -> ProgramDb -> Compiler -> Platform
                 -> PackageDBStack -> IO ()
startInterpreter verbosity programDb comp platform packageDBs =
161
  case compilerFlavor comp of
162
163
    GHC   -> GHC.startInterpreter   verbosity programDb comp platform packageDBs
    GHCJS -> GHCJS.startInterpreter verbosity programDb comp platform packageDBs
164
    _     -> die "A REPL is not supported with this compiler."
165

166
buildComponent :: Verbosity
167
               -> Flag (Maybe Int)
168
169
170
171
172
173
174
               -> PackageDescription
               -> LocalBuildInfo
               -> [PPSuffixHandler]
               -> Component
               -> ComponentLocalBuildInfo
               -> FilePath
               -> IO ()
175
buildComponent verbosity numJobs pkg_descr lbi suffixes
176
               comp@(CLib lib) clbi distPref = do
177
    preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
178
    extras <- preprocessExtras comp lbi
179
    info verbosity $ "Building library " ++ libName lib ++ "..."
180
181
182
    let libbi = libBuildInfo lib
        lib' = lib { libBuildInfo = addExtraCSources libbi extras }
    buildLib verbosity numJobs pkg_descr lbi lib' clbi
183
184
185
186

    -- Register the library in-place, so exes can depend
    -- on internally defined libraries.
    pwd <- getCurrentDirectory
187
188
    let -- The in place registration uses the "-inplace" suffix, not an ABI hash
        installedPkgInfo = inplaceInstalledPackageInfo pwd distPref pkg_descr
189
                                                       (AbiHash "") lib' lbi clbi
190

191
    registerPackage verbosity (compiler lbi) (withPrograms lbi) HcPkg.MultiInstance
192
                    (withPackageDB lbi) installedPkgInfo
193

194
buildComponent verbosity numJobs pkg_descr lbi suffixes
195
               comp@(CExe exe) clbi _ = do
196
    preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
197
    extras <- preprocessExtras comp lbi
198
    info verbosity $ "Building executable " ++ exeName exe ++ "..."
199
200
201
    let ebi = buildInfo exe
        exe' = exe { buildInfo = addExtraCSources ebi extras }
    buildExe verbosity numJobs pkg_descr lbi exe' clbi
202
203


204
buildComponent verbosity numJobs pkg_descr lbi suffixes
205
               comp@(CTest test@TestSuite { testInterface = TestSuiteExeV10{} })
206
               clbi _distPref = do
207
    let exe = testSuiteExeV10AsExe test
208
    preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
209
    extras <- preprocessExtras comp lbi
210
    info verbosity $ "Building test suite " ++ testName test ++ "..."
211
212
213
    let ebi = buildInfo exe
        exe' = exe { buildInfo = addExtraCSources ebi extras }
    buildExe verbosity numJobs pkg_descr lbi exe' clbi
214

215

216
buildComponent verbosity numJobs pkg_descr lbi0 suffixes
217
               comp@(CTest
218
                 test@TestSuite { testInterface = TestSuiteLibV09{} })
219
220
221
222
223
224
               clbi -- This ComponentLocalBuildInfo corresponds to a detailed
                    -- test suite and not a real component. It should not
                    -- be used, except to construct the CLBIs for the
                    -- library and stub executable that will actually be
                    -- built.
               distPref = do
225
    pwd <- getCurrentDirectory
226
227
    let (pkg, lib, libClbi, lbi, ipi, exe, exeClbi) =
          testSuiteLibV09AsLibAndExe pkg_descr test clbi lbi0 distPref pwd
228
    preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
229
    extras <- preprocessExtras comp lbi
230
    info verbosity $ "Building test suite " ++ testName test ++ "..."
231
    buildLib verbosity numJobs pkg lbi lib libClbi
232
233
234
    -- NB: need to enable multiple instances here, because on 7.10+
    -- the package name is the same as the library, and we still
    -- want the registration to go through.
235
    registerPackage verbosity (compiler lbi) (withPrograms lbi) HcPkg.MultiInstance
236
                    (withPackageDB lbi) ipi
237
238
239
    let ebi = buildInfo exe
        exe' = exe { buildInfo = addExtraCSources ebi extras }
    buildExe verbosity numJobs pkg_descr lbi exe' exeClbi
240
241


242
buildComponent _ _ _ _ _
243
244
245
246
247
               (CTest TestSuite { testInterface = TestSuiteUnsupported tt })
               _ _ =
    die $ "No support for building test suite type " ++ display tt


248
buildComponent verbosity numJobs pkg_descr lbi suffixes
249
               comp@(CBench bm@Benchmark { benchmarkInterface = BenchmarkExeV10 {} })
250
               clbi _ = do
251
    let (exe, exeClbi) = benchmarkExeV10asExe bm clbi
252
    preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
253
    extras <- preprocessExtras comp lbi
254
    info verbosity $ "Building benchmark " ++ benchmarkName bm ++ "..."
255
256
257
    let ebi = buildInfo exe
        exe' = exe { buildInfo = addExtraCSources ebi extras }
    buildExe verbosity numJobs pkg_descr lbi exe' exeClbi
258
259


260
buildComponent _ _ _ _ _
261
262
263
264
               (CBench Benchmark { benchmarkInterface = BenchmarkUnsupported tt })
               _ _ =
    die $ "No support for building benchmark type " ++ display tt

tibbe's avatar
tibbe committed
265

266
267
268
269
270
271
272
273
274
-- | Add extra C sources generated by preprocessing to build
-- information.
addExtraCSources :: BuildInfo -> [FilePath] -> BuildInfo
addExtraCSources bi extras = bi { cSources = new }
  where new = Set.toList $ old `Set.union` exs
        old = Set.fromList $ cSources bi
        exs = Set.fromList extras


275
276
277
278
279
280
281
282
283
284
replComponent :: Verbosity
              -> PackageDescription
              -> LocalBuildInfo
              -> [PPSuffixHandler]
              -> Component
              -> ComponentLocalBuildInfo
              -> FilePath
              -> IO ()
replComponent verbosity pkg_descr lbi suffixes
               comp@(CLib lib) clbi _ = do
285
    preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
286
287
288
289
    extras <- preprocessExtras comp lbi
    let libbi = libBuildInfo lib
        lib' = lib { libBuildInfo = libbi { cSources = cSources libbi ++ extras } }
    replLib verbosity pkg_descr lbi lib' clbi
290
291
292

replComponent verbosity pkg_descr lbi suffixes
               comp@(CExe exe) clbi _ = do
293
    preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
294
295
296
297
    extras <- preprocessExtras comp lbi
    let ebi = buildInfo exe
        exe' = exe { buildInfo = ebi { cSources = cSources ebi ++ extras } }
    replExe verbosity pkg_descr lbi exe' clbi
298
299
300
301
302
303


replComponent verbosity pkg_descr lbi suffixes
               comp@(CTest test@TestSuite { testInterface = TestSuiteExeV10{} })
               clbi _distPref = do
    let exe = testSuiteExeV10AsExe test
304
    preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
305
306
307
308
    extras <- preprocessExtras comp lbi
    let ebi = buildInfo exe
        exe' = exe { buildInfo = ebi { cSources = cSources ebi ++ extras } }
    replExe verbosity pkg_descr lbi exe' clbi
309
310


311
replComponent verbosity pkg_descr lbi0 suffixes
312
313
314
315
               comp@(CTest
                 test@TestSuite { testInterface = TestSuiteLibV09{} })
               clbi distPref = do
    pwd <- getCurrentDirectory
316
317
    let (pkg, lib, libClbi, lbi, _, _, _) =
          testSuiteLibV09AsLibAndExe pkg_descr test clbi lbi0 distPref pwd
318
    preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
319
320
321
322
    extras <- preprocessExtras comp lbi
    let libbi = libBuildInfo lib
        lib' = lib { libBuildInfo = libbi { cSources = cSources libbi ++ extras } }
    replLib verbosity pkg lbi lib' libClbi
323
324
325
326
327
328
329
330
331
332
333
334


replComponent _ _ _ _
              (CTest TestSuite { testInterface = TestSuiteUnsupported tt })
              _ _ =
    die $ "No support for building test suite type " ++ display tt


replComponent verbosity pkg_descr lbi suffixes
               comp@(CBench bm@Benchmark { benchmarkInterface = BenchmarkExeV10 {} })
               clbi _ = do
    let (exe, exeClbi) = benchmarkExeV10asExe bm clbi
335
    preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
336
337
338
339
    extras <- preprocessExtras comp lbi
    let ebi = buildInfo exe
        exe' = exe { buildInfo = ebi { cSources = cSources ebi ++ extras } }
    replExe verbosity pkg_descr lbi exe' exeClbi
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364


replComponent _ _ _ _
              (CBench Benchmark { benchmarkInterface = BenchmarkUnsupported tt })
              _ _ =
    die $ "No support for building benchmark type " ++ display tt

----------------------------------------------------
-- Shared code for buildComponent and replComponent
--

-- | Translate a exe-style 'TestSuite' component into an exe for building
testSuiteExeV10AsExe :: TestSuite -> Executable
testSuiteExeV10AsExe test@TestSuite { testInterface = TestSuiteExeV10 _ mainFile } =
    Executable {
      exeName    = testName test,
      modulePath = mainFile,
      buildInfo  = testBuildInfo test
    }
testSuiteExeV10AsExe TestSuite{} = error "testSuiteExeV10AsExe: wrong kind"

-- | Translate a lib-style 'TestSuite' component into a lib + exe for building
testSuiteLibV09AsLibAndExe :: PackageDescription
                           -> TestSuite
                           -> ComponentLocalBuildInfo
365
                           -> LocalBuildInfo
366
367
368
369
                           -> FilePath
                           -> FilePath
                           -> (PackageDescription,
                               Library, ComponentLocalBuildInfo,
370
                               LocalBuildInfo,
371
                               IPI.InstalledPackageInfo,
372
                               Executable, ComponentLocalBuildInfo)
373
testSuiteLibV09AsLibAndExe pkg_descr
374
                     test@TestSuite { testInterface = TestSuiteLibV09 _ m }
375
                     clbi lbi distPref pwd =
376
    (pkg, lib, libClbi, lbi, ipi, exe, exeClbi)
377
378
379
  where
    bi  = testBuildInfo test
    lib = Library {
380
            libName = testName test,
381
            exposedModules = [ m ],
382
            reexportedModules = [],
383
384
            requiredSignatures = [],
            exposedSignatures = [],
385
386
387
            libExposed     = True,
            libBuildInfo   = bi
          }
388
389
390
391
392
    -- This is, like, the one place where we use a CTestName for a library.
    -- Should NOT use library name, since that could conflict!
    PackageIdentifier pkg_name pkg_ver = package pkg_descr
    compat_name = computeCompatPackageName pkg_name (CTestName (testName test))
    compat_key = computeCompatPackageKey (compiler lbi) compat_name pkg_ver (componentUnitId clbi)
393
394
    libClbi = LibComponentLocalBuildInfo
                { componentPackageDeps = componentPackageDeps clbi
395
396
                , componentLocalName = CLibName (testName test)
                , componentIsPublic = False
397
                , componentIncludes = componentIncludes clbi
398
                , componentUnitId = componentUnitId clbi
399
                , componentCompatPackageName = compat_name
400
                , componentCompatPackageKey = compat_key
401
                , componentExposedModules = [IPI.ExposedModule m Nothing]
402
403
                }
    pkg = pkg_descr {
404
            package      = (package pkg_descr) { pkgName = compat_name }
405
406
407
          , buildDepends = targetBuildDepends $ testBuildInfo test
          , executables  = []
          , testSuites   = []
408
          , libraries    = [lib]
409
          }
410
    ipi    = inplaceInstalledPackageInfo pwd distPref pkg (AbiHash "") lib lbi libClbi
411
412
413
414
415
416
417
418
419
    testDir = buildDir lbi </> stubName test
          </> stubName test ++ "-tmp"
    testLibDep = thisPackageVersion $ package pkg
    exe = Executable {
            exeName    = stubName test,
            modulePath = stubFilePath test,
            buildInfo  = (testBuildInfo test) {
                           hsSourceDirs       = [ testDir ],
                           targetBuildDepends = testLibDep
420
                             : (targetBuildDepends $ testBuildInfo test),
421
                           targetBuildRenaming = Map.empty
422
423
424
425
                         }
          }
    -- | The stub executable needs a new 'ComponentLocalBuildInfo'
    -- that exposes the relevant test suite library.
426
427
428
429
    deps = (IPI.installedUnitId ipi, packageId ipi)
         : (filter (\(_, x) -> let PackageName name = pkgName x
                               in name == "Cabal" || name == "base")
                   (componentPackageDeps clbi))
430
    exeClbi = ExeComponentLocalBuildInfo {
431
432
433
                -- TODO: this is a hack, but as long as this is unique
                -- (doesn't clobber something) we won't run into trouble
                componentUnitId = mkUnitId (stubName test),
434
                componentLocalName = CExeName (stubName test),
435
436
                componentPackageDeps = deps,
                componentIncludes = zip (map fst deps) (repeat defaultRenaming)
437
              }
438
testSuiteLibV09AsLibAndExe _ TestSuite{} _ _ _ _ = error "testSuiteLibV09AsLibAndExe: wrong kind"
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453


-- | Translate a exe-style 'Benchmark' component into an exe for building
benchmarkExeV10asExe :: Benchmark -> ComponentLocalBuildInfo
                     -> (Executable, ComponentLocalBuildInfo)
benchmarkExeV10asExe bm@Benchmark { benchmarkInterface = BenchmarkExeV10 _ f }
                     clbi =
    (exe, exeClbi)
  where
    exe = Executable {
            exeName    = benchmarkName bm,
            modulePath = f,
            buildInfo  = benchmarkBuildInfo bm
          }
    exeClbi = ExeComponentLocalBuildInfo {
454
                componentUnitId = componentUnitId clbi,
455
                componentLocalName = CExeName (benchmarkName bm),
456
                componentPackageDeps = componentPackageDeps clbi,
457
                componentIncludes = componentIncludes clbi
458
459
460
              }
benchmarkExeV10asExe Benchmark{} _ = error "benchmarkExeV10asExe: wrong kind"

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
addInternalBuildTools :: PackageDescription -> LocalBuildInfo -> BuildInfo
                      -> ProgramDb -> ProgramDb
addInternalBuildTools pkg lbi bi progs =
    foldr updateProgram progs internalBuildTools
  where
    internalBuildTools =
      [ simpleConfiguredProgram toolName (FoundOnSystem toolLocation)
      | toolName <- toolNames
      , let toolLocation = buildDir lbi </> toolName </> toolName <.> exeExtension ]
    toolNames = intersect buildToolNames internalExeNames
    internalExeNames = map exeName (executables pkg)
    buildToolNames   = map buildToolName (buildTools bi)
      where
        buildToolName (Dependency (PackageName name) _ ) = name


477
-- TODO: build separate libs in separate dirs so that we can build
Ian D. Bollinger's avatar
Ian D. Bollinger committed
478
-- multiple libs, e.g. for 'LibTest' library-style test suites
479
480
buildLib :: Verbosity -> Flag (Maybe Int)
                      -> PackageDescription -> LocalBuildInfo
481
                      -> Library            -> ComponentLocalBuildInfo -> IO ()
482
buildLib verbosity numJobs pkg_descr lbi lib clbi =
483
  case compilerFlavor (compiler lbi) of
484
485
486
487
488
    GHC   -> GHC.buildLib   verbosity numJobs pkg_descr lbi lib clbi
    GHCJS -> GHCJS.buildLib verbosity numJobs pkg_descr lbi lib clbi
    JHC   -> JHC.buildLib   verbosity         pkg_descr lbi lib clbi
    LHC   -> LHC.buildLib   verbosity         pkg_descr lbi lib clbi
    UHC   -> UHC.buildLib   verbosity         pkg_descr lbi lib clbi
489
    HaskellSuite {} -> HaskellSuite.buildLib verbosity pkg_descr lbi lib clbi
490
491
    _    -> die "Building is not supported with this compiler."

492
493
buildExe :: Verbosity -> Flag (Maybe Int)
                      -> PackageDescription -> LocalBuildInfo
494
                      -> Executable         -> ComponentLocalBuildInfo -> IO ()
495
buildExe verbosity numJobs pkg_descr lbi exe clbi =
496
  case compilerFlavor (compiler lbi) of
497
498
499
500
501
502
    GHC   -> GHC.buildExe   verbosity numJobs pkg_descr lbi exe clbi
    GHCJS -> GHCJS.buildExe verbosity numJobs pkg_descr lbi exe clbi
    JHC   -> JHC.buildExe   verbosity         pkg_descr lbi exe clbi
    LHC   -> LHC.buildExe   verbosity         pkg_descr lbi exe clbi
    UHC   -> UHC.buildExe   verbosity         pkg_descr lbi exe clbi
    _     -> die "Building is not supported with this compiler."
503
504
505
506
507

replLib :: Verbosity -> PackageDescription -> LocalBuildInfo
                     -> Library            -> ComponentLocalBuildInfo -> IO ()
replLib verbosity pkg_descr lbi lib clbi =
  case compilerFlavor (compiler lbi) of
508
509
    -- 'cabal repl' doesn't need to support 'ghc --make -j', so we just pass
    -- NoFlag as the numJobs parameter.
510
511
512
    GHC   -> GHC.replLib   verbosity NoFlag pkg_descr lbi lib clbi
    GHCJS -> GHCJS.replLib verbosity NoFlag pkg_descr lbi lib clbi
    _     -> die "A REPL is not supported for this compiler."
513
514
515
516
517

replExe :: Verbosity -> PackageDescription -> LocalBuildInfo
                     -> Executable         -> ComponentLocalBuildInfo -> IO ()
replExe verbosity pkg_descr lbi exe clbi =
  case compilerFlavor (compiler lbi) of
518
519
520
    GHC   -> GHC.replExe   verbosity NoFlag pkg_descr lbi exe clbi
    GHCJS -> GHCJS.replExe verbosity NoFlag pkg_descr lbi exe clbi
    _     -> die "A REPL is not supported for this compiler."
521
522


523
524
initialBuildSteps :: FilePath -- ^"dist" prefix
                  -> PackageDescription  -- ^mostly information from the .cabal file
Ian Lynagh's avatar
Ian Lynagh committed
525
                  -> LocalBuildInfo -- ^Configuration information
526
                  -> ComponentLocalBuildInfo
527
                  -> Verbosity -- ^The verbosity to use
Ian Lynagh's avatar
Ian Lynagh committed
528
                  -> IO ()
529
initialBuildSteps _distPref pkg_descr lbi clbi verbosity = do
ijones's avatar
ijones committed
530
  -- check that there's something to build
531
  unless (not . null $ allBuildInfo pkg_descr) $ do
532
    let name = display (packageId pkg_descr)
533
534
    die $ "No libraries, executables, tests, or benchmarks "
       ++ "are enabled for package " ++ name ++ "."
ijones's avatar
ijones committed
535

536
  createDirectoryIfMissingVerbose verbosity True (componentBuildDir lbi clbi)
537

538
  writeAutogenFiles verbosity pkg_descr lbi clbi
539

540
541
542
543
544
-- | Generate and write out the Paths_<pkg>.hs and cabal_macros.h files
--
writeAutogenFiles :: Verbosity
                  -> PackageDescription
                  -> LocalBuildInfo
545
                  -> ComponentLocalBuildInfo
546
                  -> IO ()
547
548
writeAutogenFiles verbosity pkg lbi clbi = do
  createDirectoryIfMissingVerbose verbosity True (autogenModulesDir lbi clbi)
549

550
  let pathsModulePath = autogenModulesDir lbi clbi
551
                    </> ModuleName.toFilePath (autogenModuleName pkg) <.> "hs"
552
  rewriteFile pathsModulePath (Build.PathsModule.generate pkg lbi clbi)
553

554
555
  let cppHeaderPath = autogenModulesDir lbi clbi </> cppHeaderName
  rewriteFile cppHeaderPath (Build.Macros.generate pkg lbi clbi)