diff options
Diffstat (limited to 'third_party/bazel/rules_haskell/tests/binary-linkstatic-flag')
4 files changed, 132 insertions, 0 deletions
diff --git a/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/BUILD.bazel b/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/BUILD.bazel new file mode 100644 index 000000000000..4e8e7cfa6684 --- /dev/null +++ b/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/BUILD.bazel @@ -0,0 +1,118 @@ +load( + "@io_tweag_rules_haskell//haskell:haskell.bzl", + "haskell_library", + "haskell_test", +) +load("//tests:inline_tests.bzl", "sh_inline_test") + +# test whether `linkstatic` works as expected +package(default_testonly = 1) + +cc_library( + name = "c-lib", + srcs = ["c-lib.c"], +) + +haskell_library( + name = "HsLib", + srcs = ["HsLib.hs"], + deps = [ + "//tests/hackage:base", + ], +) + +haskell_test( + name = "binary-static", + srcs = ["Main.hs"], + linkstatic = True, + deps = [ + ":HsLib", + ":c-lib", + "//tests/hackage:base", + ], +) + +haskell_test( + name = "binary-dynamic", + srcs = ["Main.hs"], + linkstatic = False, + deps = [ + ":HsLib", + ":c-lib", + "//tests/hackage:base", + ], +) + +config_setting( + name = "debug_build", + values = { + "compilation_mode": "dbg", + }, +) + +# Ensure that linkstatic=True only links to static library targets. +sh_inline_test( + name = "test-binary-static-symbols", + size = "small", + args = [ + "$(rootpath :binary-static)", + ], + data = [ + ":binary-static", + ], + script = """ + set -eo pipefail + binary="$1" + # Symbols are prefixed with underscore on MacOS but not on Linux. + if nm -u "$binary" | grep -q "\<_\?value"; then + echo "C library dependency not linked statically: ${binary}" + exit 1 + fi + if nm -u "$binary" | grep -q HsLib_value_closure; then + echo "Haskell library dependency not linked statically ${binary}" + exit 1 + fi + """, +) + +# Ensure that linkstatic=False only links to dynamic library targets. +sh_inline_test( + name = "test-binary-dynamic-symbols", + size = "small", + args = [ + "$(rootpath :binary-dynamic)", + ] + select({ + ":debug_build": ["dbg"], + "//conditions:default": ["rls"], + }), + data = [ + ":binary-dynamic", + ], + script = """ + set -eo pipefail + binary="$1" + mode="$2" + if [[ $mode = dbg ]]; then + # Skip test in debug builds. Debug mode forces static linking. + exit 0 + fi + # Symbols are prefixed with underscore on MacOS but not on Linux. + if ! nm -u "$binary" | grep -q "\<_\?value"; then + echo "C library dependency not linked dynamically" + exit 1 + fi + if ! nm -u "$binary" | grep -q HsLib_value_closure; then + echo "Haskell library dependency not linked dynamically" + exit 1 + fi + """, +) + +test_suite( + name = "binary-linkstatic-flag", + tests = [ + ":test-binary-dynamic-symbols", + ":test-binary-static-symbols", + ], + visibility = ["//visibility:public"], +) diff --git a/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/HsLib.hs b/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/HsLib.hs new file mode 100644 index 000000000000..68271826d7ee --- /dev/null +++ b/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/HsLib.hs @@ -0,0 +1,4 @@ +module HsLib (value) where + +value :: Int +value = 13 diff --git a/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/Main.hs b/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/Main.hs new file mode 100644 index 000000000000..7836c2220277 --- /dev/null +++ b/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/Main.hs @@ -0,0 +1,9 @@ +{-# LANGUAGE ForeignFunctionInterface #-} + +module Main (main) where + +import qualified HsLib + +foreign import ccall "value" value :: Int + +main = print $ HsLib.value + value diff --git a/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/c-lib.c b/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/c-lib.c new file mode 100644 index 000000000000..9a1e526017ec --- /dev/null +++ b/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/c-lib.c @@ -0,0 +1 @@ +int value() { return 29; } |