summaryrefslogtreecommitdiff
path: root/hadrian/src/Rules/BinaryDist.hs
blob: c02d9787f9e25372d6b8fc4e85a98f5607738a5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
module Rules.BinaryDist where

import Hadrian.Haskell.Cabal

import Context
import Expression
import Oracles.Setting
import Packages
import Settings
import Target
import Utilities

{-
Note [Binary distributions]
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hadrian produces binary distributions under:
  <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>.tar.xz

It is generated by creating an archive from:
  <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>/

It does so by following the steps below.

- make sure we have a complete stage 2 compiler + haddock

- copy the bin and lib directories of the compiler we built:
    <build root>/stage1/{bin, lib}
  to
    <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>/{bin, lib}

- copy the generated docs (user guide, haddocks, etc):
    <build root>/docs/
  to
    <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>/docs/

- copy haddock (built by our stage2 compiler):
    <build root>/stage2/bin/haddock
  to
    <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>/bin/haddock

- use autoreconf to generate a `configure` script from
  aclocal.m4 and distrib/configure.ac, that we move to:
    <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>/configure

- write a (fixed) Makefile capable of supporting 'make install' to:
    <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>/Makefile

- write some (fixed) supporting bash code for the wrapper scripts to:
    <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>/wrappers/<program>

  where <program> is the name of the executable that the bash file will
  help wrapping.

- copy supporting configure/make related files
  (see @bindistInstallFiles@) to:
    <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>/<file>

- create a .tar.xz archive of the directory:
    <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>/
  at
    <build root>/bindist/ghc-<X>.<Y>.<Z>-<arch>-<os>.tar.xz


Note [Wrapper scripts and binary distributions]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Users of Linux, FreeBSD, Windows and OS X can unpack a
binary distribution produced by hadrian for their arch
and OS and start using @bin/ghc@, @bin/ghc-pkg@ and so on
right away, without even having to configure or install
the distribution. They would then be using the real executables
directly, not through wrapper scripts.

This works because GHCs produced by hadrian on those systems
are relocatable. This means that you can copy the @bin@ and @lib@
dirs anywhere and GHC will keep working, as long as both
directories sit next to each other. (This is achieved by having
GHC look up its $libdir relatively to where the GHC executable
resides.)

It is however still possible (and simple) to install a GHC
distribution that uses wrapper scripts. From the unpacked archive,
you can simply do:

  ./configure --prefix=<path> [... other configure options ...]
  make install

In order to support @bin@ and @lib@ directories that don't sit next to each
other, the install script:
   * installs programs into @LIBDIR/ghc-VERSION/bin@
   * installs libraries into @LIBDIR/ghc-VERSION/lib@
   * installs the wrappers scripts into @BINDIR@ directory

-}

