diff --git a/.bazelignore b/.bazelignore index 2dcb95fdb..c47f2f497 100644 --- a/.bazelignore +++ b/.bazelignore @@ -1,4 +1,6 @@ examples tutorial haskell/asterius/npm/node_modules +registry rules_haskell_tests +rules_haskell_nix diff --git a/.bazelrc b/.bazelrc index f6cdbd740..d0eda2519 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,130 +1,2 @@ -# See https://docs.bazel.build/versions/master/user-manual.html#bazelrc. - -# Global Configuration -# -------------------- - -# Force TLS 1.2. With TLS 1.3, we run into the following error on Darwin. -# > No subject alternative DNS name matching github-releases.githubusercontent.com found. -# It looks like this is a result of SNI being broken on TLS 1.3 which results -# in us getting a certificate for githubassets.com instead. -# -# Taken from https://github.com/digital-asset/daml/pull/8697. -startup --host_jvm_args=-Djdk.tls.client.protocols=TLSv1.2 - -common --incompatible_require_linker_input_cc_api - -common --incompatible_disallow_empty_glob=true - -# test environment does not propagate locales by default some tests reads files -# written in UTF8, we need to propagate the correct environment variables, such -# as LOCALE_ARCHIVE We also need to setup an utf8 locale -test --test_env=LANG=C.UTF-8 --test_env=LOCALE_ARCHIVE - -# Platform / Toolchain Selection -# ------------------------------ -build:linux-nixpkgs --config=nixpkgs -build:macos-nixpkgs --config=nixpkgs -build:nixpkgs --host_platform=@rules_nixpkgs_core//platforms:host - -# Build and Test Filters -# ---------------------- -# Note tag filters do not compose. If you wish to augment these filters on the -# command-line you have to repeat the full list. -build:linux-bindist --build_tag_filters -requires_nix,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist -build:linux-bindist --test_tag_filters -requires_nix,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist - -build:macos-nixpkgs --build_tag_filters -dont_test_on_darwin -build:macos-nixpkgs --test_tag_filters -dont_test_on_darwin - -build:macos-bindist --build_tag_filters -requires_nix,-dont_test_on_darwin,-dont_test_on_darwin_with_bindist,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist -build:macos-bindist --test_tag_filters -requires_nix,-dont_test_on_darwin,-dont_test_on_darwin_with_bindist,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist - -build:windows-bindist --build_tag_filters -requires_nix,-dont_test_on_windows,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist -build:windows-bindist --test_tag_filters -requires_nix,-dont_test_on_windows,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist - -# CI Configuration -# ---------------- -common:ci --config=ci-common -build:ci --config=remote-cache - -common:ci-common --color=no - -build:ci-common --loading_phase_threads=1 -build:ci-common --verbose_failures -# Make sure we don't rely on the names of convenience symlinks because those -# can be changed by user. -build:ci-common --symlink_prefix=bazel-ci- -build:ci-common --repository_cache=~/repo-cache/ -build:ci-common --experimental_repository_cache_hardlinks - -# Use a remote cache during CI -build:ci-common --bes_results_url=https://app.buildbuddy.io/invocation/ -build:ci-common --bes_backend=grpcs://remote.buildbuddy.io -build:ci-windows-bindist --bes_upload_mode=wait_for_upload_complete --bes_timeout=60s -build:remote-cache --remote_cache=grpcs://remote.buildbuddy.io -build:ci-common --remote_timeout=3600 -# Avoid failures of the form `deadline exceeded after 14999958197ns DEADLINE_EXCEEDED`. -# See https://github.com/tweag/rules_haskell/issues/1498. -build:ci-common --keep_backend_build_event_connections_alive=false - -test:ci-common --test_output=errors - -build:ci-linux-bindist --config=linux-bindist -build:ci-linux-nixpkgs --config=linux-nixpkgs -build:ci-macos-bindist --config=macos-bindist -build:ci-macos-nixpkgs --config=macos-nixpkgs -build:ci-windows-bindist --config=windows-bindist -build:ci-windows-bindist-rules-haskell --config=ci-windows-bindist -build:ci-windows-bindist-rules-haskell-tests --config=ci-windows-bindist - -# Windows is prone to leaking absolute paths into the remote cache which can -# poison the cache, so we do not share the cache between different modules. -# Increment these numbers to use a fresh cache. -# Errors of the following form might indicate such an issue: -# -# ERROR: D:/a/rules_haskell/rules_haskell/tests/runfiles/BUILD.bazel:20:10: Compiling tests/runfiles/main.cc failed: undeclared inclusion(s) in rule '//tests/runfiles:cc-runner': -# this rule is missing dependency declarations for the following files included by 'tests/runfiles/main.cc': -# '.../execroot/rules_haskell/external/rules_haskell_ghc_windows_amd64/mingw/include/c++/9.2.0/cstdlib' -# '.../execroot/rules_haskell/external/rules_haskell_ghc_windows_amd64/mingw/include/c++/9.2.0/x86_64-w64-mingw32/bits/c++config.h' -build:ci-windows-bindist-rules-haskell --remote_default_exec_properties=cache-silo-key=windows-rules-haskell-v0 -build:ci-windows-bindist-rules-haskell-tests --remote_default_exec_properties=cache-silo-key=windows-rules-haskell-tests-v0 - -# XXX: @com_google_protobuf sets `use_default_shell_env = True`, so we enable -# strict action env to avoid changes in `PATH` invalidating the cache. -build:linux-bindist --experimental_strict_action_env -build:macos-bindist --experimental_strict_action_env -build:windows-bindist --experimental_strict_action_env - -# Note [backward compatible options] -build:linux-bindist --incompatible_enable_cc_toolchain_resolution -build:macos-bindist --incompatible_enable_cc_toolchain_resolution -build:linux-nixpkgs --incompatible_enable_cc_toolchain_resolution -build:macos-nixpkgs --incompatible_enable_cc_toolchain_resolution -build:windows-bindist --incompatible_enable_cc_toolchain_resolution - -# Blocked by https://github.com/bazelbuild/bazel/issues/11704 -#build:ci --incompatible_load_cc_rules_from_bzl - -# This flag will become the default in a later version of bazel -# See https://github.com/bazelbuild/bazel/issues/8922 -build:ci --incompatible_load_proto_rules_from_bzl - -# This flag will become the default in bazel 6 -# https://github.com/tweag/rules_haskell/issues/1657 -build: --incompatible_override_toolchain_transition - -# This flag will become the default in bazel 7 -# https://github.com/bazelbuild/bazel/issues/17032 -build --incompatible_disable_starlark_host_transitions - -coverage --build_tag_filters "coverage-compatible" --test_tag_filters "coverage-compatible" --test_output=all - -# 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 - - -# User Configuration -# ------------------ -try-import %workspace%/.bazelrc.local +import %workspace%/.bazelrc.common +import %workspace%/.bazelrc.bzlmod \ No newline at end of file diff --git a/.bazelrc.bzlmod b/.bazelrc.bzlmod new file mode 100644 index 000000000..fd0a5a3a9 --- /dev/null +++ b/.bazelrc.bzlmod @@ -0,0 +1,2 @@ +common:bzlmod --experimental_enable_bzlmod +common:bzlmod --registry=file:%workspace%/registry --registry=https://bcr.bazel.build diff --git a/.bazelrc.common b/.bazelrc.common new file mode 100644 index 000000000..901be306d --- /dev/null +++ b/.bazelrc.common @@ -0,0 +1,132 @@ +# See https://docs.bazel.build/versions/master/user-manual.html#bazelrc. + +# Global Configuration +# -------------------- + +# Force TLS 1.2. With TLS 1.3, we run into the following error on Darwin. +# > No subject alternative DNS name matching github-releases.githubusercontent.com found. +# It looks like this is a result of SNI being broken on TLS 1.3 which results +# in us getting a certificate for githubassets.com instead. +# +# Taken from https://github.com/digital-asset/daml/pull/8697. +startup --host_jvm_args=-Djdk.tls.client.protocols=TLSv1.2 + +common --incompatible_require_linker_input_cc_api + +common --incompatible_disallow_empty_glob=true + +# test environment does not propagate locales by default some tests reads files +# written in UTF8, we need to propagate the correct environment variables, such +# as LOCALE_ARCHIVE We also need to setup an utf8 locale +test --test_env=LANG=C.UTF-8 --test_env=LOCALE_ARCHIVE + +# Platform / Toolchain Selection +# ------------------------------ +build:linux-nixpkgs --config=nixpkgs +build:macos-nixpkgs --config=nixpkgs +build:nixpkgs --host_platform=@rules_nixpkgs_core//platforms:host + +# Build and Test Filters +# ---------------------- +# Note tag filters do not compose. If you wish to augment these filters on the +# command-line you have to repeat the full list. +build:linux-bindist --build_tag_filters -requires_nix,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist +build:linux-bindist --test_tag_filters -requires_nix,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist + +build:macos-nixpkgs --build_tag_filters -dont_test_on_darwin +build:macos-nixpkgs --test_tag_filters -dont_test_on_darwin + +build:macos-bindist --build_tag_filters -requires_nix,-dont_test_on_darwin,-dont_test_on_darwin_with_bindist,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist +build:macos-bindist --test_tag_filters -requires_nix,-dont_test_on_darwin,-dont_test_on_darwin_with_bindist,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist + +build:windows-bindist --build_tag_filters -requires_nix,-dont_test_on_windows,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist +build:windows-bindist --test_tag_filters -requires_nix,-dont_test_on_windows,-requires_lz4,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist + +# CI Configuration +# ---------------- +common:ci --config=ci-common +build:ci --config=remote-cache + +common:ci-common --color=no + +build:ci-common --loading_phase_threads=1 +build:ci-common --verbose_failures +# Make sure we don't rely on the names of convenience symlinks because those +# can be changed by user. +build:ci-common --symlink_prefix=bazel-ci- +build:ci-common --repository_cache=~/repo-cache/ +build:ci-common --experimental_repository_cache_hardlinks + +# Use a remote cache during CI +build:ci-common --bes_results_url=https://app.buildbuddy.io/invocation/ +build:ci-common --bes_backend=grpcs://remote.buildbuddy.io +build:ci-windows-bindist --bes_upload_mode=wait_for_upload_complete --bes_timeout=60s +build:remote-cache --remote_cache=grpcs://remote.buildbuddy.io +build:ci-common --remote_timeout=3600 +# Avoid failures of the form `deadline exceeded after 14999958197ns DEADLINE_EXCEEDED`. +# See https://github.com/tweag/rules_haskell/issues/1498. +build:ci-common --keep_backend_build_event_connections_alive=false + +test:ci-common --test_output=errors + +build:ci-linux-bindist --config=linux-bindist +build:ci-linux-nixpkgs --config=linux-nixpkgs +build:ci-macos-bindist --config=macos-bindist +build:ci-macos-nixpkgs --config=macos-nixpkgs +build:ci-windows-bindist --config=windows-bindist +build:ci-windows-bindist-rules-haskell --config=ci-windows-bindist +build:ci-windows-bindist-rules-haskell-tests --config=ci-windows-bindist + +# Windows is prone to leaking absolute paths into the remote cache which can +# poison the cache, so we do not share the cache between different modules. +# Increment these numbers to use a fresh cache. +# Errors of the following form might indicate such an issue: +# +# ERROR: D:/a/rules_haskell/rules_haskell/tests/runfiles/BUILD.bazel:20:10: Compiling tests/runfiles/main.cc failed: undeclared inclusion(s) in rule '//tests/runfiles:cc-runner': +# this rule is missing dependency declarations for the following files included by 'tests/runfiles/main.cc': +# '.../execroot/rules_haskell/external/rules_haskell_ghc_windows_amd64/mingw/include/c++/9.2.0/cstdlib' +# '.../execroot/rules_haskell/external/rules_haskell_ghc_windows_amd64/mingw/include/c++/9.2.0/x86_64-w64-mingw32/bits/c++config.h' +build:ci-windows-bindist-rules-haskell --remote_default_exec_properties=cache-silo-key=windows-rules-haskell-v0 +build:ci-windows-bindist-rules-haskell-tests --remote_default_exec_properties=cache-silo-key=windows-rules-haskell-tests-v0 + +# XXX: @com_google_protobuf sets `use_default_shell_env = True`, so we enable +# strict action env to avoid changes in `PATH` invalidating the cache. +build:linux-bindist --experimental_strict_action_env +build:macos-bindist --experimental_strict_action_env +build:windows-bindist --experimental_strict_action_env + +# Note [backward compatible options] +build:linux-bindist --incompatible_enable_cc_toolchain_resolution +build:macos-bindist --incompatible_enable_cc_toolchain_resolution +build:linux-nixpkgs --incompatible_enable_cc_toolchain_resolution +build:macos-nixpkgs --incompatible_enable_cc_toolchain_resolution +build:windows-bindist --incompatible_enable_cc_toolchain_resolution + +# Blocked by https://github.com/bazelbuild/bazel/issues/11704 +#build:ci --incompatible_load_cc_rules_from_bzl + +# This flag will become the default in a later version of bazel +# See https://github.com/bazelbuild/bazel/issues/8922 +build:ci --incompatible_load_proto_rules_from_bzl + +# This flag will become the default in bazel 6 +# https://github.com/tweag/rules_haskell/issues/1657 +build: --incompatible_override_toolchain_transition + +coverage --build_tag_filters "coverage-compatible" --test_tag_filters "coverage-compatible" --test_output=all + +# 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 + +# Needed to build @rules_jvm_external//private/tools/java/com/github/bazelbuild/rules_jvm_external/zip (used by //tests/java_classpath) +build:macos-nixpkgs --tool_java_language_version=11 + +# This flag will become the default in bazel 7 +# https://github.com/bazelbuild/bazel/issues/17032 +build --incompatible_disable_starlark_host_transitions + +# User Configuration +# ------------------ +try-import %workspace%/.bazelrc.local diff --git a/.github/workflows/workflow.yaml b/.github/workflows/workflow.yaml index 95121f066..62493a4ca 100644 --- a/.github/workflows/workflow.yaml +++ b/.github/workflows/workflow.yaml @@ -17,6 +17,7 @@ jobs: matrix: os: [ubuntu-latest, macos-11] module: [rules_haskell, rules_haskell_tests] + bzlmod: [bzlmod, workspace] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 @@ -47,10 +48,16 @@ jobs: else cache_setting="--remote_header=x-buildbuddy-api-key=$BUILDBUDDY_API_KEY" fi + if [ ${{ matrix.bzlmod }} == bzlmod ]; then + bzlmod_setting='common --config bzlmod' + else + bzlmod_setting='' + fi cat >.bazelrc.local <~/.netrc <.bazelrc.local <~/.netrc <=1" diff --git a/BUILD.bazel b/BUILD.bazel index 17d34a89c..29ee3f17a 100755 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -31,7 +31,6 @@ filegroup( "stackage_snapshot.json", "stackage_snapshot.yaml", "//debug/linking_utils:all_files", - "//docs:all_files", "//haskell:all_files", "//nixpkgs:all_files", "//protobuf:all_files", diff --git a/MODULE.bazel b/MODULE.bazel index e69de29bb..cece35f8a 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -0,0 +1,291 @@ +module( + name = "rules_haskell", + version = "0.16", +) + +bazel_dep( + name = "platforms", + version = "0.0.6", +) + +bazel_dep( + name = "rules_java", + version = "6.1.0", +) + +bazel_dep( + name = "rules_license", + version = "0.0.7", +) + +bazel_dep( + name = "protobuf", + version = "3.19.0", + repo_name = "com_google_protobuf", +) + +bazel_dep( + name = "rules_cc", + version = "0.0.6", +) + +bazel_dep( + name = "rules_nodejs", + version = "5.8.2", +) + +bazel_dep( + name = "rules_python", + version = "0.21.0", +) + +bazel_dep( + name = "bazel_skylib", + version = "1.4.2", +) + +bazel_dep( + name = "rules_proto", + version = "5.3.0-21.7", +) + +bazel_dep( + name = "aspect_rules_js", + version = "1.27.1", +) + +bazel_dep( + name = "rules_sh", + version = "0.3.0", +) + +rules_haskell_dependencies = use_extension( + "//extensions:rules_haskell_dependencies.bzl", + "rules_haskell_dependencies", +) + +use_repo( + rules_haskell_dependencies, + "rules_haskell_python_local", + "rules_haskell_worker_dependencies", + "rules_haskell_stack_update", + "rules_haskell_stack", + "Cabal", +) + +register_toolchains("@rules_haskell_python_local//:toolchain") + +asterius = use_extension("@rules_haskell//haskell/asterius:extension.bzl", "rules_haskell_asterius") + +use_repo( + asterius, + "rules_haskell_asterius_webpack", + "rules_haskell_asterius_build_setting", +) + +# Setup node toolchain and install webpack for asterius. +node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node") + +node.toolchain(node_version = "16.9.0") + +npm = use_extension( + "@aspect_rules_js//npm:extensions.bzl", + "npm", +) + +npm.npm_translate_lock( + name = "rules_haskell_npm", + link_workspace = "rules_haskell", + pnpm_lock = "//haskell/asterius/npm:pnpm-lock.yaml", + verify_node_modules_ignored = "@rules_haskell//:.bazelignore", +) + +use_repo(npm, "rules_haskell_npm") + +#### Development dependencies #### +bazel_dep( + name = "rules_nixpkgs_core", + version = "0.9.0", + dev_dependency = True, +) + +bazel_dep( + name = "rules_nixpkgs_cc", + version = "0.9.0", + dev_dependency = True, +) + +bazel_dep( + name = "rules_nixpkgs_python", + version = "0.9.0", + dev_dependency = True, +) + +bazel_dep( + name = "rules_nixpkgs_go", + version = "0.9.0", + dev_dependency = True, +) + +bazel_dep( + name = "rules_nixpkgs_posix", + version = "0.9.0", + dev_dependency = True, +) + +bazel_dep( + name = "stardoc", + version = "0.5.4", + dev_dependency = True, + repo_name = "io_bazel_stardoc", +) + +bazel_dep( + name = "rules_nixpkgs_nodejs", + version = "0.9.0", + dev_dependency = True, +) + +non_module_dev_deps = use_extension( + "//:non_module_dev_deps.bzl", + "non_module_dev_deps", + dev_dependency = True, +) + +use_repo( + non_module_dev_deps, + "tutorial", + "examples", + "examples-arm", + "os_info", +) + +use_repo( + non_module_dev_deps, + "rules_haskell_ghc_nixpkgs", + "nixpkgs_default", + "rules_haskell_ghc_nixpkgs_toolchain", + "rules_haskell_ghc_darwin_amd64-toolchain", + "rules_haskell_ghc_darwin_arm64-toolchain", + "rules_haskell_ghc_linux_amd64-toolchain", + "rules_haskell_ghc_windows_amd64-toolchain", + "rules_haskell_ghc_linux_arm64-toolchain", + "rules_haskell_ghc_windows_amd64_cc_toolchain", +) + +register_toolchains( + "@rules_haskell_ghc_nixpkgs_toolchain//:toolchain", + "@rules_haskell_ghc_darwin_amd64-toolchain//:toolchain", + "@rules_haskell_ghc_darwin_arm64-toolchain//:toolchain", + "@rules_haskell_ghc_linux_amd64-toolchain//:toolchain", + "@rules_haskell_ghc_linux_arm64-toolchain//:toolchain", + "@rules_haskell_ghc_windows_amd64-toolchain//:toolchain", + "@rules_haskell_ghc_windows_amd64-toolchain//:toolchain", + "@rules_haskell_ghc_windows_amd64_cc_toolchain//:windows_cc_toolchain", + dev_dependency = True, +) + +use_repo( + non_module_dev_deps, + "nixpkgs_config_cc", + "nixpkgs_config_cc_info", + "nixpkgs_config_cc_toolchains", + "nixpkgs_python_toolchain", +) + +register_toolchains( + "@nixpkgs_config_cc_toolchains//:all", + "@nixpkgs_python_toolchain//:toolchain", + dev_dependency = True, +) + +# For buildifier +bazel_dep( + name = "rules_go", + version = "0.39.1", + dev_dependency = True, + repo_name = "io_bazel_rules_go", +) + +use_repo( + non_module_dev_deps, + "nixpkgs_go_sdk_toolchains", + "com_github_bazelbuild_buildtools", +) + +register_toolchains( + "@nixpkgs_go_sdk_toolchains//:all", + dev_dependency = True, +) + +go_sdk = use_extension( + "@io_bazel_rules_go//go:extensions.bzl", + "go_sdk", + dev_dependency = True, +) + +go_sdk.download( + version = "1.19.7", +) + +bazel_dep( + name = "gazelle", + version = "0.30.0", + dev_dependency = True, +) + +go_deps = use_extension( + "@gazelle//:extensions.bzl", + "go_deps", + dev_dependency = True, +) + +go_deps.module( + path = "github.com/gogo/protobuf", + sum = "h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=", + version = "v1.3.2", +) + +use_repo( + go_deps, + "com_github_gogo_protobuf", + "com_github_golang_mock", + "com_github_golang_protobuf", + "org_golang_google_genproto", + "org_golang_google_grpc", + "org_golang_google_protobuf", + "org_golang_x_net", + "org_golang_x_tools", +) + +# For documentation +use_repo( + non_module_dev_deps, + "linux_pandoc", + "nixpkgs_pandoc", + "macos_pandoc", + "zip", + "graphviz", + "sphinx", +) + +register_toolchains( + "@rules_haskell//docs/pandoc:nixpkgs", + "@rules_haskell//docs/pandoc:linux", + "@rules_haskell//docs/pandoc:macos", + "@rules_haskell//tests:protobuf-toolchain", + "@rules_haskell//tests:protobuf-toolchain-osx_arm64", + dev_dependency = True, +) + +non_module_dev_deps_2 = use_extension( + "//:non_module_dev_deps_2.bzl", + "non_module_dev_deps_2", + dev_dependency = True, +) + +use_repo( + non_module_dev_deps_2, + "stackage", + "stackage-exe", +) diff --git a/WORKSPACE b/WORKSPACE index fd51d954c..4140fffd5 100755 --- a/WORKSPACE +++ b/WORKSPACE @@ -54,6 +54,10 @@ load("//:non_module_dev_deps_2.bzl", _repositories_2 = "repositories") _repositories_2(bzlmod = False) +load("//extensions:rules_haskell_dependencies.bzl", _repositories_3 = "repositories") + +_repositories_3(bzlmod = False) + load( "@rules_haskell//haskell/asterius:repositories.bzl", "asterius_dependencies_bindist", diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod new file mode 100644 index 000000000..e69de29bb diff --git a/buildifier/BUILD.bazel b/buildifier/BUILD.bazel index 8c1a94310..da7b16fd7 100644 --- a/buildifier/BUILD.bazel +++ b/buildifier/BUILD.bazel @@ -30,6 +30,7 @@ buildifier_test( "//buildifier:all_files", "//debug/linking_utils:all_files", "//docs:all_files", + "//extensions:all_files", "//haskell:all_files", "//nixpkgs:all_files", "//rule_info:all_files", diff --git a/docs/haskell.rst b/docs/haskell.rst index 642073bba..1aa1d5a66 100644 --- a/docs/haskell.rst +++ b/docs/haskell.rst @@ -68,7 +68,7 @@ The first thing to do is to:: If you use the ``NixOS`` distribution, also run the following command:: - $ echo 'build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host' >> .bazelrc.local + $ echo 'build --host_platform=@rules_nixpkgs_core//platforms:host' >> .bazelrc.local Build with Bazel ---------------- diff --git a/examples/.bazelrc.bzlmod b/examples/.bazelrc.bzlmod new file mode 100644 index 000000000..98ab67d91 --- /dev/null +++ b/examples/.bazelrc.bzlmod @@ -0,0 +1,2 @@ +common:bzlmod --experimental_enable_bzlmod +common:bzlmod --registry=file://%workspace%/../registry --registry=https://bcr.bazel.build \ No newline at end of file diff --git a/examples/.bazelrc.common b/examples/.bazelrc.common new file mode 120000 index 000000000..95435188c --- /dev/null +++ b/examples/.bazelrc.common @@ -0,0 +1 @@ +../.bazelrc.common \ No newline at end of file diff --git a/examples/WORKSPACE b/examples/WORKSPACE index 517123f91..ac7cf7096 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -27,11 +27,8 @@ load("@rules_haskell//haskell:toolchain.bzl", "rules_haskell_toolchains") rules_haskell_toolchains(version = "9.2.5") -load( - "@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", - "nixpkgs_cc_configure", - "nixpkgs_python_configure", -) +load("@rules_nixpkgs_cc//:cc.bzl", "nixpkgs_cc_configure") +load("@rules_nixpkgs_python//:python.bzl", "nixpkgs_python_configure") nixpkgs_cc_configure( # Don't override the default cc toolchain needed for bindist mode. diff --git a/examples/arm/.bazelrc.bzlmod b/examples/arm/.bazelrc.bzlmod new file mode 100644 index 000000000..0ba38989a --- /dev/null +++ b/examples/arm/.bazelrc.bzlmod @@ -0,0 +1,2 @@ +common:bzlmod --experimental_enable_bzlmod +common:bzlmod --registry=file://%workspace%/../../registry --registry=https://bcr.bazel.build diff --git a/examples/arm/.bazelrc.common b/examples/arm/.bazelrc.common new file mode 120000 index 000000000..1a250488a --- /dev/null +++ b/examples/arm/.bazelrc.common @@ -0,0 +1 @@ +../../.bazelrc.common \ No newline at end of file diff --git a/examples/arm/WORKSPACE b/examples/arm/WORKSPACE index 03dfbd7ba..a2c2b45e2 100644 --- a/examples/arm/WORKSPACE +++ b/examples/arm/WORKSPACE @@ -21,12 +21,12 @@ load( "stack_snapshot", ) load( - "@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", - "nixpkgs_cc_configure", + "@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_git_repository", "nixpkgs_package", - "nixpkgs_python_configure", ) +load("@rules_nixpkgs_cc//:cc.bzl", "nixpkgs_cc_configure") +load("@rules_nixpkgs_python//:python.bzl", "nixpkgs_python_configure") nixpkgs_git_repository( name = "nixpkgs", diff --git a/extensions/BUILD.bazel b/extensions/BUILD.bazel new file mode 100644 index 000000000..84d1cad31 --- /dev/null +++ b/extensions/BUILD.bazel @@ -0,0 +1,6 @@ +filegroup( + name = "all_files", + testonly = True, + srcs = glob(["**"]), + visibility = ["//visibility:public"], +) diff --git a/extensions/rules_haskell_dependencies.bzl b/extensions/rules_haskell_dependencies.bzl new file mode 100644 index 000000000..2d0e5073c --- /dev/null +++ b/extensions/rules_haskell_dependencies.bzl @@ -0,0 +1,48 @@ +""" This module extension contains rules_haskell dependencies that are not available as modules """ + +load("@rules_haskell//haskell:repositories.bzl", "rules_haskell_dependencies_bzlmod") +load( + "@rules_haskell//haskell:ghc_bindist.bzl", + "LOCAL_PYTHON_REPO_NAME", + "configure_python3_toolchain", +) +load("@rules_haskell//tools:repositories.bzl", "rules_haskell_worker_dependencies") +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +def repositories(*, bzlmod): + rules_haskell_dependencies_bzlmod() + + if LOCAL_PYTHON_REPO_NAME not in native.existing_rules(): + configure_python3_toolchain(name = LOCAL_PYTHON_REPO_NAME, register = not bzlmod) + + # For persistent worker (tools/worker) + # TODO: make this customizable via a module extension so that users + # of persistant workers can use dependencies compatible with the + # selected toolchain. + rules_haskell_worker_dependencies() + + # TODO: Remove when tests are run with a ghc version containing Cabal >= 3.10 + # See https://github.com/tweag/rules_haskell/issues/1871 + http_archive( + name = "Cabal", + build_file_content = """ +load("@rules_haskell//haskell:cabal.bzl", "haskell_cabal_library") +haskell_cabal_library( + name = "Cabal", + srcs = glob(["Cabal/**"]), + verbose = False, + version = "3.6.3.0", + visibility = ["//visibility:public"], +) +""", + sha256 = "f69b46cb897edab3aa8d5a4bd7b8690b76cd6f0b320521afd01ddd20601d1356", + strip_prefix = "cabal-gg-8220-with-3630", + urls = ["https://github.com/tweag/cabal/archive/refs/heads/gg/8220-with-3630.zip"], + ) + +def _rules_haskell_dependencies_impl(_mctx): + repositories(bzlmod = True) + +rules_haskell_dependencies = module_extension( + implementation = _rules_haskell_dependencies_impl, +) diff --git a/haskell/BUILD.bazel b/haskell/BUILD.bazel index 787322d69..abe7c730d 100644 --- a/haskell/BUILD.bazel +++ b/haskell/BUILD.bazel @@ -40,6 +40,7 @@ exports_files( "private/cc_wrapper.py.tpl", "private/cc_wrapper_windows.sh.tpl", "private/pkgdb_to_bzl.py", + "private/haskell_runfiles.tpl", ], ) @@ -98,6 +99,7 @@ bzl_library( "@bazel_skylib//lib:sets", "@bazel_skylib//lib:shell", "@bazel_skylib//lib:versions", + "@bazel_skylib//rules:expand_template", "@rules_nixpkgs_core//:nixpkgs", "@rules_nixpkgs_posix//:posix", "@rules_sh//sh:posix", diff --git a/haskell/asterius/extension.bzl b/haskell/asterius/extension.bzl new file mode 100644 index 000000000..7c7739068 --- /dev/null +++ b/haskell/asterius/extension.bzl @@ -0,0 +1,35 @@ +""" This extension declares asterius related external repositories. +The `custom` tag can be used to declare a pre-existing webpack installation instead of rules_haskell default one. +""" + +load( + ":asterius_dependencies.bzl", + "asterius_dependencies_custom", +) + +_custom_tag = tag_class( + attrs = { + "webpack_cli_package_json_bzl": attr.label( + doc = "The label of a webpack-cli/package_json.bzl file generated by rules_js. For instance via `npm_translate_lock`.", + ), + }, + doc = "Use webpack from a custom rules_js setup instead of rules_haskell default one", +) + +def _select_webpack(mctx): + # Select the first custom tag according to the default iteration order over modules where the root module comes first. + for module in mctx.modules: + for _index, custom_tag in enumerate(module.tags.custom): + if custom_tag.webpack_cli_package_json_bzl: + return custom_tag.webpack_cli_package_json_bzl + return "@rules_haskell_npm//haskell/asterius/npm:webpack-cli/package_json.bzl" + +def _rules_haskell_asterius_impl(mctx): + asterius_dependencies_custom(webpack_cli_package_json_bzl = _select_webpack(mctx)) + +rules_haskell_asterius = module_extension( + implementation = _rules_haskell_asterius_impl, + tag_classes = { + "custom": _custom_tag, + }, +) diff --git a/haskell/defs.bzl b/haskell/defs.bzl index 1eb636080..f89e5b5ba 100644 --- a/haskell/defs.bzl +++ b/haskell/defs.bzl @@ -42,6 +42,7 @@ load( "//haskell/experimental:providers.bzl", "HaskellModuleInfo", ) +load("@bazel_skylib//rules:expand_template.bzl", "expand_template") # NOTE: Documentation needs to be added to the wrapper macros below. # Currently it is not possible to automatically inherit rule documentation in @@ -759,3 +760,52 @@ haskell_repl_aspect = _haskell_repl_aspect haskell_toolchain = _haskell_toolchain ghc_plugin = _ghc_plugin + +def haskell_runfiles( + name = "haskell_runfiles", + module_name = "Runfiles", + base = "@rules_haskell//tools/runfiles:base"): + """ Convenience wrapper around rules_haskell runfiles library. + + This wrapper is especially usefull with bzlmod, when a haskell library with runfiles is used from another module + (see [runfiles](https://github.com/tweag/rules_haskell/blob/master/tools/runfiles/README.md)). + + ### Usage + + The `haskell_runfiles` rule is called in a `BUILD` file as follows, in order to generate the `haskell_runfiles` library + (this default name can be customized). + + ```bzl + haskell_runfiles() + haskell_library( + ... + deps = [":haskell_runfiles"], + ) + ``` + + The library can then be used similarly to the runfiles library: + + ``` + import Runfiles + r <- Runfiles.create + let location = Runfiles.rlocation r "module_name/path/to/datafile" + ``` + + Args: + name: A unique name for this rule. + module_name: The name of the generated haskell module + base: The label of the `base` library to depend on. + """ + expand_template( + name = "{}_generated".format(module_name), + out = "{}.hs".format(module_name), + template = "@rules_haskell//haskell:private/haskell_runfiles.tpl", + substitutions = { + "%{module_name}": module_name, + }, + ) + haskell_library( + name = name, + srcs = ["{}.hs".format(module_name)], + deps = ["@rules_haskell//tools/runfiles", base], + ) diff --git a/haskell/ghc_bindist.bzl b/haskell/ghc_bindist.bzl index b8ed7a453..30ba305c0 100644 --- a/haskell/ghc_bindist.bzl +++ b/haskell/ghc_bindist.bzl @@ -35,6 +35,8 @@ GHC_BINDIST_DOCDIR = \ { } +LOCAL_PYTHON_REPO_NAME = "rules_haskell_python_local" + def _split_version(version): vs = version.split(".") if len(vs) != 3: @@ -499,9 +501,8 @@ def haskell_register_ghc_bindists( name = local_sh_posix_repo_name, register = register, ) - local_python_repo_name = "rules_haskell_python_local" - if local_python_repo_name not in native.existing_rules(): - _configure_python3_toolchain(name = local_python_repo_name, register = register) + if LOCAL_PYTHON_REPO_NAME not in native.existing_rules(): + configure_python3_toolchain(name = LOCAL_PYTHON_REPO_NAME, register = register) def _configure_python3_toolchain_impl(repository_ctx): cpu = get_cpu_value(repository_ctx) @@ -555,7 +556,7 @@ _config_python3_toolchain = repository_rule( environ = ["PATH"], ) -def _configure_python3_toolchain(name, register = True): +def configure_python3_toolchain(name, register = True): """Autoconfigure python3 toolchain for GHC bindist `rules_haskell` requires Python 3 to build Haskell targets. Under Nix we diff --git a/haskell/nixpkgs.bzl b/haskell/nixpkgs.bzl deleted file mode 100644 index 649ca0b59..000000000 --- a/haskell/nixpkgs.bzl +++ /dev/null @@ -1,414 +0,0 @@ -"""Workspace rules (Nixpkgs)""" - -load( - "@rules_nixpkgs_core//:nixpkgs.bzl", - "nixpkgs_package", -) -load( - "@rules_nixpkgs_posix//:posix.bzl", - "nixpkgs_sh_posix_configure", -) -load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value") -load( - ":private/pkgdb_to_bzl.bzl", - "pkgdb_to_bzl", -) -load( - ":private/workspace_utils.bzl", - "define_rule", - "execute_or_fail_loudly", - "resolve_labels", -) -load(":private/validate_attrs.bzl", "check_deprecated_attribute_usage") - -def check_ghc_version(repository_ctx): - ghc_name = "ghc-{}".format(repository_ctx.attr.version) - result = repository_ctx.execute(["ls", "lib"]) - bad_version = True - if result.return_code == 0: - for dir in result.stdout.splitlines(): - if dir.endswith(ghc_name): - bad_version = False - break - else: - result = repository_ctx.execute(["pwd"]) - fail("There is no lib folder in {}".format(result.stdout)) - if bad_version: - fail( - """\ -GHC version does not match expected version. -You specified {wanted}. -Available versions: -{actual} -""".format(wanted = ghc_name, actual = result.stdout), - ) - -def _ghc_nixpkgs_haskell_toolchain_impl(repository_ctx): - paths = resolve_labels(repository_ctx, [ - "@rules_haskell//haskell:private/pkgdb_to_bzl.py", - ]) - compiler_flags_select = "select({})".format( - repository_ctx.attr.compiler_flags_select or { - "//conditions:default": [], - }, - ) - nixpkgs_ghc_path = repository_ctx.path(repository_ctx.attr.nixpkgs_ghc).dirname.dirname - - # Symlink content of ghc external repo. In effect, this repo has - # the same content, but with a BUILD file that includes generated - # content (not a static one like nixpkgs_package supports). - for target in _find_children(repository_ctx, nixpkgs_ghc_path): - basename = target.rpartition("/")[-1] - repository_ctx.symlink(target, basename) - - ghc_name = "ghc-{}".format(repository_ctx.attr.version) - check_ghc_version(repository_ctx) - - toolchain_libraries = pkgdb_to_bzl(repository_ctx, paths, "lib/{}".format(ghc_name))["file_content"] - locale_archive = repository_ctx.attr.locale_archive - libdir_path = execute_or_fail_loudly(repository_ctx, ["bin/ghc", "--print-libdir"]).stdout.strip() - docdir_path = execute_or_fail_loudly(repository_ctx, ["bin/ghc-pkg", "field", "base", "haddock-html", "--simple-output"]).stdout.strip() - toolchain = define_rule( - "haskell_toolchain", - name = "toolchain-impl", - libraries = "toolchain_libraries", - # See Note [GHC toolchain files] in haskell/ghc_bindist.bzl - libdir_path = repr(libdir_path), - docdir_path = repr(docdir_path), - tools = ["@{}//:bin".format(repository_ctx.attr.nixpkgs_ghc_repo_name)], - version = repr(repository_ctx.attr.version), - static_runtime = repository_ctx.attr.static_runtime, - fully_static_link = repository_ctx.attr.fully_static_link, - ghcopts = "{} + {}".format( - repository_ctx.attr.ghcopts, - compiler_flags_select, - ), - haddock_flags = repository_ctx.attr.haddock_flags, - repl_ghci_args = repository_ctx.attr.repl_ghci_args, - cabalopts = repository_ctx.attr.cabalopts, - locale_archive = repr(locale_archive) if locale_archive else None, - locale = repr(repository_ctx.attr.locale), - ) - repository_ctx.file( - "BUILD", - executable = False, - content = """ -load( - "@rules_haskell//haskell:defs.bzl", - "haskell_import", - "haskell_toolchain", -) - -package(default_visibility = ["//visibility:public"]) - -filegroup( - name = "bin", - srcs = glob(["bin/*"]), -) - -{toolchain_libraries} - -{toolchain} - """.format( - toolchain_libraries = toolchain_libraries, - toolchain = toolchain, - ), - ) - -_ghc_nixpkgs_haskell_toolchain = repository_rule( - _ghc_nixpkgs_haskell_toolchain_impl, - attrs = { - # These attributes just forward to haskell_toolchain. - # They are documented there. - "version": attr.string(), - "static_runtime": attr.bool(), - "fully_static_link": attr.bool(), - "ghcopts": attr.string_list(), - "compiler_flags_select": attr.string_list_dict(), - "haddock_flags": attr.string_list(), - "cabalopts": attr.string_list(), - "repl_ghci_args": attr.string_list(), - "locale_archive": attr.string(), - "nixpkgs_ghc_repo_name": attr.string(), - # Unfortunately, repositories cannot depend on each other - # directly. They can only depend on files inside each - # repository. We need to be careful to depend on files that - # change anytime any content in a repository changes, like - # bin/ghc, which embeds the output path, which itself changes - # if any input to the derivation changed. - "nixpkgs_ghc": attr.label(), - "locale": attr.string( - default = "C.UTF-8", - ), - }, -) - -def _ghc_nixpkgs_toolchain_impl(repository_ctx): - # These constraints might look tautological, because they always - # match the host platform if it is the same as the target - # platform. But they are important to state because Bazel - # toolchain resolution prefers other toolchains with more specific - # constraints otherwise. - if repository_ctx.attr.target_constraints == [] and repository_ctx.attr.exec_constraints == []: - cpu_value = get_cpu_value(repository_ctx) - target_constraints = ["@platforms//cpu:{}".format( - "arm64" if ("arm64" in cpu_value or "aarch64" in cpu_value) else "x86_64", - )] - if repository_ctx.os.name == "linux": - target_constraints.append("@platforms//os:linux") - elif repository_ctx.os.name == "mac os x": - target_constraints.append("@platforms//os:osx") - exec_constraints = list(target_constraints) - else: - target_constraints = repository_ctx.attr.target_constraints - exec_constraints = list(repository_ctx.attr.exec_constraints) - - exec_constraints.append("@rules_nixpkgs_core//constraints:support_nix") - - repository_ctx.file( - "BUILD", - executable = False, - content = """ -toolchain( - name = "toolchain", - toolchain_type = "@rules_haskell//haskell:toolchain", - toolchain = "@{haskell_toolchain}//:toolchain-impl", - exec_compatible_with = {exec_constraints}, - target_compatible_with = {target_constraints}, -) - """.format( - exec_constraints = exec_constraints, - target_constraints = target_constraints, - haskell_toolchain = repository_ctx.attr.haskell_toolchain_repo_name, - ), - ) - -_ghc_nixpkgs_toolchain = repository_rule( - implementation = _ghc_nixpkgs_toolchain_impl, - attrs = { - "exec_constraints": attr.string_list(), - "target_constraints": attr.string_list(), - "haskell_toolchain_repo_name": attr.string(), - }, -) - -def haskell_register_ghc_nixpkgs( - version, - name = "rules_haskell", - is_static = None, # DEPRECATED. See _check_static_attributes_compatibility. - static_runtime = None, - fully_static_link = None, - build_file = None, - build_file_content = None, - compiler_flags = None, - ghcopts = None, - compiler_flags_select = None, - haddock_flags = None, - repl_ghci_args = None, - cabalopts = None, - locale_archive = None, - attribute_path = "haskellPackages.ghc", - sh_posix_attributes = None, - nix_file = None, - nix_file_deps = [], - nixopts = None, - locale = None, - repositories = {}, - repository = None, - nix_file_content = None, - exec_constraints = None, - target_constraints = None, - register = True): - """Register a package from Nixpkgs as a toolchain. - - Toolchains can be used to compile Haskell code. To have this - toolchain selected during [toolchain - resolution][toolchain-resolution], set a host platform that - includes the - `@io_tweag_rules_nixpkgs//nixpkgs/constraints:support_nix` - constraint value. - - [toolchain-resolution]: https://docs.bazel.build/versions/master/toolchains.html#toolchain-resolution - - ### Examples - - ``` - haskell_register_ghc_nixpkgs( - locale_archive = "@glibc_locales//:locale-archive", - atttribute_path = "haskellPackages.ghc", - version = "1.2.3", # The version of GHC - ) - ``` - - Setting the host platform can be done on the command-line like - in the following: - - ``` - --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host - ``` - - Args: - is_static: Deprecated. The functionality it previously gated - (supporting GHC versions with static runtime systems) now sits under - static_runtime, a name chosen to avoid confusion with the new flag - fully_static_link, which controls support for fully-statically-linked - binaries. During the deprecation period, we rewrite is_static to - static_runtime in this macro as long as the new attributes aren't also - used. This argument and supporting code should be removed in a future release. - static_runtime: True if and only if a static GHC runtime is to be used. This is - required in order to use statically-linked Haskell libraries with GHCi - and Template Haskell. - fully_static_link: True if and only if fully-statically-linked binaries are to be built. - compiler_flags: DEPRECADED. Use new name ghcopts. - ghcopts: A collection of flags that will be passed to GHC - compiler_flags_select: temporary workaround to pass conditional arguments. - See https://github.com/bazelbuild/bazel/issues/9199 for details. - attribute_path: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-attribute_path) - build_file: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-build_file) - build_file_content: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-build_file_content) - nix_file: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-nix_file) - nix_file_deps: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-nix_file_deps) - nix_file_content: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-nix_file_content) - nixopts: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-nixopts) - repositories: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-repositories) - repository: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-repository) - sh_posix_attributes: List of attribute paths to extract standard Unix shell tools from. - Passed to [nixpkgs_sh_posix_configure](https://github.com/tweag/rules_nixpkgs#nixpkgs_sh_posix_configure). - register: Whether to register the toolchain (must be set to False if bzlmod is enabled) - """ - nixpkgs_ghc_repo_name = "{}_ghc_nixpkgs".format(name) - nixpkgs_sh_posix_repo_name = "{}_sh_posix_nixpkgs".format(name) - haskell_toolchain_repo_name = "{}_ghc_nixpkgs_haskell_toolchain".format(name) - toolchain_repo_name = "{}_ghc_nixpkgs_toolchain".format(name) - - static_runtime, fully_static_link = _check_static_attributes_compatibility( - is_static = is_static, - static_runtime = static_runtime, - fully_static_link = fully_static_link, - ) - - # The package from the system. - nixpkgs_package( - name = nixpkgs_ghc_repo_name, - attribute_path = attribute_path, - build_file = build_file, - build_file_content = build_file_content, - nix_file = nix_file, - nix_file_deps = nix_file_deps, - nix_file_content = nix_file_content, - nixopts = nixopts, - repositories = repositories, - repository = repository, - ) - - # haskell_toolchain + haskell_import definitions. - ghcopts = check_deprecated_attribute_usage( - old_attr_name = "compiler_flags", - old_attr_value = compiler_flags, - new_attr_name = "ghcopts", - new_attr_value = ghcopts, - ) - - _ghc_nixpkgs_haskell_toolchain( - name = haskell_toolchain_repo_name, - version = version, - static_runtime = static_runtime, - fully_static_link = fully_static_link, - ghcopts = ghcopts, - compiler_flags_select = compiler_flags_select, - haddock_flags = haddock_flags, - cabalopts = cabalopts, - repl_ghci_args = repl_ghci_args, - locale_archive = locale_archive, - locale = locale, - nixpkgs_ghc_repo_name = nixpkgs_ghc_repo_name, - nixpkgs_ghc = "@{}//:bin/ghc".format(nixpkgs_ghc_repo_name), - ) - - # toolchain definition. - if (exec_constraints == None) != (target_constraints == None): - fail("Both exec_constraints and target_constraints need to be provided or none of them.") - _ghc_nixpkgs_toolchain( - name = toolchain_repo_name, - exec_constraints = exec_constraints, - target_constraints = target_constraints, - haskell_toolchain_repo_name = haskell_toolchain_repo_name, - ) - if register: - native.register_toolchains("@{}//:toolchain".format(toolchain_repo_name)) - - # Unix tools toolchain required for Cabal packages - sh_posix_nixpkgs_kwargs = dict( - nix_file_deps = nix_file_deps, - nixopts = nixopts, - repositories = repositories, - repository = repository, - ) - if sh_posix_attributes != None: - sh_posix_nixpkgs_kwargs["packages"] = sh_posix_attributes - nixpkgs_sh_posix_configure( - name = nixpkgs_sh_posix_repo_name, - register = register, - **sh_posix_nixpkgs_kwargs - ) - -def _check_static_attributes_compatibility(is_static, static_runtime, fully_static_link): - """Asserts that attributes passed to `haskell_register_ghc_nixpkgs` for - controlling use of GHC's static runtime and whether or not to build - fully-statically-linked binaries are compatible. - - Args: - is_static: Deprecated. The functionality it previously gated - (supporting GHC versions with static runtime systems) now sits under - static_runtime, a name chosen to avoid confusion with the new flag - fully_static_link, which controls support for fully-statically-linked - binaries. During the deprecation period, we rewrite is_static to - static_runtime in this macro as long as the new attributes aren't also - used. This argument and supporting code should be removed in a future release. - static_runtime: True if and only if a static GHC runtime is to be used. This is - required in order to use statically-linked Haskell libraries with GHCi - and Template Haskell. - fully_static_link: True if and only if fully-statically-linked binaries are to be built. - - Returns: - A tuple of static_runtime, fully_static_link attributes, which are guaranteed - not to be None, taking into account the deprecated is_static argument. - """ - - # Check for use of the deprecated `is_static` attribute. - if is_static != None: - if static_runtime != None or fully_static_link != None: - fail("is_static is deprecated. Please use the static_runtime attribute instead.") - - print("WARNING: is_static is deprecated. Please use the static_runtime attribute instead.") - static_runtime = is_static - - # Currently we do not support the combination of a dynamic runtime system - # and fully-statically-linked binaries, so fail if this has been selected. - if not static_runtime and fully_static_link: - fail( - """\ -Fully-statically-linked binaries with a dynamic runtime are not currently supported. -Please pass static_runtime = True if you wish to build fully-statically-linked binaries. -""", - ) - - return bool(static_runtime), bool(fully_static_link) - -def _find_children(repository_ctx, target_dir): - find_args = [ - "find", - "-L", - target_dir, - "-maxdepth", - "1", - # otherwise the directory is printed as well - "-mindepth", - "1", - # filenames can contain \n - "-print0", - ] - exec_result = repository_ctx.execute(find_args) - if exec_result.return_code: - fail("_find_children() failed.") - return exec_result.stdout.rstrip("\0").split("\0") diff --git a/haskell/nixpkgs.bzl b/haskell/nixpkgs.bzl new file mode 120000 index 000000000..612b0cad7 --- /dev/null +++ b/haskell/nixpkgs.bzl @@ -0,0 +1 @@ +../rules_haskell_nix/nixpkgs.bzl \ No newline at end of file diff --git a/haskell/platforms/BUILD.bazel b/haskell/platforms/BUILD.bazel index c363ecbaa..28fc88d40 100644 --- a/haskell/platforms/BUILD.bazel +++ b/haskell/platforms/BUILD.bazel @@ -20,14 +20,14 @@ alias( alias( name = "linux_x86_64_nixpkgs", actual = "@rules_nixpkgs_core//platforms:host", - deprecation = "Use @io_tweag_rules_nixpkgs//nixpkgs/platforms:host instead.", + deprecation = "Use @rules_nixpkgs_core//platforms:host instead.", visibility = ["//visibility:public"], ) alias( name = "darwin_x86_64_nixpkgs", actual = "@rules_nixpkgs_core//platforms:host", - deprecation = "Use @io_tweag_rules_nixpkgs//nixpkgs/platforms:host instead.", + deprecation = "Use @rules_nixpkgs_core//platforms:host instead.", visibility = ["//visibility:public"], ) diff --git a/haskell/private/haskell_runfiles.tpl b/haskell/private/haskell_runfiles.tpl new file mode 100644 index 000000000..f94db1bf2 --- /dev/null +++ b/haskell/private/haskell_runfiles.tpl @@ -0,0 +1,6 @@ +{-# LANGUAGE CPP #-} +module %{module_name} (create, createFromProgramPath, rlocation) where +import qualified Bazel.Runfiles as RulesHaskellRunfiles +create = RulesHaskellRunfiles.createFromCurrentFile __FILE__ +createFromProgramPath = RulesHaskellRunfiles.createFromProgramPathAndCurrentFile (Just __FILE__) +rlocation = RulesHaskellRunfiles.rlocation \ No newline at end of file diff --git a/haskell/repositories.bzl b/haskell/repositories.bzl index 3d485ce9b..df1456895 100644 --- a/haskell/repositories.bzl +++ b/haskell/repositories.bzl @@ -56,10 +56,10 @@ def rules_haskell_dependencies(): http_archive, name = "bazel_skylib", urls = [ - "https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz", - "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz", + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz", + "https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz", ], - sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d", + sha256 = "66ffd9315665bfaafc96b52278f57c7e2dd09f5ede279ea6d39b2be471e7e3aa", ) maybe( diff --git a/non_module_dev_deps.bzl b/non_module_dev_deps.bzl index 35d2e7051..a4e4b81ce 100644 --- a/non_module_dev_deps.bzl +++ b/non_module_dev_deps.bzl @@ -2,7 +2,6 @@ load("@rules_haskell//tools:os_info.bzl", "os_info") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@rules_haskell//tools:repositories.bzl", "rules_haskell_worker_dependencies") load( "@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_local_repository", @@ -50,28 +49,6 @@ def repositories(*, bzlmod): # Some helpers for platform-dependent configuration os_info(name = "os_info") - # For persistent worker (tools/worker) - rules_haskell_worker_dependencies() - - # TODO: Remove when tests are run with a ghc version containing Cabal >= 3.10 - # See https://github.com/tweag/rules_haskell/issues/1871 - http_archive( - name = "Cabal", - build_file_content = """ -load("@rules_haskell//haskell:cabal.bzl", "haskell_cabal_library") -haskell_cabal_library( - name = "Cabal", - srcs = glob(["Cabal/**"]), - verbose = False, - version = "3.6.3.0", - visibility = ["//visibility:public"], -) -""", - sha256 = "f69b46cb897edab3aa8d5a4bd7b8690b76cd6f0b320521afd01ddd20601d1356", - strip_prefix = "cabal-gg-8220-with-3630", - urls = ["https://github.com/tweag/cabal/archive/refs/heads/gg/8220-with-3630.zip"], - ) - starlarkified_local_repository( name = "tutorial", path = "tutorial", diff --git a/registry/bazel_registry.json b/registry/bazel_registry.json new file mode 100644 index 000000000..dae3ff272 --- /dev/null +++ b/registry/bazel_registry.json @@ -0,0 +1,4 @@ +{ + "mirrors": [], + "module_base_path": ".." +} diff --git a/registry/modules/rules_haskell/0.16/MODULE.bazel b/registry/modules/rules_haskell/0.16/MODULE.bazel new file mode 120000 index 000000000..9ea0cf85e --- /dev/null +++ b/registry/modules/rules_haskell/0.16/MODULE.bazel @@ -0,0 +1 @@ +../../../../MODULE.bazel \ No newline at end of file diff --git a/registry/modules/rules_haskell/0.16/source.json b/registry/modules/rules_haskell/0.16/source.json new file mode 100644 index 000000000..d898f7c0c --- /dev/null +++ b/registry/modules/rules_haskell/0.16/source.json @@ -0,0 +1,4 @@ +{ + "type": "local_path", + "path": "." +} diff --git a/registry/modules/rules_haskell/metadata.json b/registry/modules/rules_haskell/metadata.json new file mode 100644 index 000000000..7c9cbe95a --- /dev/null +++ b/registry/modules/rules_haskell/metadata.json @@ -0,0 +1,6 @@ +{ + "homepage": "", + "maintainers": [], + "versions": ["0.16"], + "yanked_versions": [] +} diff --git a/registry/modules/rules_haskell_nix/0.16/MODULE.bazel b/registry/modules/rules_haskell_nix/0.16/MODULE.bazel new file mode 120000 index 000000000..73c60c443 --- /dev/null +++ b/registry/modules/rules_haskell_nix/0.16/MODULE.bazel @@ -0,0 +1 @@ +../../../../rules_haskell_nix/MODULE.bazel \ No newline at end of file diff --git a/registry/modules/rules_haskell_nix/0.16/source.json b/registry/modules/rules_haskell_nix/0.16/source.json new file mode 100644 index 000000000..fcc38ed1d --- /dev/null +++ b/registry/modules/rules_haskell_nix/0.16/source.json @@ -0,0 +1,4 @@ +{ + "type": "local_path", + "path": "rules_haskell_nix" +} diff --git a/registry/modules/rules_haskell_nix/metadata.json b/registry/modules/rules_haskell_nix/metadata.json new file mode 100644 index 000000000..7c9cbe95a --- /dev/null +++ b/registry/modules/rules_haskell_nix/metadata.json @@ -0,0 +1,6 @@ +{ + "homepage": "", + "maintainers": [], + "versions": ["0.16"], + "yanked_versions": [] +} diff --git a/registry/modules/rules_nixpkgs_cc/0.9.0/MODULE.bazel b/registry/modules/rules_nixpkgs_cc/0.9.0/MODULE.bazel new file mode 100644 index 000000000..28c3bd634 --- /dev/null +++ b/registry/modules/rules_nixpkgs_cc/0.9.0/MODULE.bazel @@ -0,0 +1,8 @@ +module( + name = "rules_nixpkgs_cc", + version = "0.9.0", +) + +bazel_dep(name = "rules_nixpkgs_core", version = "0.9.0") +bazel_dep(name = "bazel_skylib", version = "1.0.3") +bazel_dep(name = "rules_cc", version = "0.0.1") diff --git a/registry/modules/rules_nixpkgs_cc/0.9.0/source.json b/registry/modules/rules_nixpkgs_cc/0.9.0/source.json new file mode 100644 index 000000000..11b332a61 --- /dev/null +++ b/registry/modules/rules_nixpkgs_cc/0.9.0/source.json @@ -0,0 +1,5 @@ +{ + "integrity" : "sha256-njiYozxfIfY0qp4tRWIOfEttVNFtRzVxqJEZO71HJco", + "strip_prefix" : "rules_nixpkgs-0c1f8f5470c7f292b7620762e224f53d837929d3/toolchains/cc", + "url": "https://github.com/tweag/rules_nixpkgs/archive/0c1f8f5470c7f292b7620762e224f53d837929d3.tar.gz" +} diff --git a/registry/modules/rules_nixpkgs_cc/metadata.json b/registry/modules/rules_nixpkgs_cc/metadata.json new file mode 100644 index 000000000..92a394339 --- /dev/null +++ b/registry/modules/rules_nixpkgs_cc/metadata.json @@ -0,0 +1,17 @@ +{ + "homepage": "", + "maintainers": [ + { + "email": "benjamin.radford@tweag.io ", + "github": "benradf", + "name": "Benjamin Radford" + }, + { + "email": "andreas.herrmann@tweag.io", + "github": "aherrmann", + "name": "Andreas Herrmann" + } + ], + "versions": ["0.9.0"], + "yanked_versions": [] +} diff --git a/registry/modules/rules_nixpkgs_core/0.9.0/MODULE.bazel b/registry/modules/rules_nixpkgs_core/0.9.0/MODULE.bazel new file mode 100644 index 000000000..2608e78f1 --- /dev/null +++ b/registry/modules/rules_nixpkgs_core/0.9.0/MODULE.bazel @@ -0,0 +1,16 @@ +module( + name = "rules_nixpkgs_core", + version = "0.9.0", +) + +bazel_dep(name = "platforms", version = "0.0.4") +bazel_dep(name = "bazel_skylib", version = "1.0.3") + +nix_repo = use_extension("//extensions:repository.bzl", "nix_repo") +use_repo(nix_repo, "nixpkgs_repositories") +nix_repo.override(name = "nixpkgs") +nix_repo.github( + name = "nixpkgs", + tag = "22.11", + sha256 = "ddc3428d9e1a381b7476750ac4dbea7a42885cbbe6e1af44b21d6447c9609a6f", +) \ No newline at end of file diff --git a/registry/modules/rules_nixpkgs_core/0.9.0/source.json b/registry/modules/rules_nixpkgs_core/0.9.0/source.json new file mode 100644 index 000000000..35aa2faea --- /dev/null +++ b/registry/modules/rules_nixpkgs_core/0.9.0/source.json @@ -0,0 +1,6 @@ +{ + "integrity" : "sha256-njiYozxfIfY0qp4tRWIOfEttVNFtRzVxqJEZO71HJco", + "strip_prefix" : "rules_nixpkgs-0c1f8f5470c7f292b7620762e224f53d837929d3/core", + "url": "https://github.com/tweag/rules_nixpkgs/archive/0c1f8f5470c7f292b7620762e224f53d837929d3.tar.gz" +} + diff --git a/registry/modules/rules_nixpkgs_core/metadata.json b/registry/modules/rules_nixpkgs_core/metadata.json new file mode 100644 index 000000000..92a394339 --- /dev/null +++ b/registry/modules/rules_nixpkgs_core/metadata.json @@ -0,0 +1,17 @@ +{ + "homepage": "", + "maintainers": [ + { + "email": "benjamin.radford@tweag.io ", + "github": "benradf", + "name": "Benjamin Radford" + }, + { + "email": "andreas.herrmann@tweag.io", + "github": "aherrmann", + "name": "Andreas Herrmann" + } + ], + "versions": ["0.9.0"], + "yanked_versions": [] +} diff --git a/registry/modules/rules_nixpkgs_go/0.9.0/MODULE.bazel b/registry/modules/rules_nixpkgs_go/0.9.0/MODULE.bazel new file mode 100644 index 000000000..c021a884e --- /dev/null +++ b/registry/modules/rules_nixpkgs_go/0.9.0/MODULE.bazel @@ -0,0 +1,9 @@ +module( + name = "rules_nixpkgs_go", + version = "0.9.0", +) + +bazel_dep(name = "rules_nixpkgs_core", version = "0.9.0") +bazel_dep(name = "rules_go", repo_name = "io_bazel_rules_go", version = "0.39.1") +bazel_dep(name = "bazel_skylib", version = "1.0.3") +bazel_dep(name = "platforms", version = "0.0.4") diff --git a/registry/modules/rules_nixpkgs_go/0.9.0/source.json b/registry/modules/rules_nixpkgs_go/0.9.0/source.json new file mode 100644 index 000000000..8df9d35dc --- /dev/null +++ b/registry/modules/rules_nixpkgs_go/0.9.0/source.json @@ -0,0 +1,5 @@ +{ + "integrity" : "sha256-njiYozxfIfY0qp4tRWIOfEttVNFtRzVxqJEZO71HJco", + "strip_prefix" : "rules_nixpkgs-0c1f8f5470c7f292b7620762e224f53d837929d3/toolchains/go", + "url": "https://github.com/tweag/rules_nixpkgs/archive/0c1f8f5470c7f292b7620762e224f53d837929d3.tar.gz" +} diff --git a/registry/modules/rules_nixpkgs_go/metadata.json b/registry/modules/rules_nixpkgs_go/metadata.json new file mode 100644 index 000000000..92a394339 --- /dev/null +++ b/registry/modules/rules_nixpkgs_go/metadata.json @@ -0,0 +1,17 @@ +{ + "homepage": "", + "maintainers": [ + { + "email": "benjamin.radford@tweag.io ", + "github": "benradf", + "name": "Benjamin Radford" + }, + { + "email": "andreas.herrmann@tweag.io", + "github": "aherrmann", + "name": "Andreas Herrmann" + } + ], + "versions": ["0.9.0"], + "yanked_versions": [] +} diff --git a/registry/modules/rules_nixpkgs_java/0.9.0/MODULE.bazel b/registry/modules/rules_nixpkgs_java/0.9.0/MODULE.bazel new file mode 100644 index 000000000..008d8f603 --- /dev/null +++ b/registry/modules/rules_nixpkgs_java/0.9.0/MODULE.bazel @@ -0,0 +1,8 @@ +module( + name = "rules_nixpkgs_java", + version = "0.9.0", +) + +bazel_dep(name = "rules_nixpkgs_core", version = "0.9.0") +bazel_dep(name = "rules_java", version = "4.0.0") +bazel_dep(name = "bazel_skylib", version = "1.0.3") diff --git a/registry/modules/rules_nixpkgs_java/0.9.0/source.json b/registry/modules/rules_nixpkgs_java/0.9.0/source.json new file mode 100644 index 000000000..7db11cd14 --- /dev/null +++ b/registry/modules/rules_nixpkgs_java/0.9.0/source.json @@ -0,0 +1,6 @@ +{ + "integrity" : "sha256-njiYozxfIfY0qp4tRWIOfEttVNFtRzVxqJEZO71HJco", + "strip_prefix" : "rules_nixpkgs-0c1f8f5470c7f292b7620762e224f53d837929d3/toolchains/java", + "url": "https://github.com/tweag/rules_nixpkgs/archive/0c1f8f5470c7f292b7620762e224f53d837929d3.tar.gz" +} + diff --git a/registry/modules/rules_nixpkgs_java/metadata.json b/registry/modules/rules_nixpkgs_java/metadata.json new file mode 100644 index 000000000..92a394339 --- /dev/null +++ b/registry/modules/rules_nixpkgs_java/metadata.json @@ -0,0 +1,17 @@ +{ + "homepage": "", + "maintainers": [ + { + "email": "benjamin.radford@tweag.io ", + "github": "benradf", + "name": "Benjamin Radford" + }, + { + "email": "andreas.herrmann@tweag.io", + "github": "aherrmann", + "name": "Andreas Herrmann" + } + ], + "versions": ["0.9.0"], + "yanked_versions": [] +} diff --git a/registry/modules/rules_nixpkgs_nodejs/0.9.0/MODULE.bazel b/registry/modules/rules_nixpkgs_nodejs/0.9.0/MODULE.bazel new file mode 100644 index 000000000..f2a65d3ee --- /dev/null +++ b/registry/modules/rules_nixpkgs_nodejs/0.9.0/MODULE.bazel @@ -0,0 +1,8 @@ +module( + name = "rules_nixpkgs_nodejs", + version = "0.9.0", +) + +bazel_dep(name = "rules_nixpkgs_core", version = "0.9.0") +bazel_dep(name = "rules_nodejs", version = "5.5.3") +bazel_dep(name = "bazel_skylib", version = "1.0.3") \ No newline at end of file diff --git a/registry/modules/rules_nixpkgs_nodejs/0.9.0/source.json b/registry/modules/rules_nixpkgs_nodejs/0.9.0/source.json new file mode 100644 index 000000000..862b9d70e --- /dev/null +++ b/registry/modules/rules_nixpkgs_nodejs/0.9.0/source.json @@ -0,0 +1,6 @@ +{ + "integrity" : "sha256-njiYozxfIfY0qp4tRWIOfEttVNFtRzVxqJEZO71HJco", + "strip_prefix" : "rules_nixpkgs-0c1f8f5470c7f292b7620762e224f53d837929d3/toolchains/nodejs", + "url": "https://github.com/tweag/rules_nixpkgs/archive/0c1f8f5470c7f292b7620762e224f53d837929d3.tar.gz" +} + diff --git a/registry/modules/rules_nixpkgs_nodejs/metadata.json b/registry/modules/rules_nixpkgs_nodejs/metadata.json new file mode 100644 index 000000000..92a394339 --- /dev/null +++ b/registry/modules/rules_nixpkgs_nodejs/metadata.json @@ -0,0 +1,17 @@ +{ + "homepage": "", + "maintainers": [ + { + "email": "benjamin.radford@tweag.io ", + "github": "benradf", + "name": "Benjamin Radford" + }, + { + "email": "andreas.herrmann@tweag.io", + "github": "aherrmann", + "name": "Andreas Herrmann" + } + ], + "versions": ["0.9.0"], + "yanked_versions": [] +} diff --git a/registry/modules/rules_nixpkgs_posix/0.9.0/MODULE.bazel b/registry/modules/rules_nixpkgs_posix/0.9.0/MODULE.bazel new file mode 100644 index 000000000..0048bef23 --- /dev/null +++ b/registry/modules/rules_nixpkgs_posix/0.9.0/MODULE.bazel @@ -0,0 +1,8 @@ +module( + name = "rules_nixpkgs_posix", + version = "0.9.0", +) + +bazel_dep(name = "rules_nixpkgs_core", version = "0.9.0") +bazel_dep(name = "rules_sh", version = "0.3.0") +bazel_dep(name = "bazel_skylib", version = "1.0.3") diff --git a/registry/modules/rules_nixpkgs_posix/0.9.0/source.json b/registry/modules/rules_nixpkgs_posix/0.9.0/source.json new file mode 100644 index 000000000..ffacabb28 --- /dev/null +++ b/registry/modules/rules_nixpkgs_posix/0.9.0/source.json @@ -0,0 +1,6 @@ +{ + "integrity" : "sha256-njiYozxfIfY0qp4tRWIOfEttVNFtRzVxqJEZO71HJco", + "strip_prefix" : "rules_nixpkgs-0c1f8f5470c7f292b7620762e224f53d837929d3/toolchains/posix", + "url": "https://github.com/tweag/rules_nixpkgs/archive/0c1f8f5470c7f292b7620762e224f53d837929d3.tar.gz" +} + diff --git a/registry/modules/rules_nixpkgs_posix/metadata.json b/registry/modules/rules_nixpkgs_posix/metadata.json new file mode 100644 index 000000000..92a394339 --- /dev/null +++ b/registry/modules/rules_nixpkgs_posix/metadata.json @@ -0,0 +1,17 @@ +{ + "homepage": "", + "maintainers": [ + { + "email": "benjamin.radford@tweag.io ", + "github": "benradf", + "name": "Benjamin Radford" + }, + { + "email": "andreas.herrmann@tweag.io", + "github": "aherrmann", + "name": "Andreas Herrmann" + } + ], + "versions": ["0.9.0"], + "yanked_versions": [] +} diff --git a/registry/modules/rules_nixpkgs_python/0.9.0/MODULE.bazel b/registry/modules/rules_nixpkgs_python/0.9.0/MODULE.bazel new file mode 100644 index 000000000..1fe15b52c --- /dev/null +++ b/registry/modules/rules_nixpkgs_python/0.9.0/MODULE.bazel @@ -0,0 +1,7 @@ +module( + name = "rules_nixpkgs_python", + version = "0.9.0", +) + +bazel_dep(name = "rules_nixpkgs_core", version = "0.9.0") +bazel_dep(name = "bazel_skylib", version = "1.0.3") diff --git a/registry/modules/rules_nixpkgs_python/0.9.0/source.json b/registry/modules/rules_nixpkgs_python/0.9.0/source.json new file mode 100644 index 000000000..759703c85 --- /dev/null +++ b/registry/modules/rules_nixpkgs_python/0.9.0/source.json @@ -0,0 +1,6 @@ +{ + "integrity" : "sha256-njiYozxfIfY0qp4tRWIOfEttVNFtRzVxqJEZO71HJco", + "strip_prefix" : "rules_nixpkgs-0c1f8f5470c7f292b7620762e224f53d837929d3/toolchains/python", + "url": "https://github.com/tweag/rules_nixpkgs/archive/0c1f8f5470c7f292b7620762e224f53d837929d3.tar.gz" +} + diff --git a/registry/modules/rules_nixpkgs_python/metadata.json b/registry/modules/rules_nixpkgs_python/metadata.json new file mode 100644 index 000000000..92a394339 --- /dev/null +++ b/registry/modules/rules_nixpkgs_python/metadata.json @@ -0,0 +1,17 @@ +{ + "homepage": "", + "maintainers": [ + { + "email": "benjamin.radford@tweag.io ", + "github": "benradf", + "name": "Benjamin Radford" + }, + { + "email": "andreas.herrmann@tweag.io", + "github": "aherrmann", + "name": "Andreas Herrmann" + } + ], + "versions": ["0.9.0"], + "yanked_versions": [] +} diff --git a/registry/modules/rules_nixpkgs_rust/0.9.0/MODULE.bazel b/registry/modules/rules_nixpkgs_rust/0.9.0/MODULE.bazel new file mode 100644 index 000000000..760d9c3d7 --- /dev/null +++ b/registry/modules/rules_nixpkgs_rust/0.9.0/MODULE.bazel @@ -0,0 +1,12 @@ +module( + name = "rules_nixpkgs_rust", + version = "0.9.0", +) + +bazel_dep(name = "rules_nixpkgs_core", version = "0.9.0") +bazel_dep(name = "bazel_skylib", version = "1.0.3") +# TODO: there is no BCR entry for `rules_rust` yet, and you will have to add a +# local registry entry to map a commit to a module "version". the caller will +# also have to know this and point `--registry` at the file from right revision +# on GitHub! +bazel_dep(name = "rules_rust", version = "0.17.0") diff --git a/registry/modules/rules_nixpkgs_rust/0.9.0/source.json b/registry/modules/rules_nixpkgs_rust/0.9.0/source.json new file mode 100644 index 000000000..3e6846235 --- /dev/null +++ b/registry/modules/rules_nixpkgs_rust/0.9.0/source.json @@ -0,0 +1,6 @@ +{ + "integrity" : "sha256-njiYozxfIfY0qp4tRWIOfEttVNFtRzVxqJEZO71HJco", + "strip_prefix" : "rules_nixpkgs-0c1f8f5470c7f292b7620762e224f53d837929d3/toolchains/rust", + "url": "https://github.com/tweag/rules_nixpkgs/archive/0c1f8f5470c7f292b7620762e224f53d837929d3.tar.gz" +} + diff --git a/registry/modules/rules_nixpkgs_rust/metadata.json b/registry/modules/rules_nixpkgs_rust/metadata.json new file mode 100644 index 000000000..92a394339 --- /dev/null +++ b/registry/modules/rules_nixpkgs_rust/metadata.json @@ -0,0 +1,17 @@ +{ + "homepage": "", + "maintainers": [ + { + "email": "benjamin.radford@tweag.io ", + "github": "benradf", + "name": "Benjamin Radford" + }, + { + "email": "andreas.herrmann@tweag.io", + "github": "aherrmann", + "name": "Andreas Herrmann" + } + ], + "versions": ["0.9.0"], + "yanked_versions": [] +} diff --git a/rules_haskell_nix/.bazelrc b/rules_haskell_nix/.bazelrc new file mode 100644 index 000000000..50877cc1b --- /dev/null +++ b/rules_haskell_nix/.bazelrc @@ -0,0 +1,2 @@ +import %workspace%/../.bazelrc.common +import %workspace%/.bazelrc.bzlmod diff --git a/rules_haskell_nix/.bazelrc.bzlmod b/rules_haskell_nix/.bazelrc.bzlmod new file mode 100644 index 000000000..98ab67d91 --- /dev/null +++ b/rules_haskell_nix/.bazelrc.bzlmod @@ -0,0 +1,2 @@ +common:bzlmod --experimental_enable_bzlmod +common:bzlmod --registry=file://%workspace%/../registry --registry=https://bcr.bazel.build \ No newline at end of file diff --git a/rules_haskell_nix/BUILD.bazel b/rules_haskell_nix/BUILD.bazel new file mode 100644 index 000000000..e69de29bb diff --git a/rules_haskell_nix/MODULE.bazel b/rules_haskell_nix/MODULE.bazel new file mode 100644 index 000000000..f28b3750a --- /dev/null +++ b/rules_haskell_nix/MODULE.bazel @@ -0,0 +1,28 @@ +module( + name = "rules_haskell_nix", + version = "0.16", +) + +bazel_dep( + name = "rules_haskell", + version = "0.16", +) + +bazel_dep( + name = "rules_nixpkgs_core", + version = "0.9.0", +) + +bazel_dep( + name = "rules_nixpkgs_posix", + version = "0.9.0", +) + +bazel_dep( + name = "platforms", + version = "0.0.6", +) + +non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps") + +use_repo(non_module_deps, "os_info") diff --git a/rules_haskell_nix/README.md b/rules_haskell_nix/README.md new file mode 100644 index 000000000..72800b123 --- /dev/null +++ b/rules_haskell_nix/README.md @@ -0,0 +1,5 @@ +With bzlmod, the [nix functionalities](https://api.haskell.build/haskell/nixpkgs.html) provided by rules_haskell must be +accessed via `@rules_haskell_nix//:nixpkgs.bzl`. + +This way the main `rules_haskell` module does not have to depend on +`rules_nixpkgs` when we do not intend to use nix toolchains. diff --git a/rules_haskell_nix/WORKSPACE b/rules_haskell_nix/WORKSPACE new file mode 100644 index 000000000..ba9bf9d23 --- /dev/null +++ b/rules_haskell_nix/WORKSPACE @@ -0,0 +1 @@ +# This WORKSPACE file is empty because the folder is only used as a repository with bzlmod diff --git a/rules_haskell_nix/nixpkgs.bzl b/rules_haskell_nix/nixpkgs.bzl new file mode 100644 index 000000000..a256921f7 --- /dev/null +++ b/rules_haskell_nix/nixpkgs.bzl @@ -0,0 +1,414 @@ +"""Workspace rules (Nixpkgs)""" + +load( + "@rules_nixpkgs_core//:nixpkgs.bzl", + "nixpkgs_package", +) +load( + "@rules_nixpkgs_posix//:posix.bzl", + "nixpkgs_sh_posix_configure", +) +load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value") +load( + "@rules_haskell//haskell:private/pkgdb_to_bzl.bzl", + "pkgdb_to_bzl", +) +load( + "@rules_haskell//haskell:private/workspace_utils.bzl", + "define_rule", + "execute_or_fail_loudly", + "resolve_labels", +) +load("@rules_haskell//haskell:private/validate_attrs.bzl", "check_deprecated_attribute_usage") + +def check_ghc_version(repository_ctx): + ghc_name = "ghc-{}".format(repository_ctx.attr.version) + result = repository_ctx.execute(["ls", "lib"]) + bad_version = True + if result.return_code == 0: + for dir in result.stdout.splitlines(): + if dir.endswith(ghc_name): + bad_version = False + break + else: + result = repository_ctx.execute(["pwd"]) + fail("There is no lib folder in {}".format(result.stdout)) + if bad_version: + fail( + """\ +GHC version does not match expected version. +You specified {wanted}. +Available versions: +{actual} +""".format(wanted = ghc_name, actual = result.stdout), + ) + +def _ghc_nixpkgs_haskell_toolchain_impl(repository_ctx): + paths = resolve_labels(repository_ctx, [ + "@rules_haskell//haskell:private/pkgdb_to_bzl.py", + ]) + compiler_flags_select = "select({})".format( + repository_ctx.attr.compiler_flags_select or { + "//conditions:default": [], + }, + ) + nixpkgs_ghc_path = repository_ctx.path(repository_ctx.attr.nixpkgs_ghc).dirname.dirname + + # Symlink content of ghc external repo. In effect, this repo has + # the same content, but with a BUILD file that includes generated + # content (not a static one like nixpkgs_package supports). + for target in _find_children(repository_ctx, nixpkgs_ghc_path): + basename = target.rpartition("/")[-1] + repository_ctx.symlink(target, basename) + + ghc_name = "ghc-{}".format(repository_ctx.attr.version) + check_ghc_version(repository_ctx) + + toolchain_libraries = pkgdb_to_bzl(repository_ctx, paths, "lib/{}".format(ghc_name))["file_content"] + locale_archive = repository_ctx.attr.locale_archive + libdir_path = execute_or_fail_loudly(repository_ctx, ["bin/ghc", "--print-libdir"]).stdout.strip() + docdir_path = execute_or_fail_loudly(repository_ctx, ["bin/ghc-pkg", "field", "base", "haddock-html", "--simple-output"]).stdout.strip() + toolchain = define_rule( + "haskell_toolchain", + name = "toolchain-impl", + libraries = "toolchain_libraries", + # See Note [GHC toolchain files] in haskell/ghc_bindist.bzl + libdir_path = repr(libdir_path), + docdir_path = repr(docdir_path), + tools = ["@{}//:bin".format(repository_ctx.attr.nixpkgs_ghc_repo_name)], + version = repr(repository_ctx.attr.version), + static_runtime = repository_ctx.attr.static_runtime, + fully_static_link = repository_ctx.attr.fully_static_link, + ghcopts = "{} + {}".format( + repository_ctx.attr.ghcopts, + compiler_flags_select, + ), + haddock_flags = repository_ctx.attr.haddock_flags, + repl_ghci_args = repository_ctx.attr.repl_ghci_args, + cabalopts = repository_ctx.attr.cabalopts, + locale_archive = repr(locale_archive) if locale_archive else None, + locale = repr(repository_ctx.attr.locale), + ) + repository_ctx.file( + "BUILD", + executable = False, + content = """ +load( + "@rules_haskell//haskell:defs.bzl", + "haskell_import", + "haskell_toolchain", +) + +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "bin", + srcs = glob(["bin/*"]), +) + +{toolchain_libraries} + +{toolchain} + """.format( + toolchain_libraries = toolchain_libraries, + toolchain = toolchain, + ), + ) + +_ghc_nixpkgs_haskell_toolchain = repository_rule( + _ghc_nixpkgs_haskell_toolchain_impl, + attrs = { + # These attributes just forward to haskell_toolchain. + # They are documented there. + "version": attr.string(), + "static_runtime": attr.bool(), + "fully_static_link": attr.bool(), + "ghcopts": attr.string_list(), + "compiler_flags_select": attr.string_list_dict(), + "haddock_flags": attr.string_list(), + "cabalopts": attr.string_list(), + "repl_ghci_args": attr.string_list(), + "locale_archive": attr.string(), + "nixpkgs_ghc_repo_name": attr.string(), + # Unfortunately, repositories cannot depend on each other + # directly. They can only depend on files inside each + # repository. We need to be careful to depend on files that + # change anytime any content in a repository changes, like + # bin/ghc, which embeds the output path, which itself changes + # if any input to the derivation changed. + "nixpkgs_ghc": attr.label(), + "locale": attr.string( + default = "C.UTF-8", + ), + }, +) + +def _ghc_nixpkgs_toolchain_impl(repository_ctx): + # These constraints might look tautological, because they always + # match the host platform if it is the same as the target + # platform. But they are important to state because Bazel + # toolchain resolution prefers other toolchains with more specific + # constraints otherwise. + if repository_ctx.attr.target_constraints == [] and repository_ctx.attr.exec_constraints == []: + cpu_value = get_cpu_value(repository_ctx) + target_constraints = ["@platforms//cpu:{}".format( + "arm64" if ("arm64" in cpu_value or "aarch64" in cpu_value) else "x86_64", + )] + if repository_ctx.os.name == "linux": + target_constraints.append("@platforms//os:linux") + elif repository_ctx.os.name == "mac os x": + target_constraints.append("@platforms//os:osx") + exec_constraints = list(target_constraints) + else: + target_constraints = repository_ctx.attr.target_constraints + exec_constraints = list(repository_ctx.attr.exec_constraints) + + exec_constraints.append("@rules_nixpkgs_core//constraints:support_nix") + + repository_ctx.file( + "BUILD", + executable = False, + content = """ +toolchain( + name = "toolchain", + toolchain_type = "@rules_haskell//haskell:toolchain", + toolchain = "@{haskell_toolchain}//:toolchain-impl", + exec_compatible_with = {exec_constraints}, + target_compatible_with = {target_constraints}, +) + """.format( + exec_constraints = exec_constraints, + target_constraints = target_constraints, + haskell_toolchain = repository_ctx.attr.haskell_toolchain_repo_name, + ), + ) + +_ghc_nixpkgs_toolchain = repository_rule( + implementation = _ghc_nixpkgs_toolchain_impl, + attrs = { + "exec_constraints": attr.string_list(), + "target_constraints": attr.string_list(), + "haskell_toolchain_repo_name": attr.string(), + }, +) + +def haskell_register_ghc_nixpkgs( + version, + name = "rules_haskell", + is_static = None, # DEPRECATED. See _check_static_attributes_compatibility. + static_runtime = None, + fully_static_link = None, + build_file = None, + build_file_content = None, + compiler_flags = None, + ghcopts = None, + compiler_flags_select = None, + haddock_flags = None, + repl_ghci_args = None, + cabalopts = None, + locale_archive = None, + attribute_path = "haskellPackages.ghc", + sh_posix_attributes = None, + nix_file = None, + nix_file_deps = [], + nixopts = None, + locale = None, + repositories = {}, + repository = None, + nix_file_content = None, + exec_constraints = None, + target_constraints = None, + register = True): + """Register a package from Nixpkgs as a toolchain. + + Toolchains can be used to compile Haskell code. To have this + toolchain selected during [toolchain + resolution][toolchain-resolution], set a host platform that + includes the + `@rules_nixpkgs_core//constraints:support_nix` + constraint value. + + [toolchain-resolution]: https://docs.bazel.build/versions/master/toolchains.html#toolchain-resolution + + ### Examples + + ``` + haskell_register_ghc_nixpkgs( + locale_archive = "@glibc_locales//:locale-archive", + atttribute_path = "haskellPackages.ghc", + version = "1.2.3", # The version of GHC + ) + ``` + + Setting the host platform can be done on the command-line like + in the following: + + ``` + --host_platform=@rules_nixpkgs_core//platforms:host + ``` + + Args: + is_static: Deprecated. The functionality it previously gated + (supporting GHC versions with static runtime systems) now sits under + static_runtime, a name chosen to avoid confusion with the new flag + fully_static_link, which controls support for fully-statically-linked + binaries. During the deprecation period, we rewrite is_static to + static_runtime in this macro as long as the new attributes aren't also + used. This argument and supporting code should be removed in a future release. + static_runtime: True if and only if a static GHC runtime is to be used. This is + required in order to use statically-linked Haskell libraries with GHCi + and Template Haskell. + fully_static_link: True if and only if fully-statically-linked binaries are to be built. + compiler_flags: DEPRECADED. Use new name ghcopts. + ghcopts: A collection of flags that will be passed to GHC + compiler_flags_select: temporary workaround to pass conditional arguments. + See https://github.com/bazelbuild/bazel/issues/9199 for details. + attribute_path: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-attribute_path) + build_file: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-build_file) + build_file_content: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-build_file_content) + nix_file: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-nix_file) + nix_file_deps: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-nix_file_deps) + nix_file_content: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-nix_file_content) + nixopts: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-nixopts) + repositories: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-repositories) + repository: Passed to [nixpkgs_package](https://github.com/tweag/rules_nixpkgs#nixpkgs_package-repository) + sh_posix_attributes: List of attribute paths to extract standard Unix shell tools from. + Passed to [nixpkgs_sh_posix_configure](https://github.com/tweag/rules_nixpkgs#nixpkgs_sh_posix_configure). + register: Whether to register the toolchain (must be set to False if bzlmod is enabled) + """ + nixpkgs_ghc_repo_name = "{}_ghc_nixpkgs".format(name) + nixpkgs_sh_posix_repo_name = "{}_sh_posix_nixpkgs".format(name) + haskell_toolchain_repo_name = "{}_ghc_nixpkgs_haskell_toolchain".format(name) + toolchain_repo_name = "{}_ghc_nixpkgs_toolchain".format(name) + + static_runtime, fully_static_link = _check_static_attributes_compatibility( + is_static = is_static, + static_runtime = static_runtime, + fully_static_link = fully_static_link, + ) + + # The package from the system. + nixpkgs_package( + name = nixpkgs_ghc_repo_name, + attribute_path = attribute_path, + build_file = build_file, + build_file_content = build_file_content, + nix_file = nix_file, + nix_file_deps = nix_file_deps, + nix_file_content = nix_file_content, + nixopts = nixopts, + repositories = repositories, + repository = repository, + ) + + # haskell_toolchain + haskell_import definitions. + ghcopts = check_deprecated_attribute_usage( + old_attr_name = "compiler_flags", + old_attr_value = compiler_flags, + new_attr_name = "ghcopts", + new_attr_value = ghcopts, + ) + + _ghc_nixpkgs_haskell_toolchain( + name = haskell_toolchain_repo_name, + version = version, + static_runtime = static_runtime, + fully_static_link = fully_static_link, + ghcopts = ghcopts, + compiler_flags_select = compiler_flags_select, + haddock_flags = haddock_flags, + cabalopts = cabalopts, + repl_ghci_args = repl_ghci_args, + locale_archive = locale_archive, + locale = locale, + nixpkgs_ghc_repo_name = nixpkgs_ghc_repo_name, + nixpkgs_ghc = "@{}//:bin/ghc".format(nixpkgs_ghc_repo_name), + ) + + # toolchain definition. + if (exec_constraints == None) != (target_constraints == None): + fail("Both exec_constraints and target_constraints need to be provided or none of them.") + _ghc_nixpkgs_toolchain( + name = toolchain_repo_name, + exec_constraints = exec_constraints, + target_constraints = target_constraints, + haskell_toolchain_repo_name = haskell_toolchain_repo_name, + ) + if register: + native.register_toolchains("@{}//:toolchain".format(toolchain_repo_name)) + + # Unix tools toolchain required for Cabal packages + sh_posix_nixpkgs_kwargs = dict( + nix_file_deps = nix_file_deps, + nixopts = nixopts, + repositories = repositories, + repository = repository, + ) + if sh_posix_attributes != None: + sh_posix_nixpkgs_kwargs["packages"] = sh_posix_attributes + nixpkgs_sh_posix_configure( + name = nixpkgs_sh_posix_repo_name, + register = register, + **sh_posix_nixpkgs_kwargs + ) + +def _check_static_attributes_compatibility(is_static, static_runtime, fully_static_link): + """Asserts that attributes passed to `haskell_register_ghc_nixpkgs` for + controlling use of GHC's static runtime and whether or not to build + fully-statically-linked binaries are compatible. + + Args: + is_static: Deprecated. The functionality it previously gated + (supporting GHC versions with static runtime systems) now sits under + static_runtime, a name chosen to avoid confusion with the new flag + fully_static_link, which controls support for fully-statically-linked + binaries. During the deprecation period, we rewrite is_static to + static_runtime in this macro as long as the new attributes aren't also + used. This argument and supporting code should be removed in a future release. + static_runtime: True if and only if a static GHC runtime is to be used. This is + required in order to use statically-linked Haskell libraries with GHCi + and Template Haskell. + fully_static_link: True if and only if fully-statically-linked binaries are to be built. + + Returns: + A tuple of static_runtime, fully_static_link attributes, which are guaranteed + not to be None, taking into account the deprecated is_static argument. + """ + + # Check for use of the deprecated `is_static` attribute. + if is_static != None: + if static_runtime != None or fully_static_link != None: + fail("is_static is deprecated. Please use the static_runtime attribute instead.") + + print("WARNING: is_static is deprecated. Please use the static_runtime attribute instead.") + static_runtime = is_static + + # Currently we do not support the combination of a dynamic runtime system + # and fully-statically-linked binaries, so fail if this has been selected. + if not static_runtime and fully_static_link: + fail( + """\ +Fully-statically-linked binaries with a dynamic runtime are not currently supported. +Please pass static_runtime = True if you wish to build fully-statically-linked binaries. +""", + ) + + return bool(static_runtime), bool(fully_static_link) + +def _find_children(repository_ctx, target_dir): + find_args = [ + "find", + "-L", + target_dir, + "-maxdepth", + "1", + # otherwise the directory is printed as well + "-mindepth", + "1", + # filenames can contain \n + "-print0", + ] + exec_result = repository_ctx.execute(find_args) + if exec_result.return_code: + fail("_find_children() failed.") + return exec_result.stdout.rstrip("\0").split("\0") diff --git a/rules_haskell_nix/non_module_deps.bzl b/rules_haskell_nix/non_module_deps.bzl new file mode 100644 index 000000000..f4b699838 --- /dev/null +++ b/rules_haskell_nix/non_module_deps.bzl @@ -0,0 +1,11 @@ +load("@rules_haskell//tools:os_info.bzl", "os_info") + +def repositories(*, bzlmod): + os_info(name = "os_info") + +def _non_module_deps_impl(_ctx): + repositories(bzlmod = True) + +non_module_deps = module_extension( + implementation = _non_module_deps_impl, +) diff --git a/rules_haskell_tests/.bazelrc b/rules_haskell_tests/.bazelrc index b14bcab68..50877cc1b 100644 --- a/rules_haskell_tests/.bazelrc +++ b/rules_haskell_tests/.bazelrc @@ -1 +1,2 @@ -import %workspace%/../.bazelrc +import %workspace%/../.bazelrc.common +import %workspace%/.bazelrc.bzlmod diff --git a/rules_haskell_tests/.bazelrc.bzlmod b/rules_haskell_tests/.bazelrc.bzlmod new file mode 100644 index 000000000..d36a1795b --- /dev/null +++ b/rules_haskell_tests/.bazelrc.bzlmod @@ -0,0 +1,2 @@ +common:bzlmod --experimental_enable_bzlmod +common:bzlmod --registry=file:%workspace%/../registry --registry=https://bcr.bazel.build diff --git a/rules_haskell_tests/BUILD.bazel b/rules_haskell_tests/BUILD.bazel index fc45f5c9b..81f0972e6 100644 --- a/rules_haskell_tests/BUILD.bazel +++ b/rules_haskell_tests/BUILD.bazel @@ -5,6 +5,7 @@ exports_files( "non_module_deps.bzl", "non_module_deps_1.bzl", "non_module_deps_2.bzl", + "non_module_deps_bzlmod.bzl", ], visibility = ["//buildifier:__pkg__"], ) diff --git a/rules_haskell_tests/MODULE.bazel b/rules_haskell_tests/MODULE.bazel index e69de29bb..61a748e28 100644 --- a/rules_haskell_tests/MODULE.bazel +++ b/rules_haskell_tests/MODULE.bazel @@ -0,0 +1,310 @@ +module( + name = "rules_haskell_tests", + version = "0.16", +) + +bazel_dep( + name = "rules_nixpkgs_core", + version = "0.9.0", +) + +bazel_dep( + name = "rules_haskell_nix", + version = "0.16", +) + +bazel_dep( + name = "rules_haskell", + version = "0.16", +) + +bazel_dep( + name = "rules_license", + version = "0.0.7", +) + +bazel_dep( + name = "rules_nixpkgs_cc", + version = "0.9.0", +) + +bazel_dep( + name = "rules_nixpkgs_python", + version = "0.9.0", +) + +bazel_dep( + name = "rules_python", + version = "0.21.0", +) + +bazel_dep( + name = "rules_nixpkgs_go", + version = "0.9.0", +) + +bazel_dep( + name = "rules_nixpkgs_posix", + version = "0.9.0", +) + +bazel_dep( + name = "rules_nixpkgs_nodejs", + version = "0.9.0", +) + +bazel_dep( + name = "platforms", + version = "0.0.6", +) + +bazel_dep( + name = "rules_cc", + version = "0.0.6", +) + +bazel_dep( + name = "rules_sh", + version = "0.3.0", +) + +bazel_dep( + name = "bazel_skylib", + version = "1.4.2", +) + +bazel_dep( + name = "rules_java", + version = "6.1.0", +) + +bazel_dep( + name = "rules_nodejs", + version = "5.8.2", +) + +bazel_dep( + name = "aspect_rules_js", + version = "1.23.0", +) + +bazel_dep( + name = "aspect_bazel_lib", + version = "1.31.2", +) + +bazel_dep( + name = "rules_proto", + version = "5.3.0-21.7", +) + +bazel_dep( + name = "protobuf", + version = "21.7", + repo_name = "com_google_protobuf", +) + +non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps") + +use_repo( + non_module_deps, + "os_info", + "zlib.hs", + "contrib_rules_bazel_integration_test", + "c2hs_repo", + "library_repo", + "haskell_package_repository_dummy", + "com_github_bazelbuild_buildtools", + "cgrindel_bazel_starlib", +) + +non_modules_deps_1 = use_extension("//:non_module_deps_1.bzl", "non_module_deps_1") + +use_repo( + non_modules_deps_1, + "glibc_locales", + "nixpkgs_default", + "rules_haskell_ghc_darwin_amd64-toolchain", + "rules_haskell_ghc_darwin_arm64-toolchain", + "rules_haskell_ghc_linux_amd64-toolchain", + "rules_haskell_ghc_windows_amd64-toolchain", + "rules_haskell_ghc_linux_arm64-toolchain", + "rules_haskell_ghc_windows_amd64_cc_toolchain", + "nixpkgs_python_toolchain", + "rules_haskell_python_local", + "nixpkgs_config_cc", + "nixpkgs_config_cc_info", + "nixpkgs_config_cc_toolchains", + "nixpkgs_go_sdk_toolchains", + "nixpkgs_go_sdk", + "nixpkgs_lz4", + "linux_amd64_asterius-toolchain", + "asterius_bundle_linux_amd64", + "toolchains_libraries", + "zlib.dev", + "bazel_5", + "build_bazel_bazel_5_0_0", + "build_bazel_bazel_5_2_0", + "build_bazel_bazel_6_2_0", + "bazel_6", +) + +register_toolchains( + "@nixpkgs_config_cc_toolchains//:all", + "@nixpkgs_go_sdk_toolchains//:all", + "@linux_amd64_asterius-toolchain//:toolchain", + "@linux_amd64_asterius-toolchain//:asterius_toolchain", + "@linux_amd64_asterius-toolchain//:wasm_cc_toolchain", + "@rules_haskell_python_local//:toolchain", + "@nixpkgs_python_toolchain//:toolchain", +) + +non_module_deps_2 = use_extension("//:non_module_deps_2.bzl", "non_module_deps_2") + +use_repo( + non_module_deps_2, + "data-default-ic", + "data-default-ol", + "ghcide", + "stackage-zlib", + "stackage-zlib-unpinned", + "ghcide-exe", + "Cabal", + "stackage-pinning-test", + "stackage-pinning-test-unpinned", + "alex", + "stackage", + "stackage-unpinned", + "stackage-exe", + "rules_haskell_stack_update", + "rules_haskell_stack", + "stackage_asterius", + "stackage_asterius-unpinned", +) + +non_modules_deps_bzlmod = use_extension( + "//:non_module_deps_bzlmod.bzl", + "non_module_deps_bzlmod", +) + +use_repo( + non_modules_deps_bzlmod, + "rules_haskell_ghc_nixpkgs_toolchain", + "rules_haskell_ghc_nixpkgs", +) + +register_toolchains( + "@rules_haskell_ghc_nixpkgs_toolchain//:toolchain", + "@rules_haskell_ghc_darwin_amd64-toolchain//:toolchain", + "@rules_haskell_ghc_darwin_arm64-toolchain//:toolchain", + "@rules_haskell_ghc_linux_amd64-toolchain//:toolchain", + "@rules_haskell_ghc_linux_arm64-toolchain//:toolchain", + "@rules_haskell_ghc_windows_amd64-toolchain//:toolchain", + "@rules_haskell_ghc_windows_amd64-toolchain//:toolchain", + "@rules_haskell_ghc_windows_amd64_cc_toolchain//:windows_cc_toolchain", +) + +[ + ( + use_repo( + non_modules_deps_bzlmod, + "nixpkgs_nodejs_{}_{}_toolchain".format(os, arch), + ), + register_toolchains("@nixpkgs_nodejs_{}_{}_toolchain//:all".format(os, arch)), + ) + for os in [ + "linux", + "darwin", + ] + for arch in [ + "amd64", + "arm64", + ] +] + +register_toolchains( + "//tests:c2hs-toolchain", + "//tests:doctest-toolchain", + "//tests:protobuf-toolchain", + "//tests:protobuf-toolchain-osx_arm64", +) + +# To test accessing runfiles with a library in another module +bazel_dep( + name = "other_module", + version = "0.0.1", +) + +local_path_override( + module_name = "other_module", + path = "tests/bzlmod_runfiles/other_module", +) + +# Go setup for buildifieR +bazel_dep( + name = "rules_go", + version = "0.39.1", + repo_name = "io_bazel_rules_go", +) + +go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk") + +go_sdk.download( + version = "1.19.7", +) + +bazel_dep( + name = "gazelle", + version = "0.30.0", +) + +go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") + +go_deps.module( + path = "github.com/gogo/protobuf", + sum = "h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=", + version = "v1.3.2", +) + +use_repo( + go_deps, + "com_github_gogo_protobuf", + "com_github_golang_mock", + "com_github_golang_protobuf", + "org_golang_google_genproto", + "org_golang_google_grpc", + "org_golang_google_protobuf", + "org_golang_x_net", + "org_golang_x_tools", +) + +# Java dependencies for the //tests/java_classpath test +bazel_dep( + name = "rules_jvm_external", + version = "5.1", +) + +maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven") + +maven.install( + name = "rules_haskell_tests_maven", + artifacts = ["org.apache.spark:spark-core_2.10:1.6.0"], +) + +use_repo(maven, "rules_haskell_tests_maven") + +# Bring some dependencies declared by rules_haskell module extensions into scope +asterius = use_extension("@rules_haskell//haskell/asterius:extension.bzl", "rules_haskell_asterius") + +use_repo( + asterius, + "rules_haskell_asterius_webpack", + "rules_haskell_asterius_build_setting", +) + +rules_haskell_dependencies = use_extension("@rules_haskell//extensions:rules_haskell_dependencies.bzl", "rules_haskell_dependencies") + +use_repo( + rules_haskell_dependencies, + "zlib", +) diff --git a/rules_haskell_tests/WORKSPACE b/rules_haskell_tests/WORKSPACE index 31aadcd17..07caabe97 100644 --- a/rules_haskell_tests/WORKSPACE +++ b/rules_haskell_tests/WORKSPACE @@ -13,6 +13,11 @@ load("//:non_module_deps.bzl", "repositories") repositories(bzlmod = False) +load("@rules_haskell//tools:repositories.bzl", "rules_haskell_worker_dependencies") + +# For persistent worker (tools/worker) +rules_haskell_worker_dependencies() + load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # For testing if bzlmod is enabled @@ -131,19 +136,6 @@ jvm_maven_import_external( server_urls = ["https://repo.maven.apache.org/maven2"], ) -http_archive( - name = "io_bazel_stardoc", - sha256 = "3fd8fec4ddec3c670bd810904e2e33170bedfe12f90adf943508184be458c8bb", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/stardoc/releases/download/0.5.3/stardoc-0.5.3.tar.gz", - "https://github.com/bazelbuild/stardoc/releases/download/0.5.3/stardoc-0.5.3.tar.gz", - ], -) - -load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories") - -stardoc_repositories() - register_toolchains( "@rules_haskell//docs/pandoc:nixpkgs", "@rules_haskell//docs/pandoc:linux", @@ -187,3 +179,8 @@ bind( name = "python_headers", actual = "@com_google_protobuf//util/python:python_headers", ) + +local_repository( + name = "other_module", + path = "tests/bzlmod_runfiles/other_module", +) diff --git a/rules_haskell_tests/WORKSPACE.bzlmod b/rules_haskell_tests/WORKSPACE.bzlmod new file mode 100644 index 000000000..e69de29bb diff --git a/rules_haskell_tests/buildifier/BUILD.bazel b/rules_haskell_tests/buildifier/BUILD.bazel index 0af9a538b..76f52b30a 100644 --- a/rules_haskell_tests/buildifier/BUILD.bazel +++ b/rules_haskell_tests/buildifier/BUILD.bazel @@ -21,6 +21,7 @@ buildifier_test( "//:non_module_deps.bzl", "//:non_module_deps_1.bzl", "//:non_module_deps_2.bzl", + "//:non_module_deps_bzlmod.bzl", "//buildifier:all_files", "//tests:all_files", ], diff --git a/rules_haskell_tests/non_module_deps.bzl b/rules_haskell_tests/non_module_deps.bzl index ccb6da25a..5a5f6e64d 100644 --- a/rules_haskell_tests/non_module_deps.bzl +++ b/rules_haskell_tests/non_module_deps.bzl @@ -6,7 +6,6 @@ load( "//tests/external-haskell-repository:workspace_dummy.bzl", "haskell_package_repository_dummy", ) -load("@rules_haskell//tools:repositories.bzl", "rules_haskell_worker_dependencies") # Replaces local_repository in bzlmod # See https://groups.google.com/g/bazel-discuss/c/xpsg3mWQPZg @@ -29,9 +28,6 @@ def repositories(*, bzlmod): # Some helpers for platform-dependent configuration os_info(name = "os_info") - # For persistent worker (tools/worker) - rules_haskell_worker_dependencies() - # module rules_bazel_integration_test requires bazel >= 6.1.0 http_archive( name = "contrib_rules_bazel_integration_test", diff --git a/rules_haskell_tests/non_module_deps_2.bzl b/rules_haskell_tests/non_module_deps_2.bzl index 69f39aea7..aaed6490f 100644 --- a/rules_haskell_tests/non_module_deps_2.bzl +++ b/rules_haskell_tests/non_module_deps_2.bzl @@ -7,6 +7,20 @@ load("@toolchains_libraries//:toolchain_libraries.bzl", "toolchain_libraries") label_builder = lambda x: Label(x) +def _empty_repo_impl(rctx): + fail(rctx.attrs.error_msg) + +_empty_repo = repository_rule( + implementation = _empty_repo_impl, + doc = """A dummy repository that can be loaded from the MODULE.bazel file but not fetched.""", + attrs = { + "error_msg": attr.string( + doc = "The error message displayed if the repository is fetched", + mandatory = True, + ), + }, +) + def repositories(*, bzlmod): # In a separate repo because not all platforms support zlib. stack_snapshot( @@ -304,17 +318,28 @@ haskell_cabal_library( label_builder = label_builder, ) - stack_snapshot( - name = "stackage_asterius", - setup_stack = False, - local_snapshot = "//tests/asterius/stack_toolchain_libraries:snapshot.yaml", - packages = [ - "xhtml", - ], - stack_snapshot_json = "//tests/asterius/stack_toolchain_libraries:snapshot.json", - toolchain_libraries = toolchain_libraries, - label_builder = label_builder, - ) if is_linux else None + if is_linux: + stack_snapshot( + name = "stackage_asterius", + setup_stack = False, + local_snapshot = "//tests/asterius/stack_toolchain_libraries:snapshot.yaml", + packages = [ + "xhtml", + ], + stack_snapshot_json = "//tests/asterius/stack_toolchain_libraries:snapshot.json", + toolchain_libraries = toolchain_libraries, + label_builder = label_builder, + ) + else: + # On non linux, we still need a dummy repository to call `use_repo` in the `MODULE.bazel` file. + _empty_repo( + name = "stackage_asterius", + error_msg = "The stackage_asterius repository should only be used on linux", + ) + _empty_repo( + name = "stackage_asterius-unpinned", + error_msg = "The stackage_asterius-unpinned repository should only be used on linux", + ) def _non_module_deps_2_impl(ctx): repositories(bzlmod = True) diff --git a/rules_haskell_tests/non_module_deps_bzlmod.bzl b/rules_haskell_tests/non_module_deps_bzlmod.bzl new file mode 100644 index 000000000..8dfe527bc --- /dev/null +++ b/rules_haskell_tests/non_module_deps_bzlmod.bzl @@ -0,0 +1,48 @@ +""" This module extension contains non module dependencies which are only used with bzlmod """ + +load( + "@rules_haskell_nix//:nixpkgs.bzl", + "haskell_register_ghc_nixpkgs", +) +load( + "@rules_haskell_tests//:non_module_deps_1.bzl", + "test_cabalopts", + "test_ghcopts", + "test_haddock_flags", + "test_repl_ghci_args", +) +load( + "@rules_haskell//:constants.bzl", + "test_ghc_version", +) +load("@rules_nixpkgs_nodejs//:nodejs.bzl", "nixpkgs_nodejs_configure_platforms") + +def _non_module_deps_bzlmod_impl(_ctx): + # Only used with bzlmod, because in WORKSPACE bzlmod we test the + # backward compatible "@rules_haskell//haskell:nixpkgs.bzl" + haskell_register_ghc_nixpkgs( + attribute_path = "", + cabalopts = test_cabalopts, + ghcopts = test_ghcopts, + haddock_flags = test_haddock_flags, + locale_archive = "@glibc_locales//:locale-archive", + nix_file_content = """with import {}; haskell.packages.ghc925.ghc""", + repl_ghci_args = test_repl_ghci_args, + repository = "@nixpkgs_default", + version = test_ghc_version, + register = False, + ) + + # Workspace mode uses the asterius_dependencies_nix convenience + # macro which sets up the node toolchain and npm. In bzlmod we + # make use of rules_js module extension for npm and rely on + # rules_nixpkgs_nodejs for the node toolchain. + nixpkgs_nodejs_configure_platforms( + name = "nixpkgs_nodejs", + repository = "@nixpkgs_default", + register = False, + ) + +non_module_deps_bzlmod = module_extension( + implementation = _non_module_deps_bzlmod_impl, +) diff --git a/rules_haskell_tests/tests/bzlmod_runfiles/BUILD.bazel b/rules_haskell_tests/tests/bzlmod_runfiles/BUILD.bazel new file mode 100644 index 000000000..29c0f1622 --- /dev/null +++ b/rules_haskell_tests/tests/bzlmod_runfiles/BUILD.bazel @@ -0,0 +1,30 @@ +load( + "@rules_haskell//haskell:defs.bzl", + "haskell_binary", + "haskell_toolchain_library", +) + +haskell_toolchain_library(name = "base") + +# This test uses a library from another module (which calls the runfiles library). +# In this case this library must use the createFromCurrentFile +# function or the `haskell_runfiles` wrapper rule. +haskell_binary( + name = "bin", + srcs = [":Main.hs"], + deps = [ + ":base", + "@other_module//:lib", + ], +) + +# This test directly calls a binary from the other module +genrule( + name = "gen", + srcs = [], + outs = ["out"], + cmd = """$(location @other_module//:bin) > $@""", + tools = [ + "@other_module//:bin", + ], +) diff --git a/rules_haskell_tests/tests/bzlmod_runfiles/Main.hs b/rules_haskell_tests/tests/bzlmod_runfiles/Main.hs new file mode 100644 index 000000000..c1965a832 --- /dev/null +++ b/rules_haskell_tests/tests/bzlmod_runfiles/Main.hs @@ -0,0 +1,5 @@ +module Main where +import Lib + +main :: IO () +main = print =<< content diff --git a/rules_haskell_tests/tests/bzlmod_runfiles/other_module/.bazelrc b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/.bazelrc new file mode 100644 index 000000000..46552aed1 --- /dev/null +++ b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/.bazelrc @@ -0,0 +1,2 @@ +import %workspace%/../../../../.bazelrc.common +import %workspace%/.bazelrc.bzlmod diff --git a/rules_haskell_tests/tests/bzlmod_runfiles/other_module/.bazelrc.bzlmod b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/.bazelrc.bzlmod new file mode 100644 index 000000000..583573e3a --- /dev/null +++ b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/.bazelrc.bzlmod @@ -0,0 +1,2 @@ +common:bzlmod --experimental_enable_bzlmod +common:bzlmod --registry=file:%workspace%/../../../../registry --registry=https://bcr.bazel.build diff --git a/rules_haskell_tests/tests/bzlmod_runfiles/other_module/BUILD.bazel b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/BUILD.bazel new file mode 100644 index 000000000..7a84b72a9 --- /dev/null +++ b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/BUILD.bazel @@ -0,0 +1,38 @@ +""" This file tests the `haskell_runfiles` wrapper rule. This is +needed here because :lib has runfiles and is used from another module. +""" + +load( + "@rules_haskell//haskell:defs.bzl", + "haskell_binary", + "haskell_library", + "haskell_runfiles", + "haskell_toolchain_library", +) + +haskell_runfiles() + +haskell_toolchain_library(name = "base") + +haskell_library( + name = "lib", + srcs = [":Lib.hs"], + data = [ + "datafile", + ], + visibility = ["//visibility:public"], + deps = [ + ":base", + ":haskell_runfiles", + ], +) + +haskell_binary( + name = "bin", + srcs = [":Main.hs"], + visibility = ["//visibility:public"], + deps = [ + ":base", + ":lib", + ], +) diff --git a/rules_haskell_tests/tests/bzlmod_runfiles/other_module/Lib.hs b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/Lib.hs new file mode 100644 index 000000000..985c17ccd --- /dev/null +++ b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/Lib.hs @@ -0,0 +1,10 @@ +module Lib where + +import Runfiles +import System.IO (IOMode (ReadMode), hGetLine, withFile) + +content :: IO String +content = do + r <- Runfiles.create + let location = Runfiles.rlocation r "other_module/datafile" + withFile location ReadMode hGetLine diff --git a/rules_haskell_tests/tests/bzlmod_runfiles/other_module/MODULE.bazel b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/MODULE.bazel new file mode 100644 index 000000000..bf3891509 --- /dev/null +++ b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/MODULE.bazel @@ -0,0 +1,24 @@ +module( + name = "other_module", + version = "0.0.1", +) + +bazel_dep( + name = "rules_haskell_nix", + version = "0.16", +) + +bazel_dep( + name = "rules_haskell", + version = "0.16", +) + +bazel_dep( + name = "rules_nixpkgs_core", + version = "0.9.0", +) + +bazel_dep( + name = "platforms", + version = "0.0.5", +) diff --git a/rules_haskell_tests/tests/bzlmod_runfiles/other_module/Main.hs b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/Main.hs new file mode 100644 index 000000000..c1965a832 --- /dev/null +++ b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/Main.hs @@ -0,0 +1,5 @@ +module Main where +import Lib + +main :: IO () +main = print =<< content diff --git a/rules_haskell_tests/tests/bzlmod_runfiles/other_module/WORKSPACE b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/WORKSPACE new file mode 100644 index 000000000..e69de29bb diff --git a/rules_haskell_tests/tests/bzlmod_runfiles/other_module/datafile b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/datafile new file mode 100644 index 000000000..6b584e8ec --- /dev/null +++ b/rules_haskell_tests/tests/bzlmod_runfiles/other_module/datafile @@ -0,0 +1 @@ +content \ No newline at end of file diff --git a/rules_haskell_tests/tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/WORKSPACE b/rules_haskell_tests/tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/WORKSPACE index 18c31ba0e..f5c4df502 100644 --- a/rules_haskell_tests/tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/WORKSPACE +++ b/rules_haskell_tests/tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/WORKSPACE @@ -27,12 +27,11 @@ load("@rules_haskell//haskell:toolchain.bzl", "rules_haskell_toolchains") rules_haskell_toolchains(version = "9.2.5") load( - "@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", - "nixpkgs_cc_configure", + "@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_local_repository", - "nixpkgs_package", - "nixpkgs_python_configure", ) +load("@rules_nixpkgs_cc//:cc.bzl", "nixpkgs_cc_configure") +load("@rules_nixpkgs_python//:python.bzl", "nixpkgs_python_configure") nixpkgs_cc_configure( name = "nixpkgs_config_cc", diff --git a/rules_haskell_tests/tests/haskell_module/repl/haskell_module_repl_test/WORKSPACE b/rules_haskell_tests/tests/haskell_module/repl/haskell_module_repl_test/WORKSPACE index 18c31ba0e..075d05f06 100644 --- a/rules_haskell_tests/tests/haskell_module/repl/haskell_module_repl_test/WORKSPACE +++ b/rules_haskell_tests/tests/haskell_module/repl/haskell_module_repl_test/WORKSPACE @@ -28,11 +28,10 @@ rules_haskell_toolchains(version = "9.2.5") load( "@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", - "nixpkgs_cc_configure", "nixpkgs_local_repository", - "nixpkgs_package", - "nixpkgs_python_configure", ) +load("@rules_nixpkgs_cc//:cc.bzl", "nixpkgs_cc_configure") +load("@rules_nixpkgs_python//:python.bzl", "nixpkgs_python_configure") nixpkgs_cc_configure( name = "nixpkgs_config_cc", diff --git a/rules_haskell_tests/tests/java_classpath/BUILD.bazel b/rules_haskell_tests/tests/java_classpath/BUILD.bazel index 9ebf19a5d..6cd3c7224 100644 --- a/rules_haskell_tests/tests/java_classpath/BUILD.bazel +++ b/rules_haskell_tests/tests/java_classpath/BUILD.bazel @@ -2,6 +2,7 @@ load( "@rules_haskell//haskell:defs.bzl", "haskell_test", ) +load("@aspect_bazel_lib//lib:utils.bzl", "is_bzlmod_enabled") package(default_testonly = 1) @@ -12,7 +13,7 @@ haskell_test( deps = [ "//tests/hackage:base", "//tests/hackage:template-haskell", - "@org_apache_spark_spark_core_2_10//jar", + ("@rules_haskell_tests_maven//:org_apache_spark_spark_core_2_10" if is_bzlmod_enabled() else "@org_apache_spark_spark_core_2_10//jar"), ], ) diff --git a/shell.nix b/shell.nix index 4b668ff76..9f95192c7 100644 --- a/shell.nix +++ b/shell.nix @@ -48,8 +48,8 @@ mkShell { echo "In order to build this project, you need to add the two" echo "following host_platform entries to your .bazelrc.local file:" echo - echo "build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host" - echo "run --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host" + echo "build --host_platform=@rules_nixpkgs_core//platforms:host" + echo "run --host_platform=@rules_nixpkgs_core//platforms:host" fi ''; } diff --git a/tools/runfiles/BUILD.bazel b/tools/runfiles/BUILD.bazel index 4cc6fced4..d441edc1f 100644 --- a/tools/runfiles/BUILD.bazel +++ b/tools/runfiles/BUILD.bazel @@ -5,7 +5,13 @@ load( "haskell_toolchain_library", ) -haskell_toolchain_library(name = "base") +haskell_toolchain_library( + name = "base", + # This may be dependency of the `haskell_runfiles` rule. + visibility = ["//visibility:public"], +) + +haskell_toolchain_library(name = "containers") haskell_toolchain_library(name = "directory") @@ -34,6 +40,7 @@ haskell_library( visibility = ["//visibility:public"], deps = [ ":base", + ":containers", ":directory", ":filepath", ":transformers", diff --git a/tools/runfiles/README.md b/tools/runfiles/README.md index b92dc52d2..6c57e1d84 100644 --- a/tools/runfiles/README.md +++ b/tools/runfiles/README.md @@ -17,3 +17,16 @@ main = do $ error $ "Incorrect contents: got: " ++ show foo callProcess (Runfiles.rlocation r "rules_haskell/tools/runfiles/bin") [] ``` + +## `haskell_runfiles` + +To resolve repository mappings with bzlmod, the runfiles library must know the name of the bazel module calling it +(see the [design document](https://github.com/bazelbuild/proposals/blob/main/designs/2022-07-21-locating-runfiles-with-bzlmod.md#3-make-the-canonical-repository-name-of-the-current-repository-available-at-runtime)). + +This is achieved by passing the `__FILE__` constant to the `createFromCurrentFile` function of the library. + +Since enabling the preprocessor can be inconvenient in some cases, the [haskell_runfiles](https://api.haskell.build/haskell/defs.html#haskell_runfiles) helper rule is provided in `@rules_haskell//haskell:defs.bzl`, +which generates a small wrapper around the main `runfiles` library. + +Note: using the `haskell_runfiles` rule is only required when the library accessing runfiles is used from another bazel module (which uses a different repository mapping). +If the binary is defined in the same module, the default behavior which recovers the current module from the path of the binary is correct. diff --git a/tools/runfiles/src/Bazel/Runfiles.hs b/tools/runfiles/src/Bazel/Runfiles.hs index 75014ab3f..ce38ff54c 100644 --- a/tools/runfiles/src/Bazel/Runfiles.hs +++ b/tools/runfiles/src/Bazel/Runfiles.hs @@ -8,6 +8,8 @@ module Bazel.Runfiles ( Runfiles , create , createFromProgramPath + , createFromCurrentFile + , createFromProgramPathAndCurrentFile , rlocation , env ) where @@ -24,26 +26,49 @@ import GHC.Stack import System.Directory (doesDirectoryExist, doesFileExist, listDirectory, makeAbsolute) import System.Environment (lookupEnv) import qualified System.FilePath -import System.FilePath ((), (<.>), addTrailingPathSeparator, takeFileName) +import System.FilePath ((), (<.>), addTrailingPathSeparator, takeFileName, splitDirectories, isAbsolute) import System.Info (os) +import qualified Data.Map.Strict as Map + + +-- | Bazel repository mapping for bzlmod +-- See https://github.com/bazelbuild/proposals/blob/7c8da4a931d83db5c25abf85f6c486ad22d330e3/designs/2022-07-21-locating-runfiles-with-bzlmod.md +type RepoMapping = Map.Map (String, String) String + +-- | Mapping from the manifest file +type Manifest = Map.Map FilePath FilePath -- | Reference to Bazel runfiles, runfiles root or manifest file. -data Runfiles - = RunfilesRoot !FilePath - -- ^ The runfiles root directory. - | RunfilesManifest !FilePath ![(FilePath, FilePath)] - -- ^ The runfiles manifest file and its content. +data Runfiles = + RunfilesRoot + !FilePath -- ^ The runfiles root directory + RepoMapping -- ^ The repository mapping + String -- ^ The current repository + | RunfilesManifest + !FilePath -- ^ The runfiles manifest file + !Manifest -- ^ The runfiles manifest content + RepoMapping -- ^ The repository mapping + String -- ^ The current repository deriving Show +-- | Apply the repository mapping to replace the first component of a path +applyRepoMapping :: RepoMapping -> String -> FilePath -> FilePath +applyRepoMapping repoMapping currentRepo path = + let (apparentRepo, rest) = break (== '/') path in + let resolvedRepo = fromMaybe apparentRepo $ Map.lookup (currentRepo, apparentRepo) repoMapping in + resolvedRepo <> rest + -- | Construct a path to a data dependency within the given runfiles. -- -- For example: @rlocation \"myworkspace\/mypackage\/myfile.txt\"@ rlocation :: Runfiles -> FilePath -> FilePath -rlocation (RunfilesRoot f) g = f normalize g -rlocation (RunfilesManifest _ m) g = fromMaybe g' $ asum [lookup g' m, lookupDir g' m] +rlocation (RunfilesRoot f repoMapping currentRepo) g = + let resolved_g = applyRepoMapping repoMapping currentRepo $ normalize g in + f resolved_g +rlocation (RunfilesManifest _ m repoMapping currentRepo) g = fromMaybe g' $ asum [Map.lookup g' m, lookupDir g' m] where - g' = normalize g + g' = applyRepoMapping repoMapping currentRepo $ normalize g -- | Lookup a directory in the manifest file. -- @@ -51,8 +76,8 @@ rlocation (RunfilesManifest _ m) g = fromMaybe g' $ asum [lookup g' m, lookupDir -- supports looking up directories. This function allows to lookup a directory -- in a manifest file, by looking for the first entry with a matching prefix -- and then stripping the superfluous suffix. -lookupDir :: FilePath -> [(FilePath, FilePath)] -> Maybe FilePath -lookupDir p = fmap stripSuffix . find match +lookupDir :: FilePath -> Manifest -> Maybe FilePath +lookupDir p manifest = fmap stripSuffix $ find match (Map.toList manifest) where p' = normalize $ addTrailingPathSeparator p match (key, value) = p' `isPrefixOf` key && drop (length p') key `isSuffixOf` value @@ -84,8 +109,8 @@ normalizeWindows = map (toLower . normalizeSlash) . System.FilePath.normalise -- during "bazel run"; thus, non-test binaries should set the -- environment manually for processes that they call. env :: Runfiles -> [(String, String)] -env (RunfilesRoot f) = [(runfilesDirEnv, f)] -env (RunfilesManifest f _) = [(manifestFileEnv, f), (manifestOnlyEnv, "1")] +env (RunfilesRoot f _ _) = [(runfilesDirEnv, f)] +env (RunfilesManifest f _ _ _) = [(manifestFileEnv, f), (manifestOnlyEnv, "1")] runfilesDirEnv :: String runfilesDirEnv = "RUNFILES_DIR" @@ -123,7 +148,22 @@ create = createFromProgramPath =<< getArg0 -- Identical to 'create' except that it accepts the path to the current program -- as an argument rather than reading it from `argv[0]`. createFromProgramPath :: HasCallStack => FilePath -> IO Runfiles -createFromProgramPath exePath = do +createFromProgramPath exePath = createFromProgramPathAndCurrentFile Nothing exePath + +-- | Locate the runfiles directory or manifest for the current binary. +-- +-- Identical to 'create' except that it accepts the path to the current file +-- as an argument. Useful when a library is used by another bazel module. +createFromCurrentFile:: HasCallStack => String -> IO Runfiles +createFromCurrentFile currentFile = createFromProgramPathAndCurrentFile (Just currentFile) =<< getArg0 + +-- | Locate the runfiles directory or manifest for the current binary. +-- +-- Identical to 'create' except that it accepts the path to the current file +-- and program as arguments. +createFromProgramPathAndCurrentFile :: HasCallStack => Maybe String -> FilePath -> IO Runfiles +createFromProgramPathAndCurrentFile currentFile exePath = do + let currentRepo = parseCurrentRepo (fromMaybe exePath currentFile) mbRunfiles <- runMaybeT $ asum [ do -- Bazel sets RUNFILES_MANIFEST_ONLY=1 if the manifest file should be @@ -150,7 +190,15 @@ createFromProgramPath exePath = do -- directory. containsData <- liftIO $ containsOneDataFile runfilesRoot guard containsData - pure $! RunfilesRoot runfilesRoot + repoMapping <- asum + [ tryRepoMapping (runfilesRoot "_repo_mapping") + , tryRepoMapping $ exePath <.> "repo_mapping" + , do + guard (os == "mingw32") + tryRepoMapping $ exePath <.> "exe" <.>"repo_mapping" + , return Map.empty + ] + pure $! RunfilesRoot runfilesRoot repoMapping currentRepo , do -- Locate manifest file relative to executable or by environment. let tryManifest file = do @@ -168,7 +216,13 @@ createFromProgramPath exePath = do ] content <- liftIO $ readFile manifestPath let mapping = parseManifest content - pure $! RunfilesManifest manifestPath mapping + repoMapping <- asum + [ do + repoMappingPath <- MaybeT (pure $ Map.lookup "_repo_mapping" mapping) + tryRepoMapping repoMappingPath, + return Map.empty + ] + pure $! RunfilesManifest manifestPath mapping repoMapping currentRepo ] case mbRunfiles of @@ -209,9 +263,45 @@ anyM predicate = loop -- | Parse Bazel's manifest file content. -- -- The manifest file holds lines of keys and values separted by a space. -parseManifest :: String -> [(FilePath, FilePath)] -parseManifest = map parseLine . lines +parseManifest :: String -> Manifest +parseManifest = Map.fromList . map parseLine . lines where parseLine l = let (key, value) = span (/= ' ') l in (normalize key, normalize $ dropWhile (== ' ') value) + +-- | Parse Bazel _repo_mapping file. +-- +-- See https://github.com/bazelbuild/proposals/blob/main/designs/2022-07-21-locating-runfiles-with-bzlmod.md +parseRepoMapping :: String -> RepoMapping +parseRepoMapping = Map.fromList . map parseLine . lines + where + parseLine l = + let (context, rest) = span (/= ',') l in + let (apparent, resolved) = span (/= ',') (tail rest) in + ((context, apparent), tail resolved) + +tryRepoMapping :: FilePath -> MaybeT IO RepoMapping +tryRepoMapping repoMappingPath = do + exists <- liftIO $ doesFileExist repoMappingPath + guard exists + content <- liftIO $ readFile repoMappingPath + return $ parseRepoMapping content + +-- | Parses the current repository out of a path. +parseCurrentRepo :: FilePath -> String +parseCurrentRepo path | isAbsolute path = + -- exePath is absolute when using `bazel run` + -- In this case we detect the first occurence of bazel-out/*/bin/external/currentRepo in the PATH + -- if there is none, we return the empty string which is the id of the main module. + parseAbsolutePath (splitDirectories path) + where + parseAbsolutePath ("bazel-out":_:"bin":"external":currentRepo:_) = currentRepo + parseAbsolutePath (_:xs) = parseAbsolutePath xs + parseAbsolutePath [] = "" + +parseCurrentRepo path = + case splitDirectories path of + "bazel-out":_:"bin":"external":currentRepo:_ -> currentRepo + "external":currentRepo:_ -> currentRepo + _ -> "" diff --git a/tutorial/.bazelrc.bzlmod b/tutorial/.bazelrc.bzlmod new file mode 100644 index 000000000..98ab67d91 --- /dev/null +++ b/tutorial/.bazelrc.bzlmod @@ -0,0 +1,2 @@ +common:bzlmod --experimental_enable_bzlmod +common:bzlmod --registry=file://%workspace%/../registry --registry=https://bcr.bazel.build \ No newline at end of file diff --git a/tutorial/.bazelrc.common b/tutorial/.bazelrc.common new file mode 120000 index 000000000..95435188c --- /dev/null +++ b/tutorial/.bazelrc.common @@ -0,0 +1 @@ +../.bazelrc.common \ No newline at end of file