Skip to content

Commit

Permalink
Configure the build to use the CC toolchain's linker for GHC >= 9.0.1
Browse files Browse the repository at this point in the history
Fixes #1736
  • Loading branch information
avdv committed May 16, 2022
1 parent a065039 commit 07c175a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion haskell/cabal.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ def _cabal_toolchain_info(hs, cc, workspace_name, runghc):
runghc = runghc.path,
ar = ar,
cc = cc.tools.cc,
ld = cc.tools.ld,
strip = cc.tools.strip,
is_windows = hs.toolchain.is_windows,
workspace = workspace_name,
ghc_cc_args = ghc_cc_program_args(hs, "$CC"),
ghc_cc_args = ghc_cc_program_args(hs, "$CC", "$LD"),
)

def _prepare_cabal_inputs(
Expand Down
10 changes: 9 additions & 1 deletion haskell/cc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,12 @@ def cc_interop_info(ctx, override_cc_toolchain = None):
]).linking_context.linker_inputs.to_list() for lib in li.libraries],
)

def ghc_cc_program_args(hs, cc):
def ghc_cc_program_args(hs, cc, ld):
"""Returns the -pgm* flags required to override cc.
Args:
cc: string, path to the C compiler (cc_wrapper).
ld: string, path to the linker (ld).
Returns:
list of string, GHC arguments.
Expand Down Expand Up @@ -220,4 +221,11 @@ def ghc_cc_program_args(hs, cc):
]
if hs.toolchain.numeric_version >= [8, 10, 3]:
args.append("-pgmc-supports-no-pie")
if hs.toolchain.numeric_version >= [9, 0, 1]:
args.extend([
"-pgmlm",
ld,
"-optlm",
"-r",
])
return args
2 changes: 1 addition & 1 deletion haskell/doctest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _haskell_doctest_single(target, ctx):
ctx,
override_cc_toolchain = hs.tools_config.maybe_exec_cc_toolchain,
)
args.add_all(ghc_cc_program_args(hs, cc.tools.cc))
args.add_all(ghc_cc_program_args(hs, cc.tools.cc, cc.tools.ld))

doctest_log = ctx.actions.declare_file(
"doctest-log-" + ctx.label.name + "-" + target.label.name,
Expand Down
7 changes: 6 additions & 1 deletion haskell/private/cabal_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def canonicalize_path(path):

ar = find_exe(toolchain_info["ar"])
cc = find_exe(toolchain_info["cc"])
ld = find_exe(toolchain_info["ld"])
strip = find_exe(toolchain_info["strip"])

def recache_db():
Expand Down Expand Up @@ -263,10 +264,14 @@ def distdir_prefix():
"--with-hsc2hs=" + hsc2hs,
"--with-ar=" + ar,
"--with-gcc=" + cc,
"--with-ld=" + ld,
"--with-strip=" + strip,
"--enable-deterministic", \
] +
[ "--ghc-option=" + flag.replace("$CC", cc) for flag in toolchain_info["ghc_cc_args"] ] +
[ "--ghc-option=" + flag.replace("$CC", cc).replace("$LD", ld) for flag in toolchain_info["ghc_cc_args"] ] +
[ "--hsc2hs-option=-c" + cc,
"--hsc2hs-option=-l" + cc,
] +
[ "--ghc-option=-optl@" + os.path.join(cfg_execroot, f) for f in [extra_ldflags_file] if f ] +
enable_relocatable_flags + \
[ \
Expand Down
10 changes: 8 additions & 2 deletions haskell/repl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,12 @@ def _create_repl(hs, cc, posix, ctx, repl_info, output):
"$RULES_HASKELL_EXEC_ROOT",
hs.toolchain.cc_wrapper.executable.path,
)
args.extend(['"{}"'.format(arg) for arg in ghc_cc_program_args(hs, cc_path)])
ld_path = paths.join(
"$RULES_HASKELL_EXEC_ROOT",
cc.ld_executable,
)
args.extend(['"{}"'.format(arg) for arg in ghc_cc_program_args(hs, cc_path, ld_path)])


# Load source files
# Force loading by source with `:add *...`.
Expand Down Expand Up @@ -518,7 +523,8 @@ def _create_hie_bios(hs, cc, posix, ctx, repl_info, path_prefix):
path_prefix = paths.join("", *path_prefix)
args, inputs = _compiler_flags_and_inputs(hs, cc, repl_info, path_prefix = path_prefix, static = True)
cc_path = paths.join(path_prefix, hs.toolchain.cc_wrapper.executable.path)
args.extend(ghc_cc_program_args(hs, cc_path))
ld_path = paths.join(path_prefix, cc.ld_executable)
args.extend(ghc_cc_program_args(hs, cc_path, ld_path))
args.extend(hs.toolchain.ghcopts)
args.extend(repl_info.load_info.compiler_flags)

Expand Down
2 changes: 1 addition & 1 deletion haskell/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _run_ghc(hs, cc, inputs, outputs, mnemonic, arguments, env, params_file = No

# XXX: We should also tether Bazel's CC toolchain to GHC's, so that we can properly mix Bazel-compiled
# C libraries with Haskell targets.
args.add_all(ghc_cc_program_args(hs, cc.tools.cc))
args.add_all(ghc_cc_program_args(hs, cc.tools.cc, cc.tools.ld))

compile_flags_file = hs.actions.declare_file("compile_flags_%s_%s_%s" % (hs.name, extra_name, mnemonic))
extra_args_file = hs.actions.declare_file("extra_args_%s_%s_%s" % (hs.name, extra_name, mnemonic))
Expand Down

0 comments on commit 07c175a

Please sign in to comment.