bindistRules :: Rules ()
bindistRules = do
    root <- buildRootRules
    phony "binary-dist-dir" $ do
        -- We 'need' all binaries and libraries
        targets <- mapM pkgTarget =<< stagePackages Stage1
        need targets

        version        <- setting ProjectVersion
        targetPlatform <- setting TargetPlatformFull
        distDir        <- Context.distDir Stage1
        rtsDir         <- pkgIdentifier rts
        windows        <- windowsHost

        let ghcBuildDir      = root -/- stageString Stage1
            bindistFilesDir  = root -/- "bindist" -/- ghcVersionPretty
            ghcVersionPretty = "ghc-" ++ version ++ "-" ++ targetPlatform
            rtsIncludeDir    = ghcBuildDir -/- "lib" -/- distDir -/- rtsDir
                               -/- "include"

        -- We create the bindist directory at <root>/bindist/ghc-X.Y.Z-platform/
        -- and populate it with Stage2 build results
        createDirectory bindistFilesDir
        copyDirectory (ghcBuildDir -/- "bin") bindistFilesDir
        copyDirectory (ghcBuildDir -/- "lib") bindistFilesDir
        copyDirectory (rtsIncludeDir)         bindistFilesDir
        need ["docs"]
        -- TODO: we should only embed the docs that have been generated
        -- depending on the current settings (flavours' "ghcDocs" field and
        -- "--docs=.." command-line flag)
        -- Currently we embed the "docs" directory if it exists but it may
        -- contain outdated or even invalid data.
        whenM (doesDirectoryExist (root -/- "docs")) $ do
          copyDirectory (root -/- "docs") bindistFilesDir
        when windows $ do
          copyDirectory (root -/- "mingw") bindistFilesDir
          -- we use that opportunity to delete the .stamp file that we use
          -- as a proxy for the whole mingw toolchain, there's no point in
          -- shipping it
          removeFile (bindistFilesDir -/- mingwStamp)

        -- We copy the binary (<build root>/stage1/bin/haddock) to
        -- the bindist's bindir (<build root>/bindist/ghc-.../bin/).
        haddockPath <- programPath (vanillaContext Stage1 haddock)
        copyFile haddockPath (bindistFilesDir -/- "bin" -/- "haddock")

        -- We then 'need' all the files necessary to configure and install
        -- (as in, './configure [...] && make install') this build on some
        -- other machine.
        need $ map (bindistFilesDir -/-)
                   (["configure", "Makefile"] ++ bindistInstallFiles)
        need $ map ((bindistFilesDir -/- "wrappers") -/-) ["check-api-annotations"
                   , "check-ppr", "ghc", "ghc-iserv", "ghc-pkg"
                   , "ghci-script", "haddock", "hpc", "hp2ps", "hsc2hs"
                   , "runghc"]


    phony "binary-dist" $ do

        need ["binary-dist-dir"]

        version        <- setting ProjectVersion
        targetPlatform <- setting TargetPlatformFull

        let ghcVersionPretty = "ghc-" ++ version ++ "-" ++ targetPlatform

        -- Finally, we create the archive <root>/bindist/ghc-X.Y.Z-platform.tar.xz
        tarPath <- builderPath (Tar Create)
        cmd [Cwd $ root -/- "bindist"] tarPath
            [ "-c", "--xz", "-f"
            , ghcVersionPretty <.> "tar.xz"
            , ghcVersionPretty ]

    -- Prepare binary distribution configure script
    -- (generated under <ghc root>/distrib/configure by 'autoreconf')
    root -/- "bindist" -/- "ghc-*" -/- "configure" %> \configurePath -> do
        ghcRoot <- topDirectory
        copyFile (ghcRoot -/- "aclocal.m4") (ghcRoot -/- "distrib" -/- "aclocal.m4")
        buildWithCmdOptions [] $
            target (vanillaContext Stage1 ghc) (Autoreconf $ ghcRoot -/- "distrib") [] []
        -- We clean after ourselves, moving the configure script we generated in
        -- our bindist dir
        removeFile (ghcRoot -/- "distrib" -/- "aclocal.m4")
        moveFile (ghcRoot -/- "distrib" -/- "configure") configurePath

    -- Generate the Makefile that enables the "make install" part
    root -/- "bindist" -/- "ghc-*" -/- "Makefile" %> \makefilePath ->
        writeFile' makefilePath bindistMakefile

    root -/- "bindist" -/- "ghc-*" -/- "wrappers/*" %> \wrapperPath ->
        writeFile' wrapperPath $ wrapper (takeFileName wrapperPath)

    -- Copy various configure-related files needed for a working
    -- './configure [...] && make install' workflow
    -- (see the list of files needed in the 'binary-dist' rule above, before
    -- creating the archive).
    forM_ bindistInstallFiles $ \file ->
        root -/- "bindist" -/- "ghc-*" -/- file %> \dest -> do
            ghcRoot <- topDirectory
            copyFile (ghcRoot -/- fixup file) dest

  where
    fixup f | f `elem` ["INSTALL", "README"] = "distrib" -/- f
            | otherwise                      = f

