Skip to content

Commit

Permalink
Disambiguate build directories.
Browse files Browse the repository at this point in the history
We suffix all the build directories and files that we make with a
package name and version. As package name is a target name, there can
only be a unique one in single project and we preserve some sanity in
build environment, at least for the directories we declare and pass
around. This allows multiple `haskell_library` and `haskell_binary`
targets in a single package.

Closes #7.
  • Loading branch information
Mateusz Kowalczyk committed Dec 10, 2017
1 parent 2e61cbf commit d95ae9b
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
31 changes: 15 additions & 16 deletions haskell/haskell.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ load(":toolchain.bzl",
"src_to_ext",
"get_object_suffix",
"get_interface_suffix",
"hsc2hs_args"
"hsc2hs_args",
"mk_name",
)

def _haskell_binary_impl(ctx):
Expand All @@ -26,7 +27,7 @@ def _haskell_binary_impl(ctx):

depInputs += ctx.files.srcs

objDir = ctx.actions.declare_directory("objects")
objDir = ctx.actions.declare_directory(mk_name(ctx, "objects"))
binObjs = [ctx.actions.declare_file(src_to_ext(ctx, s, get_object_suffix(ctx), directory=objDir))
for s in ctx.files.srcs]

Expand Down Expand Up @@ -57,12 +58,11 @@ def _haskell_binary_impl(ctx):
)

def _haskell_library_impl(ctx):

objDir = ctx.actions.declare_directory("objects")
objDir = ctx.actions.declare_directory(mk_name(ctx, "objects"))
objectFiles = [ctx.actions.declare_file(src_to_ext(ctx, s, get_object_suffix(ctx), directory=objDir))
for s in ctx.files.srcs]

ifaceDir = ctx.actions.declare_directory("interfaces")
ifaceDir = ctx.actions.declare_directory(mk_name(ctx, "interfaces"))
interfaceFiles = [ctx.actions.declare_file(src_to_ext(ctx, s, get_interface_suffix(ctx), directory=ifaceDir))
for s in ctx.files.srcs ]

Expand Down Expand Up @@ -119,9 +119,10 @@ def _haskell_library_impl(ctx):
# Make library archive; currently only static
#
# TODO: configurable shared &c. see various scenarios in buck
libDir = ctx.actions.declare_directory("lib")
pkgLib = ctx.actions.declare_file("{0}/lib{1}.a".format(libDir.basename, ctx.attr.name))

pkgId = "{0}-{1}".format(ctx.attr.name, ctx.attr.version)
libDir = ctx.actions.declare_directory(mk_name(ctx, "lib"))
# We need libDir because ghc-pkg wants a directory for library-dirs.
pkgLib = ctx.actions.declare_file("{0}/lib{1}.a".format(libDir.basename, pkgId))
ctx.actions.run(
inputs = objectFiles,
outputs = [pkgLib, libDir],
Expand All @@ -131,7 +132,6 @@ def _haskell_library_impl(ctx):
)

# Create and register ghc package.
pkgId = "{0}-{1}".format(ctx.attr.name, ctx.attr.version)
pkgDbDir = ctx.actions.declare_directory(pkgId)
confFile = ctx.actions.declare_file("{0}/{1}.conf".format(pkgDbDir.basename, pkgId))
cacheFile = ctx.actions.declare_file("package.cache", sibling=confFile)
Expand Down Expand Up @@ -190,7 +190,11 @@ _haskell_common_attrs = {
),
"prebuiltDeps": attr.string_list(
doc="Haskell packages which are magically available such as wired-in packages."
)
),
"version": attr.string(
default="1.0.0",
doc="Package/binary version"
),
}

haskell_library = rule(
Expand All @@ -199,12 +203,7 @@ haskell_library = rule(
"conf": "%{name}-%{version}/%{name}-%{version}.conf",
"packageCache": "%{name}-%{version}/package.cache"
},
attrs = _haskell_common_attrs + {
"version": attr.string(
default="1.0.0",
doc="Library version"
),
}
attrs = _haskell_common_attrs,
)

haskell_binary = rule(
Expand Down
12 changes: 10 additions & 2 deletions haskell/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ HaskellPackageInfo = provider(
}
)

def mk_name(ctx, namePrefix):
"""Make a target-unique name.
Args:
namePrefix: Template for the name.
"""
return "{0}-{1}-{2}".format(namePrefix, ctx.attr.name, ctx.attr.version)

def ghc_bin_obj_args(ctx, objDir):
"""Build arguments for Haskell binary object building.
Expand Down Expand Up @@ -173,7 +181,7 @@ def mk_registration_file(ctx, pkgId, interfaceDir, libDir):
interfaceDir: Directory with interface files.
libDir: Directory containing library archive(s).
"""
registrationFile = ctx.actions.declare_file("registration-file")
registrationFile = ctx.actions.declare_file(mk_name(ctx, "registration-file"))
registrationFileDict = {
"name": ctx.attr.name,
"version": ctx.attr.version,
Expand All @@ -186,7 +194,7 @@ def mk_registration_file(ctx, pkgId, interfaceDir, libDir):
for f in ctx.files.srcs]),
"import-dirs": "${{pkgroot}}/{0}".format(interfaceDir.basename),
"library-dirs": "${{pkgroot}}/{0}".format(libDir.basename),
"hs-libraries": ctx.attr.name,
"hs-libraries": pkgId,
"depends": ", ".join([ d[HaskellPackageInfo].pkgName for d in ctx.attr.deps ])
}
ctx.actions.write(
Expand Down
11 changes: 11 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ haskell_binary(
deps = ["//tests/test-lib"]
)

haskell_binary(
name = "two-libs-one-BUILD",
main = "TogetherTest.main",
srcs = ["TogetherTest.hs"],
deps = [
"//tests/together-lib:one",
"//tests/together-lib:two"
]
)

[sh_test(
name = "Run" + binary,
srcs = ["test_binary.sh"],
Expand All @@ -18,4 +28,5 @@ haskell_binary(
timeout="short",
) for binary in [
"hello",
"two-libs-one-BUILD",
]]
7 changes: 7 additions & 0 deletions tests/TogetherTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module TogetherTest (main) where

import One (one)
import Two (two)

main :: IO ()
main = putStrLn $ "One and Two makes " ++ show (one + two)
16 changes: 16 additions & 0 deletions tests/together-lib/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package(default_visibility = ["//visibility:public"])

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

haskell_library(
name = 'one',
srcs = [ 'One.hs' ]
)

haskell_library(
name = 'two',
srcs = [ 'Two.hs' ]
)
4 changes: 4 additions & 0 deletions tests/together-lib/One.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module One (one) where

one :: Int
one = 1
4 changes: 4 additions & 0 deletions tests/together-lib/Two.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Two (two) where

two :: Int
two = 2

0 comments on commit d95ae9b

Please sign in to comment.