GhcCabal.hs 5.31 KB
Newer Older
1
module Settings.GhcCabal (
2
    cabalArgs, bootPackageDbArgs, customPackageArgs
3
4
    ) where

5
import Way
6
import Base
Andrey Mokhov's avatar
Andrey Mokhov committed
7
8
import Builder
import Package
9
import Util
10
import Expression
11
12
import Switches
import Oracles.Base
13
import Settings.User
14
15
16
import Settings.Ways
import Settings.Util
import Settings.Packages
17
import Settings.TargetDirectory
18
import Data.List
19

20
21
cabalArgs :: Args
cabalArgs = builder GhcCabal ? do
22
23
24
    stage <- asks getStage
    pkg   <- asks getPackage
    mconcat [ arg "configure"
25
26
            , argPath $ pkgPath pkg
            , argPath $ targetDirectory stage pkg
27
            , dllArgs
Andrey Mokhov's avatar
Andrey Mokhov committed
28
29
            , with $ Ghc stage
            , with $ GhcPkg stage
30
31
            , stage0 ? bootPackageDbArgs
            , libraryArgs
Andrey Mokhov's avatar
Andrey Mokhov committed
32
            , configKeyNonEmpty "hscolour" ? with HsColour
33
            , configureArgs
34
            , stage0 ? packageConstraints
Andrey Mokhov's avatar
Andrey Mokhov committed
35
36
37
38
39
            , with $ Gcc stage
            , notStage Stage0 ? with Ld
            , with Ar
            , with Alex
            , with Happy ]
40
41

-- TODO: Isn't vanilla always built? If yes, some conditions are redundant.
42
43
libraryArgs :: Args
libraryArgs = do
44
    ways            <- fromDiffExpr Settings.Ways.ways
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    ghcInterpreter  <- ghcWithInterpreter
    dynamicPrograms <- dynamicGhcPrograms
    append [ if vanilla `elem` ways
             then  "--enable-library-vanilla"
             else "--disable-library-vanilla"
           , if vanilla `elem` ways && ghcInterpreter && not dynamicPrograms
             then  "--enable-library-for-ghci"
             else "--disable-library-for-ghci"
           , if profiling `elem` ways
             then  "--enable-library-profiling"
             else "--disable-library-profiling"
           , if dynamic `elem` ways
             then  "--enable-shared"
             else "--disable-shared" ]

60
61
configureArgs :: Args
configureArgs = do
62
    stage <- asks getStage
Andrey Mokhov's avatar
Andrey Mokhov committed
63
    let conf key = appendSubD $ "--configure-option=" ++ key
64
        cFlags   = mconcat [ ccArgs
Andrey Mokhov's avatar
Andrey Mokhov committed
65
66
                           , remove ["-Werror"]
                           , argStagedConfig "conf-cc-args" ]
67
68
        ldFlags  = ldArgs <> argStagedConfig "conf-gcc-linker-args"
        cppFlags = cppArgs <> argStagedConfig "conf-cpp-args"
69
    mconcat
Andrey Mokhov's avatar
Andrey Mokhov committed
70
71
72
73
        [ conf "CFLAGS"   cFlags
        , conf "LDFLAGS"  ldFlags
        , conf "CPPFLAGS" cppFlags
        , appendSubD "--gcc-options" $ cFlags <> ldFlags
74
75
76
77
78
79
        , conf "--with-iconv-includes"  $ argConfig "iconv-include-dirs"
        , conf "--with-iconv-libraries" $ argConfig "iconv-lib-dirs"
        , conf "--with-gmp-includes"    $ argConfig "gmp-include-dirs"
        , conf "--with-gmp-libraries"   $ argConfig "gmp-lib-dirs"
        -- TODO: why TargetPlatformFull and not host?
        , crossCompiling ? (conf "--host" $ argConfig "target-platform-full")
Andrey Mokhov's avatar
Andrey Mokhov committed
80
        , conf "--with-cc" . argM . builderPath $ Gcc stage ]
81

82
83
bootPackageDbArgs :: Args
bootPackageDbArgs = do
84
85
86
    sourcePath <- lift $ askConfig "ghc-source-path"
    arg $ "--package-db=" ++ sourcePath </> "libraries/bootstrapping.conf"

Andrey Mokhov's avatar
Andrey Mokhov committed
87
88
89
-- This is a positional argument, hence:
-- * if it is empty, we need to emit one empty string argument;
-- * otherwise, we must collapse it into one space-separated string.
90
91
dllArgs :: Args
dllArgs = arg ""
92

93
packageConstraints :: Args
94
packageConstraints = do
95
    pkgs <- fromDiffExpr packages
96
97
98
99
100
101
102
103
104
105
    constraints <- lift $ forM pkgs $ \pkg -> do
        let cabal  = pkgPath pkg </> pkgCabal pkg
            prefix = dropExtension (pkgCabal pkg) ++ " == "
        need [cabal]
        content <- lines <$> liftIO (readFile cabal)
        let vs = filter (("ersion:" `isPrefixOf`) . drop 1) content
        case vs of
            [v] -> return $ prefix ++ dropWhile (not . isDigit) v
            _   -> redError $ "Cannot determine package version in '"
                            ++ cabal ++ "'."
106
    append $ concatMap (\c -> ["--constraint", c]) $ constraints
107

108
-- TODO: should be in a different file
Andrey Mokhov's avatar
Andrey Mokhov committed
109
-- TODO: put all validating options together in one file
110
111
ccArgs :: Args
ccArgs = validating ? do
112
    let gccGe46 = liftM not gccLt46
113
    mconcat [ arg "-Werror"
114
115
116
117
118
119
            , arg "-Wall"
            , gccIsClang ??
              ( arg "-Wno-unknown-pragmas" <>
                gccGe46 ? windowsHost ? arg "-Werror=unused-but-set-variable"
              , gccGe46 ? arg "-Wno-error=inline" )]

120
121
ldArgs :: Args
ldArgs = mempty
122

123
124
cppArgs :: Args
cppArgs = mempty
125

126
127
customPackageArgs :: Args
customPackageArgs = mconcat
128
129
130
    [ package integerGmp2 ?
      mconcat [ windowsHost ? builder GhcCabal ?
                arg "--configure-option=--with-intree-gmp"
131
              , appendCcArgs ["-Ilibraries/integer-gmp2/gmp"] ]
132
133
134
135
136
137

    , package base ?
      builder GhcCabal ? arg ("--flags=" ++ pkgName integerLibrary)

    , package ghcPrim ?
      builder GhcCabal ? arg "--flag=include-ghc-prim" ]
Andrey Mokhov's avatar
Andrey Mokhov committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

withBuilderKey :: Builder -> String
withBuilderKey builder = case builder of
    Ar       -> "--with-ar="
    Ld       -> "--with-ld="
    Gcc _    -> "--with-gcc="
    Ghc _    -> "--with-ghc="
    Alex     -> "--with-alex="
    Happy    -> "--with-happy="
    GhcPkg _ -> "--with-ghc-pkg="
    HsColour -> "--with-hscolour="
    _        -> error "withBuilderKey: not supported builder"

-- Expression 'with Gcc' appends "--with-gcc=/path/to/gcc" and needs Gcc.
with :: Builder -> Args
with builder = do
    path <- lift $ builderPath builder
    lift $ needBuilder builder
    append [withBuilderKey builder ++ path]