Skip to content

Instantly share code, notes, and snippets.

@r2r-dev
Created August 29, 2023 17:45
Show Gist options
  • Save r2r-dev/fb20745404b96d300c3c278e5b6b8ae5 to your computer and use it in GitHub Desktop.
Save r2r-dev/fb20745404b96d300c3c278e5b6b8ae5 to your computer and use it in GitHub Desktop.
bazel libdeps test
#!/usr/bin/env bash
function test_cpp_libdeps_fail() {
echo ">>> THIS WILL FAIL"
rm -fr pkg WORKSPACE
mkdir -p pkg
cat <<'EOF' >pkg/BUILD
cc_library(
name = "a.so",
srcs = ["a.cc"],
)
cc_library(
name = "b",
srcs = ["b.cc", "a.so"],
)
cc_binary(
name = "main",
srcs = ["main.cc"],
deps = [":b"],
)
EOF
cat <<'EOF' >pkg/a.cc
#include <string>
std::string get_hello(std::string world) {
return "Hello, " + world + "!";
}
EOF
cat <<'EOF' >pkg/b.cc
#include <string>
#include <iostream>
std::string get_hello(std::string);
void print_hello(std::string world) {
std::cout << get_hello(world) << std::endl;
}
EOF
cat <<'EOF' >pkg/main.cc
#include <string>
void print_hello(std::string);
int main() {
print_hello(std::string("World"));
}
EOF
touch WORKSPACE
bazel build //pkg:a.so
bazel build //pkg:b
bazel run //pkg:main
echo "<<< THIS WILL FAIL"
}
function test_cpp_libdeps_success() {
echo ">>> THIS WILL PASS"
rm -fr pkg WORKSPACE
mkdir -p pkg
cat <<'EOF' >pkg/BUILD
cc_library(
name = "a.so",
srcs = ["a.cc"],
)
cc_library(
name = "b",
srcs = ["b.cc"],
deps = ["a.so"],
)
cc_binary(
name = "main",
srcs = ["main.cc"],
deps = [":b"],
)
EOF
cat <<'EOF' >pkg/a.cc
#include <string>
std::string get_hello(std::string world) {
return "Hello, " + world + "!";
}
EOF
cat <<'EOF' >pkg/b.cc
#include <string>
#include <iostream>
std::string get_hello(std::string);
void print_hello(std::string world) {
std::cout << get_hello(world) << std::endl;
}
EOF
cat <<'EOF' >pkg/main.cc
#include <string>
void print_hello(std::string);
int main() {
print_hello(std::string("World"));
}
EOF
touch WORKSPACE
bazel build //pkg:a.so
bazel build //pkg:b
bazel run //pkg:main
echo "<<< THIS WILL PASS"
}
test_cpp_libdeps_fail
echo "----------------------------------"
test_cpp_libdeps_success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment