about summary refs log tree commit diff
path: root/third_party/bazel/rules_haskell/haskell/import.bzl
blob: a79a9cff7b3da7de28e250efa8eac5782316590a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Importing prebuilt packages into bazel"""

load("@bazel_skylib//lib:paths.bzl", "paths")
load(
    "@io_tweag_rules_haskell//haskell:providers.bzl",
    "HaddockInfo",
    "HaskellInfo",
    "HaskellLibraryInfo",
    "empty_HaskellCcInfo",
)
load(":private/context.bzl", "haskell_context")
load(":private/path_utils.bzl", "copy_all", "link_forest", "ln")
load(":private/set.bzl", "set")
load(":private/version_macros.bzl", "generate_version_macros")

def _haskell_import_impl(ctx):
    hs = haskell_context(ctx)

    package_cache = ctx.actions.declare_file(
        paths.join("package.conf.d", "package.cache"),
    )

    local_package_confs = link_forest(
        ctx = ctx,
        srcs = ctx.attr.package_confs.files,
        sibling = package_cache,
    )

    local_haddock_html = ctx.actions.declare_directory("haddock-html")
    copy_all(
        ctx = ctx,
        srcs = ctx.attr.haddock_html.files,
        dest = local_haddock_html,
    )

    ctx.actions.run(
        outputs = [package_cache],
        inputs = local_package_confs,
        mnemonic = "HaskellCreatePackageCache",
        executable = hs.tools.ghc_pkg,
        arguments = [
            "recache",
            "--package-db",
            package_cache.dirname,
        ],
    )
    ln(ctx, package_cache, ctx.outputs.cache)

    dependencies_caches = set.singleton(package_cache)
    for dep in ctx.attr.deps:
        if HaskellInfo in dep:
            set.mutable_union(dependencies_caches, dep[HaskellInfo].package_databases)

    deps_ids = [
        dep[HaskellLibraryInfo].package_id
        for dep in ctx.attr.deps
        if HaskellLibraryInfo in dep
    ]

    version_macros = set.empty()
    if ctx.attr.version != None:
        version_macros = set.singleton(
            generate_version_macros(ctx, hs.name, ctx.attr.version),
        )

    libInfo = HaskellLibraryInfo(
        package_id = ctx.attr.package_id,
        version = ctx.attr.version,
    )
    buildInfo = HaskellInfo(
        package_ids = set.from_list([ctx.attr.package_id] + deps_ids),
        package_databases = dependencies_caches,
        version_macros = version_macros,
        import_dirs = [],
        source_files = set.empty(),
        extra_source_files = set.empty(),
        static_libraries = [],
        static_libraries_prof = [],
        dynamic_libraries = set.empty(),
        interface_dirs = set.empty(),
        compile_flags = [],
        prebuilt_dependencies = set.empty(),
        direct_prebuilt_deps = set.empty(),
        cc_dependencies = empty_HaskellCcInfo(),
        transitive_cc_dependencies = empty_HaskellCcInfo(),
    )
    html_files = ctx.attr.haddock_html.files.to_list()
    transitive_html = {ctx.attr.package_id: local_haddock_html} if html_files != [] else {}
    interface_files = ctx.attr.haddock_interfaces.files.to_list()
    transitive_haddocks = {ctx.attr.package_id: interface_files[0]} if interface_files != [] else {}

    haddockInfo = HaddockInfo(
        package_id = ctx.attr.package_id,
        transitive_html = transitive_html,
        transitive_haddocks = transitive_haddocks,
    )
    return [buildInfo, libInfo, haddockInfo]

haskell_import = rule(
    _haskell_import_impl,
    attrs = dict(
        package_id = attr.string(doc = "Workspace unique package identifier"),
        deps = attr.label_list(doc = "Haskell dependencies for the package"),
        version = attr.string(doc = "Package version."),
        haddock_interfaces = attr.label(doc = "List of haddock interfaces"),
        haddock_html = attr.label(doc = "List of haddock html dirs"),
        package_confs = attr.label(doc = "List of ghc-pkg package.conf files"),
        _version_macros = attr.label(
            executable = True,
            cfg = "host",
            default = Label("@io_tweag_rules_haskell//haskell:version_macros"),
        ),
    ),
    outputs = {
        "cache": "%{name}-cache",
    },
    toolchains = ["@io_tweag_rules_haskell//haskell:toolchain"],
)