Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take Bazel package names into account when generating GHC package names #222

Merged
merged 3 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions haskell/actions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def create_ghc_package(ctx, interfaces_dir, static_library, dynamic_library, exp
# Create a file from which ghc-pkg will create the actual package from.
registration_file = ctx.actions.declare_file(target_unique_name(ctx, "registration-file"))
registration_file_entries = {
"name": ctx.attr.name,
"name": get_pkg_name(ctx),
"version": ctx.attr.version,
"id": get_pkg_id(ctx),
"key": get_pkg_id(ctx),
Expand Down Expand Up @@ -846,16 +846,47 @@ def _compilation_defaults(ctx):
),
)

def _zencode(s):
"""Zero-escape a given string.

Args:
s: inputs string.

Returns:
string: zero-escaped string.
"""
return s.replace("Z", "ZZ").replace("_", "ZU").replace("/", "ZS")

def get_pkg_name(ctx):
"""Get package name. Package name includes Bazel package and name of the
component. We can't use just the latter because then two components with
the same names in different packages would clash.

Args:
ctx: Rule context

Returns:
string: GHC package name to use.
"""
return _zencode("/".join([i for i in [
ctx.label.workspace_root,
ctx.label.package,
ctx.attr.name,
] if i != ""]))

def get_pkg_id(ctx):
"""Get package identifier. This is name-version.
"""Get package identifier of the form `name-version`.

Args:
ctx: Rule context

Returns:
string: GHC package ID to use.
"""
return "{0}-{1}".format(ctx.attr.name, ctx.attr.version)
return "{0}-{1}".format(
get_pkg_name(ctx),
ctx.attr.version,
)

def _get_library_name(ctx):
"""Get core library name for this package. This is "HS" followed by package ID.
Expand Down
15 changes: 11 additions & 4 deletions haskell/haddock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
load (":path_utils.bzl", "module_name")
load (":set.bzl", "set")

load(":actions.bzl",
"get_pkg_name",
)

