Skip to content

Commit

Permalink
Add ‘hidden_modules’ attribute to ‘haskell_library’ rule
Browse files Browse the repository at this point in the history
This allows to selectively hide modules, which is necessary in order to
compile some existing code from Hackage.
  • Loading branch information
mrkkrp authored and mboes committed Feb 27, 2018
1 parent d3f1bcb commit 8e4ae6c
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 6 deletions.
14 changes: 12 additions & 2 deletions haskell/actions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,16 @@ def create_ghc_package(ctx, interfaces_dir, static_library, dynamic_library):
conf_file = ctx.actions.declare_file(paths.join(pkg_db_dir.basename, "{0}.conf".format(get_pkg_id(ctx))))
cache_file = ctx.actions.declare_file("package.cache", sibling=conf_file)

# Infer collection of public modules in the library.

hidden_modules = set.from_list(ctx.attr.hidden_modules)
public_modules = []

for f in _hs_srcs(ctx):
mname = module_name(ctx, f)
if not set.is_member(hidden_modules, mname):
public_modules.append(mname)

# 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 = {
Expand All @@ -455,8 +465,8 @@ def create_ghc_package(ctx, interfaces_dir, static_library, dynamic_library):
"id": get_pkg_id(ctx),
"key": get_pkg_id(ctx),
"exposed": "True",
"exposed-modules":
" ".join([module_name(ctx, f) for f in _hs_srcs(ctx)]),
"exposed-modules": " ".join(public_modules),
"hidden-modules": " ".join(ctx.attr.hidden_modules),
"import-dirs": paths.join("${pkgroot}", interfaces_dir.basename),
"library-dirs": "${pkgroot}",
"dynamic-library-dirs": "${pkgroot}",
Expand Down
8 changes: 6 additions & 2 deletions haskell/haskell.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ _haskell_common_attrs = {
),
"srcs": attr.label_list(
allow_files = FileType([".hs", ".hsc", ".lhs", ".hs-boot", ".lhs-boot", ".h"]),
doc = "Haskell source files",
doc = "Haskell source files.",
),
"deps": attr.label_list(
doc = "List of other Haskell libraries to be linked to this target.",
Expand Down Expand Up @@ -177,7 +177,11 @@ def _haskell_library_impl(ctx):

haskell_library = rule(
_haskell_library_impl,
attrs = _haskell_common_attrs,
attrs = dict(
_haskell_common_attrs,
hidden_modules = attr.string_list(
doc = "Modules that should be made unavailable for import by dependencies."
)),
host_fragments = ["cpp"],
toolchains = ["@io_tweag_rules_haskell//haskell:toolchain"],
)
Expand Down
2 changes: 1 addition & 1 deletion haskell/set.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _is_member(s, e):
Result:
Bool, true if `e` is in `s`, false otherwise.
"""
e in s._set_items
return e in s._set_items

def _insert(s, e):
"""Insert an element into the set.
Expand Down
12 changes: 11 additions & 1 deletion tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@ rule_test(
rule_test(
name = "test-haskell_test",
generates = ["haskell_test"],
rule = "//tests/haskell_test",
rule = "//tests/haskell_test:haskell_test",
size = "small",
)

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"
],
rule = "//tests/hidden-modules:lib-c",
size = "small",
)

Expand Down
28 changes: 28 additions & 0 deletions tests/hidden-modules/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package(default_testonly = 1, default_visibility = ["//visibility:public"])

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

haskell_library(
name = "lib-a",
src_strip_prefix = "lib-a",
srcs = glob(["lib-a/*.hs"]),
hidden_modules = ["Foo"],
prebuilt_dependencies = ["base"],
)

haskell_library(
name = "lib-b",
src_strip_prefix = "lib-b",
srcs = glob(["lib-b/*.hs"]),
prebuilt_dependencies = ["base"],
)

haskell_library(
name = "lib-c",
src_strip_prefix = "lib-c",
srcs = glob(["lib-c/*.hs"]),
prebuilt_dependencies = ["base"],
deps = [":lib-a", ":lib-b"],
)
6 changes: 6 additions & 0 deletions tests/hidden-modules/lib-a/Bar.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Bar (bar) where

import Foo (foo)

bar :: Int
bar = foo * 2
4 changes: 4 additions & 0 deletions tests/hidden-modules/lib-a/Foo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Foo (foo) where

foo :: Int
foo = 15
4 changes: 4 additions & 0 deletions tests/hidden-modules/lib-b/Foo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Foo (foo) where

foo :: Int
foo = 16
7 changes: 7 additions & 0 deletions tests/hidden-modules/lib-c/Baz.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Baz (bar) where

import Foo (foo)
import Bar (bar)

baz :: Int
baz = foo + bar

0 comments on commit 8e4ae6c

Please sign in to comment.