packages feed

cabal-buck2-0.6.0.0: buck2/toolchains/haskell.bzl

load("@prelude//haskell:toolchain.bzl", "HaskellPlatformInfo", "HaskellToolchainInfo", "per_module_worker_info", "print_libdir")
load("@prelude//haskell:per_module_worker.bzl", "build_per_module_worker", "per_module_worker_rts_options", "per_module_worker_session_mode", "per_module_worker_sessions")

def _tool(tool):
    if isinstance(tool, Dependency):
        return tool[RunInfo]
    return tool

def _haskell_toolchain_impl(ctx):
    compiler = _tool(ctx.attrs.compiler)
    worker = _worker(ctx, compiler)
    sessions = int(ctx.attrs.per_module_worker_sessions) if ctx.attrs.per_module_worker_sessions else None
    session_mode = ctx.attrs.per_module_worker_session_mode or None
    return [
        DefaultInfo(),
        HaskellToolchainInfo(
            compile_granularity = ctx.attrs.compile_granularity,
            compiler = compiler,
            packager = _tool(ctx.attrs.packager),
            linker = compiler,
            haddock = _tool(ctx.attrs.haddock),
            hsc2hs = _tool(ctx.attrs.hsc2hs),
            compiler_flags = ctx.attrs.compiler_flags,
            linker_flags = ctx.attrs.linker_flags,
            compile_env = ctx.attrs.compile_env,
            link_env = ctx.attrs.link_env,
            dynamic_ghc = ctx.attrs.dynamic_ghc,
            ghc_version = ctx.attrs.ghc_version,
            libdir = print_libdir(ctx.actions, "libdir", compiler, ctx.attrs.compile_env),
            # The WorkerInfo is made once, here, so that every target names
            # the same worker. buck2 starts a process for each distinct
            # worker.
            per_module_shared_worker = per_module_worker_info(worker, ctx.attrs.compile_env, ctx.attrs.per_module_worker_rts_options, sessions, session_mode) if worker != None else None,
            per_module_worker_rts_options = ctx.attrs.per_module_worker_rts_options,
            per_module_worker_sessions = sessions,
            per_module_worker_session_mode = session_mode,
            per_module_deps = ctx.attrs.per_module_deps[RunInfo] if ctx.attrs.per_module_deps else None,
            per_module_compile = ctx.attrs.per_module_compile[RunInfo] if ctx.attrs.per_module_compile else None,
        ),
        HaskellPlatformInfo(name = host_info().arch),
    ]

# The toolchain's persistent worker, or None if it has none (see
# prelude/haskell/per_module_worker.bzl). It is the toolchain's
# `per_module_worker_program` if that is set. Otherwise the toolchain builds
# the worker itself, with its own compiler, from the sources in the prelude.
def _worker(ctx, compiler):
    if ctx.attrs.per_module_worker == "none":
        return None
    if ctx.attrs.per_module_worker_program:
        return ctx.attrs.per_module_worker_program[RunInfo]
    srcs = ctx.attrs._per_module_worker_srcs[DefaultInfo].default_outputs[0]
    return build_per_module_worker(ctx.actions, compiler, ctx.attrs.compile_env, srcs, ctx.attrs.dynamic_ghc)

# A tool is either the name of a program on $PATH, or a target that builds it.
def _tool_attr(default):
    return attrs.one_of(attrs.exec_dep(providers = [RunInfo]), attrs.string(), default = default)

haskell_toolchain = rule(
    impl = _haskell_toolchain_impl,
    attrs = {
        "compiler": _tool_attr("ghc"),
        "packager": _tool_attr("ghc-pkg"),
        "haddock": _tool_attr("haddock"),
        "hsc2hs": _tool_attr("hsc2hs"),
        "compiler_flags": attrs.list(attrs.string(), default = []),
        "linker_flags": attrs.list(attrs.string(), default = []),
        "compile_env": attrs.dict(attrs.string(), attrs.string(), default = {}),
        "link_env": attrs.dict(attrs.string(), attrs.string(), default = {}),
        # Whether the `compiler` above is itself dynamically linked.
        # We need to know this because it affects how Template Haskell
        # code needs to be compiled. It can be found by running `ghc
        # --info`: look for the "GHC Dynamic" value.
        "dynamic_ghc": attrs.bool(default = True),
        # The version of the `compiler` above, as `ghc --numeric-version`
        # prints it (see HaskellToolchainInfo).
        "ghc_version": attrs.string(),
        # See HaskellToolchainInfo.
        "compile_granularity": attrs.enum(["target", "module"], default = "target"),
        "per_module_worker": attrs.enum(["builtin", "none"], default = "builtin"),
        "per_module_worker_rts_options": attrs.list(attrs.string(), default = per_module_worker_rts_options()),
        # An empty string means that the worker uses its own default, since
        # `attrs.option` can't default to a value.
        "per_module_worker_sessions": attrs.string(default = per_module_worker_sessions()),
        "per_module_worker_session_mode": attrs.string(default = per_module_worker_session_mode()),
        # The worker, if the toolchain's GHC can't build it (see
        # prelude/haskell/per_module_worker.bzl).
        "per_module_worker_program": attrs.option(attrs.exec_dep(providers = [RunInfo]), default = None),
        "_per_module_worker_srcs": attrs.dep(default = "prelude//haskell/tools:worker-srcs"),
        # The tools of the per-module compiles, if the prelude's can't be
        # used (see per_module_deps in prelude/haskell/toolchain.bzl).
        "per_module_deps": attrs.option(attrs.exec_dep(providers = [RunInfo]), default = None),
        "per_module_compile": attrs.option(attrs.exec_dep(providers = [RunInfo]), default = None),
    },
    is_toolchain_rule = True,
)