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

Configure -optl-static via feature flag #1410

Merged
merged 1 commit into from
Aug 17, 2020
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
13 changes: 9 additions & 4 deletions docs/haskell-use-cases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -712,21 +712,26 @@ expression that supplies appropriate ``cc`` and ``binutils`` derivations::
)

With the toolchain taken care of, you can then create fully-statically-linked
binaries by passing the ``-optl-static`` flag as ``compiler_flags`` to GHC,
e.g. in ``haskell_binary``::
binaries by enabling the ``fully_static_link`` feature flag, e.g. in ``haskell_binary``::

haskell_binary(
name = ...,
srcs = [
...,
],
...,
compiler_flags = [
"-optl-static",
features = [
"fully_static_link",
],
)

Note, feature flags can be configured `per target`_, `per package`_, or
globally on the `command line`_.

.. _static-haskell-nix: https://github.com/nh2/static-haskell-nix
.. _per target: https://docs.bazel.build/versions/master/be/common-definitions.html#common.features
.. _per package: https://docs.bazel.build/versions/master/be/functions.html#package.features
.. _command line: https://docs.bazel.build/versions/master/command-line-reference.html#flag--features

Containerization with rules_docker
----------------------------------
Expand Down
3 changes: 3 additions & 0 deletions haskell/cabal.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ def _prepare_cabal_inputs(
if hs.toolchain.fully_static_link:
args.add("--hsc2hs-option=--lflag=-static")

if hs.features.fully_static_link:
args.add("--ghc-option=-optl-static")

args.add("--")
args.add_all(package_databases, map_each = _dirname, format_each = "--package-db=%s")
args.add_all(direct_include_dirs, format_each = "--extra-include-dirs=%s")
Expand Down
4 changes: 4 additions & 0 deletions haskell/private/actions/link.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def link_binary(
# so we just default to passing it.
args.add("-optl-pthread")

if hs.features.fully_static_link:
# Create a fully statically linked binary.
args.add("-optl-static")

args.add_all(["-o", executable.path])

(pkg_info_inputs, pkg_info_args) = pkg_info_to_compile_flags(
Expand Down
3 changes: 3 additions & 0 deletions haskell/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def haskell_context(ctx, attr = None):
bin_dir = ctx.bin_dir,
genfiles_dir = ctx.genfiles_dir,
coverage_enabled = coverage_enabled,
features = struct(
fully_static_link = "fully_static_link" in ctx.features,
),
)

def render_env(env):
Expand Down