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

Add a new rule haskell_prebuilt_library. #337

Merged
merged 2 commits into from
Jul 18, 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
40 changes: 40 additions & 0 deletions haskell/haskell.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load(":private/providers.bzl",
"HaskellBuildInfo",
"HaskellLibraryInfo",
"HaskellBinaryInfo",
"HaskellPrebuiltPackageInfo",
"HaskellProtobufInfo",
"CcSkylarkApiProviderHacked",
)
Expand Down Expand Up @@ -226,6 +227,45 @@ $ bazel-bin/.../hello-lib-repl # run the script
```
"""

def _haskell_import_impl(ctx):
if ctx.attr.package:
package = ctx.attr.package
else:
package = ctx.label.name
return [HaskellPrebuiltPackageInfo(package = package)]

"""Wrap a prebuilt dependency as a rule.

Example:
```bzl
haskell_import(
name = "base_pkg",
package = "base",
)
haskell_library(
name = "hello-lib",
srcs = ["Lib.hs"],
deps = [
":base_pkg",
"//hello-sublib:lib",
],
)
```

This rule may wrap any prebuilt dependencies, i.e., GHC packages that aren't
supplied by Bazel. Depending on the wrapped rule eliminates the need to list
the package name in prebuilt_dependencies.

Often, rules of this type will be generated automatically by frameworks such
as Hazel.
"""
haskell_import = rule(
_haskell_import_impl,
attrs = dict(
package = attr.string(doc = "A non-Bazel-supplied GHC package name. Defaults to the name of the rule."),
),
)

haskell_doc = _haskell_doc

haskell_lint = _haskell_lint
Expand Down
14 changes: 14 additions & 0 deletions haskell/private/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load(":private/providers.bzl",
"HaskellBuildInfo",
"HaskellBinaryInfo",
"HaskellLibraryInfo",
"HaskellPrebuiltPackageInfo",
"CcSkylarkApiProviderHacked",
)
load(":private/set.bzl", "set")
Expand Down Expand Up @@ -95,6 +96,19 @@ def gather_dep_info(ctx):
external_libraries = dicts.add(acc.external_libraries, binfo.external_libraries),
direct_prebuilt_deps = acc.direct_prebuilt_deps,
)
elif HaskellPrebuiltPackageInfo in dep:
pkg = dep[HaskellPrebuiltPackageInfo].package
acc = HaskellBuildInfo(
package_ids = acc.package_ids,
package_confs = acc.package_confs,
package_caches = acc.package_caches,
static_libraries = acc.static_libraries,
dynamic_libraries = acc.dynamic_libraries,
interface_files = acc.interface_files,
prebuilt_dependencies = set.mutable_insert(acc.prebuilt_dependencies, pkg),
external_libraries = acc.external_libraries,
direct_prebuilt_deps = set.mutable_insert(acc.direct_prebuilt_deps, pkg),
)
else:
# If not a Haskell dependency, pass it through as-is to the
# linking phase.
Expand Down
7 changes: 7 additions & 0 deletions haskell/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ HaskellBinaryInfo = provider(
},
)

HaskellPrebuiltPackageInfo = provider(
doc = "Information about a prebuilt GHC package.",
fields = {
"package": "Package name",
},
)

HaddockInfo = provider(
doc = "Haddock information.",
fields = {
Expand Down
35 changes: 35 additions & 0 deletions tests/haskell_import/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package(default_testonly = 1)

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

haskell_import(
name = "bytestring",
)

haskell_import(
name = "text_pkg",
package = "text",
)

haskell_library(
name = "Lib",
srcs = ["Lib.hs"],
prebuilt_dependencies = ["base"],
deps = [":bytestring"],
)

haskell_test(
name = "binary",
srcs = ["Bin.hs"],
main_file = "Bin.hs",
prebuilt_dependencies = ["base"],
deps = [
":text_pkg",
":Lib",
],
)
8 changes: 8 additions & 0 deletions tests/haskell_import/Bin.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Main (main) where

import qualified Data.Text.IO as T
import qualified Data.Text.Encoding as E

import Lib (message)

main = T.putStrLn $ E.decodeUtf8 message
5 changes: 5 additions & 0 deletions tests/haskell_import/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Lib (message) where

import qualified Data.ByteString.Char8 as B

message = B.pack "hello, world"