|
1
|
|
-{-# LANGUAGE TypeFamilies #-}
|
|
2
|
|
-
|
|
3
|
|
-module Rules.Libffi (
|
|
4
|
|
- LibffiDynLibs(..),
|
|
5
|
|
- needLibffi, askLibffilDynLibs, libffiRules, libffiLibrary, libffiHeaderFiles,
|
|
6
|
|
- libffiHeaderDir, libffiSystemHeaderDir, libffiName
|
|
7
|
|
- ) where
|
|
8
|
|
-
|
|
9
|
|
-import Hadrian.Utilities
|
|
10
|
|
-
|
|
11
|
|
-import Packages
|
|
12
|
|
-import Settings.Builders.Common
|
|
13
|
|
-import Target
|
|
14
|
|
-import Utilities
|
|
15
|
|
-import GHC.Toolchain (targetPlatformTriple)
|
|
16
|
|
-
|
|
17
|
|
-{- Note [Libffi indicating inputs]
|
|
18
|
|
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
19
|
|
-First see https://gitlab.haskell.org/ghc/ghc/wikis/Developing-Hadrian for an
|
|
20
|
|
-explanation of "indicating input". Part of the definition is copied here for
|
|
21
|
|
-your convenience:
|
|
22
|
|
-
|
|
23
|
|
- change in the vital output -> change in the indicating inputs
|
|
24
|
|
-
|
|
25
|
|
-In the case of building libffi `vital output = built libffi library files` and
|
|
26
|
|
-we can consider the libffi archive file (i.e. the "libffi-tarballs/libffi*.tar.gz"
|
|
27
|
|
-file) to be the only indicating input besides the build tools (e.g. make).
|
|
28
|
|
-Note building libffi is split into a few rules, but we also expect that:
|
|
29
|
|
-
|
|
30
|
|
- no change in the archive file -> no change in the intermediate build artifacts
|
|
31
|
|
-
|
|
32
|
|
-and so the archive file is still a valid choice of indicating input for
|
|
33
|
|
-all libffi rules. Hence we can get away with `need`ing only the archive file and
|
|
34
|
|
-don't have to `need` intermediate build artifacts (besides those to trigger
|
|
35
|
|
-dependant libffi rules i.e. to generate vital inputs as is noted on the wiki).
|
|
36
|
|
-It is then safe to `trackAllow` the libffi build directory as is done in
|
|
37
|
|
-`needLibfffiArchive`.
|
|
38
|
|
-
|
|
39
|
|
-A disadvantage to this approach is that changing the archive file forces a clean
|
|
40
|
|
-build of libffi i.e. we cannot incrementally build libffi. This seems like a
|
|
41
|
|
-performance issue, but is justified as building libffi is fast and the archive
|
|
42
|
|
-file is rarely changed.
|
|
43
|
|
-
|
|
44
|
|
--}
|
|
45
|
|
-
|
|
46
|
|
--- | Oracle question type. The oracle returns the list of dynamic
|
|
47
|
|
--- libffi library file paths (all but one of which should be symlinks).
|
|
48
|
|
-newtype LibffiDynLibs = LibffiDynLibs Stage
|
|
49
|
|
- deriving (Eq, Show, Hashable, Binary, NFData)
|
|
50
|
|
-type instance RuleResult LibffiDynLibs = [FilePath]
|
|
51
|
|
-
|
|
52
|
|
-askLibffilDynLibs :: Stage -> Action [FilePath]
|
|
53
|
|
-askLibffilDynLibs stage = askOracle (LibffiDynLibs stage)
|
|
54
|
|
-
|
|
55
|
|
--- | The path to the dynamic library manifest file. The file contains all file
|
|
56
|
|
--- paths to libffi dynamic library file paths.
|
|
57
|
|
--- The path is calculated but not `need`ed.
|
|
58
|
|
-dynLibManifest' :: Monad m => m FilePath -> Stage -> m FilePath
|
|
59
|
|
-dynLibManifest' getRoot stage = do
|
|
60
|
|
- root <- getRoot
|
|
61
|
|
- return $ root -/- stageString stage -/- pkgName libffi -/- ".dynamiclibs"
|
|
62
|
|
-
|
|
63
|
|
-dynLibManifestRules :: Stage -> Rules FilePath
|
|
64
|
|
-dynLibManifestRules = dynLibManifest' buildRootRules
|
|
65
|
|
-
|
|
66
|
|
-dynLibManifest :: Stage -> Action FilePath
|
|
67
|
|
-dynLibManifest = dynLibManifest' buildRoot
|
|
68
|
|
-
|
|
69
|
|
--- | Need the (locally built) libffi library.
|
|
70
|
|
-needLibffi :: Stage -> Action ()
|
|
71
|
|
-needLibffi stage = do
|
|
72
|
|
- jsTarget <- isJsTarget stage
|
|
73
|
|
- unless jsTarget $ do
|
|
74
|
|
- manifest <- dynLibManifest stage
|
|
75
|
|
- need [manifest]
|
|
76
|
|
-
|
|
77
|
|
--- | Context for @libffi@.
|
|
78
|
|
-libffiContext :: Stage -> Action Context
|
|
79
|
|
-libffiContext stage = do
|
|
80
|
|
- ways <- interpretInContext
|
|
81
|
|
- (Context stage libffi (error "libffiContext: way not set") (error "libffiContext: iplace not set"))
|
|
82
|
|
- getLibraryWays
|
|
83
|
|
- return $ (\w -> Context stage libffi w Final) (if any (wayUnit Dynamic) ways
|
|
84
|
|
- then dynamic
|
|
85
|
|
- else vanilla)
|
|
86
|
|
-
|
|
87
|
|
--- | The name of the library
|
|
88
|
|
-libffiName :: Expr String
|
|
89
|
|
-libffiName = do
|
|
90
|
|
- useSystemFfi <- succStaged (buildFlag UseSystemFfi)
|
|
91
|
|
- if useSystemFfi
|
|
92
|
|
- then pure "ffi"
|
|
93
|
|
- else libffiLocalName Nothing
|
|
94
|
|
-
|
|
95
|
|
--- | The name of the (locally built) library
|
|
96
|
|
-libffiLocalName :: Maybe Bool -> Expr String
|
|
97
|
|
-libffiLocalName force_dynamic = do
|
|
98
|
|
- way <- getWay
|
|
99
|
|
- stage <- getStage
|
|
100
|
|
- winTarget <- expr (isWinTarget stage)
|
|
101
|
|
- let dynamic = fromMaybe (Dynamic `wayUnit` way) force_dynamic
|
|
102
|
|
- pure $ mconcat
|
|
103
|
|
- [ if dynamic then "" else "C"
|
|
104
|
|
- , if winTarget then "ffi-6" else "ffi"
|
|
105
|
|
- ]
|
|
106
|
|
-
|
|
107
|
|
-libffiLibrary :: FilePath
|
|
108
|
|
-libffiLibrary = "inst/lib/libffi.a"
|
|
109
|
|
-
|
|
110
|
|
--- | These are the headers that we must package with GHC since foreign export
|
|
111
|
|
--- adjustor code produced by GHC depends upon them.
|
|
112
|
|
--- See Note [Packaging libffi headers] in GHC.Driver.CodeOutput.
|
|
113
|
|
-libffiHeaderFiles :: [FilePath]
|
|
114
|
|
-libffiHeaderFiles = ["ffi.h", "ffitarget.h"]
|
|
115
|
|
-
|
|
116
|
|
-libffiHeaderDir :: Stage -> Action FilePath
|
|
117
|
|
-libffiHeaderDir stage = do
|
|
118
|
|
- path <- libffiBuildPath stage
|
|
119
|
|
- return $ path -/- "inst/include"
|
|
120
|
|
-
|
|
121
|
|
-libffiSystemHeaderDir :: Stage -> Action FilePath
|
|
122
|
|
-libffiSystemHeaderDir = buildSetting FfiIncludeDir
|
|
123
|
|
-
|
|
124
|
|
-fixLibffiMakefile :: FilePath -> String -> String
|
|
125
|
|
-fixLibffiMakefile top =
|
|
126
|
|
- replace "-MD" "-MMD"
|
|
127
|
|
- . replace "@toolexeclibdir@" "$(libdir)"
|
|
128
|
|
- . replace "@INSTALL@" ("$(subst ../install-sh," ++ top ++ "/install-sh,@INSTALL@)")
|
|
129
|
|
-
|
|
130
|
|
--- TODO: check code duplication w.r.t. ConfCcArgs
|
|
131
|
|
-configureEnvironment :: Stage -> Action [CmdOption]
|
|
132
|
|
-configureEnvironment stage = do
|
|
133
|
|
- context <- libffiContext stage
|
|
134
|
|
- cFlags <- interpretInContext context getStagedCCFlags
|
|
135
|
|
- isCross <- flag CrossCompiling
|
|
136
|
|
- sequence $ [ builderEnvironment "CC" $ Cc CompileC stage
|
|
137
|
|
- , builderEnvironment "CXX" $ Cc CompileC stage
|
|
138
|
|
- , builderEnvironment "AR" $ Ar Unpack stage
|
|
139
|
|
- , builderEnvironment "NM" $ Nm stage
|
|
140
|
|
- , builderEnvironment "RANLIB" $ Ranlib stage
|
|
141
|
|
- , return . AddEnv "CFLAGS" $ unwords cFlags ++ " -w"
|
|
142
|
|
- , return . AddEnv "LDFLAGS" $ "-w" ] ++
|
|
143
|
|
- -- When we're building a cross-compiler, we have to be careful
|
|
144
|
|
- -- which environment variables are propagated for the non-target
|
|
145
|
|
- -- stages.
|
|
146
|
|
- (if isHostStage stage && isCross then [remBuilderEnvironment "LD"] else [])
|
|
147
|
|
-
|
|
148
|
|
--- Need the libffi archive and `trackAllow` all files in the build directory.
|
|
149
|
|
--- See [Libffi indicating inputs].
|
|
150
|
|
-needLibfffiArchive :: FilePath -> Action FilePath
|
|
151
|
|
-needLibfffiArchive buildPath = do
|
|
152
|
|
- top <- topDirectory
|
|
153
|
|
- tarball <- unifyPath
|
|
154
|
|
- . fromSingleton "Exactly one LibFFI tarball is expected"
|
|
155
|
|
- <$> getDirectoryFiles top ["libffi-tarballs/libffi*.tar.gz"]
|
|
156
|
|
- need [top -/- tarball]
|
|
157
|
|
- trackAllow [buildPath -/- "**"]
|
|
158
|
|
- return tarball
|
|
159
|
|
-
|
|
160
|
|
-libffiRules :: Rules ()
|
|
161
|
|
-libffiRules = do
|
|
162
|
|
- _ <- addOracleCache $ \ (LibffiDynLibs stage)
|
|
163
|
|
- -> do
|
|
164
|
|
- jsTarget <- isJsTarget stage
|
|
165
|
|
- if jsTarget
|
|
166
|
|
- then return []
|
|
167
|
|
- else readFileLines =<< dynLibManifest stage
|
|
168
|
|
- forM_ [Stage1, Stage2, Stage3] $ \stage -> do
|
|
169
|
|
- root <- buildRootRules
|
|
170
|
|
- let path = root -/- stageString stage
|
|
171
|
|
- libffiPath = path -/- pkgName libffi -/- "build"
|
|
172
|
|
-
|
|
173
|
|
- -- We set a higher priority because this rule overlaps with the build rule
|
|
174
|
|
- -- for static libraries 'Rules.Library.libraryRules'.
|
|
175
|
|
- dynLibMan <- dynLibManifestRules stage
|
|
176
|
|
- let topLevelTargets = [ libffiPath -/- libffiLibrary
|
|
177
|
|
- , dynLibMan
|
|
178
|
|
- ]
|
|
179
|
|
- priority 2 $ topLevelTargets &%> \_ -> do
|
|
180
|
|
- _ <- needLibfffiArchive libffiPath
|
|
181
|
|
- context <- libffiContext stage
|
|
182
|
|
-
|
|
183
|
|
- -- Note this build needs the Makefile, triggering the rules bellow.
|
|
184
|
|
- build $ target context (Make libffiPath) [] []
|
|
185
|
|
- libffiName' <- interpretInContext context (libffiLocalName (Just True))
|
|
186
|
|
-
|
|
187
|
|
- -- Produces all install files.
|
|
188
|
|
- produces =<< (\\ topLevelTargets)
|
|
189
|
|
- <$> liftIO (getDirectoryFilesIO "." [libffiPath -/- "inst//*"])
|
|
190
|
|
-
|
|
191
|
|
- -- Find dynamic libraries.
|
|
192
|
|
- osxTarget <- isOsxTarget stage
|
|
193
|
|
- winTarget <- isWinTarget stage
|
|
194
|
|
-
|
|
195
|
|
- dynLibFiles <- do
|
|
196
|
|
- let libfilesDir = libffiPath -/-
|
|
197
|
|
- (if winTarget then "inst" -/- "bin" else "inst" -/- "lib")
|
|
198
|
|
- dynlibext
|
|
199
|
|
- | winTarget = "dll"
|
|
200
|
|
- | osxTarget = "dylib"
|
|
201
|
|
- | otherwise = "so"
|
|
202
|
|
- filepat = "lib" ++ libffiName' ++ "." ++ dynlibext ++ "*"
|
|
203
|
|
- liftIO $ getDirectoryFilesIO "." [libfilesDir -/- filepat]
|
|
204
|
|
-
|
|
205
|
|
- writeFileLines dynLibMan dynLibFiles
|
|
206
|
|
- putSuccess "| Successfully build libffi."
|
|
207
|
|
-
|
|
208
|
|
- fmap (libffiPath -/-) ( "Makefile.in" :& "configure" :& Nil ) &%>
|
|
209
|
|
- \ ( mkIn :& _ ) -> do
|
|
210
|
|
- -- Extract libffi tar file
|
|
211
|
|
- context <- libffiContext stage
|
|
212
|
|
- removeDirectory libffiPath
|
|
213
|
|
- tarball <- needLibfffiArchive libffiPath
|
|
214
|
|
- -- Go from 'libffi-3.99999+git20171002+77e130c.tar.gz' to 'libffi-3.99999'
|
|
215
|
|
- let libname = takeWhile (/= '+') $ fromJust $ stripExtension "tar.gz" $ takeFileName tarball
|
|
216
|
|
-
|
|
217
|
|
- -- Move extracted directory to libffiPath.
|
|
218
|
|
- root <- buildRoot
|
|
219
|
|
- removeDirectory (root -/- libname)
|
|
220
|
|
- actionFinally (do
|
|
221
|
|
- build $ target context (Tar Extract) [tarball] [path]
|
|
222
|
|
- moveDirectory (path -/- libname) libffiPath) $
|
|
223
|
|
- -- And finally:
|
|
224
|
|
- removeFiles (path) [libname -/- "**"]
|
|
225
|
|
-
|
|
226
|
|
- top <- topDirectory
|
|
227
|
|
- fixFile mkIn (fixLibffiMakefile top)
|
|
228
|
|
-
|
|
229
|
|
- files <- liftIO $ getDirectoryFilesIO "." [libffiPath -/- "**"]
|
|
230
|
|
- produces files
|
|
231
|
|
-
|
|
232
|
|
- fmap (libffiPath -/-) ("Makefile" :& "config.guess" :& "config.sub" :& Nil)
|
|
233
|
|
- &%> \( mk :& _ ) -> do
|
|
234
|
|
- _ <- needLibfffiArchive libffiPath
|
|
235
|
|
- context <- libffiContext stage
|
|
236
|
|
-
|
|
237
|
|
- -- This need rule extracts the libffi tar file to libffiPath.
|
|
238
|
|
- need [mk <.> "in"]
|
|
239
|
|
-
|
|
240
|
|
- -- Configure.
|
|
241
|
|
- forM_ ["config.guess", "config.sub"] $ \file -> do
|
|
242
|
|
- copyFile file (libffiPath -/- file)
|
|
243
|
|
- env <- configureEnvironment stage
|
|
244
|
|
- buildWithCmdOptions env $
|
|
245
|
|
- target context (Configure libffiPath) [mk <.> "in"] [mk]
|
|
246
|
|
-
|
|
247
|
|
- dir <- queryBuildTarget targetPlatformTriple
|
|
248
|
|
- files <- liftIO $ getDirectoryFilesIO "." [libffiPath -/- dir -/- "**"]
|
|
249
|
|
- produces files |