Skip to content

Commit

Permalink
Fix haskell_module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
facundominguez committed Nov 1, 2021
1 parent 76e7420 commit c9435f3
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 21 deletions.
9 changes: 0 additions & 9 deletions haskell/private/actions/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,6 @@ def compile_binary(
env = c.env,
arguments = c.args,
)
else:
# The list of sources might be empty if the binary is only
# built from modules produced with haskell_module
hs.actions.run_shell(
outputs = c.outputs,
command = """
mkdir -p {objects_dir}
""".format(objects_dir = c.objects_dir.path),
)

return struct(
object_files = c.object_files,
Expand Down
25 changes: 17 additions & 8 deletions haskell/private/haskell_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ load(
"get_dynamic_hs_lib_name",
"get_lib_extension",
"get_static_hs_lib_name",
"infer_main_module",
"ln",
"match_label",
"parse_pattern",
Expand Down Expand Up @@ -144,6 +145,20 @@ def _expand_make_variables(name, ctx, strings):
]
return expand_make_variables(name, ctx, strings, extra_label_attrs)

def haskell_module_from_info(info):
""" Produces the module name from a HaskellModuleInfo """
return paths.relativize(
paths.replace_extension(info.interface_file.path, ""),
info.import_dir,
).replace("/", ".")

def is_main_as_haskell_module(modules, main_function):
main_module = infer_main_module(main_function).replace(".", "/")
for m in modules:
if haskell_module_from_info(m[HaskellModuleInfo]) == main_module:
return True
return False

def _haskell_binary_common_impl(ctx, is_test):
hs = haskell_context(ctx)
dep_info = gather_dep_info(ctx, ctx.attr.deps)
Expand Down Expand Up @@ -174,7 +189,8 @@ def _haskell_binary_common_impl(ctx, is_test):

with_profiling = is_profiling_enabled(hs)
srcs_files, import_dir_map = _prepare_srcs(ctx.attr.srcs)
module_map = determine_module_names(srcs_files, True, ctx.attr.main_function, ctx.file.main_file)
main_as_haskell_module = is_main_as_haskell_module(modules, ctx.attr.main_function)
module_map = determine_module_names(srcs_files, not main_as_haskell_module, ctx.attr.main_function, ctx.file.main_file)
inspect_coverage = _should_inspect_coverage(ctx, hs, is_test)

dynamic = not ctx.attr.linkstatic
Expand Down Expand Up @@ -343,13 +359,6 @@ def _haskell_binary_common_impl(ctx, is_test):
)),
]

def haskell_module_from_info(info):
""" Produces the module name from a HaskellModuleInfo """
return paths.relativize(
paths.replace_extension(info.interface_file.path, ""),
info.import_dir,
).replace("\\", ".")

def haskell_library_impl(ctx):
hs = haskell_context(ctx)
deps = ctx.attr.deps + ctx.attr.exports
Expand Down
6 changes: 3 additions & 3 deletions haskell/private/path_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _module_map_insert(module_map, module_name, module_file, is_boot = False):
src = module_file,
)

def determine_module_names(src_files, is_binary = False, main_function = "", main_file = None):
def determine_module_names(src_files, search_binary = False, main_function = "", main_file = None):
"""Determine a mapping from module names to source files.
The module name is inferred from the source file name. See
Expand All @@ -94,7 +94,7 @@ def determine_module_names(src_files, is_binary = False, main_function = "", mai
Args:
src_files: sequence of File, source files.
is_binary: bool, whether target requires a main module.
search_binary: bool, whether we need to ensure there's a main module.
main_function: string, optional, the `main_function` attribute to a Haskell binary rule.
main_file: File, optional, the `main_file` attribute to a Haskell binary rule.
Expand All @@ -117,7 +117,7 @@ def determine_module_names(src_files, is_binary = False, main_function = "", mai
else:
_module_map_insert(module_map, module_name, src, is_boot = src.short_path.endswith("-boot"))

if is_binary:
if search_binary:
main_module = infer_main_module(main_function)
if main_file:
_module_map_insert(module_map, main_module, main_file)
Expand Down
4 changes: 3 additions & 1 deletion tests/haskell_module/binary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ haskell_test(

haskell_module(
name = "TestBinModule",
src = "TestBin.hs",
# TODO: Test naming the Main module as something different
# after adding some attribute to make the module name explicit
src = "Main.hs",
deps = ["//tests/hackage:base"],
)

Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions tests/haskell_module/hs-boot/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ haskell_module(

haskell_library(
name = "hs-boot-lib",
# TODO: remove when haskell_module supports dynamic builds
linkstatic = True,
modules = [
":lib-B",
":lib-A",
Expand Down
2 changes: 2 additions & 0 deletions tests/haskell_module/library/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package(default_testonly = 1)

haskell_library(
name = "TestLib",
# TODO: Remove when haskell_module supports dynamic builds
linkstatic = True,
modules = [
":TestLibModule",
],
Expand Down
2 changes: 2 additions & 0 deletions tests/haskell_module/plugin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ haskell_module(

haskell_library(
name = "lib",
# TODO: remove when haskell_module supports dynamic builds
linkstatic = True,
modules = [
":module-with-plugin",
":module-without-plugin",
Expand Down

0 comments on commit c9435f3

Please sign in to comment.