-- | A list of files that allow us to support a simple
-- @./configure [...] && make install@ workflow.
bindistInstallFiles :: [FilePath]
bindistInstallFiles =
    [ "config.sub", "config.guess", "install-sh", "mk" -/- "config.mk.in"
    , "mk" -/- "install.mk.in", "mk" -/- "project.mk", "README"
    , "INSTALL" ]

-- | This auxiliary function gives us a top-level 'Filepath' that we can 'need'
-- for all libraries and programs that are needed for a complete build.
-- For libraries, it returns the path to the @.conf@ file in the package
-- database. For programs, it returns the path to the compiled executable.
pkgTarget :: Package -> Action FilePath
pkgTarget pkg
    | isLibrary pkg = pkgConfFile (vanillaContext Stage1 pkg)
    | otherwise     = programPath =<< programContext Stage1 pkg

-- TODO: Augment this Makefile to match the various parameters that the current
-- bindist scripts support.
-- | A trivial Makefile that only takes @$prefix@ into account, and not e.g
-- @$datadir@ (for docs) and other variables, yet.
bindistMakefile :: String
bindistMakefile = unlines
    [ "MAKEFLAGS += --no-builtin-rules"
    , ".SUFFIXES:"
    , ""
    , "include mk/install.mk"
    , "include mk/config.mk"
    , ""
    , ".PHONY: default"
    , "default:"
    , "\t@echo 'Run \"make install\" to install'"
    , "\t@false"
    , ""
    , "#-----------------------------------------------------------------------"
    , "# INSTALL RULES"
    , ""
    , "# Hacky function to check equality of two strings"
    , "# TODO : find if a better function exists"
    , "eq=$(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))"
    , ""
    , "define installscript"
    , "# $1 = package name"
    , "# $2 = wrapper path"
    , "# $3 = bindir"
    , "# $4 = ghcbindir"
    , "# $5 = Executable binary path"
    , "# $6 = Library Directory"
    , "# $7 = Docs Directory"
    , "# $8 = Includes Directory"
    , "# We are installing wrappers to programs by searching corresponding"
    , "# wrappers. If wrapper is not found, we are attaching the common wrapper"
    , "# to it. This implementation is a bit hacky and depends on consistency"
    , "# of program names. For hadrian build this will work as programs have a"
    , "# consistent naming procedure."
    , "\trm -f '$2'"
    , "\t$(CREATE_SCRIPT) '$2'"
    , "\t@echo \"#!$(SHELL)\" >>  '$2'"
    , "\t@echo \"exedir=\\\"$4\\\"\" >> '$2'"
    , "\t@echo \"exeprog=\\\"$1\\\"\" >> '$2'"
    , "\t@echo \"executablename=\\\"$5\\\"\" >> '$2'"
    , "\t@echo \"bindir=\\\"$3\\\"\" >> '$2'"
    , "\t@echo \"libdir=\\\"$6\\\"\" >> '$2'"
    , "\t@echo \"docdir=\\\"$7\\\"\" >> '$2'"
    , "\t@echo \"includedir=\\\"$8\\\"\" >> '$2'"
    , "\t@echo \"\" >> '$2'"
    , "\tcat wrappers/$1 >> '$2'"
    , "\t$(EXECUTABLE_FILE) '$2' ;"
    , "endef"
    , ""
    , "# Hacky function to patch up the 'haddock-interfaces' and 'haddock-html'"
    , "# fields in the package .conf files"
    , "define patchpackageconf"
    , "# $1 = package name (ex: 'bytestring')"
    , "# $2 = path to .conf file"
    , "# $3 = Docs Directory"
    , "\tcat '$2' | sed 's|haddock-interfaces.*|haddock-interfaces: $3/html/libraries/$1/$1.haddock|' \\"
    , "\t         | sed 's|haddock-html.*|haddock-html: $3/html/libraries/$1|' \\"
    , "\t       > '$2.copy'"
    , "\tmv '$2.copy' '$2'"
    , "endef"
    , ""
    , "# QUESTION : should we use shell commands?"
    , ""
    , ""
    , ".PHONY: install"
    , "install: install_lib install_bin install_includes"
    , "install: install_docs install_wrappers install_ghci"
    , "install: install_mingw update_package_db"
    , ""
    , "ActualBinsDir=${ghclibdir}/bin"
    , "ActualLibsDir=${ghclibdir}/lib"
    , "WrapperBinsDir=${bindir}"
    , ""
    , "# We need to install binaries relative to libraries."
    , "BINARIES = $(wildcard ./bin/*)"
    , "install_bin:"
    , "\t@echo \"Copying binaries to $(ActualBinsDir)\""
    , "\t$(INSTALL_DIR) \"$(ActualBinsDir)\""
    , "\tfor i in $(BINARIES); do \\"
    , "\t\tcp -R $$i \"$(ActualBinsDir)\"; \\"
    , "\tdone"
    , ""
    , "install_ghci:"
    , "\t@echo \"Installing ghci wrapper\""
    , "\t@echo \"#!$(SHELL)\" >  '$(WrapperBinsDir)/ghci'"
    , "\tcat wrappers/ghci-script >> '$(WrapperBinsDir)/ghci'"
    , "\t$(EXECUTABLE_FILE) '$(WrapperBinsDir)/ghci'"
    , ""
    , "LIBRARIES = $(wildcard ./lib/*)"
    , "install_lib:"
    , "\t@echo \"Copying libraries to $(ActualLibsDir)\""
    , "\t$(INSTALL_DIR) \"$(ActualLibsDir)\""
    , "\tfor i in $(LIBRARIES); do \\"
    , "\t\tcp -R $$i \"$(ActualLibsDir)/\"; \\"
    , "\tdone"
    , ""
    , "INCLUDES = $(wildcard ./include/*)"
    , "install_includes:"
    , "\t@echo \"Copying libraries to $(includedir)\""
    , "\t$(INSTALL_DIR) \"$(includedir)\""
    , "\tfor i in $(INCLUDES); do \\"
    , "\t\tcp -R $$i \"$(includedir)/\"; \\"
    , "\tdone"
    , ""
    , "DOCS = $(wildcard ./docs/*)"
    , "install_docs:"
    , "\t@echo \"Copying libraries to $(docdir)\""
    , "\t$(INSTALL_DIR) \"$(docdir)\""
    , "\tfor i in $(DOCS); do \\"
    , "\t\tcp -R $$i \"$(docdir)/\"; \\"
    , "\tdone"
    , ""
    , "BINARY_NAMES=$(shell ls ./wrappers/)"
    , "install_wrappers:"
    , "\t@echo \"Installing Wrapper scripts\""
    , "\t$(INSTALL_DIR) \"$(WrapperBinsDir)\""
    , "\t$(foreach p, $(BINARY_NAMES),\\"
    , "\t\t$(call installscript,$p,$(WrapperBinsDir)/$p," ++
      "$(WrapperBinsDir),$(ActualBinsDir),$(ActualBinsDir)/$p," ++
      "$(ActualLibsDir),$(docdir),$(includedir)))"
    , "\trm -f '$(WrapperBinsDir)/ghci-script'" -- FIXME: we shouldn't generate it in the first place
    , ""
    , "PKG_CONFS = $(wildcard $(ActualLibsDir)/package.conf.d/*)"
    , "update_package_db:"
    , "\t@echo \"Updating the package DB\""
    , "\t$(foreach p, $(PKG_CONFS),\\"
    , "\t\t$(call patchpackageconf," ++
      "$(shell echo $(notdir $p) | sed 's/-\\([0-9]*[0-9]\\.\\)*conf//g')," ++
      "$p,$(docdir)))"
    , "\t'$(WrapperBinsDir)/ghc-pkg' recache"
    , ""
    , "# The 'foreach' that copies the mingw directory will only trigger a copy"
    , "# when the wildcard matches, therefore only on Windows."
    , "MINGW = $(wildcard ./mingw)"
    , "install_mingw:"
    , "\t@echo \"Installing MingGW\""
    , "\t$(INSTALL_DIR) \"$(prefix)/mingw\""
    , "\t$(foreach d, $(MINGW),\\"
    , "\t\tcp -R ./mingw \"$(prefix)\")"
    , "# END INSTALL"
    , "# ----------------------------------------------------------------------"
    ]

