diff --git a/.bazelrc b/.bazelrc index 7d6623d..bd092a0 100644 --- a/.bazelrc +++ b/.bazelrc @@ -38,4 +38,8 @@ common --repo_env=BAZEL_NO_APPLE_CPP_TOOLCHAIN=1 build --define=protobuf_allow_msvc=true # To facilitate testing in bazelci incompatible flags -build --incompatible_autoload_externally= \ No newline at end of file +build --incompatible_autoload_externally= + +import %workspace%/toolchain/toolchains.bazelrc + +build:offline --nofetch \ No newline at end of file diff --git a/BUILD.bazel b/BUILD.bazel index f6c08af..901b564 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -8,41 +8,8 @@ load("//:protobuf_javascript_release.bzl", "package_naming") exports_files(["package.json"]) -config_setting( - name = "x64_x86_windows", - values = {"cpu": "x64_x86_windows"}, -) - -config_setting( - name = "x64_windows", - values = {"cpu": "x64_windows"}, -) - -config_setting( - name = "k8", - values = {"cpu": "k8"}, -) - -config_setting( - name = "darwin_arm64", - values = {"cpu": "darwin_arm64"}, -) - -config_setting( - name = "darwin_x86_64", - values = {"cpu": "darwin_x86_64"}, -) - package_naming( name = "protobuf_javascript_pkg_naming", - platform = select({ - ":k8": "linux-x86_64", # currently the only supported build type in Github Actions - ":x64_x86_windows": "win32", - ":x64_windows": "win64", - ":darwin_arm64": "osx-aarch_64", - ":darwin_x86_64": "osx-x86_64", - "//conditions:default": "", # continues with current behavior when no --cpu is specified allowing existing internal builds to function - }), ) pkg_files( diff --git a/MODULE.bazel b/MODULE.bazel index 7e9ed5e..d662700 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -6,3 +6,18 @@ bazel_dep(name = "abseil-cpp", version = "20250814.1") bazel_dep(name = "protobuf", version = "33.4", repo_name = "com_google_protobuf") bazel_dep(name = "rules_cc", version = "0.2.15") bazel_dep(name = "rules_pkg", version = "1.0.1", dev_dependency = True) +bazel_dep(name = "platforms", version = "1.0.0") +bazel_dep(name = "apple_support", version = "1.17.1", repo_name = "build_bazel_apple_support") +bazel_dep(name = "bazel_skylib", version = "1.7.1") + +register_toolchains( + "//toolchain:osx-x86_64-toolchain", + "//toolchain:osx-aarch_64-toolchain", + "//toolchain:linux-aarch_64-toolchain", + "//toolchain:linux-ppcle_64-toolchain", + "//toolchain:linux-s390_64-toolchain", + "//toolchain:linux-x86_32-toolchain", + "//toolchain:linux-x86_64-toolchain", + "//toolchain:win32-toolchain", + "//toolchain:win64-toolchain", +) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index b130cf6..fa0f80e 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -16,6 +16,7 @@ "https://bcr.bazel.build/modules/abseil-cpp/20250814.1/source.json": "cea3901d7e299da7320700abbaafe57a65d039f10d0d7ea601c4a66938ea4b0c", "https://bcr.bazel.build/modules/apple_support/1.11.1/MODULE.bazel": "1843d7cd8a58369a444fc6000e7304425fba600ff641592161d9f15b179fb896", "https://bcr.bazel.build/modules/apple_support/1.15.1/MODULE.bazel": "a0556fefca0b1bb2de8567b8827518f94db6a6e7e7d632b4c48dc5f865bc7c85", + "https://bcr.bazel.build/modules/apple_support/1.17.1/MODULE.bazel": "655c922ab1209978a94ef6ca7d9d43e940cd97d9c172fb55f94d91ac53f8610b", "https://bcr.bazel.build/modules/apple_support/1.21.0/MODULE.bazel": "ac1824ed5edf17dee2fdd4927ada30c9f8c3b520be1b5fd02a5da15bc10bff3e", "https://bcr.bazel.build/modules/apple_support/1.21.1/MODULE.bazel": "5809fa3efab15d1f3c3c635af6974044bac8a4919c62238cce06acee8a8c11f1", "https://bcr.bazel.build/modules/apple_support/1.24.2/MODULE.bazel": "0e62471818affb9f0b26f128831d5c40b074d32e6dda5a0d3852847215a41ca4", @@ -188,6 +189,28 @@ }, "selectedYankedVersions": {}, "moduleExtensions": { + "@@pybind11_bazel+//:internal_configure.bzl%internal_configure_extension": { + "general": { + "bzlTransitiveDigest": "06cynZ1bCvvy8zHPrrDlXq+Z68xmjctHpfFxi+zEpJY=", + "usagesDigest": "D1r3lfzMuUBFxgG8V6o0bQTLMk3GkaGOaPzw53wrwyw=", + "recordedInputs": [ + "REPO_MAPPING:pybind11_bazel+,bazel_tools bazel_tools", + "FILE:@@pybind11_bazel+//MODULE.bazel e6f4c20442eaa7c90d7190d8dc539d0ab422f95c65a57cc59562170c58ae3d34" + ], + "generatedRepoSpecs": { + "pybind11": { + "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", + "attributes": { + "build_file": "@@pybind11_bazel+//:pybind11-BUILD.bazel", + "strip_prefix": "pybind11-2.12.0", + "urls": [ + "https://github.com/pybind/pybind11/archive/v2.12.0.zip" + ] + } + } + } + } + }, "@@rules_kotlin+//src/main/starlark/core/repositories:bzlmod_setup.bzl%rules_kotlin_extensions": { "general": { "bzlTransitiveDigest": "ABI1D/sbS1ovwaW/kHDoj8nnXjQ0oKU9fzmzEG4iT8o=", diff --git a/protobuf_javascript_release.bzl b/protobuf_javascript_release.bzl index 5a18008..f152085 100644 --- a/protobuf_javascript_release.bzl +++ b/protobuf_javascript_release.bzl @@ -25,6 +25,10 @@ def _package_naming_impl(ctx): cpu = "aarch_64" elif cpu == "ppc64": cpu = "ppcle_64" + elif cpu == "i686": + cpu = "x86_32" + elif cpu == "x86_64": + cpu = "x86_64" # use the system name to determine the os and then create platform names if "apple" in system_name: diff --git a/toolchain/BUILD.bazel b/toolchain/BUILD.bazel new file mode 100644 index 0000000..514889a --- /dev/null +++ b/toolchain/BUILD.bazel @@ -0,0 +1,173 @@ +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +load("@rules_cc//cc/toolchains:cc_toolchain.bzl", "cc_toolchain") +load("@rules_cc//cc/toolchains:cc_toolchain_suite.bzl", "cc_toolchain_suite") +load(":platforms.bzl", "PROTOBUF_PLATFORMS") +load(":cc_toolchain_config.bzl", "cc_toolchain_config") + +package(default_visibility = ["//visibility:public"]) + +filegroup(name = "empty") + +bool_flag( + name = "release", + build_setting_default = False, +) + +config_setting( + name = "is_cross_compiling", + flag_values = {":release": "True"}, +) + +[ + platform( + name = name, + constraint_values = config["compatible_with"], + ) + for name, config in PROTOBUF_PLATFORMS.items() +] + +[ + cc_toolchain( + name = name + "-cc_toolchain", + all_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + dynamic_runtime_lib = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + output_licenses = ["restricted"], + static_runtime_lib = ":empty", + strip_files = ":empty", + toolchain_config = ":" + name + "-config", + toolchain_identifier = name + "-cc_toolchain", + ) + for name in PROTOBUF_PLATFORMS.keys() +] + +[ + toolchain( + name = name + "-toolchain", + target_compatible_with = config["compatible_with"], + target_settings = [":is_cross_compiling"], + toolchain = ":" + name + "-cc_toolchain", + toolchain_type = "@rules_cc//cc:toolchain_type", + ) + for name, config in PROTOBUF_PLATFORMS.items() +] + +cc_toolchain_config( + name = "k8-config", + linker_path = "/usr/bin/ld", + sysroot = "/opt/manylinux/2014/x86_64", + target_cpu = "x86_64", + target_full_name = "x86_64-linux-gnu", +) + +cc_toolchain_config( + name = "linux-aarch_64-config", + linker_path = "/usr/bin/ld", + sysroot = "/opt/manylinux/2014/aarch64", + target_cpu = "aarch64", + target_full_name = "aarch64-linux-gnu", +) + +cc_toolchain_config( + name = "linux-ppcle_64-config", + linker_path = "/usr/bin/ld", + sysroot = "/opt/manylinux/2014/ppc64le", + target_cpu = "ppc64", + target_full_name = "powerpc64le-linux-gnu", +) + +cc_toolchain_config( + name = "linux-s390_64-config", + linker_path = "/usr/bin/ld", + sysroot = "/opt/manylinux/2014/s390x", + target_cpu = "systemz", + target_full_name = "s390x-linux-gnu", +) + +cc_toolchain_config( + name = "linux-x86_32-config", + linker_path = "/usr/bin/ld", + sysroot = "/opt/manylinux/2014/i686", + target_cpu = "x86_32", + target_full_name = "i386-linux-gnu", +) + +cc_toolchain_config( + name = "linux-x86_64-config", + linker_path = "/usr/bin/ld", + sysroot = "/opt/manylinux/2014/x86_64", + target_cpu = "x86_64", + target_full_name = "x86_64-linux-gnu", +) + +cc_toolchain_config( + name = "osx-aarch_64-config", + extra_compiler_flags = [ + "-I/usr/tools/xcode_14_0/macosx/usr/include/c++/v1", + "-I/usr/tools/xcode_14_0/macosx/usr/include", + "-F/usr/tools/xcode_14_0/macosx/System/Library/Frameworks", + "-Wno-error=nullability-completeness", + "-Wno-error=availability", + "-Wno-error=elaborated-enum-base", + ], + extra_linker_flags = ["-framework CoreFoundation"], + linker_path = "/usr/tools", + sysroot = "/usr/tools/xcode_14_0/macosx", + target_cpu = "aarch64", + target_full_name = "aarch64-apple-macosx12.0", +) + +cc_toolchain_config( + name = "osx-x86_64-config", + extra_compiler_flags = [ + "-I/usr/tools/xcode_14_0/macosx/usr/include/c++/v1", + "-I/usr/tools/xcode_14_0/macosx/usr/include", + "-F/usr/tools/xcode_14_0/macosx/System/Library/Frameworks", + "-Wno-error=nullability-completeness", + "-Wno-error=availability", + "-Wno-error=elaborated-enum-base", + ], + extra_linker_flags = ["-framework CoreFoundation"], + linker_path = "/usr/tools", + sysroot = "/usr/tools/xcode_14_0/macosx", + target_cpu = "x86_64", + target_full_name = "x86_64-apple-macosx12.0", +) + +cc_toolchain_config( + name = "win32-config", + extra_compiler_flags = [ + "-isystem/usr/lib/gcc/i686-w64-mingw32/10-posix/include/c++", + "-isystem/usr/lib/gcc/i686-w64-mingw32/10-posix/include/c++/i686-w64-mingw32", + ], + extra_include = "/usr/lib/gcc/i686-w64-mingw32", + extra_linker_flags = [ + "-L/usr/lib/gcc/i686-w64-mingw32/10-posix", + "-ldbghelp", + "-pthread", + ], + linker_path = "/usr/bin/ld", + sysroot = "/usr/i686-w64-mingw32", + target_cpu = "x86_32", + target_full_name = "i686-w64-mingw32", +) + +cc_toolchain_config( + name = "win64-config", + extra_compiler_flags = [ + "-isystem/usr/lib/gcc/x86_64-w64-mingw32/10-posix/include/c++/", + "-isystem/usr/lib/gcc/x86_64-w64-mingw32/10-posix/include/c++/x86_64-w64-mingw32", + ], + extra_include = "/usr/lib/gcc/x86_64-w64-mingw32/10-posix/include", + extra_linker_flags = [ + "-L/usr/lib/gcc/x86_64-w64-mingw32/10-posix", + "-ldbghelp", + ], + linker_path = "/usr/bin/ld", + sysroot = "/usr/x86_64-w64-mingw32", + target_cpu = "x86_64", + target_full_name = "x86_64-w64-mingw32", +) diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/cc_toolchain_config.bzl new file mode 100644 index 0000000..aca60b8 --- /dev/null +++ b/toolchain/cc_toolchain_config.bzl @@ -0,0 +1,236 @@ +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") +load( + "@rules_cc//cc:cc_toolchain_config_lib.bzl", + "artifact_name_pattern", + "feature", + "flag_group", + "flag_set", + "tool_path", + "with_feature_set", +) +load("@rules_cc//cc/toolchains:cc_toolchain_config_info.bzl", "CcToolchainConfigInfo") +load("@rules_cc//cc/common:cc_common.bzl", "cc_common") + +all_link_actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, +] + +all_compile_actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.clif_match, + ACTION_NAMES.lto_backend, +] + +def _impl(ctx): + if "mingw" in ctx.attr.target_full_name: + artifact_name_patterns = [ + artifact_name_pattern( + category_name = "executable", + prefix = "", + extension = ".exe", + ), + ] + else: + artifact_name_patterns = [] + + tool_paths = [ + tool_path( + name = "gcc", + path = "/usr/local/bin/clang", + ), + tool_path( + name = "ld", + path = ctx.attr.linker_path, + ), + tool_path( + name = "ar", + path = "/usr/local/bin/llvm-ar", + ), + tool_path( + name = "compat-ld", + path = ctx.attr.linker_path, + ), + tool_path( + name = "cpp", + path = "/bin/false", + ), + tool_path( + name = "dwp", + path = "/bin/false", + ), + tool_path( + name = "gcov", + path = "/bin/false", + ), + tool_path( + name = "nm", + path = "/bin/false", + ), + tool_path( + name = "objcopy", + path = "/bin/false", + ), + tool_path( + name = "objdump", + path = "/bin/false", + ), + tool_path( + name = "strip", + path = "/bin/false", + ), + ] + + linker_flags = feature( + name = "default_linker_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [ + flag_group( + flags = [ + "-B" + ctx.attr.linker_path, + "-lstdc++", + "-lm", + "--target=" + ctx.attr.target_full_name, + ] + ctx.attr.extra_linker_flags, + ), + ], + ), + ], + ) + + if "osx" in ctx.attr.target_full_name: + sysroot_action_set = all_link_actions + else: + sysroot_action_set = all_link_actions + all_compile_actions + + sysroot_flags = feature( + name = "sysroot_flags", + #Only enable this if a sysroot was specified + enabled = (ctx.attr.sysroot != ""), + flag_sets = [ + flag_set( + actions = sysroot_action_set, + flag_groups = [ + flag_group( + flags = [ + "--sysroot", + ctx.attr.sysroot, + ], + ), + ], + ), + ], + ) + + if ctx.attr.target_cpu == "x86_32": + bit_flag = "-m32" + else: + bit_flag = "-m64" + compiler_flags = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = [ + bit_flag, + "-Wall", + "-no-canonical-prefixes", + "--target=" + ctx.attr.target_full_name, + "-fvisibility=hidden", + ] + ctx.attr.extra_compiler_flags + [ + "-isystem", + ctx.attr.sysroot, + ], + ), + ], + ), + flag_set( + actions = all_compile_actions, + flag_groups = [flag_group(flags = ["-DNDEBUG", "-O3"])], + with_features = [with_feature_set(features = ["opt"])], + ), + flag_set( + actions = all_compile_actions, + flag_groups = [flag_group(flags = ["-g"])], + with_features = [with_feature_set(features = ["dbg"])], + ), + flag_set( + actions = all_compile_actions, + flag_groups = [flag_group(flags = ["-O1"])], + with_features = [with_feature_set(features = ["fastbuild"])], + ), + ], + ) + + features = [ + linker_flags, + compiler_flags, + sysroot_flags, + feature(name = "dbg"), + feature(name = "opt"), + ] + + if "mingw" in ctx.attr.target_full_name: + features.append( + feature( + name = "targets_windows", + enabled = True, + ), + ) + else: + features.append( + feature( + name = "supports_pic", + enabled = True, + ), + ) + + return cc_common.create_cc_toolchain_config_info( + abi_libc_version = ctx.attr.abi_version, + abi_version = ctx.attr.abi_version, + artifact_name_patterns = artifact_name_patterns, + ctx = ctx, + compiler = "clang", + cxx_builtin_include_directories = [ + ctx.attr.sysroot, + ctx.attr.extra_include, + "/usr/local/include", + "/usr/local/lib/clang", + ], + features = features, + host_system_name = "local", + target_cpu = ctx.attr.target_cpu, + target_libc = ctx.attr.target_cpu, + target_system_name = ctx.attr.target_full_name, + toolchain_identifier = ctx.attr.target_full_name, + tool_paths = tool_paths, + ) + +cc_toolchain_config = rule( + implementation = _impl, + attrs = { + "abi_version": attr.string(default = "local"), + "extra_compiler_flags": attr.string_list(), + "extra_include": attr.string(mandatory = False), + "extra_linker_flags": attr.string_list(), + "linker_path": attr.string(mandatory = True), + "sysroot": attr.string(mandatory = False), + "target_cpu": attr.string(mandatory = True, values = ["aarch64", "ppc64", "systemz", "x86_32", "x86_64"]), + "target_full_name": attr.string(mandatory = True), + }, + provides = [CcToolchainConfigInfo], +) diff --git a/toolchain/platforms.bzl b/toolchain/platforms.bzl new file mode 100644 index 0000000..dbd386a --- /dev/null +++ b/toolchain/platforms.bzl @@ -0,0 +1,64 @@ +"List of published platforms on protobuf GitHub releases" + +# Keys are chosen to match the filenames published on protocolbuffers/protobuf releases +# NB: keys in this list are nearly identical to /toolchain/BUILD.bazel#TOOLCHAINS +# Perhaps we should share code. +PROTOBUF_PLATFORMS = { + # "k8", # this is in /toolchain/BUILD.bazel but not a released platform + # "osx-universal_binary", # this is not in /toolchain/BUILD.bazel + # but also Bazel will never request it, as we have a darwin binary for each architecture + "linux-aarch_64": { + "compatible_with": [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], + }, + "linux-ppcle_64": { + "compatible_with": [ + "@platforms//os:linux", + "@platforms//cpu:ppc64le", + ], + }, + "linux-s390_64": { + "compatible_with": [ + "@platforms//os:linux", + "@platforms//cpu:s390x", + ], + }, + "linux-x86_32": { + "compatible_with": [ + "@platforms//os:linux", + "@platforms//cpu:x86_32", + ], + }, + "linux-x86_64": { + "compatible_with": [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + }, + "osx-aarch_64": { + "compatible_with": [ + "@platforms//os:macos", + "@platforms//cpu:aarch64", + ], + }, + "osx-x86_64": { + "compatible_with": [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], + }, + "win32": { + "compatible_with": [ + "@platforms//os:windows", + "@platforms//cpu:x86_32", + ], + }, + "win64": { + "compatible_with": [ + "@platforms//os:windows", + "@platforms//cpu:x86_64", + ], + }, +} diff --git a/toolchain/toolchains.bazelrc b/toolchain/toolchains.bazelrc new file mode 100644 index 0000000..b8775ef --- /dev/null +++ b/toolchain/toolchains.bazelrc @@ -0,0 +1,15 @@ +build:cross_config --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 +build:cross_config --//toolchain:release=true + +build:linux-aarch_64 --config=cross_config --platforms=//toolchain:linux-aarch_64 +build:linux-aarch64 --config=cross_config --platforms=//toolchain:linux-aarch_64 +build:linux-ppcle_64 --config=cross_config --platforms=//toolchain:linux-ppcle_64 +build:linux-ppc64le --config=cross_config --platforms=//toolchain:linux-ppcle_64 +build:linux-s390_64 --config=cross_config --platforms=//toolchain:linux-s390_64 +build:linux-s390x --config=cross_config --platforms=//toolchain:linux-s390_64 +build:linux-x86_32 --config=cross_config --platforms=//toolchain:linux-x86_32 +build:linux-x86_64 --config=cross_config --platforms=//toolchain:linux-x86_64 +build:osx-aarch_64 --config=cross_config --action_env=MACOSX_DEPLOYMENT_TARGET=11.0 --platforms=//toolchain:osx-aarch_64 +build:osx-x86_64 --config=cross_config --action_env=MACOSX_DEPLOYMENT_TARGET=12.0 --platforms=//toolchain:osx-x86_64 +build:win32 --config=cross_config --platforms=//toolchain:win32 +build:win64 --config=cross_config --platforms=//toolchain:win64