1
1
mirror of https://github.com/tweag/asterius.git synced 2024-10-05 13:17:19 +03:00

Bazel files for building asterius binaries (#888)

Co-authored-by: Yves-Stan Le Cornec <yves.stan.lecornec@tweag.io>
This commit is contained in:
Yves-Stan Le Cornec 2022-01-19 13:58:52 +01:00 committed by GitHub
parent 5c2f89526e
commit 641ade855c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 562 additions and 0 deletions

13
.bazelrc Normal file
View File

@ -0,0 +1,13 @@
# User Configuration
# ------------------
build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
run --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
# We make use of ctx.actions.declare_symlink in bazel/bazel_utils/cabal_filegroup.bzl
build --experimental_allow_unresolved_symlinks
build --incompatible_enable_cc_toolchain_resolution
build:ci --repository_cache=~/repo-cache/
try-import %workspace%/.bazelrc.local

57
.github/workflows/bazel.yaml vendored Normal file
View File

@ -0,0 +1,57 @@
name: shell
on:
- push
- pull_request
env:
# Bump this number to invalidate the GH actions cache
cache-version: 0
jobs:
shell:
name: bazel-${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
with:
submodules: recursive
- name: Mount Bazel cache
uses: actions/cache@v2
with:
path: ~/repo-cache
key: repo-cache-nixpkgs-${{ env.cache-version }}
- name: setup-nix
uses: cachix/install-nix-action@v16
with:
extra_nix_config: |
substituters = https://cache.nixos.org https://hydra.iohk.io
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=
install_url: https://releases.nixos.org/nix/nix-2.3.16/install
- name: setup-cachix
uses: cachix/cachix-action@v10
with:
name: asterius
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
- name: cachix-watch-store
run: |
cachix watch-store -c9 -j2 asterius &
- name: Bazel build
run: |
nix-shell bazel/nix/bazel_build_shell.nix --pure --run '
set -euo pipefail
bazel build //asterius:binaries --config=ci
'

2
.gitignore vendored
View File

@ -28,4 +28,6 @@ cabal.project.local
sysroot
/bazel-*
.bazelrc.local
*~

0
BUILD Normal file
View File

220
WORKSPACE Normal file
View File

@ -0,0 +1,220 @@
workspace(name = "bazel_asterius")
# This commit must be the same as the one from bazel/stack.yaml
HASKELL_BINARYEN_COMMIT = "7d0ea214e47946fc830d332a6e3e1b73f60f61fd"
######################
# BAZEL DEPENDENCIES #
######################
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load(
"@bazel_tools//tools/build_defs/repo:git.bzl",
"git_repository",
"new_git_repository",
)
git_repository(
name = "rules_haskell",
commit = "26a966abf7328b08c449c682f07d0e00e6f14466",
remote = "https://github.com/tweag/rules_haskell.git",
shallow_since = "1641820378 +0000",
)
load("@rules_haskell//haskell:repositories.bzl", "rules_haskell_dependencies")
rules_haskell_dependencies()
load(
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
"nixpkgs_cc_configure",
"nixpkgs_local_repository",
"nixpkgs_package",
"nixpkgs_python_configure",
"nixpkgs_sh_posix_configure",
)
# Setup the posix toolchain
http_archive(
name = "rules_sh",
sha256 = "83a065ba6469135a35786eb741e17d50f360ca92ab2897857475ab17c0d29931",
strip_prefix = "rules_sh-0.2.0",
urls = ["https://github.com/tweag/rules_sh/archive/v0.2.0.tar.gz"],
)
load("@rules_sh//sh:repositories.bzl", "rules_sh_dependencies")
rules_sh_dependencies()
#####################
# Setup toolchains #
#####################
nixpkgs_local_repository(
name = "nixpkgs",
nix_file = "//bazel/nix:bazel_deps.nix",
nix_file_deps = [
"//nix:sources.nix",
"//nix:binaryen.nix",
"//nix:binaryen.patch",
"//nix:wasi-sdk.nix",
"//nix:sources.json",
"//ghc-toolkit:cbits/ghc_constants.c",
"//nix:ghcconstants.nix",
],
)
nixpkgs_sh_posix_configure(repository = "@nixpkgs")
nixpkgs_cc_configure(repository = "@nixpkgs")
nixpkgs_python_configure(repository = "@nixpkgs")
load("@rules_haskell//haskell:nixpkgs.bzl", "haskell_register_ghc_nixpkgs")
haskell_register_ghc_nixpkgs(
attribute_path = "ghc",
repository = "@nixpkgs",
version = "8.10.7",
)
##############################################
# External dependencies specific to asterius #
##############################################
# We recover the binaryen C library through nix
nixpkgs_package(
name = "binaryen_dev",
attribute_path = "binaryen",
build_file_content = """
load("@rules_cc//cc:defs.bzl", "cc_library")
filegroup(
name = "include",
srcs = glob(["include/*.h"]),
visibility = ["//visibility:public"],
)
filegroup(
name = "binaryen",
srcs = glob(["lib/*.so", "lib/*.a"]),
visibility = ["//visibility:public"],
)
cc_library(
name = "binaryen_cc",
srcs = [":binaryen"],
deps = [":binaryen_import"],
hdrs = [":include"],
includes = ["include"],
visibility = ["//visibility:public"],
)
cc_import(
name = "binaryen_import",
hdrs = [":include"],
shared_library = "lib/libbinaryen.so",
visibility = ["//visibility:public"],
)
""",
repository = "@nixpkgs",
)
load(
"@rules_haskell//haskell:cabal.bzl",
"stack_snapshot",
)
stack_snapshot(
name = "stackage",
components = {
"ahc-pkg": ["exe"],
},
local_snapshot = "//:bazel/stack.yaml",
packages = [
"inline-js-core",
"base",
"binary",
"bytestring",
"containers",
"directory",
"filepath",
"Cabal",
"ghc-prim",
"process",
"stm",
"template-haskell",
"text",
"haskeline",
"exceptions",
# dependencies from ghc-asterius
# "ahc-bin",
"ahc-pkg",
"ghc-asterius",
"ghc-boot-asterius",
"ghc-boot-th-asterius",
"ghc-heap-asterius",
"ghci-asterius",
"template-haskell-asterius",
],
# To update the stackage dependencies, run `bazel run @stackage-unpinned//:pin` and
stack_snapshot_json = "//:bazel/stackage_snapshot.json",
)
# We not use the custom stack snapshot for this in order to work around the following bug.
# https://github.com/tweag/rules_haskell/issues/1676
new_git_repository(
name = "haskell_binaryen",
build_file_content = """
load(
"@rules_haskell//haskell:cabal.bzl",
"haskell_cabal_library",
)
load(
"@rules_haskell//haskell:defs.bzl",
"haskell_library",
"haskell_toolchain_library",
)
haskell_toolchain_library(name = "base")
cc_library(
name = "libbinaryen",
srcs = ["cbits/wrappers.c"],
deps = [
"@binaryen_dev//:binaryen_cc",
],
visibility = ["//visibility:public"]
)
haskell_library(
name = "binaryen",
srcs = glob(["src/**"]),
deps = [
"libbinaryen",
"base",
],
visibility = ["//visibility:public"],
)
""",
commit = HASKELL_BINARYEN_COMMIT,
remote = "https://github.com/tweag/haskell-binaryen",
shallow_since = "1610373671 +0100",
)
######################
# Tools from nixpkgs #
######################
[
nixpkgs_package(
name = pack,
repository = "@nixpkgs",
)
for pack in [
"hpack",
]
]

134
asterius/BUILD.bazel Normal file
View File

@ -0,0 +1,134 @@
load(
"@rules_haskell//haskell:cabal.bzl",
"haskell_cabal_binary",
"haskell_cabal_library",
)
load(
"@rules_haskell//haskell:defs.bzl",
"haskell_library",
"haskell_toolchain_library",
)
load(
"//:bazel/bazel_utils/cabal_filegroup.bzl",
"cabal_filegroup",
)
genrule(
name = "hpack",
srcs = ["package.yaml"],
outs = ["asterius.cabal"],
cmd = "$(location @hpack//:bin) asterius && cp asterius/asterius.cabal $@",
tools = ["@hpack//:bin"],
)
cabal_filegroup(
name = "cabal_src",
srcs = glob([
"src/**",
"src-types/**",
"LICENSE",
"ghc-bin-asterius/**",
"cabal/**",
"rts/**",
"app/**",
"test/**",
]),
generated_cabal_file = ":asterius.cabal",
prefix_path = "asterius",
)
exports_files(["boot.sh"])
# runtime dependencies accessed through the AHC_SRCDIR variable
filegroup(
name = "ahc_srcdir_content",
srcs = glob([
"cabal/**",
"rts/**",
]),
visibility = ["//visibility:public"],
)
executables = [
"Setup-ghc-prim",
"ahc",
"ahc-cabal",
"ahc-dist",
"ahc-ld",
"ahc-link",
"genconstants",
"unlit",
]
filegroup(
name = "binaries",
srcs = executables,
)
[
haskell_cabal_binary(
name = executable,
srcs = [":cabal_src"],
exe_name = executable,
visibility = ["//visibility:public"],
deps = [
":asterius",
"@binaryen_dev//:binaryen_cc",
"@haskell_binaryen//:binaryen",
"@haskell_binaryen//:libbinaryen",
],
)
for executable in executables
]
# We use a haskell_library rule for the asterius target in order to
# work around https://github.com/tweag/rules_haskell/issues/1676.
# A haskell_cabal_library rule would require less manual intervention if
# we update the build as it would make use of the .cabal file.
[
haskell_toolchain_library(name = lib)
for lib in [
"base",
"bytestring",
"directory",
"filepath",
"process",
"Cabal",
"containers",
"mtl",
"deepseq",
"exceptions",
"binary",
"template-haskell",
]
]
haskell_library(
name = "asterius",
srcs = glob([
"src/**",
"src-types/**",
]),
visibility = [
"//bazel/bazel_build:__pkg__",
],
deps = [
"Cabal",
"base",
"binary",
"bytestring",
"containers",
"deepseq",
"directory",
"exceptions",
"filepath",
"mtl",
"process",
"template-haskell",
"@haskell_binaryen//:binaryen",
"@stackage//:ghc-asterius",
"@stackage//:ghci-asterius",
"@stackage//:inline-js-core",
"@stackage//:template-haskell-asterius",
],
)

15
bazel/bazel.md Normal file
View File

@ -0,0 +1,15 @@
# The bazel build
## External dependencies to keep in sync.
- The `bazel/stack.yaml` file should be kept in sync with the `nix.cabal.project` files as
we rely on this custom stack snapshot for external haskell dependencies.
On updating this file, the `bazel/stackage_snapshot.json` should be generated again by executing `bazel run @stackage-unpinned//:pin`.
- The `HASKELL_BINARYEN_COMMIT` variable from the `WORKSPACE` file.
Because of a `rules_haskell` [issue](https://github.com/tweag/rules_haskell/issues/1676) we do not recover this library through the stack snapshot.
- The `bazel/nix/bazel_deps.nix` file mimics the `shell.nix` file and exposes necessary attributes to bazel.
It the other nix files are refactored it may be necessary to also modify this one.

View File

@ -0,0 +1,30 @@
# This rule enables to mix the generated cabal file produced by hpack
# with the rest of the source files.
load("@bazel_skylib//lib:paths.bzl", "paths")
def _cabal_filegroup_impl(ctx):
f = ctx.file.generated_cabal_file
outputs = []
for g in ctx.files.srcs:
gl = ctx.actions.declare_symlink(
paths.relativize(g.path, ctx.attr.prefix_path),
)
ctx.actions.symlink(output = gl, target_path = g.path)
outputs.append(gl)
return [
DefaultInfo(files = depset(outputs + [f])),
]
cabal_filegroup = rule(
_cabal_filegroup_impl,
attrs = {
"srcs": attr.label_list(
allow_files = True,
),
"prefix_path": attr.string(),
"generated_cabal_file": attr.label(
allow_single_file = True,
),
},
)

0
bazel/nix/BUILD.bazel Normal file
View File

View File

@ -0,0 +1,6 @@
{ pkgs ? import ./bazel_deps.nix {} }:
with pkgs;
mkShell {
LANG="C.UTF-8";
buildInputs = [gcc cacert git nix bazel_4 ];
}

18
bazel/nix/bazel_deps.nix Normal file
View File

@ -0,0 +1,18 @@
{ sources ? import ../../nix/sources.nix { }
, haskellNix ? import sources.haskell-nix { }
, pkgs ? import haskellNix.sources.nixpkgs-unstable (haskellNix.nixpkgsArgs // {
overlays = haskellNix.nixpkgsArgs.overlays
++ [ (import ../../nix/binaryen.nix) ];
})
, config ? {}
, overlays ? []
}:
pkgs // {
wasi_sdk = import "${import ../../nix/wasi-sdk.nix { }}/nix/default.nix" { };
src32 = import "${sources.ghc-asterius}/nix/src-32.nix" { };
wasmtime = (pkgs.callPackage "${import ../../nix/wasi-sdk.nix { }}/nix/wasmtime.nix" { });
wizer = (pkgs.callPackage "${sources.ghc-asterius}/nix/wizer.nix" { });
src = import "${sources.ghc-asterius}/nix/src.nix" { };
ahc_rts = import "${sources.ghc-asterius}/nix/wasi-rts.nix" { };
wasm = import ../../nix/ghcconstants.nix {};
}

27
bazel/stack.yaml Normal file
View File

@ -0,0 +1,27 @@
resolver: "lts-18.14" # ghc version must be kept in sync with the bazel's haskell toolchain, which is setup through nix.
packages:
- github: tweag/haskell-binaryen
commit: "7d0ea214e47946fc830d332a6e3e1b73f60f61fd"
- git: "https://github.com/tweag/inline-js.git"
commit: "8512b09d2c0533a41d5d2aef182b11a58c420c10"
subdirs:
- inline-js-core
- github: tweag/ghc-asterius
# must be the same commit as the one from the WORKSPACE file.
commit: "686d0e7ee841d4ada6a2d1b52faf2b26a41cb927"
subdirs:
- ahc-bin
- ahc-pkg
- ghc-asterius
- ghc-boot-asterius
- ghc-boot-th-asterius
- ghc-heap-asterius
- ghci-asterius
- template-haskell-asterius
flags:
binaryen:
system-binaryen: true

View File

@ -0,0 +1,39 @@
{
"__GENERATED_FILE_DO_NOT_MODIFY_MANUALLY": -1641511610,
"all-cabal-hashes": "https://raw.githubusercontent.com/commercialhaskell/all-cabal-hashes/4af64eb3bcf5b484e1bc24e65df3bb14a44dec49",
"resolved": {
"Cabal": {"dependencies":[],"location":{"type":"core"},"name":"Cabal","version":"3.2.1.0"},
"ahc-pkg": {"dependencies":["Cabal","base","binary","bytestring","containers","directory","filepath","ghc-boot-asterius","process","terminfo","unix"],"location":{"type":"archive","url":"https://github.com/tweag/ghc-asterius/archive/686d0e7ee841d4ada6a2d1b52faf2b26a41cb927.tar.gz"},"name":"ahc-pkg","pinned":{"sha256":"6d3db2ddbffd97a319a0b38e7dbd7fc129340328e8577933be160b62cf95feff","strip-prefix":"ghc-asterius-686d0e7ee841d4ada6a2d1b52faf2b26a41cb927/ahc-pkg"},"version":"8.10.7"},
"array": {"dependencies":[],"location":{"type":"core"},"name":"array","version":"0.5.4.0"},
"base": {"dependencies":[],"location":{"type":"core"},"name":"base","version":"4.14.3.0"},
"binary": {"dependencies":[],"location":{"type":"core"},"name":"binary","version":"0.8.8.0"},
"bytestring": {"dependencies":[],"location":{"type":"core"},"name":"bytestring","version":"0.10.12.0"},
"containers": {"dependencies":[],"location":{"type":"core"},"name":"containers","version":"0.6.5.1"},
"deepseq": {"dependencies":[],"location":{"type":"core"},"name":"deepseq","version":"1.4.4.0"},
"directory": {"dependencies":[],"location":{"type":"core"},"name":"directory","version":"1.3.6.0"},
"exceptions": {"dependencies":["base","mtl","stm","template-haskell","transformers"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/exceptions-0.10.4/exceptions-0.10.4.tar.gz"},"name":"exceptions","pinned":{"url":["https://hackage.haskell.org/package/exceptions-0.10.4/exceptions-0.10.4.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/exceptions-0.10.4.tar.gz"],"sha256":"4d0bfb4355cffcd67d300811df9d5fe44ea3594ed63750795bfc1f797abd84cf","cabal-sha256":"7ed09aed03683d5b4337088061106c2389d274b3472031a330ff1b220bad2b2d"},"version":"0.10.4"},
"filepath": {"dependencies":[],"location":{"type":"core"},"name":"filepath","version":"1.4.2.1"},
"ghc-asterius": {"dependencies":["array","base","binary","bytestring","containers","deepseq","directory","exceptions","filepath","ghc-boot-asterius","ghc-boot-th-asterius","ghc-heap-asterius","ghc-prim","ghci-asterius","haskeline","hpc","integer-gmp","process","template-haskell-asterius","terminfo","time","transformers","unix"],"location":{"type":"archive","url":"https://github.com/tweag/ghc-asterius/archive/686d0e7ee841d4ada6a2d1b52faf2b26a41cb927.tar.gz"},"name":"ghc-asterius","pinned":{"sha256":"6d3db2ddbffd97a319a0b38e7dbd7fc129340328e8577933be160b62cf95feff","strip-prefix":"ghc-asterius-686d0e7ee841d4ada6a2d1b52faf2b26a41cb927/ghc-asterius"},"version":"8.10.7"},
"ghc-boot-asterius": {"dependencies":["base","binary","bytestring","containers","directory","filepath","ghc-boot-th-asterius"],"location":{"type":"archive","url":"https://github.com/tweag/ghc-asterius/archive/686d0e7ee841d4ada6a2d1b52faf2b26a41cb927.tar.gz"},"name":"ghc-boot-asterius","pinned":{"sha256":"6d3db2ddbffd97a319a0b38e7dbd7fc129340328e8577933be160b62cf95feff","strip-prefix":"ghc-asterius-686d0e7ee841d4ada6a2d1b52faf2b26a41cb927/ghc-boot-asterius"},"version":"8.10.7"},
"ghc-boot-th-asterius": {"dependencies":["base"],"location":{"type":"archive","url":"https://github.com/tweag/ghc-asterius/archive/686d0e7ee841d4ada6a2d1b52faf2b26a41cb927.tar.gz"},"name":"ghc-boot-th-asterius","pinned":{"sha256":"6d3db2ddbffd97a319a0b38e7dbd7fc129340328e8577933be160b62cf95feff","strip-prefix":"ghc-asterius-686d0e7ee841d4ada6a2d1b52faf2b26a41cb927/ghc-boot-th-asterius"},"version":"8.10.7"},
"ghc-heap-asterius": {"dependencies":["base","ghc-prim","rts"],"location":{"type":"archive","url":"https://github.com/tweag/ghc-asterius/archive/686d0e7ee841d4ada6a2d1b52faf2b26a41cb927.tar.gz"},"name":"ghc-heap-asterius","pinned":{"sha256":"6d3db2ddbffd97a319a0b38e7dbd7fc129340328e8577933be160b62cf95feff","strip-prefix":"ghc-asterius-686d0e7ee841d4ada6a2d1b52faf2b26a41cb927/ghc-heap-asterius"},"version":"8.10.7"},
"ghc-prim": {"dependencies":[],"location":{"type":"core"},"name":"ghc-prim","version":"0.6.1"},
"ghci-asterius": {"dependencies":["array","base","binary","bytestring","containers","deepseq","filepath","ghc-boot-asterius","ghc-boot-th-asterius","ghc-heap-asterius","rts","template-haskell-asterius","transformers","unix"],"location":{"type":"archive","url":"https://github.com/tweag/ghc-asterius/archive/686d0e7ee841d4ada6a2d1b52faf2b26a41cb927.tar.gz"},"name":"ghci-asterius","pinned":{"sha256":"6d3db2ddbffd97a319a0b38e7dbd7fc129340328e8577933be160b62cf95feff","strip-prefix":"ghc-asterius-686d0e7ee841d4ada6a2d1b52faf2b26a41cb927/ghci-asterius"},"version":"8.10.7"},
"haskeline": {"dependencies":["base","bytestring","containers","directory","exceptions","filepath","process","stm","terminfo","transformers","unix"],"location":{"type":"core"},"name":"haskeline","version":"0.8.2"},
"hpc": {"dependencies":[],"location":{"type":"core"},"name":"hpc","version":"0.6.1.0"},
"inline-js-core": {"dependencies":["Cabal","base","binary","bytestring","containers","directory","filepath","ghc-prim","process","stm","template-haskell","text"],"location":{"type":"git","url":"https://github.com/tweag/inline-js.git","commit":"8512b09d2c0533a41d5d2aef182b11a58c420c10","subdir":"inline-js-core"},"name":"inline-js-core","version":"0.0.1.0"},
"integer-gmp": {"dependencies":[],"location":{"type":"core"},"name":"integer-gmp","version":"1.0.3.0"},
"mtl": {"dependencies":[],"location":{"type":"core"},"name":"mtl","version":"2.2.2"},
"pretty": {"dependencies":[],"location":{"type":"core"},"name":"pretty","version":"1.1.3.6"},
"process": {"dependencies":[],"location":{"type":"core"},"name":"process","version":"1.6.13.2"},
"rts": {"dependencies":[],"location":{"type":"core"},"name":"rts","version":"1.0.1"},
"stm": {"dependencies":[],"location":{"type":"core"},"name":"stm","version":"2.5.0.1"},
"template-haskell": {"dependencies":[],"location":{"type":"core"},"name":"template-haskell","version":"2.16.0.0"},
"template-haskell-asterius": {"dependencies":["base","ghc-boot-th-asterius","ghc-prim","pretty"],"location":{"type":"archive","url":"https://github.com/tweag/ghc-asterius/archive/686d0e7ee841d4ada6a2d1b52faf2b26a41cb927.tar.gz"},"name":"template-haskell-asterius","pinned":{"sha256":"6d3db2ddbffd97a319a0b38e7dbd7fc129340328e8577933be160b62cf95feff","strip-prefix":"ghc-asterius-686d0e7ee841d4ada6a2d1b52faf2b26a41cb927/template-haskell-asterius"},"version":"2.16.0.0"},
"terminfo": {"dependencies":[],"location":{"type":"core"},"name":"terminfo","version":"0.4.1.4"},
"text": {"dependencies":[],"location":{"type":"core"},"name":"text","version":"1.2.4.1"},
"time": {"dependencies":[],"location":{"type":"core"},"name":"time","version":"1.9.3"},
"transformers": {"dependencies":[],"location":{"type":"core"},"name":"transformers","version":"0.5.6.2"},
"unix": {"dependencies":[],"location":{"type":"core"},"name":"unix","version":"2.7.2.2"}
}
}

0
ghc-toolkit/BUILD.bazel Normal file
View File

1
nix/BUILD.bazel Normal file
View File

@ -0,0 +1 @@
exports_files(glob(["*.nix"]))