packages feed

cabal-buck2-0.6.0.0: buck2/prelude/decls/haskell_rules.bzl

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is dual-licensed under either the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree or the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree. You may select, at your option, one of the
# above-listed licenses.

# TODO(cjhopman): This was generated by scripts/hacks/rules_shim_with_docs.py,
# but should be manually edited going forward. There may be some errors in
# the generated docs, and so those should be verified to be accurate and
# well-formatted (and then delete this TODO)

load("@prelude//linking:types.bzl", "Linkage")
load(":common.bzl", "LinkableDepType", "buck", "prelude_rule")
load(":haskell_common.bzl", "haskell_common")
load(":native_common.bzl", "native_common")

haskell_binary = prelude_rule(
    name = "haskell_binary",
    docs = """
        A `haskell_binary()` rule represents a groups of Haskell sources
        and deps which build an executable.
    """,
    examples = """
        ```
        haskell_binary(
          name = 'foo',
          srcs = [
            'Foo.hs',
          ],
        )
        ```
    """,
    further = None,
    attrs = (
        # @unsorted-dict-items
        {
            "exe_name": attrs.option(attrs.string(), default = None, doc = "The file name of the executable, if not the name of the target."),
            "main": attrs.option(
                attrs.string(),
                default = None,
                doc = """
                The main module serving as the entry point into the binary. If not specified,
                 the compiler default is used.
            """,
            ),
        }
        | native_common.link_group_deps()
        | native_common.link_group_public_deps_label()
        | native_common.link_style()
        | haskell_common.srcs_arg()
        | haskell_common.compiler_flags_arg()
        | haskell_common.deps_arg()
        | haskell_common.build_tool_depends_arg()
        | haskell_common.data_dirs_arg()
        | haskell_common.run_tool_depends_arg()
        | {
            "deps_query": attrs.option(attrs.query(), default = None),
            "enable_profiling": attrs.bool(default = False),
            "ghci_platform_preload_deps": attrs.list(attrs.tuple(attrs.regex(), attrs.set(attrs.dep(), sorted = True)), default = []),
            "ghci_preload_deps": attrs.set(attrs.dep(), sorted = True, default = []),
            "link_deps_query_whole": attrs.bool(default = False),
            # See haskell_library's compile_granularity.
            "compile_granularity": attrs.option(attrs.enum(["target", "module"]), default = None),
            "_per_module_compile": attrs.default_only(attrs.exec_dep(providers = [RunInfo], default = "prelude//haskell/tools:per_module_compile")),
            "_per_module_deps": attrs.default_only(attrs.exec_dep(providers = [RunInfo], default = "prelude//haskell/tools:per_module_deps")),
            "splice_depends": attrs.list(attrs.dep(), default = []),  # see haskell_library's splice_depends
            "linker_flags": attrs.list(attrs.arg(), default = []),
            "platform": attrs.option(attrs.string(), default = None),
        }
        | buck.licenses_arg()
        | buck.labels_arg()
        | buck.contacts_arg()
    ),
)

haskell_ghci = prelude_rule(
    name = "haskell_ghci",
    docs = """
        A `haskell_ghci()` rule produces a deployable script that
        launches a GHCi session with the given sources and dependencies
        preloaded. Building the rule compiles all dependent libraries
        (including any C++ shared libraries that GHCi needs to load);
        running the produced script enters the session ready for
        `:reload`-driven development.
    """,
    examples = None,
    further = None,
    attrs = (
        # @unsorted-dict-items
        {
            "compiler_flags": attrs.list(attrs.string(), default = []),
            "deps": attrs.list(attrs.dep(), default = []),
            "deps_query": attrs.option(attrs.query(), default = None),
            "enable_profiling": attrs.bool(default = False),
            "extra_script_templates": attrs.list(attrs.source(), default = []),
            "ghci_bin_dep": attrs.option(attrs.dep(), default = None),
            "ghci_init": attrs.option(attrs.source(), default = None),
            "linker_flags": attrs.list(attrs.arg(), default = []),
            "platform": attrs.option(attrs.string(), default = None),
            "preload_deps": attrs.set(attrs.dep(), sorted = True, default = []),
            "srcs": attrs.named_set(attrs.source(), sorted = True, default = []),
        }
        | buck.licenses_arg()
        | buck.labels_arg()
        | buck.contacts_arg()
    ),
)

