Commit 8c8cf882 authored by donghuixu's avatar donghuixu

bazel support example and unittest

parent 6b5fc524
......@@ -5,8 +5,14 @@ exports_files(["LICENSE"])
load(":bazel/brpc.bzl", "brpc_proto_library")
config_setting(
name = "glog_mode",
define_values = {"with_glog": "1"}
name = "with_glog",
define_values = {"with_glog": "true"},
visibility = ["//visibility:public"],
)
config_setting(
name = "unittest",
define_values = {"unittest": "true"},
)
COPTS = [
......@@ -20,7 +26,7 @@ COPTS = [
"-D__STDC_CONSTANT_MACROS",
"-DGFLAGS_NS=google",
] + select({
":glog_mode": ["-DBRPC_WITH_GLOG=1"],
":with_glog": ["-DBRPC_WITH_GLOG=1"],
"//conditions:default": ["-DBRPC_WITH_GLOG=0"],
})
......@@ -47,7 +53,7 @@ genrule(
#undef BRPC_WITH_GLOG
#endif
#define BRPC_WITH_GLOG """ + select({
":glog_mode": "1",
":with_glog": "1",
"//conditions:default": "0",
}) +
"""
......@@ -191,12 +197,20 @@ cc_library(
deps = [
"@com_google_protobuf//:protobuf",
"@com_github_gflags_gflags//:gflags",
"@com_github_google_glog//:glog",
],
] + select({
":with_glog": ["@com_github_google_glog//:glog"],
"//conditions:default": [],
}),
includes = [
"src/",
],
copts = COPTS,
copts = COPTS + select({
":unittest": [
"-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
"-DUNIT_TEST",
],
"//conditions:default": [],
}),
linkopts = LINKOPTS,
visibility = ["//visibility:public"],
)
......@@ -206,7 +220,13 @@ cc_library(
srcs = glob([
"src/bvar/*.cpp",
"src/bvar/detail/*.cpp",
]),
],
exclude = [
"src/bvar/default_variables.cpp",
]) + select({
":unittest": [],
"//conditions:default": ["src/bvar/default_variables.cpp"],
}),
hdrs = glob([
"src/bvar/*.h",
"src/bvar/utils/*.h",
......@@ -218,7 +238,13 @@ cc_library(
deps = [
":butil",
],
copts = COPTS,
copts = COPTS + select({
":unittest": [
"-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
"-DUNIT_TEST",
],
"//conditions:default": [],
}),
linkopts = LINKOPTS,
visibility = ["//visibility:public"],
)
......
......@@ -10,13 +10,27 @@ if [ -z "$CC" ]; then
echo "CC must be set"
exit 1
fi
function runcmd()
{
eval $@
[[ $? != 0 ]] && {
exit 1
}
return 0
}
echo "build combination: PURPOSE=$PURPOSE CXX=$CXX CC=$CC"
if [ "$PURPOSE" = "compile-with-bazel" ]; then
bazel build -j 8 --copt -DHAVE_ZLIB=1 //...
runcmd "bazel build -c opt --copt -DHAVE_ZLIB=1 //..."
runcmd "bazel test -c opt --copt -DHAVE_ZLIB=1 --define=unittest=true //..."
# Build with glog
runcmd "bazel build -c opt --copt -DHAVE_ZLIB=1 --define=with_glog=true //..."
runcmd "bazel test -c opt --copt -DHAVE_ZLIB=1 --define=with_glog=true --define=unittest=true //..."
exit 0
fi
# The default env in travis-ci is Ubuntu.
if ! sh config_brpc.sh --headers=/usr/include --libs=/usr/lib --nodebugsymbols --cxx=$CXX --cc=$CC; then
echo "Fail to configure brpc"
......
COPTS = [
"-D__STDC_FORMAT_MACROS",
"-DBTHREAD_USE_FAST_PTHREAD_MUTEX",
"-D__const__=",
"-D_GNU_SOURCE",
"-DUSE_SYMBOLIZE",
"-DNO_TCMALLOC",
"-D__STDC_LIMIT_MACROS",
"-D__STDC_CONSTANT_MACROS",
"-fPIC",
"-Wno-unused-parameter",
"-fno-omit-frame-pointer",
"-DGFLAGS_NS=google",
] + select({
"//:with_glog": ["-DBRPC_WITH_GLOG=1"],
"//conditions:default": ["-DBRPC_WITH_GLOG=0"],
})
proto_library(
name = "echo_c++_proto",
srcs = [
"echo_c++/echo.proto",
],
)
cc_proto_library(
name = "cc_echo_c++_proto",
deps = [
":echo_c++_proto",
],
)
cc_binary(
name = "echo_c++_server",
srcs = [
"echo_c++/server.cpp",
],
includes = [
"echo_c++",
],
deps = [
":cc_echo_c++_proto",
"//:brpc",
],
copts = COPTS,
)
cc_binary(
name = "echo_c++_client",
srcs = [
"echo_c++/client.cpp",
],
includes = [
"echo_c++",
],
deps = [
":cc_echo_c++_proto",
"//:brpc",
],
copts = COPTS,
)
load("//:bazel/brpc.bzl", "brpc_proto_library")
COPTS = [
"-D__STDC_FORMAT_MACROS",
"-DBTHREAD_USE_FAST_PTHREAD_MUTEX",
"-D__const__=",
"-D_GNU_SOURCE",
"-DUSE_SYMBOLIZE",
"-DNO_TCMALLOC",
"-D__STDC_LIMIT_MACROS",
"-D__STDC_CONSTANT_MACROS",
"-fPIC",
"-Wno-unused-parameter",
"-fno-omit-frame-pointer",
"-DGFLAGS_NS=google",
"-Dprivate=public",
"-Dprotected=public",
"--include test/sstream_workaround.h",
"-DBAZEL_TEST=1",
"-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
"-DUNIT_TEST",
] + select({
"//:with_glog": ["-DBRPC_WITH_GLOG=1"],
"//conditions:default": ["-DBRPC_WITH_GLOG=0"],
})
LINKOPTS = [
"-lpthread",
"-lrt",
"-lssl",
"-lcrypto",
"-ldl",
"-lz",
]
TEST_BUTIL_SOURCES = [
"at_exit_unittest.cc",
"atomicops_unittest.cc",
"base64_unittest.cc",
"big_endian_unittest.cc",
"bits_unittest.cc",
"hash_tables_unittest.cc",
"linked_list_unittest.cc",
"mru_cache_unittest.cc",
"small_map_unittest.cc",
"stack_container_unittest.cc",
"cpu_unittest.cc",
"crash_logging_unittest.cc",
"leak_tracker_unittest.cc",
"proc_maps_linux_unittest.cc",
"stack_trace_unittest.cc",
"environment_unittest.cc",
"file_util_unittest.cc",
"dir_reader_posix_unittest.cc",
"file_path_unittest.cc",
"file_unittest.cc",
"scoped_temp_dir_unittest.cc",
"guid_unittest.cc",
"hash_unittest.cc",
"lazy_instance_unittest.cc",
"md5_unittest.cc",
"aligned_memory_unittest.cc",
"linked_ptr_unittest.cc",
"ref_counted_memory_unittest.cc",
"ref_counted_unittest.cc",
"scoped_ptr_unittest.cc",
"scoped_vector_unittest.cc",
"singleton_unittest.cc",
"weak_ptr_unittest.cc",
"observer_list_unittest.cc",
"file_descriptor_shuffle_unittest.cc",
"rand_util_unittest.cc",
"safe_numerics_unittest.cc",
"scoped_clear_errno_unittest.cc",
"scoped_generic_unittest.cc",
"security_unittest.cc",
"sha1_unittest.cc",
"stl_util_unittest.cc",
"nullable_string16_unittest.cc",
"safe_sprintf_unittest.cc",
"string16_unittest.cc",
"stringprintf_unittest.cc",
"string_number_conversions_unittest.cc",
"string_piece_unittest.cc",
"string_split_unittest.cc",
"string_tokenizer_unittest.cc",
"string_util_unittest.cc",
"stringize_macros_unittest.cc",
"sys_string_conversions_unittest.cc",
"utf_offset_string_conversions_unittest.cc",
"utf_string_conversions_unittest.cc",
"cancellation_flag_unittest.cc",
"condition_variable_unittest.cc",
"lock_unittest.cc",
"waitable_event_unittest.cc",
"type_traits_unittest.cc",
"non_thread_safe_unittest.cc",
"platform_thread_unittest.cc",
"simple_thread_unittest.cc",
"thread_checker_unittest.cc",
"thread_collision_warner_unittest.cc",
"thread_id_name_manager_unittest.cc",
"thread_local_storage_unittest.cc",
"thread_local_unittest.cc",
"watchdog_unittest.cc",
"pr_time_unittest.cc",
"time_unittest.cc",
"version_unittest.cc",
"logging_unittest.cc",
"cacheline_unittest.cpp",
"class_name_unittest.cpp",
"endpoint_unittest.cpp",
"unique_ptr_unittest.cpp",
"errno_unittest.cpp",
"fd_guard_unittest.cpp",
"file_watcher_unittest.cpp",
"find_cstr_unittest.cpp",
"scoped_lock_unittest.cpp",
"status_unittest.cpp",
"string_printf_unittest.cpp",
"string_splitter_unittest.cpp",
"synchronous_event_unittest.cpp",
"temp_file_unittest.cpp",
"baidu_thread_local_unittest.cpp",
"baidu_time_unittest.cpp",
"flat_map_unittest.cpp",
"crc32c_unittest.cc",
"iobuf_unittest.cpp",
"test_switches.cc",
"scoped_locale.cc",
"test_file_util_linux.cc",
#"popen_unittest.cpp",
"butil_unittest_main.cpp",
]
proto_library(
name = "test_proto",
srcs = glob([
"*.proto",
],
exclude = [
"echo.proto",
]
),
visibility = ["//visibility:public"],
)
cc_proto_library(
name = "cc_test_proto",
deps = [
":test_proto",
],
visibility = ["//visibility:public"],
)
cc_library(
name = "sstream_workaround",
hdrs = [
"sstream_workaround.h",
]
)
cc_test(
name = "butil_test",
srcs = TEST_BUTIL_SOURCES + [
"scoped_locale.h",
"multiprocess_func_list.h",
"test_switches.h",
],
deps = [
":sstream_workaround",
":cc_test_proto",
"//:brpc",
"@com_google_googletest//:gtest",
],
copts = COPTS,
)
cc_test(
name = "bvar_test",
srcs = glob([
"bvar_*_unittest.cpp",
],
exclude = [
"bvar_lock_timer_unittest.cpp",
"bvar_recorder_unittest.cpp",
]),
deps = [
":sstream_workaround",
"//:bvar",
"@com_google_googletest//:gtest",
],
copts = COPTS,
)
cc_test(
name = "bthread_test",
srcs = glob([
"bthread_*_unittest.cpp",
],
exclude = [
"bthread_cond_unittest.cpp",
"bthread_execution_queue_unittest.cpp",
"bthread_dispatcher_unittest.cpp",
"bthread_fd_unittest.cpp",
"bthread_mutex_unittest.cpp",
"bthread_setconcurrency_unittest.cpp",
# glog CHECK die with a fatal error
"bthread_key_unittest.cpp"
]),
deps = [
":sstream_workaround",
"//:brpc",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
copts = COPTS,
)
......@@ -108,7 +108,7 @@ TEST_BUTIL_SOURCES = \
baidu_time_unittest.cpp \
flat_map_unittest.cpp \
crc32c_unittest.cc \
iobuf_unittest.cc \
iobuf_unittest.cpp \
test_switches.cc \
scoped_locale.cc \
test_file_util_linux.cc \
......
......@@ -17,7 +17,11 @@
#include <butil/fd_guard.h>
#include <butil/errno.h>
#include <butil/fast_rand.h>
#if BAZEL_TEST
#include "test/iobuf.pb.h"
#else
#include "iobuf.pb.h"
#endif // BAZEL_TEST
namespace butil {
namespace iobuf {
......
build --copt -DHAVE_ZLIB=1
# bazel build with glog
# build --define=with_glog=true
build -c opt
# unittest
test --define=unittest=true
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment