Skip to content

Commit

Permalink
Add a new rule haskell_prebuilt_library.
Browse files Browse the repository at this point in the history
This rule forwards a prebuilt dependency, so that it may be
specified directly in `deps` rather than `prebuilt_dependencies`.

This gives progress on #75.  Similarly, it simplifies the use of Hazel,
which can now generate repositories for the prebuilt dependencies
just like for regular packages.
  • Loading branch information
judah committed Jul 16, 2018
1 parent d867758 commit 404ee90
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
38 changes: 38 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,43 @@ $ bazel-bin/.../hello-lib-repl # run the script
```
"""

def _haskell_prebuilt_library(ctx):
# TODO: double-check it actually exists in ghc-pkg?
return [HaskellPrebuiltPackageInfo(
package = ctx.attr.package)]

"""Wrap a prebuilt dependency as a rule.
Example:
```bzl
haskell_prebuilt_library(
name = "base_pkg",
package = "base",
)
haskell_library(
name = "hello-lib",
srcs = ["Lib.hs"],
deps = [
":base",
"//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_prebuilt_library = rule(
_haskell_prebuilt_library,
attrs = dict(
package = attr.string(doc = "A non-Bazel-supplied GHC package name."),
),
)

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.insert(acc.prebuilt_dependencies, pkg),
external_libraries = acc.external_libraries,
direct_prebuilt_deps = set.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
36 changes: 36 additions & 0 deletions tests/prebuilt-library/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package(default_testonly = 1)

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

haskell_prebuilt_library(
name = "bytestring",
package = "bytestring",
)

haskell_prebuilt_library(
name = "text",
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",
":Lib",
],
)
8 changes: 8 additions & 0 deletions tests/prebuilt-library/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/prebuilt-library/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"

0 comments on commit 404ee90

Please sign in to comment.