Core Haskell rules
Bazel

Core Haskell rules


haskell_binary

haskell_binary(name, deps, data, srcs, compiler_flags, extra_srcs, linkstatic, main_function, plugins, repl_ghci_args, runcompile_flags, src_strip_prefix, version)

Build an executable from Haskell source.

Every haskell_binary target also defines an optional REPL target that is not built by default, but can be built on request. The name of the REPL target is the same as the name of binary with "@repl" added at the end. For example, the target above also defines main@repl.

You can call the REPL like this (requires Bazel 0.15 or later):

$ bazel run //:hello@repl

Attributes

name

Name; Required

A unique name for this rule.

deps

List of labels; Optional; Default is []

List of other Haskell libraries to be linked to this target.

data

List of labels; Optional; Default is []

See Bazel documentation.

srcs

List of labels; Optional; Default is []

Haskell source files.

compiler_flags

List of strings; Optional; Default is []

Flags to pass to Haskell compiler.

extra_srcs

List of labels; Optional; Default is []

Extra (non-Haskell) source files that will be needed at compile time (e.g. by Template Haskell).

linkstatic

Boolean; Optional; Default is True

Link dependencies statically wherever possible. Some system libraries may still be linked dynamically, as are libraries for which there is no static library. So the resulting executable will still be dynamically linked, hence only mostly static.

main_function

String; Optional; Default is 'Main.main'

A function with type IO _, either the qualified name of a function from any module or the bare name of a function from a Main module. It is also possible to give the qualified name of any module exposing a main function.

plugins

List of labels; Optional; Default is []

Compiler plugins to use during compilation.

repl_ghci_args

List of strings; Optional; Default is []

Arbitrary extra arguments to pass to GHCi. This extends compiler_flags and repl_ghci_args from the toolchain

runcompile_flags

List of strings; Optional; Default is []

Arbitrary extra arguments to pass to runghc. This extends compiler_flags and repl_ghci_args from the toolchain

src_strip_prefix

String; Optional; Default is ''

Directory in which module hierarchy starts.

version

String; Optional; Default is ''

Executable version. If this is specified, CPP version macros will be generated for this build.

Examples

haskell_binary(
    name = "hello",
    srcs = ["Main.hs", "Other.hs"],
    deps = ["//lib:some_lib"]
)

haskell_library

haskell_library(name, deps, data, srcs, compiler_flags, exports, extra_srcs, hidden_modules, linkstatic, plugins, repl_ghci_args, runcompile_flags, src_strip_prefix, version)

Build a library from Haskell source.

Every haskell_library target also defines an optional REPL target that is not built by default, but can be built on request. It works the same way as for haskell_binary.

Attributes

name

Name; Required

A unique name for this rule.

deps

List of labels; Optional; Default is []

List of other Haskell libraries to be linked to this target.

data

List of labels; Optional; Default is []

See Bazel documentation.

srcs

List of labels; Optional; Default is []

Haskell source files.

compiler_flags

List of strings; Optional; Default is []

Flags to pass to Haskell compiler.

exports

Dictionary mapping labels to strings; Optional; Default is {}

A dictionary mapping dependencies to module reexports that should be available for import by dependencies.

extra_srcs

List of labels; Optional; Default is []

Extra (non-Haskell) source files that will be needed at compile time (e.g. by Template Haskell).

hidden_modules

List of strings; Optional; Default is []

Modules that should be unavailable for import by dependencies.

linkstatic

Boolean; Optional; Default is False

Create a static library, not both a static and a shared library.

plugins

List of labels; Optional; Default is []

Compiler plugins to use during compilation.

repl_ghci_args

List of strings; Optional; Default is []

Arbitrary extra arguments to pass to GHCi. This extends compiler_flags and repl_ghci_args from the toolchain

runcompile_flags

List of strings; Optional; Default is []

Arbitrary extra arguments to pass to runghc. This extends compiler_flags and repl_ghci_args from the toolchain

src_strip_prefix

String; Optional; Default is ''

Directory in which module hierarchy starts.

version

String; Optional; Default is ''

