diff --git a/haskell/ghc_bindist.bzl b/haskell/ghc_bindist.bzl index a33f08195..9e4313900 100644 --- a/haskell/ghc_bindist.bzl +++ b/haskell/ghc_bindist.bzl @@ -18,6 +18,7 @@ load( "resolve_labels", ) load("//haskell:ghc.bzl", "DEFAULT_GHC_VERSION") +load(":private/bazel_platforms.bzl", "bazel_platforms") _GHC_DEFAULT_VERSION = DEFAULT_GHC_VERSION @@ -567,7 +568,7 @@ def haskell_register_ghc_bindists( configure_python3_toolchain(name = LOCAL_PYTHON_REPO_NAME, register = register) def _configure_python3_toolchain_impl(repository_ctx): - cpu = get_cpu_value(repository_ctx) + os_cpu = get_cpu_value(repository_ctx) python3_path = find_python(repository_ctx) if check_bazel_version("4.2.0")[0]: stub_shebang = """stub_shebang = "#!{python3_path}",""".format( @@ -595,20 +596,18 @@ toolchain( toolchain = ":py_runtime_pair", toolchain_type = "@bazel_tools//tools/python:toolchain_type", exec_compatible_with = [ - "@platforms//cpu:x86_64", + "@platforms//cpu:{cpu}", "@platforms//os:{os}", ], target_compatible_with = [ - "@platforms//cpu:x86_64", + "@platforms//cpu:{cpu}", "@platforms//os:{os}", ], ) """.format( python3 = python3_path, - os = { - "darwin": "osx", - "x64_windows": "windows", - }.get(cpu, "linux"), + os = bazel_platforms.get_os(os_cpu), + cpu = bazel_platforms.get_cpu(os_cpu), stub_shebang = stub_shebang, )) diff --git a/haskell/private/bazel_platforms.bzl b/haskell/private/bazel_platforms.bzl new file mode 100644 index 000000000..3377eaf9d --- /dev/null +++ b/haskell/private/bazel_platforms.bzl @@ -0,0 +1,32 @@ +"""Module for managing/deriving official Bazel platform values.""" + +# The expected values for os_cpu can be found by looking at the +# implementation for get_cpu_value in lib_cc_configure.bzl. +# https://github.com/bazelbuild/bazel/blob/e11506feaea7401c3d27f55b47183ef49bd1d5a8/tools/cpp/lib_cc_configure.bzl#L186 + +def _get_os(os_cpu): + if os_cpu.find("darwin") >= 0: + return "osx" + if os_cpu.find("windows") >= 0: + return "windows" + return "linux" + +def _get_cpu(os_cpu): + # This value could appear in older versions of Bazel. + if os_cpu == "darwin": + return "x86_64" + + # This handles modern os-cpu values like darwin_arm64 or darwin_x86_64. + if os_cpu.startswith("darwin_"): + return os_cpu.removeprefix("darwin_") + if os_cpu == "arm64_windows": + return "arm64" + if os_cpu == "x64_windows": + return "x86_64" + + return "x86_64" + +bazel_platforms = struct( + get_os = _get_os, + get_cpu = _get_cpu, +) diff --git a/rules_haskell_nix/shell.nix b/rules_haskell_nix/shell.nix deleted file mode 100644 index d040173ab..000000000 --- a/rules_haskell_nix/shell.nix +++ /dev/null @@ -1,57 +0,0 @@ -{ pkgs ? import ./nixpkgs { }, docTools ? true, ghcVersion ? "9.2.8" }: - -with pkgs; -mkShell { - # XXX: hack for macosX, this flags disable bazel usage of xcode - # Note: this is set even for linux so any regression introduced by this flag - # will be catched earlier - # See: https://github.com/bazelbuild/bazel/issues/4231 - BAZEL_USE_CPP_ONLY_TOOLCHAIN = 1; - TMPDIR = "/tmp"; - - GHC_VERSION = ghcVersion; - - # Set UTF-8 local so that run-tests can parse GHC's unicode output. - LANG = "C.UTF-8"; - - buildInputs = [ - go - nix - which - perl - python3 - jdk11 - # For stack_install. - stack - # Needed for ghcide which expects ghc in PATH. - haskell.packages."ghc${ builtins.replaceStrings [ "." ] [ "" ] ghcVersion }".ghc - # Needed for @com_github_golang_protobuf, itself needed by buildifier. - git - # Needed to get correct locale for tests with encoding - glibcLocales - # to avoid CA certificate failures on macOS CI - cacert - # Needed for debug/linking_utils - binutils - # check the start script for problems - shellcheck - file - ] ++ lib.optionals docTools [ graphviz python39Packages.sphinx zip unzip ]; - - packages = [ bazel_6 ]; - - shellHook = '' - # Add nix config flags to .bazelrc.local. - # - BAZELRC_LOCAL=".bazelrc.local" - if [ ! -e "$BAZELRC_LOCAL" ] - then - echo "[!] It looks like you are using a Nix-based system." - 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=@rules_nixpkgs_core//platforms:host" - echo "run --host_platform=@rules_nixpkgs_core//platforms:host" - fi - ''; -} diff --git a/tests/haskell_tests/BUILD.bazel b/tests/haskell_tests/BUILD.bazel new file mode 100644 index 000000000..8c4ffa0d4 --- /dev/null +++ b/tests/haskell_tests/BUILD.bazel @@ -0,0 +1,3 @@ +load(":bazel_platforms_tests.bzl", "bazel_platforms_test_suite") + +bazel_platforms_test_suite() diff --git a/tests/haskell_tests/bazel_platforms_tests.bzl b/tests/haskell_tests/bazel_platforms_tests.bzl new file mode 100644 index 000000000..78e851623 --- /dev/null +++ b/tests/haskell_tests/bazel_platforms_tests.bzl @@ -0,0 +1,97 @@ +"""Tests for `bazel_platforms` module.""" + +load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") +load("//haskell:private/bazel_platforms.bzl", "bazel_platforms") + +def _get_os_test(ctx): + env = unittest.begin(ctx) + + tests = [ + struct( + msg = "darwin (legacy MacOS on x86_64)", + os_cpu = "darwin", + exp = "osx", + ), + struct( + os_cpu = "darwin_x86_64", + exp = "osx", + ), + struct( + os_cpu = "darwin_arm64", + exp = "osx", + ), + struct( + os_cpu = "arm64_windows", + exp = "windows", + ), + struct( + os_cpu = "x64_windows", + exp = "windows", + ), + struct( + os_cpu = "freebsd", + exp = "linux", + ), + struct( + os_cpu = "openbsd", + exp = "linux", + ), + ] + for t in tests: + actual = bazel_platforms.get_os(t.os_cpu) + msg = getattr(t, "msg", t.os_cpu) + asserts.equals(env, t.exp, actual, msg) + + return unittest.end(env) + +get_os_test = unittest.make(_get_os_test) + +def _get_cpu_test(ctx): + env = unittest.begin(ctx) + + tests = [ + struct( + msg = "darwin (legacy MacOS on x86_64)", + os_cpu = "darwin", + exp = "x86_64", + ), + struct( + os_cpu = "darwin_x86_64", + exp = "x86_64", + ), + struct( + os_cpu = "darwin_arm64", + exp = "arm64", + ), + struct( + os_cpu = "arm64_windows", + exp = "arm64", + ), + struct( + os_cpu = "x64_windows", + exp = "x86_64", + ), + struct( + os_cpu = "freebsd", + exp = "x86_64", + ), + struct( + os_cpu = "openbsd", + exp = "x86_64", + ), + ] + for t in tests: + actual = bazel_platforms.get_cpu(t.os_cpu) + msg = getattr(t, "msg", t.os_cpu) + asserts.equals(env, t.exp, actual, msg) + + return unittest.end(env) + +get_cpu_test = unittest.make(_get_cpu_test) + +def bazel_platforms_test_suite(name = "bazel_platforms_tests"): + return unittest.suite( + name, + get_os_test, + get_cpu_test, + ) diff --git a/tools/os_info.bzl b/tools/os_info.bzl index 7fc127b27..1e9090481 100644 --- a/tools/os_info.bzl +++ b/tools/os_info.bzl @@ -16,6 +16,7 @@ def _os_info_impl(repository_ctx): "aarch64", "darwin", "darwin_arm64", + "darwin_x86_64", "k8", "x64_windows", ]