7937319ed70c1923e58689a98b98bbe853f4e450
[hadrian.git] / src / Builder.hs
1 {-# LANGUAGE DeriveGeneric, LambdaCase #-}
2 module Builder (
3 CcMode (..), GhcMode (..), GhcPkgMode (..), Builder (..),
4 trackedArgument, isOptional
5 ) where
6
7 import Data.Char
8 import GHC.Generics
9
10 import Base
11 import Stage
12
13 -- | A compiler can typically be used in different modes:
14 -- * Compiling or preprocessing a source file;
15 -- * Extracting source dependencies, e.g. by passing @-M@ command line argument;
16 -- * Linking object files & static libraries into an executable.
17 -- We have CcMode for C compiler and GhcMode for GHC.
18 data CcMode = CompileC | FindCDependencies deriving (Eq, Generic, Show)
19 data GhcMode = CompileHs | FindHsDependencies | LinkHs
20 deriving (Eq, Generic, Show)
21
22 -- | GhcPkg can initialise a package database and register packages in it.
23 data GhcPkgMode = Init | Update deriving (Eq, Generic, Show)
24
25 -- | A 'Builder' is an external command invoked in a separate process via 'cmd'.
26 -- @Ghc Stage0@ is the bootstrapping compiler.
27 -- @Ghc StageN@, N > 0, is the one built in stage (N - 1).
28 -- @GhcPkg Stage0@ is the bootstrapping @GhcPkg@.
29 -- @GhcPkg Stage1@ is the one built in Stage0.
30 data Builder = Alex
31 | Ar Stage
32 | DeriveConstants
33 | Cc CcMode Stage
34 | Configure FilePath
35 | GenApply
36 | GenPrimopCode
37 | Ghc GhcMode Stage
38 | GhcCabal
39 | GhcCabalHsColour -- synonym for 'GhcCabal hscolour'
40 | GhcPkg GhcPkgMode Stage
41 | Haddock
42 | Happy
43 | Hpc
44 | HsColour
45 | HsCpp
46 | Hsc2Hs
47 | Ld
48 | Make FilePath
49 | Nm
50 | Objdump
51 | Patch
52 | Perl
53 | Ranlib
54 | Tar
55 | Unlit
56 deriving (Eq, Generic, Show)
57
58 -- TODO: Some builders are required only on certain platforms. For example,
59 -- Objdump is only required on OpenBSD and AIX, as mentioned in #211. Add
60 -- support for platform-specific optional builders as soon as we can reliably
61 -- test this feature.
62 isOptional :: Builder -> Bool
63 isOptional = \case
64 HsColour -> True
65 Objdump -> True
66 _ -> False
67
68 -- | Some arguments do not affect build results and therefore do not need to be
69 -- tracked by the build system. A notable example is "-jN" that controls Make's
70 -- parallelism. Given a 'Builder' and an argument, this function should return
71 -- 'True' only if the argument needs to be tracked.
72 trackedArgument :: Builder -> String -> Bool
73 trackedArgument (Make _) = not . threadArg
74 trackedArgument _ = const True
75
76 threadArg :: String -> Bool
77 threadArg s = dropWhileEnd isDigit s `elem` ["-j", "MAKEFLAGS=-j", "THREADS="]
78
79 instance Binary Builder
80 instance Hashable Builder
81 instance NFData Builder
82
83 instance Binary CcMode
84 instance Hashable CcMode
85 instance NFData CcMode
86
87 instance Binary GhcMode
88 instance Hashable GhcMode
89 instance NFData GhcMode
90
91 instance Binary GhcPkgMode
92 instance Hashable GhcPkgMode
93 instance NFData GhcPkgMode