wrapper :: FilePath -> String
wrapper "ghc"         = ghcWrapper
wrapper "ghc-pkg"     = ghcPkgWrapper
wrapper "ghci-script" = ghciScriptWrapper
wrapper "haddock"     = haddockWrapper
wrapper "hsc2hs"      = hsc2hsWrapper
wrapper "runghc"      = runGhcWrapper
wrapper _             = commonWrapper

-- | Wrapper scripts for different programs. Common is default wrapper.

ghcWrapper :: String
ghcWrapper = "exec \"$executablename\" -B\"$libdir\" ${1+\"$@\"}\n"

ghcPkgWrapper :: String
ghcPkgWrapper = unlines
    [ "PKGCONF=\"$libdir/package.conf.d\""
    , "exec \"$executablename\" --global-package-db \"$PKGCONF\" ${1+\"$@\"}" ]

haddockWrapper :: String
haddockWrapper = "exec \"$executablename\" -B\"$libdir\" -l\"$libdir\" ${1+\"$@\"}\n"

commonWrapper :: String
commonWrapper = "exec \"$executablename\" ${1+\"$@\"}\n"

hsc2hsWrapper :: String
hsc2hsWrapper = unlines
    [ "HSC2HS_EXTRA=\"--cflag=-fno-stack-protector --lflag=-fuse-ld=gold\""
    , "tflag=\"--template=$libdir/template-hsc.h\""
    , "Iflag=\"-I$includedir/\""
    , "for arg do"
    , "    case \"$arg\" in"
    , "# On OS X, we need to specify -m32 or -m64 in order to get gcc to"
    , "# build binaries for the right target. We do that by putting it in"
    , "# HSC2HS_EXTRA. When cabal runs hsc2hs, it passes a flag saying which"
    , "# gcc to use, so if we set HSC2HS_EXTRA= then we don't get binaries"
    , "# for the right platform. So for now we just don't set HSC2HS_EXTRA="
    , "# but we probably want to revisit how this works in the future."
    , "#        -c*)          HSC2HS_EXTRA=;;"
    , "#        --cc=*)       HSC2HS_EXTRA=;;"
    , "        -t*)          tflag=;;"
    , "        --template=*) tflag=;;"
    , "        --)           break;;"
    , "    esac"
    , "done"
    , "exec \"$executablename\" ${tflag:+\"$tflag\"} $HSC2HS_EXTRA ${1+\"$@\"} \"$Iflag\"" ]

runGhcWrapper :: String
runGhcWrapper = "exec \"$executablename\" -f \"$exedir/ghc\" ${1+\"$@\"}\n"

-- | We need to ship ghci executable, which basically just calls ghc with
-- | --interactive flag.
ghciScriptWrapper :: String
ghciScriptWrapper = unlines
    [ "DIR=`dirname \"$0\"`"
    , "executable=\"$DIR/ghc\""
    , "exec $executable --interactive \"$@\"" ]

-- | When not on Windows, we want to ship the 3 flavours of the iserv program
--   in binary distributions. This isn't easily achievable by just asking for
--   the package to be built, since here we're generating 3 different
--   executables out of just one package, so we need to specify all 3 contexts
--   explicitly and 'need' the result of building them.
needIservBins :: Action ()
needIservBins = do
    windows <- windowsHost
    when (not windows) $ do
        rtsways <- interpretInContext (vanillaContext Stage1 ghc) getRtsWays
        need =<< traverse programPath
                   [ Context Stage1 iserv w
                   | w <- [vanilla, profiling, dynamic]
                   , w `elem` rtsways
                   ]