Skip to content

Commit 5834cbb

Browse files
authored
Merge pull request #1424 from tweag/no-library-no-pie
Only pass -no-pie for binary targets
2 parents aabeedc + d81268f commit 5834cbb

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

haskell/cabal.bzl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ def _prepare_cabal_inputs(
140140
package_database,
141141
verbose,
142142
transitive_haddocks,
143-
dynamic_binary = None):
143+
is_library = False,
144+
dynamic_file = None):
144145
"""Compute Cabal wrapper, arguments, inputs."""
145146
with_profiling = is_profiling_enabled(hs)
146147

@@ -212,16 +213,19 @@ def _prepare_cabal_inputs(
212213
for arg in ["-package-db", "./" + _dirname(package_db)]
213214
], join_with = " ", format_each = "--ghc-arg=%s", omit_if_empty = False)
214215
args.add("--flags=" + " ".join(flags))
215-
if not hs.toolchain.is_darwin and not hs.toolchain.is_windows:
216+
if dynamic_file:
216217
# See Note [No PIE when linking] in haskell/private/actions/link.bzl
217-
args.add("--ghc-option=-optl-no-pie")
218+
if not (hs.toolchain.is_darwin or hs.toolchain.is_windows):
219+
version = [int(x) for x in hs.toolchain.version.split(".")]
220+
if version < [8, 10] or not is_library:
221+
args.add("--ghc-option=-optl-no-pie")
218222
args.add_all(hs.toolchain.cabalopts)
219223
args.add_all(cabalopts)
220-
if dynamic_binary:
224+
if dynamic_file:
221225
args.add_all(
222226
[
223227
"--ghc-option=-optl-Wl,-rpath," + create_rpath_entry(
224-
binary = dynamic_binary,
228+
binary = dynamic_file,
225229
dependency = lib,
226230
keep_filename = False,
227231
prefix = relative_rpath_prefix(hs.toolchain.is_darwin),
@@ -426,7 +430,8 @@ def _haskell_cabal_library_impl(ctx):
426430
cabal_wrapper = ctx.executable._cabal_wrapper,
427431
package_database = package_database,
428432
verbose = ctx.attr.verbose,
429-
dynamic_binary = dynamic_library,
433+
is_library = True,
434+
dynamic_file = dynamic_library,
430435
transitive_haddocks = _gather_transitive_haddocks(ctx.attr.deps),
431436
)
432437
outputs = [
@@ -702,7 +707,7 @@ def _haskell_cabal_binary_impl(ctx):
702707
cabal_wrapper = ctx.executable._cabal_wrapper,
703708
package_database = package_database,
704709
verbose = ctx.attr.verbose,
705-
dynamic_binary = binary,
710+
dynamic_file = binary,
706711
transitive_haddocks = _gather_transitive_haddocks(ctx.attr.deps),
707712
)
708713
ctx.actions.run(

haskell/private/actions/compile.bzl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,12 @@ def compile_library(
475475
c = _compilation_defaults(hs, cc, java, posix, dep_info, plugin_dep_info, srcs, import_dir_map, extra_srcs, user_compile_flags, with_profiling, my_pkg_id = my_pkg_id, version = my_pkg_id.version, plugins = plugins, preprocessors = preprocessors)
476476
if with_shared:
477477
c.args.add("-dynamic-too")
478+
479+
# See Note [No PIE when linking] in haskell/private/actions/link.bzl
478480
if not hs.toolchain.is_darwin and not hs.toolchain.is_windows:
479-
# See Note [No PIE when linking] in haskell/private/actions/link.bzl
480-
c.args.add("-optl-no-pie")
481+
version = [int(x) for x in hs.toolchain.version.split(".")]
482+
if version < [8, 10]:
483+
c.args.add("-optl-no-pie")
481484

482485
coverage_data = []
483486
if hs.coverage_enabled:

haskell/private/actions/link.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ def link_binary(
213213
# on this setting GHC would then automatically set `-no-pie`. However,
214214
# rules_haskell uses GHC's `-pgmc` flag to point GHC to the CC toolchain's C
215215
# compiler. This disables the flags configured in the `setting` file. Instead,
216-
# we have to pass `-no-pie` explicitly.
216+
# we have to pass `-no-pie` explicitly when linking binaries.
217+
#
218+
# In GHC < 8.10, we must always pass -no-pie. In newer compilers, we
219+
# must take care to only pass -no-pie when compiling libraries.
217220
#
218221
# Ideally, we would determine whether the CC toolchain's C compiler supports
219222
# `-no-pie` before setting it. Unfortunately, this is complicated by the fact

0 commit comments

Comments
 (0)