Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
Per OS ghc_workspace
Browse files Browse the repository at this point in the history
Allow to configure separate GHC workspaces for different operating
systes. This allows to use different GHC distributions on e.g. Linux and
Windows.
  • Loading branch information
aherrmann committed Feb 6, 2019
1 parent cddfd05 commit 4afbe5e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
18 changes: 12 additions & 6 deletions hazel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl",
load("@bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive",
)
load("//tools:ghc.bzl", "get_ghc_workspace", "default_ghc_workspaces")
load("//tools:mangling.bzl", "hazel_binary", "hazel_library", "hazel_workspace")

def _cabal_haskell_repository_impl(ctx):
ghc_workspace = get_ghc_workspace(ctx.attr.ghc_workspaces, ctx)

pkg = "{}-{}".format(ctx.attr.package_name, ctx.attr.package_version)
url = "https://hackage.haskell.org/package/{}.tar.gz".format(pkg)
# If the SHA is wrong, the error message is very unhelpful:
Expand All @@ -31,7 +34,7 @@ def _cabal_haskell_repository_impl(ctx):
symlink_and_invoke_hazel(
ctx,
ctx.attr.hazel_base_repo_name,
ctx.attr.ghc_workspace,
ghc_workspace,
ctx.attr.package_flags,
ctx.attr.package_name + ".cabal",
"package.bzl"
Expand All @@ -45,7 +48,7 @@ _cabal_haskell_repository = repository_rule(
"package_flags": attr.string_dict(mandatory=True),
"hazel_base_repo_name": attr.string(mandatory=True),
"sha256": attr.string(mandatory=True),
"ghc_workspace": attr.string(mandatory=True),
"ghc_workspaces": attr.string_dict(mandatory=True),
})

def _core_library_repository_impl(ctx):
Expand Down Expand Up @@ -110,7 +113,7 @@ def hazel_repositories(
extra_libs_hdrs={},
extra_libs_strip_include_prefix={},
exclude_packages=[],
ghc_workspace="@ghc"):
ghc_workspaces=default_ghc_workspaces):
"""Generates external dependencies for a set of Haskell packages.
This macro should be invoked in the WORKSPACE. It generates a set of
Expand Down Expand Up @@ -138,15 +141,18 @@ def hazel_repositories(
extra_libs_hdrs: Similar to extra_libs, but provides header files.
extra_libs_strip_include_prefix: Similar to extra_libs, but allows to
get include prefix to strip.
ghc_workspace: Workspace in which GHC is provided. Default "@ghc".
ghc_workspaces: Dictionary mapping OS names to GHC workspaces.
Default: Linux/MacOS: "@ghc", Windows: "@ghc_windows".
Dictionary keys correspond to CPU values as returned by
`get_cpu_value` from `@bazel_tools//tools/cpp:lib_cc_configure.bzl`.
"""
hazel_base_repo_name = "hazel_base_repository"

pkgs = {n: packages[n] for n in packages if n not in exclude_packages}

hazel_base_repository(
name = hazel_base_repo_name,
ghc="{}//:bin/ghc".format(ghc_workspace),
ghc_workspaces = ghc_workspaces,
extra_libs = extra_libs,
extra_libs_hdrs = extra_libs_hdrs,
extra_libs_strip_include_prefix = extra_libs_strip_include_prefix,
Expand All @@ -173,7 +179,7 @@ def hazel_repositories(
package_flags = flags,
sha256 = pkgs[p].sha256 if hasattr(pkgs[p], "sha256") else None,
hazel_base_repo_name = hazel_base_repo_name,
ghc_workspace = ghc_workspace,
ghc_workspaces = ghc_workspaces,
)

for p in core_packages:
Expand Down
12 changes: 8 additions & 4 deletions hazel_base_repository/hazel_base_repository.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
load("//tools:ghc.bzl", "get_ghc_workspace", "get_executable_name")

def _hazel_base_repository_impl(ctx):
ctx.symlink(ctx.attr.ghc, "ghc")
ghc_workspace = get_ghc_workspace(ctx.attr.ghc_workspaces, ctx)
ghc_label = get_executable_name("{}//:bin/ghc".format(ghc_workspace), ctx)
ghc = ctx.path(Label(ghc_label))

cabal2bazel_srcs = [
"@ai_formation_hazel//hazel_base_repository:cabal2bazel.hs",
Expand All @@ -13,7 +17,7 @@ def _hazel_base_repository_impl(ctx):
ctx.symlink(Label(f), l.name)

res = ctx.execute([
"./ghc",
ghc,
"-Wall",
"-Werror",
# Only use core packages of GHC, nothing from the the user level:
Expand All @@ -26,7 +30,7 @@ def _hazel_base_repository_impl(ctx):
if res.return_code != 0:
fail("Couldn't build cabal2bazel:\n{}\n{}".format(res.stdout,res.stderr))

res = ctx.execute(["./ghc", "--numeric-version"])
res = ctx.execute([ghc, "--numeric-version"])
if res.return_code != 0:
fail("Couldn't get GHC version:\n{}\n{}".format(res.stdout,res.stderr))

Expand All @@ -53,7 +57,7 @@ extra_libs_strip_include_prefix = {}
hazel_base_repository = repository_rule(
implementation=_hazel_base_repository_impl,
attrs={
"ghc": attr.label(mandatory=True),
"ghc_workspaces": attr.string_dict(mandatory=True),
"extra_libs": attr.string_dict(mandatory=True),
"extra_libs_hdrs": attr.string_dict(mandatory=True),
"extra_libs_strip_include_prefix": attr.string_dict(mandatory=True),
Expand Down
26 changes: 26 additions & 0 deletions tools/ghc.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value")

default_ghc_workspaces = {
"k8": "@ghc",
"darwin": "@ghc",
"x64_windows": "@ghc_windows",
}

def get_ghc_workspace(ghc_workspaces, repository_ctx):
"""Return the GHC workspace appropriate for the current OS."""
cpu_value = get_cpu_value(repository_ctx)
if cpu_value not in ghc_workspaces:
fail("No known GHC workspace for CPU {} in {}".format(
cpu_value, ghc_workspaces))
return ghc_workspaces[cpu_value]


def get_executable_name(name, repository_ctx):
"""Return the executable name for the current platform.
On Windows, appends `.exe` to `name`. Otherwise, returns `name`.
"""
if get_cpu_value(repository_ctx) == "x64_windows":
return "%s.exe"%name
else:
return name

0 comments on commit 4afbe5e

Please sign in to comment.