Skip to content

Commit ddb44d1

Browse files
authored
Merge pull request #156 from tweag/fix-152
Add ‘hidden_modules’ attribute to ‘haskell_library’ rule
2 parents d3f1bcb + 8e4ae6c commit ddb44d1

File tree

9 files changed

+79
-6
lines changed

9 files changed

+79
-6
lines changed

haskell/actions.bzl

+12-2
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,16 @@ def create_ghc_package(ctx, interfaces_dir, static_library, dynamic_library):
447447
conf_file = ctx.actions.declare_file(paths.join(pkg_db_dir.basename, "{0}.conf".format(get_pkg_id(ctx))))
448448
cache_file = ctx.actions.declare_file("package.cache", sibling=conf_file)
449449

450+
# Infer collection of public modules in the library.
451+
452+
hidden_modules = set.from_list(ctx.attr.hidden_modules)
453+
public_modules = []
454+
455+
for f in _hs_srcs(ctx):
456+
mname = module_name(ctx, f)
457+
if not set.is_member(hidden_modules, mname):
458+
public_modules.append(mname)
459+
450460
# Create a file from which ghc-pkg will create the actual package from.
451461
registration_file = ctx.actions.declare_file(target_unique_name(ctx, "registration-file"))
452462
registration_file_entries = {
@@ -455,8 +465,8 @@ def create_ghc_package(ctx, interfaces_dir, static_library, dynamic_library):
455465
"id": get_pkg_id(ctx),
456466
"key": get_pkg_id(ctx),
457467
"exposed": "True",
458-
"exposed-modules":
459-
" ".join([module_name(ctx, f) for f in _hs_srcs(ctx)]),
468+
"exposed-modules": " ".join(public_modules),
469+
"hidden-modules": " ".join(ctx.attr.hidden_modules),
460470
"import-dirs": paths.join("${pkgroot}", interfaces_dir.basename),
461471
"library-dirs": "${pkgroot}",
462472
"dynamic-library-dirs": "${pkgroot}",

haskell/haskell.bzl

+6-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ _haskell_common_attrs = {
4343
),
4444
"srcs": attr.label_list(
4545
allow_files = FileType([".hs", ".hsc", ".lhs", ".hs-boot", ".lhs-boot", ".h"]),
46-
doc = "Haskell source files",
46+
doc = "Haskell source files.",
4747
),
4848
"deps": attr.label_list(
4949
doc = "List of other Haskell libraries to be linked to this target.",
@@ -177,7 +177,11 @@ def _haskell_library_impl(ctx):
177177

178178
haskell_library = rule(
179179
_haskell_library_impl,
180-
attrs = _haskell_common_attrs,
180+
attrs = dict(
181+
_haskell_common_attrs,
182+
hidden_modules = attr.string_list(
183+
doc = "Modules that should be made unavailable for import by dependencies."
184+
)),
181185
host_fragments = ["cpp"],
182186
toolchains = ["@io_tweag_rules_haskell//haskell:toolchain"],
183187
)

haskell/set.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def _is_member(s, e):
2020
Result:
2121
Bool, true if `e` is in `s`, false otherwise.
2222
"""
23-
e in s._set_items
23+
return e in s._set_items
2424

2525
def _insert(s, e):
2626
"""Insert an element into the set.

tests/BUILD

+11-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,17 @@ rule_test(
111111
rule_test(
112112
name = "test-haskell_test",
113113
generates = ["haskell_test"],
114-
rule = "//tests/haskell_test",
114+
rule = "//tests/haskell_test:haskell_test",
115+
size = "small",
116+
)
117+
118+
rule_test(
119+
name = "test-hidden-modules",
120+
generates = [
121+
"lib-c-1.0.0/lib-c-1.0.0.conf",
122+
"lib-c-1.0.0/package.cache"
123+
],
124+
rule = "//tests/hidden-modules:lib-c",
115125
size = "small",
116126
)
117127

tests/hidden-modules/BUILD

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package(default_testonly = 1, default_visibility = ["//visibility:public"])
2+
3+
load("@io_tweag_rules_haskell//haskell:haskell.bzl",
4+
"haskell_library",
5+
)
6+
7+
haskell_library(
8+
name = "lib-a",
9+
src_strip_prefix = "lib-a",
10+
srcs = glob(["lib-a/*.hs"]),
11+
hidden_modules = ["Foo"],
12+
prebuilt_dependencies = ["base"],
13+
)
14+
15+
haskell_library(
16+
name = "lib-b",
17+
src_strip_prefix = "lib-b",
18+
srcs = glob(["lib-b/*.hs"]),
19+
prebuilt_dependencies = ["base"],
20+
)
21+
22+
haskell_library(
23+
name = "lib-c",
24+
src_strip_prefix = "lib-c",
25+
srcs = glob(["lib-c/*.hs"]),
26+
prebuilt_dependencies = ["base"],
27+
deps = [":lib-a", ":lib-b"],
28+
)

tests/hidden-modules/lib-a/Bar.hs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Bar (bar) where
2+
3+
import Foo (foo)
4+
5+
bar :: Int
6+
bar = foo * 2

tests/hidden-modules/lib-a/Foo.hs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Foo (foo) where
2+
3+
foo :: Int
4+
foo = 15

tests/hidden-modules/lib-b/Foo.hs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Foo (foo) where
2+
3+
foo :: Int
4+
foo = 16

tests/hidden-modules/lib-c/Baz.hs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Baz (bar) where
2+
3+
import Foo (foo)
4+
import Bar (bar)
5+
6+
baz :: Int
7+
baz = foo + bar

0 commit comments

Comments
 (0)