Library version. Not normally necessary unless to build a library originally defined as a Cabal package. If this is specified, CPP version macro will be generated.

Examples

haskell_library(
    name = "hello-lib",
    srcs = glob(["src/**/*.hs"]),
    src_strip_prefix = "src",
    deps = [
        "//hello-sublib:lib",
    ],
    exports = {
        "//hello-sublib:lib": "Lib1 as HelloLib1, Lib2",
    },
)

haskell_test

haskell_test(name, deps, data, srcs, compiler_flags, coverage_report_format, expected_covered_expressions_percentage, expected_uncovered_expression_count, experimental_coverage_source_patterns, extra_srcs, linkstatic, main_function, plugins, repl_ghci_args, runcompile_flags, src_strip_prefix, strict_coverage_analysis, version)

Build a test suite.

Additionally, it accepts all common bazel test rule fields. This allows you to influence things like timeout and resource allocation for the test.

Attributes

name

Name; Required

A unique name for this rule.

deps

List of labels; Optional; Default is []

List of other Haskell libraries to be linked to this target.

data

List of labels; Optional; Default is []

See Bazel documentation.

srcs

List of labels; Optional; Default is []

Haskell source files.

compiler_flags

List of strings; Optional; Default is []

Flags to pass to Haskell compiler.

coverage_report_format

String; Optional; Default is 'text'

The format to output the coverage report in. Supported values: "text", "html". Default: "text". Report can be seen in the testlog XML file, or by setting --test_output=all when running bazel coverage.

expected_covered_expressions_percentage

Integer; Optional; Default is -1

The expected percentage of expressions covered by testing.

expected_uncovered_expression_count

Integer; Optional; Default is -1

The expected number of expressions which are not covered by testing.

experimental_coverage_source_patterns

List of strings; Optional; Default is ['//...']

The path patterns specifying which targets to analyze for test coverage metrics.

            Wild-card targets such as //... or //:all are allowed. The paths must be relative to the workspace, which means they must start with "//".

            Note, this attribute may leave experimental status depending on the outcome of https://github.com/bazelbuild/bazel/issues/7763.
extra_srcs

List of labels; Optional; Default is []

Extra (non-Haskell) source files that will be needed at compile time (e.g. by Template Haskell).

linkstatic

Boolean; Optional; Default is True

Link dependencies statically wherever possible. Some system libraries may still be linked dynamically, as are libraries for which there is no static library. So the resulting executable will still be dynamically linked, hence only mostly static.

main_function

String; Optional; Default is 'Main.main'

A function with type IO _, either the qualified name of a function from any module or the bare name of a function from a Main module. It is also possible to give the qualified name of any module exposing a main function.

plugins

List of labels; Optional; Default is []

Compiler plugins to use during compilation.

repl_ghci_args

List of strings; Optional; Default is []

Arbitrary extra arguments to pass to GHCi. This extends compiler_flags and repl_ghci_args from the toolchain

runcompile_flags

List of strings; Optional; Default is []

Arbitrary extra arguments to pass to runghc. This extends compiler_flags and repl_ghci_args from the toolchain

src_strip_prefix

String; Optional; Default is ''

Directory in which module hierarchy starts.

strict_coverage_analysis

Boolean; Optional; Default is False

Requires that the coverage metric is matched exactly, even doing better than expected is not allowed.

version

String; Optional; Default is ''

Executable version. If this is specified, CPP version macros will be generated for this build.


haskell_toolchain_library

haskell_toolchain_library(name, package)

Import packages that are prebuilt outside of Bazel.

Use this rule to make dependencies that are prebuilt (supplied as part of the compiler toolchain) available as targets.

Attributes

name

Name; Required

A unique name for this rule.

package

String; Optional; Default is ''

The name of a GHC package not built by Bazel. Defaults to the name of the rule.

Examples

haskell_toolchain_library(
    name = "base_pkg",
    package = "base",
)

haskell_library(
    name = "hello-lib",
    srcs = ["Lib.hs"],
    deps = [
        ":base_pkg",
        "//hello-sublib:lib",
    ],
)