From f723b8b878a3c4a4687b9e337a875500bebb39b1 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 4 Jul 2019 11:18:12 +0100 Subject: feat(third_party/bazel): Check in rules_haskell from Tweag --- .../bazel/rules_haskell/haskell/private/pkg_id.bzl | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 third_party/bazel/rules_haskell/haskell/private/pkg_id.bzl (limited to 'third_party/bazel/rules_haskell/haskell/private/pkg_id.bzl') diff --git a/third_party/bazel/rules_haskell/haskell/private/pkg_id.bzl b/third_party/bazel/rules_haskell/haskell/private/pkg_id.bzl new file mode 100644 index 000000000000..0a3c5fa439d2 --- /dev/null +++ b/third_party/bazel/rules_haskell/haskell/private/pkg_id.bzl @@ -0,0 +1,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, +) -- cgit 1.4.1