summary refs log tree commit diff
path: root/third_party/bazel/rules_haskell/tests/binary-linkstatic-flag/BUILD.bazel
blob: 4e8e7cfa668435998734b2c5809830b49a31298f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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"],
)