Skip to content

Commit

Permalink
Merge pull request #148 from reltuk/reltuk/test-so-cleanup
Browse files Browse the repository at this point in the history
Move heavy lifting of test.so implementation to boost.bzl.
  • Loading branch information
nelhage authored Nov 19, 2019
2 parents 8fbf4ea + bf720a6 commit 29d1426
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 74 deletions.
57 changes: 5 additions & 52 deletions BUILD.boost
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_library", "boost_so_binary", "hdr_list")
load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_library", "boost_so_library", "hdr_list")

_w_no_deprecated = select({
":linux": [
Expand Down Expand Up @@ -1929,67 +1929,20 @@ boost_library(
deps = _BOOST_TEST_DEPS,
)

# This gets a little messy. Boost.Test compiled with BOOST_TEST_DYN_LINK is not
# a cc_library because it cannot be linked statically. Instead, we build an
# explicit .so with cc_binary's linkshared=True. Then we need to reimport it as
# a C++ library and make it ergonomic to use from cc_test targets.
boost_so_binary(
name = "libtest.so",
boost_name = "test",
exclude_src = glob([
"libs/test/src/test_main.cpp",
"libs/test/src/cpp_main.cpp",
]),
defines = ["BOOST_TEST_DYN_LINK"],
deps = _BOOST_TEST_DEPS,
visibility = ["//visibility:private"],
)

boost_so_binary(
name = "libtest.dll",
boost_name = "test",
exclude_src = glob([
"libs/test/src/test_main.cpp",
"libs/test/src/cpp_main.cpp",
]),
defines = ["BOOST_TEST_DYN_LINK"],
deps = _BOOST_TEST_DEPS,
visibility = ["//visibility:private"],
)

filegroup(
name = "libtest_dll_interface_file",
srcs = [":libtest.dll"],
output_group = "interface_library",
)

cc_import(
name = "_imported_libtest.so",
shared_library = ":libtest.so",
)

cc_import(
name = "_imported_libtest.dll",
shared_library = ":libtest.dll",
interface_library = ":libtest_dll_interface_file",
)

# A dynamically linked Boost.Test library, used through boost/test/*. Bazel
# will add -DBOOST_TEST_DYN_LINK for you if you depend on this library.
boost_library(
boost_so_library(
name = "test.so",
boost_name = "test",
exclude_hdr = glob([
"boost/test/included/*.hpp",
]),
exclude_src = glob([
"libs/test/**",
"libs/test/src/test_main.cpp",
"libs/test/src/cpp_main.cpp",
]),
defines = ["BOOST_TEST_DYN_LINK"],
deps = _BOOST_TEST_DEPS + select({
":windows": [":_imported_libtest.dll"],
"//conditions:default": [":_imported_libtest.so"],
}),
deps = _BOOST_TEST_DEPS,
)

boost_library(
Expand Down
79 changes: 57 additions & 22 deletions boost/boost.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -94,36 +94,71 @@ def boost_library(
licenses = ["notice"],
)

def boost_so_binary(
# Some boost libraries are not safe to use as dynamic libraries unless a
# BOOST_*_DYN_LINK define is set when they are compiled and included, notably
# Boost.Test. When the define is set, the libraries are not safe to use
# statically. This is an attempt to work around that. We build an explicit .so
# with cc_binary's linkshared=True and then we reimport it as a C++ library and
# expose it as a boost_library.

def boost_so_library(
name,
boost_name = None,
defines = [],
srcs = None,
deps = None,
copts = None,
srcs = [],
deps = [],
copts = [],
exclude_src = [],
visibility = ["//visibility:public"]):
exclude_hdr = []):
if boost_name == None:
boost_name = name

if srcs == None:
srcs = []

if deps == None:
deps = []

if copts == None:
copts = []

return native.cc_binary(
for suffix in ["so", "dll", "dylib"]:
native.cc_binary(
name = "lib_internal_%s.%s" % (name, suffix),
visibility = ["//visibility:private"],
srcs = hdr_list(boost_name, exclude_hdr) + srcs_list(boost_name, exclude_src) + srcs,
deps = deps,
copts = default_copts + copts,
defines = default_defines + defines,
linkshared = True,
licenses = ["notice"],
)
native.filegroup(
name = "%s_dll_interface_file" % name,
srcs = [":lib_internal_%s.dll" % name],
output_group = "interface_library",
visibility = ["//visibility:private"],
)
native.cc_import(
name = "_imported_%s.so" % name,
shared_library = ":lib_internal_%s.so" % name,
visibility = ["//visibility:private"],
)
native.cc_import(
name = "_imported_%s.dylib" % name,
shared_library = ":lib_internal_%s.dylib" % name,
visibility = ["//visibility:private"],
)
native.cc_import(
name = "_imported_%s.dll" % name,
shared_library = ":lib_internal_%s.dll" % name,
interface_library = ":%s_dll_interface_file" % name,
visibility = ["//visibility:private"],
)
return boost_library(
name = name,
visibility = visibility,
srcs = hdr_list(boost_name) + srcs_list(boost_name, exclude_src) + srcs,
deps = deps,
copts = default_copts + copts,
defines = default_defines + defines,
linkshared = True,
licenses = ["notice"],
boost_name = boost_name,
exclude_hdr = exclude_hdr,
exclude_src = native.glob([
"libs/%s/**" % boost_name,
]),
defines = defines,
deps = deps + select({
"@boost//:linux": [":_imported_%s.so" % name],
"@boost//:osx": [":_imported_%s.dylib" % name],
"@boost//:windows": [":_imported_%s.dll" % name],
}),
)

def boost_deps():
Expand Down

0 comments on commit 29d1426

Please sign in to comment.