diff --git a/tools/cpp/BUILD.static.freebsd b/tools/cpp/BUILD.static.bsd similarity index 76% rename from tools/cpp/BUILD.static.freebsd rename to tools/cpp/BUILD.static.bsd index d9a3b5c50580d9..2e02dbf8f49d84 100644 --- a/tools/cpp/BUILD.static.freebsd +++ b/tools/cpp/BUILD.static.bsd @@ -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"]) @@ -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", }, ) @@ -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", diff --git a/tools/cpp/BUILD.tpl b/tools/cpp/BUILD.tpl index 9241326d9200b7..730224f0969746 100644 --- a/tools/cpp/BUILD.tpl +++ b/tools/cpp/BUILD.tpl @@ -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"]) diff --git a/tools/cpp/freebsd_cc_toolchain_config.bzl b/tools/cpp/bsd_cc_toolchain_config.bzl similarity index 93% rename from tools/cpp/freebsd_cc_toolchain_config.bzl rename to tools/cpp/bsd_cc_toolchain_config.bzl index d8818d4afbd4b2..dc45e85917275f 100644 --- a/tools/cpp/freebsd_cc_toolchain_config.bzl +++ b/tools/cpp/bsd_cc_toolchain_config.bzl @@ -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", @@ -56,13 +56,14 @@ 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", @@ -70,7 +71,7 @@ def _impl(ctx): 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", @@ -224,7 +225,7 @@ def _impl(ctx): ], ) - if cpu == "freebsd": + if is_bsd: features = [ default_compile_flags_feature, default_link_flags_feature, @@ -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"), diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl index 0120ebdedeaf98..82d41b90c11975 100644 --- a/tools/cpp/cc_configure.bzl +++ b/tools/cpp/cc_configure.bzl @@ -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). diff --git a/tools/cpp/cc_toolchain_config.bzl b/tools/cpp/cc_toolchain_config.bzl index fcd3d111c3cab7..358a4497cd6bf1 100644 --- a/tools/cpp/cc_toolchain_config.bzl +++ b/tools/cpp/cc_toolchain_config.bzl @@ -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"): @@ -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"): @@ -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"): @@ -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"): @@ -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" @@ -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"): @@ -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"): @@ -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"): @@ -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", @@ -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] @@ -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, @@ -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, @@ -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, @@ -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", @@ -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", @@ -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, @@ -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"): @@ -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"), diff --git a/tools/cpp/lib_cc_configure.bzl b/tools/cpp/lib_cc_configure.bzl index fbce23b72b5ba6..773e913bf2f4a8 100644 --- a/tools/cpp/lib_cc_configure.bzl +++ b/tools/cpp/lib_cc_configure.bzl @@ -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"