From f723b8b878a3c4a4687b9e337a875500bebb39b1 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 4 Jul 2019 11:18:12 +0100 Subject: feat(third_party/bazel): Check in rules_haskell from Tweag --- .../tests/binary-linkstatic-flag/BUILD.bazel | 118 +++++++++++++++++++++ .../tests/binary-linkstatic-flag/HsLib.hs | 4 + .../tests/binary-linkstatic-flag/Main.hs | 9 ++ .../tests/binary-linkstatic-flag/c-lib.c | 1 + 4 files changed, 132 insertions(+) create mode 100644 third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/BUILD.bazel create mode 100644 third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/HsLib.hs create mode 100644 third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/Main.hs create mode 100644 third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/c-lib.c (limited to 'third_party/bazel/rules_haskell/tests/binary-linkstatic-flag') 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; } -- cgit 1.4.1