cabal-buck2-0.6.0.0: buck2/prelude/haskell/util.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.
load("@prelude//:paths.bzl", "paths")
load(
"@prelude//haskell:library_info.bzl",
"HaskellLibraryInfo",
"HaskellLibraryProvider",
)
load(
"@prelude//haskell:link_info.bzl",
"HaskellLinkInfo",
"HaskellProfLinkInfo",
)
load(
"@prelude//linking:link_info.bzl",
"LinkStyle",
"MergedLinkInfo",
)
load(
"@prelude//linking:shared_libraries.bzl",
"SharedLibraryInfo",
)
HASKELL_EXTENSIONS = [
".hs",
".lhs",
".hsc",
".chs",
".x",
".y",
]
# We take a named_set for srcs, which is sometimes a list, sometimes a dict.
# In future we should only accept a list, but for now, cope with both.
def srcs_to_pairs(srcs) -> list[(str, Artifact)]:
if type(srcs) == type({}):
return srcs.items()
else:
return [(src_to_module_name(src.short_path), src) for src in srcs]
# The name of the files of a haskell_library() (lib<name>.a, say), from its
# label.
#
# A package path containing a literal "." (e.g. a directory unpacked
# from a versioned tarball, like "hackage-security-0.6.4.0") isn't
# just cosmetic here: ghc-pkg's own .conf parser rejects a "name:"/
# "id:"/"key:" token containing *any* hyphen-delimited component
# that's purely numeric unless it's the token's very last component
# (it's parsing for a trailing name-version split, same as any
# ordinary "foo-1.2.3" package identifier) - confirmed empirically:
# both a literal "." and a plain "_"-for-"." substitution (still
# hyphenated into an all-digits component by the final "_" -> "-"
# below) fail the same way ("unexpected Empty component, after ..."),
# while splicing in a letter so the component is never purely numeric
# (e.g. "0.6.4.0" -> "0d6d4d0") is accepted regardless of position.
def haskell_library_name(label: Label | TargetLabel) -> str:
return repr(label.path).replace("//", "_").replace("/", "_").replace(".", "d").removesuffix("_") + "_" + label.name
# The unit id of a haskell_library(), from its label.
def haskell_library_unit_id(label: Label | TargetLabel) -> str:
return haskell_library_name(label).replace("_", "-")
def is_haskell_src(x: Artifact) -> bool:
_, ext = paths.split_extension(x.short_path)
return ext in HASKELL_EXTENSIONS
def src_to_module_name(x: str) -> str:
base, _ext = paths.split_extension(x)
return base.replace("/", ".")
def module_name_to_file(x: str, ext: str) -> str:
return x.replace(".", "/") + "." + ext
def attr_deps(ctx: AnalysisContext) -> list[Dependency]:
# A library's internal_deps are dependencies like the others, except in
# what it passes on to be linked with it.
return ctx.attrs.deps + (getattr(ctx.attrs, "internal_deps", None) or [])
def attr_deps_haskell_link_infos(ctx: AnalysisContext) -> list[HaskellLinkInfo]:
return dedupe(
filter(
None,
[d.get(HaskellLinkInfo) for d in attr_deps(ctx) + ctx.attrs.template_deps],
)
)
# DONT CALL THIS FUNCTION, you want attr_deps_haskell_link_infos instead
def attr_deps_haskell_link_infos_sans_template_deps(ctx: AnalysisContext) -> list[HaskellLinkInfo]:
return dedupe(
filter(
None,
[d.get(HaskellLinkInfo) for d in attr_deps(ctx)],
)
)
def attr_deps_haskell_lib_infos(ctx: AnalysisContext, link_style: LinkStyle, enable_profiling: bool) -> list[HaskellLibraryInfo]:
if enable_profiling and link_style == LinkStyle("shared"):
fail("Profiling isn't supported when using dynamic linking")
return [
x.prof_lib[link_style] if enable_profiling else x.lib[link_style]
for x in filter(None, [d.get(HaskellLibraryProvider) for d in attr_deps(ctx) + ctx.attrs.template_deps])
]
# The *_link_infos and *_library_infos functions are of `deps`, if given,
# instead of all the deps.
def attr_deps_merged_link_infos(ctx: AnalysisContext, deps: [list[Dependency], None] = None) -> list[MergedLinkInfo]:
return dedupe(
filter(
None,
[d.get(MergedLinkInfo) for d in (attr_deps(ctx) if deps == None else deps)],
)
)
def attr_deps_profiling_link_infos(ctx: AnalysisContext, deps: [list[Dependency], None] = None) -> list[MergedLinkInfo]:
return filter(
None,
[d.get(HaskellProfLinkInfo).prof_infos if d.get(HaskellProfLinkInfo) else d.get(MergedLinkInfo) for d in (attr_deps(ctx) if deps == None else deps)],
)
def attr_deps_shared_library_infos(ctx: AnalysisContext, deps: [list[Dependency], None] = None) -> list[SharedLibraryInfo]:
return filter(
None,
[d.get(SharedLibraryInfo) for d in (attr_deps(ctx) if deps == None else deps)],
)
def _link_style_extensions(link_style: LinkStyle) -> (str, str):
if link_style == LinkStyle("shared"):
return ("dyn_o", "dyn_hi")
elif link_style == LinkStyle("static_pic"):
return ("o", "hi") # is this right?
elif link_style == LinkStyle("static"):
return ("o", "hi")
fail("unknown LinkStyle")
def output_extensions(link_style: LinkStyle, profiled: bool) -> (str, str):
osuf, hisuf = _link_style_extensions(link_style)
if profiled:
return ("p_" + osuf, "p_" + hisuf)
else:
return (osuf, hisuf)
# The name of a 'variant', one way of building a target's code, such as "static-prof".
#
# A target often builds the same thing in several variants, so the rules
# use the variant to tell the actions and outputs apart.
#
# - An action's category says what kind of action it is, such as
# 'haskell_compile', which is useful to match on. The variant goes in the
# action's identifier, along with any other context, such as the module
# of a per-module compile.
# - An output only has a path, so its path is made from what the output is
# and the variant.
def variant_name(link_style: LinkStyle, enable_profiling: bool) -> str:
variant = link_style.value
if enable_profiling:
variant += "-prof"
return variant