Skip to content

Commit

Permalink
Add C++ toolchain configuration for OpenBSD.
Browse files Browse the repository at this point in the history
In my limited testing, these changes appear to work. When I built Bazel on OpenBSD incorporating these changes, the resulting `bazel` binary could build C++ libraries and binaries. Likewise on FreeBSD.

This change, split out of the larger PR #10274, is part of the OpenBSD port in #10250.

Closes #10436.

PiperOrigin-RevId: 289091510
  • Loading branch information
aldersondrive authored and copybara-github committed Jan 10, 2020
1 parent 6071dfa commit 644b7d4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 26 deletions.
38 changes: 37 additions & 1 deletion tools/cpp/BUILD.static.freebsd → tools/cpp/BUILD.static.bsd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# This becomes the BUILD file for @local_config_cc// under FreeBSD.
# This becomes the BUILD file for @local_config_cc// under FreeBSD and OpenBSD.

package(default_visibility = ["//visibility:public"])

Expand All @@ -34,8 +34,10 @@ cc_toolchain_suite(
toolchains = {
"armeabi-v7a|compiler": ":cc-compiler-armeabi-v7a",
"freebsd|compiler": ":cc-compiler-freebsd",
"openbsd|compiler": ":cc-compiler-openbsd",
"armeabi-v7a": ":cc-compiler-armeabi-v7a",
"freebsd": ":cc-compiler-freebsd",
"openbsd": ":cc-compiler-openbsd",
},
)

Expand Down Expand Up @@ -73,6 +75,40 @@ toolchain(
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

cc_toolchain(
name = "cc-compiler-openbsd",
all_files = ":empty",
ar_files = ":empty",
as_files = ":empty",
compiler_files = ":empty",
dwp_files = ":empty",
linker_files = ":empty",
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 0,
toolchain_config = ":local_openbsd",
toolchain_identifier = "local_openbsd",
)

cc_toolchain_config(
name = "local_openbsd",
cpu = "openbsd",
)

toolchain(
name = "cc-toolchain-openbsd",
exec_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:openbsd",
],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:openbsd",
],
toolchain = ":cc-compiler-openbsd",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

