xref: /aosp_15_r20/external/bazelbuild-rules_cc/tests/system_library/system_library_test.sh (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1# --- begin runfiles.bash initialization ---
2set -euo pipefail
3if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
4  if [[ -f "$0.runfiles_manifest" ]]; then
5    export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
6  elif [[ -f "$0.runfiles/MANIFEST" ]]; then
7    export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
8  elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
9    export RUNFILES_DIR="$0.runfiles"
10  fi
11fi
12if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
13  source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
14elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
15  source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
16            "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
17else
18  echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
19  exit 1
20fi
21# --- end runfiles.bash initialization ---
22
23source "$(rlocation rules_cc/tests/system_library/unittest.bash)" \
24  || { echo "Could not rules_cc/source tests/system_library/unittest.bash" >&2; exit 1; }
25
26
27function setup_system_library() {
28  mkdir -p systemlib
29
30  cat << EOF > systemlib/foo.cc
31int bar() {
32  return 42;
33}
34EOF
35
36  cat << EOF > systemlib/foo.h
37int bar();
38EOF
39
40  cd systemlib
41
42  g++ -c -fpic foo.cc || fail "Expected foo.o to build successfully"
43  g++ -shared -o libfoo.so foo.o || fail "Expected foo.so to build successfully"
44  g++ -c foo.cc || fail "Expected foo.o to build successfully"
45  ar rvs foo.a foo.o || fail "Expected foo.a to build successfully"
46
47  cd ..
48
49  cat << EOF > WORKSPACE
50load("//:cc/system_library.bzl", "system_library")
51system_library(
52    name = "foo",
53    hdrs = [
54        "foo.h",
55    ],
56    static_lib_names = ["libfoo.a"],
57    shared_lib_names = ["libfoo.so"]
58)
59
60system_library(
61    name = "foo_hardcoded_path",
62    hdrs = [
63        "foo.h",
64    ],
65    static_lib_names = ["libfoo.a"],
66    shared_lib_names = ["libfoo.so"],
67    lib_path_hints = ["${PWD}/systemlib"],
68    includes = ["${PWD}/systemlib"]
69)
70EOF
71
72  cat << EOF > BUILD
73cc_binary(
74    name = "test",
75    srcs = ["test.cc"],
76    deps = ["@foo"]
77)
78
79cc_binary(
80    name = "test_static",
81    srcs = ["test.cc"],
82    deps = ["@foo"],
83    linkstatic = True
84)
85
86cc_binary(
87    name = "test_hardcoded_path",
88    srcs = ["test.cc"],
89    deps = ["@foo_hardcoded_path"]
90)
91
92cc_binary(
93    name = "test_static_hardcoded_path",
94    srcs = ["test.cc"],
95    deps = ["@foo_hardcoded_path"],
96    linkstatic = True
97)
98
99cc_binary(
100    name = "fake_rbe",
101    srcs = ["test.cc"],
102    deps = ["@foo_hardcoded_path"]
103)
104EOF
105
106  cat << EOF > test.cc
107#include "foo.h"
108
109int main() {
110  return 42 - bar();
111}
112EOF
113}
114#### TESTS #############################################################
115
116# Make sure it fails with a correct message when no library is found
117function test_system_library_not_found() {
118  setup_system_library
119
120  bazel run //:test \
121  --experimental_starlark_cc_import \
122  --experimental_repo_remote_exec \
123  &> $TEST_log \
124  || true
125  expect_log "Library foo could not be found"
126
127    bazel run //:test_static \
128  --experimental_starlark_cc_import \
129  --experimental_repo_remote_exec \
130  &> $TEST_log \
131  || true
132  expect_log "Library foo could not be found"
133  }
134
135function test_override_paths() {
136  setup_system_library
137
138  bazel run //:test \
139  --experimental_starlark_cc_import \
140  --experimental_repo_remote_exec \
141  --action_env=BAZEL_LIB_OVERRIDE_PATHS=foo="${PWD}"/systemlib \
142  --action_env=BAZEL_INCLUDE_OVERRIDE_PATHS=foo="${PWD}"/systemlib \
143  || fail "Expected test to run successfully"
144
145  bazel run //:test_static \
146  --experimental_starlark_cc_import \
147  --experimental_repo_remote_exec \
148  --action_env=BAZEL_LIB_OVERRIDE_PATHS=foo="${PWD}"/systemlib \
149  --action_env=BAZEL_INCLUDE_OVERRIDE_PATHS=foo="${PWD}"/systemlib \
150  || fail "Expected test_static to run successfully"
151}
152
153function test_additional_paths() {
154  setup_system_library
155
156  bazel run //:test \
157  --experimental_starlark_cc_import \
158  --experimental_repo_remote_exec \
159  --action_env=BAZEL_LIB_ADDITIONAL_PATHS=foo="${PWD}"/systemlib \
160  --action_env=BAZEL_INCLUDE_ADDITIONAL_PATHS=foo="${PWD}"/systemlib \
161  || fail "Expected test to run successfully"
162
163  bazel run //:test_static \
164  --experimental_starlark_cc_import \
165  --experimental_repo_remote_exec \
166  --action_env=BAZEL_LIB_ADDITIONAL_PATHS=foo="${PWD}"/systemlib \
167  --action_env=BAZEL_INCLUDE_ADDITIONAL_PATHS=foo="${PWD}"/systemlib \
168  || fail "Expected test_static to run successfully"
169}
170
171function test_hardcoded_paths() {
172  setup_system_library
173
174  bazel run //:test_hardcoded_path \
175  --experimental_starlark_cc_import \
176  --experimental_repo_remote_exec \
177  || fail "Expected test_hardcoded_path to run successfully"
178
179  bazel run //:test_static_hardcoded_path \
180  --experimental_starlark_cc_import \
181  --experimental_repo_remote_exec \
182  || fail "Expected test_static_hardcoded_path to run successfully"
183}
184
185function test_system_library_no_lib_names() {
186  cat << EOF > WORKSPACE
187load("//:cc/system_library.bzl", "system_library")
188system_library(
189    name = "foo",
190    hdrs = [
191        "foo.h",
192    ]
193)
194EOF
195
196  cat << EOF > BUILD
197cc_binary(
198    name = "test",
199    srcs = ["test.cc"],
200    deps = ["@foo"]
201)
202EOF
203
204  # It should fail when no static_lib_names and static_lib_names are given
205  bazel run //:test \
206  --experimental_starlark_cc_import \
207  --experimental_repo_remote_exec \
208  &> $TEST_log \
209  || true
210  expect_log "Library foo could not be found"
211}
212
213run_suite "Integration tests for system_library."