load(":tools.bzl",
"get_build_tools_path",
"tools",
Expand All @@ -20,22 +24,22 @@ def _haskell_doc_aspect_impl(target, ctx):
if HaskellBuildInfo not in target or HaskellLibraryInfo not in target:
return []

pkg_id = "{0}-{1}".format(ctx.rule.attr.name, ctx.rule.attr.version)
pkg_id = target[HaskellLibraryInfo].package_name

doc_dir = ctx.actions.declare_directory("doc-{0}".format(pkg_id))
haddock_interface = ctx.actions.declare_file(
paths.join(doc_dir.basename, "{0}.haddock".format(pkg_id)),

)
hoogle_file = ctx.actions.declare_file(
paths.replace_extension(ctx.rule.attr.name, ".txt"),
pkg_id + ".txt",
sibling = haddock_interface
)

args = ctx.actions.args()
args.add([
"-D", haddock_interface,
"--package-name={0}".format(ctx.rule.attr.name),
"--package-name={0}".format(pkg_id),
"--package-version={0}".format(ctx.rule.attr.version),
"-o", doc_dir,
"--html", "--hoogle",
Expand Down Expand Up @@ -87,7 +91,10 @@ def _haskell_doc_aspect_impl(target, ctx):
for f in input_sources
]

self_outputs = [doc_dir, haddock_interface, hoogle_file] + static_haddock_outputs + module_htmls
self_outputs = [
doc_dir,
haddock_interface,
hoogle_file] + static_haddock_outputs + module_htmls

ctx.actions.run(
inputs = depset(transitive = [
Expand Down
62 changes: 36 additions & 26 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ sh_test(
rule_test(
name = "test-library-deps",
generates =
["library-deps-1.0.0/library-deps-1.0.0.conf",
"library-deps-1.0.0/package.cache",
["testsZSlibrary-depsZSlibrary-deps-1.0.0/testsZSlibrary-depsZSlibrary-deps-1.0.0.conf",
"testsZSlibrary-depsZSlibrary-deps-1.0.0/package.cache",
],
rule = "//tests/library-deps",
size = "small",
Expand All @@ -83,8 +83,8 @@ rule_test(
rule_test(
name = "test-library-with-sysdeps",
generates =
["library-with-sysdeps-1.0.0/library-with-sysdeps-1.0.0.conf",
"library-with-sysdeps-1.0.0/package.cache",
["testsZSlibrary-with-sysdepsZSlibrary-with-sysdeps-1.0.0/testsZSlibrary-with-sysdepsZSlibrary-with-sysdeps-1.0.0.conf",
"testsZSlibrary-with-sysdepsZSlibrary-with-sysdeps-1.0.0/package.cache",
],
rule = "//tests/library-with-sysdeps",
size = "small",
Expand All @@ -100,18 +100,18 @@ rule_test(
rule_test(
name = "test-haddock",
generates = [
"doc-haddock-lib-b-1.0.0",
"doc-haddock-lib-b-1.0.0/LibB.html",
"doc-haddock-lib-b-1.0.0/doc-index.html",
"doc-haddock-lib-b-1.0.0/haddock-lib-b-1.0.0.haddock",
"doc-haddock-lib-b-1.0.0/haddock-lib-b.txt",
"doc-haddock-lib-b-1.0.0/haddock-util.js",
"doc-haddock-lib-b-1.0.0/hslogo-16.png",
"doc-haddock-lib-b-1.0.0/index.html",
"doc-haddock-lib-b-1.0.0/minus.gif",
"doc-haddock-lib-b-1.0.0/ocean.css",
"doc-haddock-lib-b-1.0.0/plus.gif",
"doc-haddock-lib-b-1.0.0/synopsis.png",
"doc-testsZShaddockZShaddock-lib-b-1.0.0",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/LibB.html",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/doc-index.html",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/testsZShaddockZShaddock-lib-b-1.0.0.haddock",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/testsZShaddockZShaddock-lib-b-1.0.0.txt",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/haddock-util.js",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/hslogo-16.png",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/index.html",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/minus.gif",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/ocean.css",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/plus.gif",
"doc-testsZShaddockZShaddock-lib-b-1.0.0/synopsis.png",
],
rule = "//tests/haddock",
size = "small",
Expand Down Expand Up @@ -177,8 +177,8 @@ rule_test(
rule_test(
name = "test-hidden-modules",
generates = [
"lib-c-1.0.0/lib-c-1.0.0.conf",
"lib-c-1.0.0/package.cache"
"testsZShidden-modulesZSlib-c-1.0.0/testsZShidden-modulesZSlib-c-1.0.0.conf",
"testsZShidden-modulesZSlib-c-1.0.0/package.cache"
],
rule = "//tests/hidden-modules:lib-c",
size = "small",
Expand All @@ -187,13 +187,23 @@ rule_test(
rule_test(
name = "test-library-with-sysincludes",
generates =
["library-with-sysincludes-1.0.0/library-with-sysincludes-1.0.0.conf",
"library-with-sysincludes-1.0.0/package.cache",
["testsZSlibrary-with-sysincludesZSlibrary-with-sysincludes-1.0.0/testsZSlibrary-with-sysincludesZSlibrary-with-sysincludes-1.0.0.conf",
"testsZSlibrary-with-sysincludesZSlibrary-with-sysincludes-1.0.0/package.cache",
],
rule = "//tests/library-with-sysincludes",
size = "small",
)

rule_test(
name = "test-package-id-clash",
generates =
["testsZSpackage-id-clashZSlib-1.0.0/testsZSpackage-id-clashZSlib-1.0.0.conf",
"testsZSpackage-id-clashZSlib-1.0.0/package.cache",
],
rule = "//tests/package-id-clash:lib",
size = "small",
)

rule_test(
name = "test-java_classpath",
generates = ["java_classpath"],
Expand All @@ -205,13 +215,13 @@ rule_test(
name = "test-cc_haskell_import-output",
generates = select({
"@bazel_tools//src/conditions:darwin":
[ "libHShs-lib-a-1.0.0-ghc8.2.2.dylib",
"libHShs-lib-b-1.0.0-ghc8.2.2.dylib",
],
["libHStestsZSccZUhaskellZUimportZShs-lib-a-1.0.0-ghc8.2.2.dylib",
"libHStestsZSccZUhaskellZUimportZShs-lib-b-1.0.0-ghc8.2.2.dylib",
],
"//conditions:default":
[ "libHShs-lib-a-1.0.0-ghc8.2.2.so",
"libHShs-lib-b-1.0.0-ghc8.2.2.so",
]}),
["libHStestsZSccZUhaskellZUimportZShs-lib-a-1.0.0-ghc8.2.2.so",
"libHStestsZSccZUhaskellZUimportZShs-lib-b-1.0.0-ghc8.2.2.so",
]}),
rule = "//tests/cc_haskell_import:hs-lib-b-so",
size = "small",
)
Expand Down
2 changes: 1 addition & 1 deletion tests/generated-modules/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ haskell_test(
src_strip_prefix = "src",
prebuilt_dependencies = ["base"],
deps = [":GenModule", ":BinModule"],
size = "small",
)

24 changes: 24 additions & 0 deletions tests/package-id-clash/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package(default_testonly = 1)

load(
"@io_tweag_rules_haskell//haskell:haskell.bzl",
"haskell_library",
)

haskell_library(
name = "sublib",
srcs = ["Foo.hs"],
prebuilt_dependencies = ["base"],
visibility = ["//visibility:public"],
)

haskell_library(
name = "lib",
srcs = ["Baz.hs"],
deps = [
":sublib",
"//tests/package-id-clash/sublib:sublib",
],
prebuilt_dependencies = ["base"],
visibility = ["//visibility:public"],
)
7 changes: 7 additions & 0 deletions tests/package-id-clash/Baz.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Baz (baz) where

import Foo (foo)
import Bar (bar)

baz :: Int
baz = foo + bar
4 changes: 4 additions & 0 deletions tests/package-id-clash/Foo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Foo (foo) where

foo :: Int
foo = 5
13 changes: 13 additions & 0 deletions tests/package-id-clash/sublib/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package(default_testonly = 1)

load(
"@io_tweag_rules_haskell//haskell:haskell.bzl",
"haskell_library",
)

haskell_library(
name = "sublib",
srcs = ["Bar.hs"],
prebuilt_dependencies = ["base"],
visibility = ["//visibility:public"],
)
4 changes: 4 additions & 0 deletions tests/package-id-clash/sublib/Bar.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Bar (bar) where

bar :: Int
bar = 6