3
3
load ("@bazel_tools//tools/build_defs/repo:utils.bzl" , "patch" )
4
4
load ("@bazel_tools//tools/cpp:lib_cc_configure.bzl" , "get_cpu_value" )
5
5
load ("@rules_sh//sh:posix.bzl" , "sh_posix_configure" )
6
- load (":private/workspace_utils.bzl" , "execute_or_fail_loudly" )
6
+ load (
7
+ ":private/pkgdb_to_bzl.bzl" ,
8
+ "pkgdb_to_bzl" ,
9
+ )
10
+ load (
11
+ ":private/workspace_utils.bzl" ,
12
+ "define_rule" ,
13
+ "execute_or_fail_loudly" ,
14
+ "find_python" ,
15
+ "resolve_labels" ,
16
+ )
7
17
8
- # If you change this, change stackage's version
9
- # in the start script (see stackage.org)
18
+ # If you change this, change stackage's version in the start script
19
+ # (see stackage.org).
10
20
_GHC_DEFAULT_VERSION = "8.6.5"
11
21
12
22
# Generated with `bazel run @rules_haskell//haskell:gen-ghc-bindist`
@@ -271,14 +281,13 @@ GHC_BINDIST = \
271
281
}
272
282
273
283
def _ghc_bindist_impl (ctx ):
274
- # Avoid rule restart by resolving these labels early. See
275
- # https://github.com/bazelbuild/bazel/blob/master/tools/cpp/lib_cc_configure.bzl#L17.
276
- ghc_build = ctx . path ( Label ( "@rules_haskell//haskell:ghc.BUILD.tpl" ))
277
-
284
+ paths = resolve_labels ( ctx , [
285
+ "@rules_haskell//haskell:ghc.BUILD.tpl" ,
286
+ "@rules_haskell//haskell:private/pkgdb_to_bzl.py" ,
287
+ ])
278
288
version = ctx .attr .version
279
289
target = ctx .attr .target
280
290
os , _ , arch = target .partition ("_" )
281
- python_bin = _find_python (ctx )
282
291
283
292
if GHC_BINDIST [version ].get (target ) == None :
284
293
fail ("Operating system {0} does not have a bindist for GHC version {1}" .format (ctx .os .name , ctx .attr .version ))
@@ -295,6 +304,13 @@ def _ghc_bindist_impl(ctx):
295
304
stripPrefix = "ghc-" + version ,
296
305
)
297
306
307
+ if os == "windows" :
308
+ # These libraries cause linking errors on Windows when linking
309
+ # pthreads, due to libwinpthread-1.dll not being loaded.
310
+ execute_or_fail_loudly (ctx , ["rm" , "mingw/lib/gcc/x86_64-w64-mingw32/7.2.0/libstdc++.dll.a" ])
311
+ execute_or_fail_loudly (ctx , ["rm" , "mingw/x86_64-w64-mingw32/lib/libpthread.dll.a" ])
312
+ execute_or_fail_loudly (ctx , ["rm" , "mingw/x86_64-w64-mingw32/lib/libwinpthread.dll.a" ])
313
+
298
314
# We apply some patches, if needed.
299
315
patch (ctx )
300
316
@@ -332,73 +348,33 @@ DISTDIR="$( dirname "$(resolved="$0"; cd "$(dirname "$resolved")"; while tmp="$(
332
348
))
333
349
execute_or_fail_loudly (ctx , ["./patch_bins" ])
334
350
335
- # The default locale is OS specific.
336
- if ctx .attr .locale :
337
- locale = ctx .attr .locale
338
- else :
339
- locale = "en_US.UTF-8" if os == "darwin" else "C.UTF-8"
340
-
341
- # Generate BUILD file entries describing each prebuilt package.
342
- # Cannot use //haskell:pkgdb_to_bzl because that's a generated
343
- # target. ctx.path() only works on source files.
344
- pkgdb_to_bzl = ctx .path (Label ("@rules_haskell//haskell:private/pkgdb_to_bzl.py" ))
345
- result = ctx .execute ([
346
- python_bin ,
347
- pkgdb_to_bzl ,
348
- ctx .attr .name ,
349
- "lib" ,
350
- ])
351
- if result .return_code :
352
- fail ("Error executing pkgdb_to_bzl.py: {stderr}" .format (stderr = result .stderr ))
353
- toolchain_libraries = result .stdout
354
- toolchain = """
355
- {toolchain_libraries}
356
-
357
- haskell_toolchain(
358
- name = "toolchain-impl",
359
- tools = [":bin"],
360
- libraries = toolchain_libraries,
361
- version = "{version}",
362
- static_runtime = {static_runtime},
363
- fully_static_link = {fully_static_link},
364
- compiler_flags = {compiler_flags},
365
- haddock_flags = {haddock_flags},
366
- repl_ghci_args = {repl_ghci_args},
367
- cabalopts = {cabalopts},
368
- visibility = ["//visibility:public"],
369
- locale = "{locale}",
370
- )
371
- """ .format (
372
- toolchain_libraries = toolchain_libraries ,
373
- version = ctx .attr .version ,
351
+ toolchain_libraries = pkgdb_to_bzl (ctx , paths , "lib" )
352
+ locale = ctx .attr .locale or "en_US.UTF-8" if os == "darwin" else "C.UTF-8"
353
+ toolchain = define_rule (
354
+ "haskell_toolchain" ,
355
+ name = "toolchain-impl" ,
356
+ tools = [":bin" ],
357
+ libraries = "toolchain_libraries" ,
358
+ # See Note [GHC toolchain files]
359
+ files = [":lib/settings" ],
360
+ version = repr (ctx .attr .version ),
374
361
static_runtime = os == "windows" ,
375
-
376
- # At present we don't support fully-statically-linked binaries with GHC
377
- # bindists.
378
- fully_static_link = False ,
362
+ fully_static_link = False , # XXX not yet supported for bindists.
379
363
compiler_flags = ctx .attr .compiler_flags ,
380
364
haddock_flags = ctx .attr .haddock_flags ,
381
365
repl_ghci_args = ctx .attr .repl_ghci_args ,
382
366
cabalopts = ctx .attr .cabalopts ,
383
- locale = locale ,
367
+ locale = repr ( locale ) ,
384
368
)
385
-
386
- if os == "windows" :
387
- # These libraries cause linking errors on Windows when linking
388
- # pthreads, due to libwinpthread-1.dll not being loaded.
389
- execute_or_fail_loudly (ctx , ["rm" , "mingw/lib/gcc/x86_64-w64-mingw32/7.2.0/libstdc++.dll.a" ])
390
- execute_or_fail_loudly (ctx , ["rm" , "mingw/x86_64-w64-mingw32/lib/libpthread.dll.a" ])
391
- execute_or_fail_loudly (ctx , ["rm" , "mingw/x86_64-w64-mingw32/lib/libwinpthread.dll.a" ])
392
-
393
369
ctx .template (
394
370
"BUILD" ,
395
- ghc_build ,
371
+ paths [ "@rules_haskell//haskell:ghc.BUILD.tpl" ] ,
396
372
substitutions = {
373
+ "%{toolchain_libraries}" : toolchain_libraries ,
397
374
"%{toolchain}" : toolchain ,
398
375
},
399
376
executable = False ,
400
377
)
401
- ctx .file ("WORKSPACE" )
402
378
403
379
_ghc_bindist = repository_rule (
404
380
_ghc_bindist_impl ,
@@ -590,18 +566,9 @@ def haskell_register_ghc_bindists(
590
566
if local_python_repo_name not in native .existing_rules ():
591
567
_configure_python3_toolchain (name = local_python_repo_name )
592
568
593
- def _find_python (repository_ctx ):
594
- python = repository_ctx .which ("python3" )
595
- if not python :
596
- python = repository_ctx .which ("python" )
597
- result = repository_ctx .execute ([python , "--version" ])
598
- if not result .stdout .startswith ("Python 3" ):
599
- fail ("rules_haskell requires Python >= 3.3." )
600
- return python
601
-
602
569
def _configure_python3_toolchain_impl (repository_ctx ):
603
570
cpu = get_cpu_value (repository_ctx )
604
- python3_path = _find_python (repository_ctx )
571
+ python3_path = find_python (repository_ctx )
605
572
repository_ctx .file ("BUILD.bazel" , executable = False , content = """
606
573
load(
607
574
"@bazel_tools//tools/python:toolchain.bzl",
@@ -683,3 +650,26 @@ def _configure_python3_toolchain(name):
683
650
"""
684
651
_config_python3_toolchain (name = name )
685
652
native .register_toolchains ("@{}//:toolchain" .format (name ))
653
+
654
+ # Note [GHC toolchain files]
655
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~
656
+ #
657
+ # The GHC distribution includes various files that may be required during
658
+ # compilation, or may be referenced by template Haskell code. These files
659
+ # need to be tracked by Bazel and declared as inputs to the relevant actions
660
+ # to ensure that they are present in the build sandbox.
661
+ #
662
+ # Surprisingly, builds succeed with little to none of such files declared as
663
+ # inputs. In case of the nixpkgs toolchain this is not surprising, as the
664
+ # files will all be present in the Nix store. However, files of the bindist
665
+ # toolchain are fully tracked by Bazel and one would expect errors due to
666
+ # missing files if they are not declared.
667
+ #
668
+ # The first instance of such an error occurred with the GHC bindist on
669
+ # BazelCI [1] and shortly after on the GitHub actions CI pipeline. However,
670
+ # only the `lib/settings` file was reported missing.
671
+ #
672
+ # Do extend the `files` toolchain attributes with further files if such
673
+ # errors occurr in the future.
674
+ #
675
+ # [1]: https://github.com/tweag/rules_haskell/issues/1470
0 commit comments