Base.hs 5.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
{-# LANGUAGE NoImplicitPrelude #-}
module Package.Base (
    module Base,
    module Ways,
    module Util,
    module Oracles,
    Package (..), Settings (..), TodoItem (..),
    defaultSettings, libraryPackage,
    commonCcArgs, commonLdArgs, commonCppArgs, commonCcWarninigArgs,
10
    bootPkgConstraints,
11
12
    pathArgs, packageArgs, includeArgs, pkgHsSources, 
    pkgDepObjects, pkgLibObjects
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    ) where

import Base
import Ways
import Util
import Oracles

data Settings = Settings
     {
         customConfArgs  :: Args,
         customCcArgs    :: Args,
         customLdArgs    :: Args,
         customCppArgs   :: Args,
         customDllArgs   :: Args,
         registerPackage :: Bool,
         ways            :: Action [Way]
     }

defaultSettings :: Stage -> Settings
32
33
defaultSettings stage =
    Settings mempty mempty mempty mempty mempty True (defaultWays stage)
34
35
36

-- Stage is the stage of the GHC that we use to build the package
-- FilePath is the directory to put the build results
37
38
39
-- Settings may be different for different combinations of Stage & FilePath
type TodoItem = (Stage, FilePath, Settings)

40
41
42
-- pkgPath is the path to the source code relative to the root
data Package = Package
     {
43
44
45
         pkgName :: String,    -- For example: "deepseq"
         pkgPath :: FilePath,  -- "libraries/deepseq"
         pkgTodo :: [TodoItem] -- [(Stage1, "dist-install", defaultSettings)]
46
47
48
49
50
51
     }

libraryPackage :: String -> Stage -> (Stage -> Settings) -> Package
libraryPackage name stage settings =
    Package
        name
Andrey Mokhov's avatar
Andrey Mokhov committed
52
        (toStandard $ "libraries" </> name)
53
54
55
56
57
58
59
        [(
            stage,
            if stage == Stage0 then "dist-boot" else "dist-install",
            settings stage
        )]

commonCcArgs :: Args
Andrey Mokhov's avatar
Andrey Mokhov committed
60
commonCcArgs = when Validating $ arg ["-Werror", "-Wall"]
61
62
63
64
65
66
67

commonLdArgs :: Args
commonLdArgs = mempty -- TODO: Why empty? Perhaps drop it altogether?

commonCppArgs :: Args
commonCppArgs = mempty -- TODO: Why empty? Perhaps drop it altogether?

68
-- TODO: simplify
69
commonCcWarninigArgs :: Args
Andrey Mokhov's avatar
Andrey Mokhov committed
70
commonCcWarninigArgs = when Validating $
71
72
73
74
       GccIsClang <?> arg "-Wno-unknown-pragmas" 
    <> (not GccIsClang && not GccLt46) <?> arg "-Wno-error=inline" 
    <> (GccIsClang && not GccLt46 && windowsHost) <?>
       arg "-Werror=unused-but-set-variable"
75
76
77
78
79

bootPkgConstraints :: Args
bootPkgConstraints = mempty

-- TODO: implement bootPkgConstraints oracle
80
81
82
83
84
-- BOOT_PKG_CONSTRAINTS := \
-- $(foreach d,$(PACKAGES_STAGE0),\
--  $(foreach p,$(basename $(notdir $(wildcard libraries/$d/*.cabal))),\
--   --constraint "$p == $(shell grep -i "^Version:" libraries/$d/$p.cabal |
--     sed "s/[^0-9.]//g")"))
85

86
pathArgs :: ShowArgs a => String -> FilePath -> a -> Args
87
88
pathArgs key path as =
    map (\a -> key ++ toStandard (normaliseEx $ path </> a)) <$> arg as
89

90
91
92
93
packageArgs :: Stage -> FilePath -> Args
packageArgs stage pkgData = do
    usePackageKey <- SupportsPackageKey || stage /= Stage0
    arg ["-hide-all-packages", "-no-user-package-db", "-include-pkg-deps"]
94
        <> (stage == Stage0) <?> arg "-package-db libraries/bootstrapping.conf"
95
96
        <> keyArgs usePackageKey
  where
97
98
99
100
    keyArgs True  = productArgs "-this-package-key" (PackageKey pkgData) <>
                    productArgs "-package-key"      (DepKeys    pkgData)
    keyArgs False = productArgs "-package-name"     (PackageKey pkgData) <>
                    productArgs "-package"          (Deps       pkgData)
101

102
103
includeArgs :: FilePath -> FilePath -> Args
includeArgs path dist = 
104
105
    let pkgData  = toStandard $ path </> dist </> "package-data.mk"
        buildDir = toStandard $ path </> dist </> "build"
106
107
    in arg "-i"
    <> pathArgs "-i" path     (SrcDirs pkgData)
108
    <> concatArgs ["-i", "-I"] [buildDir, toStandard $ buildDir </> "autogen"]
109
110
    <> pathArgs "-I" path     (IncludeDirs pkgData)
    <> arg "-optP-include" -- TODO: Shall we also add -cpp?
111
    <> concatArgs "-optP" (toStandard $ buildDir </> "autogen/cabal_macros.h")
112

113
114
115
116
117
118
119
120
121
122
123
124
pkgHsSources :: FilePath -> FilePath -> Action [FilePath]
pkgHsSources path dist = do
    let pkgData = path </> dist </> "package-data.mk"
    dirs <- map (path </>) <$> arg (SrcDirs pkgData)
    findModuleFiles pkgData dirs [".hs", ".lhs"]

-- Find objects we depend on (we don't want to depend on split objects)
-- TODO: look for non-hs objects too 
pkgDepObjects :: FilePath -> FilePath -> Way -> Action [FilePath]
pkgDepObjects path dist way = do
    let pkgData  = path </> dist </> "package-data.mk"
        buildDir = path </> dist </> "build"
125
126
127
128
    dirs <- map (normaliseEx . (path </>)) <$> arg (SrcDirs pkgData)
    fmap concat $ forM dirs $ \d -> 
        map (toStandard . (buildDir ++) . (-<.> osuf way) . drop (length d))
        <$> (findModuleFiles pkgData [d] [".hs", ".lhs"])
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

-- Find objects that go to library
pkgLibObjects :: FilePath -> FilePath -> Stage -> Way -> Action [FilePath]
pkgLibObjects path dist stage way = do
    let pkgData  = path </> dist </> "package-data.mk"
        buildDir = path </> dist </> "build"
    split <- splitObjects stage
    if split
    then do
         let suffixes = ["_" ++ osuf way ++ "_split//*"]
         findModuleFiles pkgData [buildDir] suffixes
    else pkgDepObjects path dist way

findModuleFiles :: FilePath -> [FilePath] -> [String] -> Action [FilePath]
findModuleFiles pkgData directories suffixes = do
    mods  <- arg (Modules pkgData)
    files <- getDirectoryFiles "" $ do
        dir     <- directories
        modPath <- map (replaceEq '.' pathSeparator) mods
        suffix  <- suffixes
        return $ dir </> modPath ++ suffix
Andrey Mokhov's avatar
Andrey Mokhov committed
150
    return $ map (toStandard . normaliseEx) files