cc_toolchain(
name = "cc-compiler-armeabi-v7a",
all_files = ":empty",
Expand Down
2 changes: 1 addition & 1 deletion tools/cpp/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# This becomes the BUILD file for @local_config_cc// under non-FreeBSD unixes.
# This becomes the BUILD file for @local_config_cc// under non-BSD unixes.

package(default_visibility = ["//visibility:public"])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""A Starlark cc_toolchain configuration rule for freebsd."""
"""A Starlark cc_toolchain configuration rule for FreeBSD and OpenBSD."""

load(
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
Expand Down Expand Up @@ -56,21 +56,22 @@ all_link_actions = [

def _impl(ctx):
cpu = ctx.attr.cpu
is_bsd = cpu == "freebsd" or cpu == "openbsd"
compiler = "compiler"
toolchain_identifier = "local_freebsd" if cpu == "freebsd" else "stub_armeabi-v7a"
host_system_name = "local" if cpu == "freebsd" else "armeabi-v7a"
target_system_name = "local" if cpu == "freebsd" else "armeabi-v7a"
target_libc = "local" if cpu == "freebsd" else "armeabi-v7a"
abi_version = "local" if cpu == "freebsd" else "armeabi-v7a"
abi_libc_version = "local" if cpu == "freebsd" else "armeabi-v7a"
toolchain_identifier = "local_{}".format(cpu) if is_bsd else "stub_armeabi-v7a"
host_system_name = "local" if is_bsd else "armeabi-v7a"
target_system_name = "local" if is_bsd else "armeabi-v7a"
target_libc = "local" if is_bsd else "armeabi-v7a"
abi_version = "local" if is_bsd else "armeabi-v7a"
abi_libc_version = "local" if is_bsd else "armeabi-v7a"

objcopy_embed_data_action = action_config(
action_name = "objcopy_embed_data",
enabled = True,
tools = [tool(path = "/usr/bin/objcopy")],
)

action_configs = [objcopy_embed_data_action] if cpu == "freebsd" else []
action_configs = [objcopy_embed_data_action] if is_bsd else []

default_link_flags_feature = feature(
name = "default_link_flags",
Expand Down Expand Up @@ -224,7 +225,7 @@ def _impl(ctx):
],
)

if cpu == "freebsd":
if is_bsd:
features = [
default_compile_flags_feature,
default_link_flags_feature,
Expand All @@ -240,12 +241,12 @@ def _impl(ctx):
else:
features = [supports_dynamic_linker_feature, supports_pic_feature]

if (cpu == "freebsd"):
if (is_bsd):
cxx_builtin_include_directories = ["/usr/lib/clang", "/usr/local/include", "/usr/include"]
else:
cxx_builtin_include_directories = []

if cpu == "freebsd":
if is_bsd:
tool_paths = [
tool_path(name = "ar", path = "/usr/bin/ar"),
tool_path(name = "compat-ld", path = "/usr/bin/ld"),
Expand Down
18 changes: 9 additions & 9 deletions tools/cpp/cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ def cc_autoconf_impl(repository_ctx, overriden_tools = dict()):
])
repository_ctx.symlink(paths["@bazel_tools//tools/cpp:empty_cc_toolchain_config.bzl"], "cc_toolchain_config.bzl")
repository_ctx.symlink(paths["@bazel_tools//tools/cpp:BUILD.empty"], "BUILD")
elif cpu_value == "freebsd":
elif cpu_value == "freebsd" or cpu_value == "openbsd":
paths = resolve_labels(repository_ctx, [
"@bazel_tools//tools/cpp:BUILD.static.freebsd",
"@bazel_tools//tools/cpp:freebsd_cc_toolchain_config.bzl",
"@bazel_tools//tools/cpp:BUILD.static.bsd",
"@bazel_tools//tools/cpp:bsd_cc_toolchain_config.bzl",
])

# This is defaulting to a static crosstool, we should eventually
# autoconfigure this platform too. Theorically, FreeBSD should be
# straightforward to add but we cannot run it in a docker container so
# skipping until we have proper tests for FreeBSD.
repository_ctx.symlink(paths["@bazel_tools//tools/cpp:freebsd_cc_toolchain_config.bzl"], "cc_toolchain_config.bzl")
repository_ctx.symlink(paths["@bazel_tools//tools/cpp:BUILD.static.freebsd"], "BUILD")
# This is defaulting to a static crosstool. We should eventually
# autoconfigure this platform too. Theorically, FreeBSD and OpenBSD
# should be straightforward to add but we cannot run them in a Docker
# container so skipping until we have proper tests for these platforms.
repository_ctx.symlink(paths["@bazel_tools//tools/cpp:bsd_cc_toolchain_config.bzl"], "cc_toolchain_config.bzl")
repository_ctx.symlink(paths["@bazel_tools//tools/cpp:BUILD.static.bsd"], "BUILD")
elif cpu_value == "x64_windows":
# TODO(ibiryukov): overriden_tools are only supported in configure_unix_toolchain.
# We might want to add that to Windows too(at least for msys toolchain).
Expand Down
41 changes: 37 additions & 4 deletions tools/cpp/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def _impl(ctx):
toolchain_identifier = "local_darwin"
elif (ctx.attr.cpu == "freebsd"):
toolchain_identifier = "local_freebsd"
elif (ctx.attr.cpu == "openbsd"):
toolchain_identifier = "local_openbsd"
elif (ctx.attr.cpu == "local"):
toolchain_identifier = "local_linux"
elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_clang"):
Expand All @@ -119,6 +121,7 @@ def _impl(ctx):
host_system_name = "armeabi-v7a"
elif (ctx.attr.cpu == "darwin" or
ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local" or
ctx.attr.cpu == "x64_windows" or
ctx.attr.cpu == "x64_windows_msvc"):
Expand All @@ -130,6 +133,7 @@ def _impl(ctx):
target_system_name = "armeabi-v7a"
elif (ctx.attr.cpu == "darwin" or
ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local" or
ctx.attr.cpu == "x64_windows" or
ctx.attr.cpu == "x64_windows_msvc"):
Expand All @@ -143,6 +147,8 @@ def _impl(ctx):
target_cpu = "darwin"
elif (ctx.attr.cpu == "freebsd"):
target_cpu = "freebsd"
elif (ctx.attr.cpu == "openbsd"):
target_cpu = "openbsd"
elif (ctx.attr.cpu == "local"):
target_cpu = "local"
elif (ctx.attr.cpu == "x64_windows"):
Expand All @@ -155,6 +161,7 @@ def _impl(ctx):
if (ctx.attr.cpu == "armeabi-v7a"):
target_libc = "armeabi-v7a"
elif (ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local" or
ctx.attr.cpu == "x64_windows"):
target_libc = "local"
Expand All @@ -170,6 +177,7 @@ def _impl(ctx):
elif (ctx.attr.cpu == "armeabi-v7a" or
ctx.attr.cpu == "darwin" or
ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local"):
compiler = "compiler"
elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_clang"):
Expand All @@ -187,6 +195,7 @@ def _impl(ctx):
abi_version = "armeabi-v7a"
elif (ctx.attr.cpu == "darwin" or
ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local" or
ctx.attr.cpu == "x64_windows" or
ctx.attr.cpu == "x64_windows_msvc"):
Expand All @@ -198,6 +207,7 @@ def _impl(ctx):
abi_libc_version = "armeabi-v7a"
elif (ctx.attr.cpu == "darwin" or
ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local" or
ctx.attr.cpu == "x64_windows" or
ctx.attr.cpu == "x64_windows_msvc"):
Expand All @@ -211,6 +221,7 @@ def _impl(ctx):

if (ctx.attr.cpu == "darwin" or
ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local"):
objcopy_embed_data_action = action_config(
action_name = "objcopy_embed_data",
Expand Down Expand Up @@ -274,6 +285,7 @@ def _impl(ctx):
action_configs = [c_compile_action, cpp_compile_action]
elif (ctx.attr.cpu == "darwin" or
ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local" or
ctx.attr.cpu == "x64_windows"):
action_configs = [objcopy_embed_data_action]
Expand Down Expand Up @@ -348,7 +360,8 @@ def _impl(ctx):
),
],
)
elif (ctx.attr.cpu == "freebsd"):
elif (ctx.attr.cpu == "freebsd" or
ctx.atr.cpu == "openbsd"):
default_link_flags_feature = feature(
name = "default_link_flags",
enabled = True,
Expand Down Expand Up @@ -417,7 +430,8 @@ def _impl(ctx):
)

if (ctx.attr.cpu == "darwin" or
ctx.attr.cpu == "freebsd"):
ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd"):
unfiltered_compile_flags_feature = feature(
name = "unfiltered_compile_flags",
enabled = True,
Expand Down Expand Up @@ -685,7 +699,8 @@ def _impl(ctx):
),
],
)
elif (ctx.attr.cpu == "freebsd"):
elif (ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd"):
default_compile_flags_feature = feature(
name = "default_compile_flags",
enabled = True,
Expand Down Expand Up @@ -941,6 +956,7 @@ def _impl(ctx):

if (ctx.attr.cpu == "darwin" or
ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local"):
user_compile_flags_feature = feature(
name = "user_compile_flags",
Expand Down Expand Up @@ -996,6 +1012,7 @@ def _impl(ctx):

if (ctx.attr.cpu == "darwin" or
ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local"):
sysroot_feature = feature(
name = "sysroot",
Expand Down Expand Up @@ -1152,6 +1169,7 @@ def _impl(ctx):
unfiltered_compile_flags_feature,
]
elif (ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd" or
ctx.attr.cpu == "local"):
features = [
default_compile_flags_feature,
Expand Down Expand Up @@ -1196,7 +1214,8 @@ def _impl(ctx):
cxx_builtin_include_directories = []
elif (ctx.attr.cpu == "darwin"):
cxx_builtin_include_directories = ["/"]
elif (ctx.attr.cpu == "freebsd"):
elif (ctx.attr.cpu == "freebsd" or
ctx.attr.cpu == "openbsd"):
cxx_builtin_include_directories = ["/usr/lib/clang", "/usr/local/include", "/usr/include"]
elif (ctx.attr.cpu == "local" or
ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_clang"):
Expand Down Expand Up @@ -1297,6 +1316,20 @@ def _impl(ctx):
tool_path(name = "objdump", path = "/usr/bin/objdump"),
tool_path(name = "strip", path = "/usr/bin/strip"),
]
elif (ctx.attr.cpu == "openbsd"):
tool_paths = [
tool_path(name = "ar", path = "/usr/bin/ar"),
tool_path(name = "compat-ld", path = "/usr/bin/ld"),
tool_path(name = "cpp", path = "/usr/bin/cpp"),
tool_path(name = "dwp", path = "/usr/bin/false"),
tool_path(name = "gcc", path = "/usr/bin/clang"),
tool_path(name = "gcov", path = "/usr/bin/gcov"),
tool_path(name = "ld", path = "/usr/bin/ld"),
tool_path(name = "nm", path = "/usr/bin/nm"),
tool_path(name = "objcopy", path = "/usr/bin/objcopy"),
tool_path(name = "objdump", path = "/usr/bin/objdump"),
tool_path(name = "strip", path = "/usr/bin/strip"),
]
elif (ctx.attr.cpu == "local"):
tool_paths = [
tool_path(name = "ar", path = "/usr/bin/ar"),
Expand Down
2 changes: 2 additions & 0 deletions tools/cpp/lib_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ def get_cpu_value(repository_ctx):
return "darwin"
if os_name.find("freebsd") != -1:
return "freebsd"
if os_name.find("openbsd") != -1:
return "openbsd"
if os_name.find("windows") != -1:
return "x64_windows"

Expand Down

0 comments on commit 644b7d4

Please sign in to comment.