Skip to content

Commit 57b54f3

Browse files
committed
test: add meson_simple test
Test meson rule and the new support for extra targets such as introspect.
1 parent 57e6892 commit 57b54f3

File tree

8 files changed

+140
-0
lines changed

8 files changed

+140
-0
lines changed

examples/meson_simple/BUILD.bazel

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_test")
2+
load("@rules_foreign_cc//foreign_cc:defs.bzl", "meson")
3+
load("@rules_shell//shell:sh_test.bzl", "sh_test")
4+
5+
meson(
6+
name = "meson_lib",
7+
build_data = ["//meson_simple/code:clang_wrapper.sh"],
8+
env = select({
9+
"//:windows": {
10+
"CLANG_WRAPPER": "$(execpath //meson_simple/code:clang_wrapper.sh)",
11+
"CXX_FLAGS": "/MD",
12+
},
13+
"//conditions:default": {
14+
"CLANG_WRAPPER": "$(execpath //meson_simple/code:clang_wrapper.sh)",
15+
"CXX_FLAGS": "-fPIC",
16+
},
17+
}),
18+
lib_source = "//meson_simple/code:srcs",
19+
out_static_libs = ["liba.a"],
20+
)
21+
22+
cc_test(
23+
name = "test_lib",
24+
srcs = [
25+
"test_libb.cpp",
26+
],
27+
deps = [
28+
":meson_lib",
29+
],
30+
)
31+
32+
meson(
33+
name = "meson_lib-introspect",
34+
lib_source = "//meson_simple/code:srcs",
35+
out_data_files = ["introspect.json"],
36+
out_include_dir = "",
37+
targets = ["introspect"],
38+
)
39+
40+
sh_test(
41+
name = "test_introspect",
42+
srcs = ["test_introspect.sh"],
43+
args = ["$(location meson_lib-introspect)"],
44+
data = [":meson_lib-introspect"],
45+
deps = ["@bazel_tools//tools/bash/runfiles"],
46+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
exports_files(["clang_wrapper.sh"])
2+
3+
filegroup(
4+
name = "srcs",
5+
srcs = [
6+
"liba.cpp",
7+
"liba.h",
8+
"meson.build",
9+
],
10+
visibility = ["//meson_simple:__subpackages__"],
11+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
if [[ $(uname) == *"NT"* ]]; then
7+
# If Windows
8+
exec clang-cl "$@"
9+
else
10+
exec clang "$@"
11+
fi
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "liba.h"
2+
3+
std::string hello_liba(void) { return "Hello from LIBA!"; }

examples/meson_simple/code/liba.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef LIBA_H_
2+
#define LIBA_H_ (1)
3+
4+
#include <stdio.h>
5+
6+
#include <string>
7+
8+
std::string hello_liba(void);
9+
10+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
project('liba', 'cpp')
2+
3+
builddir = 'rules_foreign_cc_build'
4+
5+
cxx = meson.get_compiler('cpp')
6+
7+
liba = static_library(
8+
'a',
9+
'liba.cpp',
10+
cpp_args: cxx.get_supported_arguments(),
11+
include_directories: include_directories('.'),
12+
install: true,
13+
install_dir: join_paths(get_option('prefix'), 'lib'),
14+
)
15+
16+
install_headers('liba.h', subdir: 'include')
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# --- begin runfiles.bash initialization ---
4+
# The runfiles library itself defines rlocation which you would need to look
5+
# up the library's runtime location, thus we have a chicken-and-egg problem.
6+
#
7+
# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
8+
set -euo pipefail
9+
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
10+
if [[ -f "$0.runfiles_manifest" ]]; then
11+
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
12+
elif [[ -f "$0.runfiles/MANIFEST" ]]; then
13+
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
14+
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
15+
export RUNFILES_DIR="$0.runfiles"
16+
fi
17+
fi
18+
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
19+
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
20+
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
21+
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
22+
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
23+
else
24+
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
25+
exit 1
26+
fi
27+
# --- end runfiles.bash initialization ---
28+
29+
[[ -f $(rlocation $TEST_WORKSPACE/$1) ]] && exit 0
30+
exit 1
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
#include <stdexcept>
3+
#include <string>
4+
5+
#include "include/liba.h"
6+
7+
int main(int argc, char* argv[]) {
8+
std::string result = hello_liba();
9+
if (result != "Hello from LIBA!") {
10+
throw std::runtime_error("Wrong result: " + result);
11+
}
12+
std::cout << "Everything's fine!";
13+
}

0 commit comments

Comments
 (0)