haskell_haddock = prelude_rule(
    name = "haskell_haddock",
    docs = """
        A `haskell_haddock()` rule generates Haddock documentation for
        the given Haskell dependencies. Extra flags can be passed to the
        `haddock` tool via `haddock_flags`.
    """,
    examples = None,
    further = None,
    attrs = (
        # @unsorted-dict-items
        {
            "deps": attrs.list(attrs.dep(), default = []),
            "deps_query": attrs.option(attrs.query(), default = None),
            "haddock_flags": attrs.list(attrs.arg(), default = []),
            "platform": attrs.option(attrs.string(), default = None),
        }
        | buck.licenses_arg()
        | buck.labels_arg()
        | buck.contacts_arg()
    ),
)

haskell_ide = prelude_rule(
    name = "haskell_ide",
    docs = """
        A `haskell_ide()` rule defines a project set for use with an
        LSP-based Haskell IDE (such as `ghcide`/HLS). When built, it
        compiles the dependencies of the listed sources and produces the
        GHC flags needed to load them as a single multi-component IDE
        session, which is faster than loading each project individually
        and avoids `ghcide`'s multi-component limitations.
    """,
    examples = None,
    further = None,
    attrs = (
        # @unsorted-dict-items
        {
            "compiler_flags": attrs.list(attrs.string(), default = []),
            "deps": attrs.list(attrs.dep(), default = []),
            "deps_query": attrs.option(attrs.query(), default = None),
            "extra_script_templates": attrs.list(attrs.source(), default = []),
            "link_style": attrs.enum(LinkableDepType),
            "linker_flags": attrs.list(attrs.arg(), default = []),
            "platform": attrs.option(attrs.string(), default = None),
            "srcs": attrs.named_set(attrs.source(), sorted = True, default = []),
        }
        | buck.licenses_arg()
        | buck.labels_arg()
        | buck.contacts_arg()
    ),
)

haskell_library = prelude_rule(
    name = "haskell_library",
    docs = """
        A `haskell_library()` rule is used to identity a group of
        Haskell sources.
    """,
    examples = """
        ```
        haskell_library(
          name = 'fileutil',
          srcs = [
            'FileUtil.hs',
          ],
        )
        ```
    """,
    further = None,
    attrs = (
        # @unsorted-dict-items
        haskell_common.srcs_arg()
        | haskell_common.compiler_flags_arg()
        | haskell_common.deps_arg()
        | haskell_common.exported_linker_flags_arg()
        | haskell_common.build_tool_depends_arg()
        | haskell_common.data_dirs_arg()
        | native_common.link_whole(link_whole_type = attrs.bool(default = False))
        | native_common.preferred_linkage(preferred_linkage_type = attrs.enum(Linkage.values()))
        | {
            "enable_profiling": attrs.bool(default = False),
            "ghci_platform_preload_deps": attrs.list(attrs.tuple(attrs.regex(), attrs.set(attrs.dep(), sorted = True)), default = []),
            "ghci_preload_deps": attrs.set(attrs.dep(), sorted = True, default = []),
            "haddock_flags": attrs.list(attrs.arg(), default = []),
            # How finely the target is compiled: "target", by one `ghc
            # --make` action for the whole target, or "module", by an action
            # for each module (experimental). If it isn't set, the
            # toolchain's compile_granularity decides.
            "compile_granularity": attrs.option(attrs.enum(["target", "module"]), default = None),
            # Tools for per-module compilation
            "_per_module_compile": attrs.default_only(attrs.exec_dep(providers = [RunInfo], default = "prelude//haskell/tools:per_module_compile")),
            "_per_module_deps": attrs.default_only(attrs.exec_dep(providers = [RunInfo], default = "prelude//haskell/tools:per_module_deps")),
            # The local dependencies (compiled per-module or built with
            # --make) whose code the target's Template Haskell splices and
            # ANN pragmas run. The target's compile (a Template Haskell
            # module's, with per_module) waits until these libraries, and
            # the libraries they depend on, are linked. Its splices can't run
            # code of any other local library. Splices can always run code of prebuilt packages, so
            # listing those makes no difference. The default, an empty list,
            # means that splices run no local library's code.
            "splice_depends": attrs.list(attrs.dep(), default = []),
            # Libraries that are part of this one, as the C of a Cabal package
            # is part of its library: their objects are in its archive, and
            # linked into its shared library, and what depends on it isn't
            # linked with them again (nor with their own dependencies, which
            # the library should depend on). They should have static
            # preferred linkage, and their link info be objects
            # (use_archive = False, for a cxx_library).
            "internal_deps": attrs.list(attrs.dep(), default = []),
            # The internal_deps as built for profiling (as Cabal compiles a
            # package's C for each way): the profiled library has their
            # objects instead.
            "prof_internal_deps": attrs.list(attrs.dep(), default = []),
            # Whether the library's package is exposed: for a GHC that uses
            # its package database as an installed one (as its global one,
            # say). The build itself hides all packages.
            "exposed": attrs.bool(default = False),
            # Modules of the library that are only visible to modules
            # in the library itself (a Cabal package's other-modules).
            "hidden_modules": attrs.list(attrs.string(), default = []),
            # The soname of the library's shared library, when it isn't its
            # file's name.
            "soname": attrs.option(attrs.string(), default = None),
            "linker_flags": attrs.list(attrs.arg(), default = []),
            # The name the library's package has in the package db, for
            # `import "name" Module`. Not its id, which is derived from the
            # label and is unique.
            "package_name": attrs.option(attrs.string(), default = None),
            # Modules the library re-exports from the library of one of its
            # dependencies (or from itself, when there is none): the new name to
            # the dependency and the module's original name (the
            # `reexported-modules` of a Cabal package).
            "reexported_modules": attrs.dict(attrs.string(), attrs.tuple(attrs.option(attrs.dep()), attrs.string()), default = {}),
            # The library's unit id, when it must be a particular one (that of
            # a package GHC knows by its unit id, say: rts, ghc-internal).
            # Otherwise it is derived from the label.
            "unit_id": attrs.option(attrs.string(), default = None),
            # Its version, which GHC defines `VERSION_<package>` macros with:
            # they must agree with the ones of the package's cabal_macros.h.
            "package_version": attrs.option(attrs.string(), default = None),
            "platform": attrs.option(attrs.string(), default = None),
        }
        | buck.licenses_arg()
        | buck.labels_arg()
        | buck.contacts_arg()
    ),
)

haskell_prebuilt_library = prelude_rule(
    name = "haskell_prebuilt_library",
    docs = """
        A `prebuilt_haskell_library()` rule is used to identify Haskell
        prebuilt libraries and their associated interface files.
    """,
    examples = """
        ```
        prebuilt_haskell_library(
          name = 'file',
          static_interfaces = [
            'interfaces',
          ],
          shared_interfaces = [
            'interfaces_dyn',
          ],
          static_libs = [
            'libFileUtil.a',
          ],
          shared_libs = {
            'libFileUtil.so': 'libFileUtil.so',
          },
        )
        ```
    """,
    further = None,
    attrs = (
        # @unsorted-dict-items
        {
            "deps": attrs.list(
                attrs.dep(),
                default = [],
                doc = """
                Other `prebuilt_haskell_library()` rules from which this library
                 imports modules.
            """,
            ),
            "static_libs": attrs.list(
                attrs.source(),
                default = [],
                doc = """
                The libraries to use when building a statically linked top-level target.
            """,
            ),
            "shared_libs": attrs.dict(
                key = attrs.string(),
                value = attrs.source(),
                sorted = False,
                default = {},
                doc = """
                A map of shared library names to shared library paths to use when building a
                 dynamically linked top-level target.
            """,
            ),
            "runtime_shared_libs": attrs.dict(
                key = attrs.string(),
                value = attrs.source(),
                sorted = False,
                default = {},
                doc = """
                A map of shared library names to shared library paths that a dynamically
                 linked top-level target needs when it runs, but which should not be
                 explicitly linked. This is needed to support the RTS: GHC automatically
                 links the appropriate flavour of the RTS when linking an executable,
                 so we have to ensure the RTS shared libraries are available when
                 the executable runs.
            """,
            ),
            "exported_compiler_flags": attrs.list(
                attrs.string(),
                default = [],
                doc = """
                Compiler flags used by dependent rules when compiling with this library.
            """,
            ),
        }
        | haskell_common.exported_linker_flags_arg()
        | {
            "cxx_header_dirs": attrs.list(attrs.source(), default = []),
            "db": attrs.source(allow_directory = True),
            "enable_profiling": attrs.bool(default = False),
            "exported_post_linker_flags": attrs.list(attrs.arg(anon_target_compatible = True), default = []),
            "id": attrs.string(default = ""),
            "import_dirs": attrs.list(attrs.source(), default = []),
            "pic_profiled_static_libs": attrs.list(attrs.source(), default = []),
            "pic_static_libs": attrs.list(attrs.source(), default = []),
            "profiled_static_libs": attrs.list(attrs.source(), default = []),
            "version": attrs.string(default = ""),
        }
        | buck.licenses_arg()
        | buck.labels_arg()
        | buck.contacts_arg()
    ),
)

haskell_rules = struct(
    haskell_binary = haskell_binary,
    haskell_ghci = haskell_ghci,
    haskell_haddock = haskell_haddock,
    haskell_ide = haskell_ide,
    haskell_library = haskell_library,
    haskell_prebuilt_library = haskell_prebuilt_library,
)