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

added hie_bios_path_prefix attribute to haskell_repl #1531

Merged
merged 8 commits into from
Apr 14, 2021
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
23 changes: 13 additions & 10 deletions haskell/repl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ def _compiler_flags_and_inputs(hs, repl_info, static = False, path_prefix = ""):
cc_library_files = _concat(get_library_files(hs, cc_libraries_info, cc_libraries))
else:
cc_library_files = get_ghci_library_files(hs, cc_libraries_info, cc_libraries)

link_libraries(cc_library_files, args, path_prefix = path_prefix)

if static:
Expand All @@ -336,7 +335,6 @@ def _compiler_flags_and_inputs(hs, repl_info, static = False, path_prefix = ""):
depset([hs.toolchain.locale_archive] if hs.toolchain.locale_archive else []),
repl_info.dep_info.interface_dirs,
])

return (args, inputs)

def _create_repl(hs, posix, ctx, repl_info, output):
Expand Down Expand Up @@ -479,7 +477,7 @@ def _create_repl(hs, posix, ctx, repl_info, output):
runfiles = _merge_runfiles(runfiles),
)]

def _create_hie_bios(hs, posix, ctx, repl_info):
def _create_hie_bios(hs, posix, ctx, repl_info, path_prefix):
"""Build a hie-bios argument file.

Args:
Expand All @@ -492,28 +490,28 @@ def _create_hie_bios(hs, posix, ctx, repl_info):
List of providers:
OutputGroupInfo provider for the hie-bios argument file.
"""
args, inputs = _compiler_flags_and_inputs(hs, repl_info, static = True)
args.extend(ghc_cc_program_args(hs.toolchain.cc_wrapper.executable.path))
path_prefix = paths.join("", *path_prefix)
args, inputs = _compiler_flags_and_inputs(hs, repl_info, path_prefix = path_prefix, static = True)
args.extend(ghc_cc_program_args(paths.join(path_prefix, hs.toolchain.cc_wrapper.executable.path)))
args.extend(hs.toolchain.ghcopts)
args.extend(repl_info.load_info.compiler_flags)

# Add import directories.
# Note, src_strip_prefix is deprecated. However, for now ghcide depends on
# `-i` flags to find source files to modules.
for import_dir in repl_info.load_info.import_dirs.to_list():
args.append("-i" + (import_dir if import_dir else "."))
args.append("-i" + (paths.join(path_prefix, import_dir) or "."))

# List modules (Targets) covered by this cradle.
args.extend([f.path for f in repl_info.load_info.source_files.to_list()])
args.extend([paths.join(path_prefix, f.path) for f in repl_info.load_info.source_files.to_list()])

# List boot files
args.extend([f.path for f in repl_info.load_info.boot_files.to_list()])

args_file = ctx.actions.declare_file(".%s.hie-bios" % ctx.label.name)
args_link = ctx.actions.declare_file("%s@hie-bios" % ctx.label.name)
ctx.actions.write(args_file, "\n".join(args))
ctx.actions.write(args_file, "\n".join(args) + "\n")
ln(hs, posix, args_file, args_link, extra_inputs = inputs)

return [OutputGroupInfo(hie_bios = [args_link])]

def _haskell_repl_aspect_impl(target, ctx):
Expand Down Expand Up @@ -561,7 +559,7 @@ def _haskell_repl_impl(ctx):
hs = haskell_context(ctx)
posix = ctx.toolchains["@rules_sh//sh/posix:toolchain_type"]
return _create_repl(hs, posix, ctx, repl_info, ctx.outputs.repl) + \
_create_hie_bios(hs, posix, ctx, repl_info)
_create_hie_bios(hs, posix, ctx, repl_info, ctx.attr.hie_bios_path_prefix)

haskell_repl = rule(
implementation = _haskell_repl_impl,
Expand Down Expand Up @@ -621,6 +619,11 @@ haskell_repl = rule(
doc = "Whether to collect the data runfiles from the dependencies in srcs, data and deps attributes.",
default = True,
),
"hie_bios_path_prefix": attr.string_list(
doc = """Path prefix for hie-bios paths. The elements of the list are joined together to build the path.
See [IDE support](#ide-support-experimental).""",
default = [],
),
},
executable = True,
outputs = {
Expand Down
55 changes: 55 additions & 0 deletions tests/hie-bios/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
load(
"@rules_haskell//haskell:defs.bzl",
"haskell_repl",
"haskell_test",
)

haskell_test(
name = "binary-hie-bios-test",
srcs = ["Main.hs"],
deps = ["//tests/hackage:base"],
)

haskell_repl(
name = "hie-bios",
testonly = 1,
collect_data = True,
hie_bios_path_prefix = ["$magic_string"],
deps = [":binary-hie-bios-test"],
)

haskell_repl(
name = "hie-bios-no-prefix",
testonly = 1,
collect_data = True,
deps = [":binary-hie-bios-test"],
)

filegroup(
name = "hie-bios-file",
testonly = 1,
srcs = [":hie-bios"],
output_group = "hie_bios",
)

filegroup(
name = "hie-bios-file-no-prefix",
testonly = 1,
srcs = [":hie-bios-no-prefix"],
output_group = "hie_bios",
)

sh_test(
name = "test-hie-bios",
size = "small",
srcs = ["test_hie_bios.sh"],
args = [
"$(rootpath :hie-bios-file)",
"$(rootpath :hie-bios-file-no-prefix)",
],
data = [
":hie-bios-file",
":hie-bios-file-no-prefix",
],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
3 changes: 3 additions & 0 deletions tests/hie-bios/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Main where

main = putStrLn "hello world"
37 changes: 37 additions & 0 deletions tests/hie-bios/test_hie_bios.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

# To test the hie_bios_path_prefix argument of haskell_repl rule.
# we check that the prefix $magic_string is added to some paths of file $1
# and that if we remove them, we get back file $2

# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---

FILE1="$(rlocation "$TEST_WORKSPACE/$1")"
FILE2="$(rlocation "$TEST_WORKSPACE/$2")"
if grep -q '$magic_string' "$FILE1"; then
if sed 's#$magic_string/##' "$FILE1" | diff "$FILE2" -; then
exit 0
else
echo "$2 ="
od -c "$FILE2"
echo "$1 ="
od -c "$FILE1"
echo "$1 (sed output) ="
sed 's#$magic_string/##' "$FILE1" | od -c
exit 1
fi
else
echo "\$magic_string should be in file: $1"
echo "$1 ="
od -c "$FILE1"
exit 1
fi