From 5563dc9b9f5e8780906663e2bd3202635d160902 Mon Sep 17 00:00:00 2001 From: GuillaumeGen Date: Thu, 22 Sep 2022 14:13:30 +0200 Subject: [PATCH 1/4] Add an Asterius example --- examples/.gitignore | 1 + examples/WORKSPACE | 2 +- examples/asterius/BUILD.bazel | 40 ++++++++++++++++++++++ examples/asterius/Main.hs | 6 ++++ examples/asterius/README.md | 17 ++++++++++ examples/asterius/WORKSPACE | 63 +++++++++++++++++++++++++++++++++++ examples/asterius/src/Lib.hs | 13 ++++++++ 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 examples/asterius/BUILD.bazel create mode 100644 examples/asterius/Main.hs create mode 100644 examples/asterius/README.md create mode 100644 examples/asterius/WORKSPACE create mode 100644 examples/asterius/src/Lib.hs diff --git a/examples/.gitignore b/examples/.gitignore index a6ef824c1..a59516ff8 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -1 +1,2 @@ /bazel-* +/asterius/bazel-* \ No newline at end of file diff --git a/examples/WORKSPACE b/examples/WORKSPACE index a860f6fea..5392b2e5c 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -111,7 +111,7 @@ stack_snapshot( "text", "text-show", ], - snapshot = "lts-18.0", + snapshot = "lts-18.28", # This example uses an unpinned version of stack_snapshot, meaning that stack is invoked on every build. # To switch to pinned stackage dependencies, run `bazel run @stackage-unpinned//:pin` and # uncomment the following line. diff --git a/examples/asterius/BUILD.bazel b/examples/asterius/BUILD.bazel new file mode 100644 index 000000000..8a10dbcf1 --- /dev/null +++ b/examples/asterius/BUILD.bazel @@ -0,0 +1,40 @@ +load( + "@rules_haskell//haskell:defs.bzl", + "haskell_binary", +) +load( + "@rules_haskell//haskell/asterius:defs.bzl", + "ahc_dist", + "asterius_binary", + "asterius_webpack", +) + +# We first define an Haskell binary. +haskell_binary( + name = "main-bin", + main_file = "Main.hs", + srcs = ["Main.hs", "src/Lib.hs"], + src_strip_prefix = "src/", + deps = [ + "@stackage//:base", + "@stackage//:transformers", + ], +) + +# Then an Asterius rule depending of this Haskell binary. +ahc_dist( + name = "asterius", + dep = ":main-bin", +) + +# It is this Asterius rule which is then used to construct +# the Asterius binary and Asterius bundle. +asterius_binary( + name = "asterius_binary", + ahc_dist_dep = ":asterius" +) + +asterius_webpack( + name = "asterius_bundle", + ahc_dist_dep = ":asterius" +) diff --git a/examples/asterius/Main.hs b/examples/asterius/Main.hs new file mode 100644 index 000000000..1bded5e8e --- /dev/null +++ b/examples/asterius/Main.hs @@ -0,0 +1,6 @@ +module Main where + +import Lib (printValue) + +main :: IO () +main = printValue diff --git a/examples/asterius/README.md b/examples/asterius/README.md new file mode 100644 index 000000000..edaa7cd5d --- /dev/null +++ b/examples/asterius/README.md @@ -0,0 +1,17 @@ +# asterius - An example on how to use 'asterius' with 'rules_haskell' + +This project implements a very basic project with 2 modules. +It uses [Bazel][bazel] build system, using [rules_haskell][rules_haskell] +to define the Asterius Haskell build and output the webassmebly. + +[bazel]: https://bazel.build/ +[rules_haskell]: https://haskell.build/ + +## Instructions + +To build the package execute the following command *in the `examples/asterius` +directory*: + +``` +$ bazel build //asterius:asterius_bundle +``` diff --git a/examples/asterius/WORKSPACE b/examples/asterius/WORKSPACE new file mode 100644 index 000000000..438f4ff24 --- /dev/null +++ b/examples/asterius/WORKSPACE @@ -0,0 +1,63 @@ +workspace(name = "asterius_example") + +# Rules_haskell is imported +local_repository( + name = "rules_haskell", + path = "../..", +) + +# We setup the dependencies related to rules_haskell. +load("@rules_haskell//haskell:repositories.bzl", "rules_haskell_dependencies") + +rules_haskell_dependencies() + +load("@rules_haskell//haskell:toolchain.bzl", "rules_haskell_toolchains") + +rules_haskell_toolchains(version = "8.10.7") + +# We setup the Asterius related dependencies. +load( + "@rules_haskell//haskell/asterius:repositories.bzl", + "asterius_dependencies_bindist", + "rules_haskell_asterius_toolchains", +) +asterius_dependencies_bindist() + +rules_haskell_asterius_toolchains() + +# This example relies on Nixpkgs to get a lot of tools, +# including GHC. +load("@rules_haskell//haskell:nixpkgs.bzl", "haskell_register_ghc_nixpkgs") + +haskell_register_ghc_nixpkgs( + attribute_path = "haskell.compiler.ghc8107", + repository = "@rules_haskell//nixpkgs:default.nix", + version = "8.10.7", +) + +load( + "@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", + "nixpkgs_cc_configure", + "nixpkgs_python_configure", +) + +nixpkgs_cc_configure( + name = "nixpkgs_config_cc", + repository = "@rules_haskell//nixpkgs:default.nix", +) + +nixpkgs_python_configure( + repository = "@rules_haskell//nixpkgs:default.nix", +) + +# Our small example uses libraries that we get via Stackage. +load("@rules_haskell//haskell:cabal.bzl", "stack_snapshot") + +stack_snapshot( + name = "stackage", + packages = [ + "base", + "transformers", + ], + snapshot = "lts-18.28", +) diff --git a/examples/asterius/src/Lib.hs b/examples/asterius/src/Lib.hs new file mode 100644 index 000000000..a8a3c9901 --- /dev/null +++ b/examples/asterius/src/Lib.hs @@ -0,0 +1,13 @@ +module Lib (printValue) where + +import Control.Monad (void) +import Control.Monad.Trans.Class (lift) +import Control.Monad.Trans.Except (ExceptT, runExceptT) + +getValue :: Monad m => ExceptT e m String +getValue = pure "Hello world!" + +printValue :: IO () +printValue = void $ runExceptT $ do + value <- getValue + lift $ print value From ff8a6554cc1ed14f30593b74c434fa2af6f03039 Mon Sep 17 00:00:00 2001 From: GuillaumeGen Date: Thu, 22 Sep 2022 17:04:36 +0200 Subject: [PATCH 2/4] Add bits in asterius/WORKSPACE to examples/WORKSPACE and update deleted packages --- .bazelrc | 5 ++--- examples/WORKSPACE | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.bazelrc b/.bazelrc index 99e942915..97c3c6561 100644 --- a/.bazelrc +++ b/.bazelrc @@ -113,9 +113,8 @@ coverage --build_tag_filters "coverage-compatible" --test_tag_filters "coverage- # To update these lines, execute # `bazel run @contrib_rules_bazel_integration_test//tools:update_deleted_packages` -build --deleted_packages=examples,examples/arm,examples/cat_hs,examples/cat_hs/exec/cat_hs,examples/cat_hs/lib/args,examples/cat_hs/lib/cat,examples/primitive,examples/rts,examples/transformers,examples/vector,tests/c2hs/repo,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-a,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-b,tests/haskell_module/repl/haskell_module_repl_test,tests/library-external-workspace/repo,tests/repl-targets/hs_bin_repl_test,tests/repl-targets/hs_lib_repl_test,tests/stack-snapshot-deps/hs_override_stack_test,tutorial,tutorial/lib,tutorial/main,tutorial/tools/build_rules -query --deleted_packages=examples,examples/arm,examples/cat_hs,examples/cat_hs/exec/cat_hs,examples/cat_hs/lib/args,examples/cat_hs/lib/cat,examples/primitive,examples/rts,examples/transformers,examples/vector,tests/c2hs/repo,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-a,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-b,tests/haskell_module/repl/haskell_module_repl_test,tests/library-external-workspace/repo,tests/repl-targets/hs_bin_repl_test,tests/repl-targets/hs_lib_repl_test,tests/stack-snapshot-deps/hs_override_stack_test,tutorial,tutorial/lib,tutorial/main,tutorial/tools/build_rules - +build --deleted_packages=examples,examples/arm,examples/asterius,examples/basic_modules,examples/cat_hs,examples/cat_hs/exec/cat_hs,examples/cat_hs/lib/args,examples/cat_hs/lib/cat,examples/primitive,examples/rts,examples/transformers,examples/vector,tests/c2hs/repo,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-a,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-b,tests/haskell_module/repl/haskell_module_repl_test,tests/library-external-workspace/repo,tests/repl-targets/hs_bin_repl_test,tests/repl-targets/hs_lib_repl_test,tests/stack-snapshot-deps/hs_override_stack_test,tutorial,tutorial/lib,tutorial/main,tutorial/tools/build_rules +query --deleted_packages=examples,examples/arm,examples/asterius,examples/basic_modules,examples/cat_hs,examples/cat_hs/exec/cat_hs,examples/cat_hs/lib/args,examples/cat_hs/lib/cat,examples/primitive,examples/rts,examples/transformers,examples/vector,tests/c2hs/repo,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-a,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-b,tests/haskell_module/repl/haskell_module_repl_test,tests/library-external-workspace/repo,tests/repl-targets/hs_bin_repl_test,tests/repl-targets/hs_lib_repl_test,tests/stack-snapshot-deps/hs_override_stack_test,tutorial,tutorial/lib,tutorial/main,tutorial/tools/build_rules # User Configuration # ------------------ diff --git a/examples/WORKSPACE b/examples/WORKSPACE index 5392b2e5c..17c882cfc 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -110,6 +110,7 @@ stack_snapshot( "optparse-applicative", "text", "text-show", + "transformers", ], snapshot = "lts-18.28", # This example uses an unpinned version of stack_snapshot, meaning that stack is invoked on every build. @@ -119,3 +120,13 @@ stack_snapshot( # stack_snapshot_json = "//:stackage_snapshot.json", vendored_packages = {"split": "@split//:split"}, ) + +# We setup the Asterius related dependencies. +load( + "@rules_haskell//haskell/asterius:repositories.bzl", + "asterius_dependencies_bindist", + "rules_haskell_asterius_toolchains", +) +asterius_dependencies_bindist() + +rules_haskell_asterius_toolchains() From ab5dcc8fdaa9d217ab0138cb68effc952b491bdd Mon Sep 17 00:00:00 2001 From: GuillaumeGen Date: Thu, 22 Sep 2022 18:34:02 +0200 Subject: [PATCH 3/4] Please buildifier --- examples/WORKSPACE | 1 + examples/asterius/BUILD.bazel | 9 ++++++--- examples/asterius/WORKSPACE | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/WORKSPACE b/examples/WORKSPACE index 17c882cfc..40242d70c 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -127,6 +127,7 @@ load( "asterius_dependencies_bindist", "rules_haskell_asterius_toolchains", ) + asterius_dependencies_bindist() rules_haskell_asterius_toolchains() diff --git a/examples/asterius/BUILD.bazel b/examples/asterius/BUILD.bazel index 8a10dbcf1..140cf9b48 100644 --- a/examples/asterius/BUILD.bazel +++ b/examples/asterius/BUILD.bazel @@ -12,8 +12,11 @@ load( # We first define an Haskell binary. haskell_binary( name = "main-bin", + srcs = [ + "Main.hs", + "src/Lib.hs", + ], main_file = "Main.hs", - srcs = ["Main.hs", "src/Lib.hs"], src_strip_prefix = "src/", deps = [ "@stackage//:base", @@ -31,10 +34,10 @@ ahc_dist( # the Asterius binary and Asterius bundle. asterius_binary( name = "asterius_binary", - ahc_dist_dep = ":asterius" + ahc_dist_dep = ":asterius", ) asterius_webpack( name = "asterius_bundle", - ahc_dist_dep = ":asterius" + ahc_dist_dep = ":asterius", ) diff --git a/examples/asterius/WORKSPACE b/examples/asterius/WORKSPACE index 438f4ff24..9e2abe716 100644 --- a/examples/asterius/WORKSPACE +++ b/examples/asterius/WORKSPACE @@ -21,6 +21,7 @@ load( "asterius_dependencies_bindist", "rules_haskell_asterius_toolchains", ) + asterius_dependencies_bindist() rules_haskell_asterius_toolchains() From c478f3758b8b488a405c433d03b81f9a8836c280 Mon Sep 17 00:00:00 2001 From: GuillaumeGen Date: Fri, 23 Sep 2022 11:52:21 +0200 Subject: [PATCH 4/4] Adding asterius Nix dependencies. Thanks @LBjerke --- examples/asterius/WORKSPACE | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/examples/asterius/WORKSPACE b/examples/asterius/WORKSPACE index 9e2abe716..fa192cd17 100644 --- a/examples/asterius/WORKSPACE +++ b/examples/asterius/WORKSPACE @@ -20,6 +20,7 @@ load( "@rules_haskell//haskell/asterius:repositories.bzl", "asterius_dependencies_bindist", "rules_haskell_asterius_toolchains", + "asterius_dependencies_nix", ) asterius_dependencies_bindist() @@ -30,6 +31,12 @@ rules_haskell_asterius_toolchains() # including GHC. load("@rules_haskell//haskell:nixpkgs.bzl", "haskell_register_ghc_nixpkgs") +asterius_dependencies_nix( + nix_repository = "@nixpkgs", + nixpkgs_package_rule = nixpkgs_package, + nixpkgs_nodejs = "nodejs-16_x", +) + haskell_register_ghc_nixpkgs( attribute_path = "haskell.compiler.ghc8107", repository = "@rules_haskell//nixpkgs:default.nix",