about summary refs log tree commit diff
path: root/third_party/bazel/rules_haskell/haskell/private/pkg_id.bzl
blob: 0a3c5fa439d234a4c8f35a23c64726209c352cc1 (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
"""Package identifiers"""

load(":private/mode.bzl", "is_profiling_enabled")
load("@bazel_skylib//lib:paths.bzl", "paths")

def _zencode(s):
    """Z-escape special characters to make a valid GHC package identifier.

    Args:
      s: string
    """
    return s.replace("Z", "ZZ").replace("_", "ZU").replace("/", "ZS")

def _to_string(my_pkg_id):
    """Get a globally unique package identifier.

    The identifier is required to be unique for each Haskell rule.
    It includes the Bazel package and the name of this component.
    We can't use just the latter because then two components with
    the same names in different packages would clash.
    """
    return _zencode(
        paths.join(
            my_pkg_id.label.workspace_root,
            my_pkg_id.label.package,
            my_pkg_id.name,
        ),
    )

def _new(label, version = None):
    """Create a new package identifier.

    Package identifiers should be globally unique. This is why we use
    a label to identify them.

    Args:
      label: The label of the rule declaring the package.
      version: an optional version annotation.

    Returns:
      string: GHC package ID to use.

    """
    return struct(
        label = label,
        name = label.name.replace("_", "-"),
        version = version,
    )

def _library_name(hs, my_pkg_id, prof_suffix = False):
    """Get library name.

    Args:
      hs: Haskell context.
      my_pkg_id: pkg_id struct.
      prof_suffix: whether to automatically add profiling suffix.
    """
    library_name = "HS" + _to_string(my_pkg_id)
    if is_profiling_enabled(hs) and prof_suffix:
        library_name += "_p"
    return library_name

pkg_id = struct(
    new = _new,
    to_string = _to_string,
    library_name = _library_name,
)