From 87957bbfd8419fb712d2470709c309caa0497558 Mon Sep 17 00:00:00 2001 From: Borja Lorente Date: Sat, 20 Jun 2026 12:55:37 +0100 Subject: [PATCH 1/7] build: Migrate to Bazel 8.5.1 Signed-off-by: Borja Lorente --- .bazelrc | 6 + .bazelversion | 2 +- BUILD | 32 ----- BUILD.bazel | 222 +++++++++++++++++++++++++++++++ MODULE.bazel | 73 ++++++++++ bazel/BUILD.bazel | 22 +++ bazel/always_link_transition.bzl | 60 +++++++++ bazel/flags.bzl | 61 +++++++++ common/BUILD.bazel | 66 +++++++++ goext/BUILD.bazel | 87 ++++++++++++ pyext/BUILD.bazel | 92 +++++++++++++ pyext/swsscommon.i | 46 +++++++ sonic-db-cli/BUILD.bazel | 17 +++ 13 files changed, 753 insertions(+), 33 deletions(-) create mode 100644 .bazelrc delete mode 100644 BUILD create mode 100644 BUILD.bazel create mode 100644 MODULE.bazel create mode 100644 bazel/BUILD.bazel create mode 100644 bazel/always_link_transition.bzl create mode 100644 bazel/flags.bzl create mode 100644 common/BUILD.bazel create mode 100644 goext/BUILD.bazel create mode 100644 pyext/BUILD.bazel create mode 100644 sonic-db-cli/BUILD.bazel diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 000000000..7aa64918f --- /dev/null +++ b/.bazelrc @@ -0,0 +1,6 @@ +common --check_direct_dependencies=off +common --noincompatible_disallow_empty_glob + +# TODO(bazel-ready): This assumes this repo is a submodule of sonic-buildimage. +common --registry=file://%workspace%/../../tools/bazel/registry +common --registry=https://bcr.bazel.build/ diff --git a/.bazelversion b/.bazelversion index fcdb2e109..f9c71a52e 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1 +1 @@ -4.0.0 +8.5.1 diff --git a/BUILD b/BUILD deleted file mode 100644 index 480c0db5a..000000000 --- a/BUILD +++ /dev/null @@ -1,32 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -exports_files(["LICENSE"]) - -cc_library( - name = "common", - srcs = glob(["common/*.cpp"], exclude=["common/loglevel.cpp", "common/loglevel_util.cpp"]), - hdrs = glob([ - "common/*.h", - "common/*.hpp", - ]), - copts = [ - "-std=c++14", - "-I/usr/include/libnl3", # Expected location in the SONiC build container" - ], - includes = [ - "common", - ], - linkopts = ["-lpthread -lhiredis -lnl-genl-3 -lnl-nf-3 -lnl-route-3 -lnl-3 -lzmq -lboost_serialization -luuid -lyang"], - visibility = ["//visibility:public"], -) - -cc_library( - name = "libswsscommon", - hdrs = glob([ - "common/*.h", - "common/*.hpp", - ]), - include_prefix = "swss", - strip_include_prefix = "common", - deps = [":common"], -) diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 000000000..65153ae46 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,222 @@ +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +load("@rules_distroless//distroless:defs.bzl", "flatten") +load("@tar.bzl", "mtree_mutate", "mtree_spec", "tar") +load("//bazel:always_link_transition.bzl", "alwayslink_cc_binary") +load("//bazel:flags.bzl", "CXXFLAGS_COMMON", "DBGFLAGS") + +package(default_visibility = ["//visibility:public"]) + +exports_files(["LICENSE"]) + +swss_common_hdrs = ["//common:hdrs"] + +swss_common_deps = [ + "@bookworm//libhiredis-dev:libhiredis", + "@bookworm//nlohmann-json3-dev:nlohmann-json3", + "@libnl3//:libnl_3", + "@libnl3//:libnl_route_3", + "@libnl3//:libnl_nf_3", + "@bookworm//libyang2-dev:libyang2", + "@bookworm//libzmq3-dev:libzmq3", + "@bookworm//uuid-dev:uuid", + "@bookworm//libboost-dev:libboost", + "@bookworm//libboost-serialization-dev:libboost-serialization", +] + +filegroup( + name = "hdrs", + srcs = swss_common_hdrs, +) + +# Necessary for when we import swss_common as a prebuilt .so file. +# We want all the deps from swsscommon, but none of the symbols. +# The symbols will come from the prebuilt .so file. +# TODO Remove once the cc_shared_library version of swss_common works with rules_distroless. +cc_library( + name = "common_deps", + deps = swss_common_deps, +) + +# Necessary for when we import swss_common as a dynamic .so file. +# We don't want to include the whole archive when we want to override them with mocks. +# TODO Remove once the cc_shared_library version of swss_common works with rules_distroless. +bool_flag( + name = "alwayslink", + build_setting_default = False, +) + +config_setting( + name = "alwayslink_false", + flag_values = { ":alwayslink": "false" } +) + +config_setting( + name = "alwayslink_true", + flag_values = { ":alwayslink": "true" } +) + +cc_library( + name = "common", + srcs = ["//common:srcs"], + hdrs = swss_common_hdrs, + copts = [ + "-fPIC", + "-std=c++14", + ], + includes = [ + "common", + ], + linkopts = ["-lboost_serialization"], + # Approach 1: + deps = swss_common_deps, + # Approach 2: BCR entries compiled from source + # deps = [ + # "@boost.algorithm", + # "@boost.serialization", + # "@nlohmann_json//:json", + # "@libuuid//:libuuid", + # "@swig//:swig", + # ], + visibility = ["//visibility:public"], + # Force all symbols to be included when linking into shared library + # This is required for the consolidated .so to export all symbols needed by SWIG bindings + # However, some builds like `sonic-swss` will refuse to link if they are always linked, + # because symbols in `libcommon.a` will conflict with test mocks. Hence, the select() + alwayslink = select({ + ":alwayslink_true": True, + ":alwayslink_false": False, + }), +) + +cc_library( + name = "libswsscommon", + hdrs = swss_common_hdrs, + include_prefix = "swss", + strip_include_prefix = "common", + deps = [":common"], +) + +# Consolidated shared library for cgo to avoid argument list too long +# The :common library has alwayslink=True to ensure all symbols are exported +cc_binary( + name = "libswsscommon_consolidated_base", + srcs = [ + # Include static libraries directly to force static linking and bypass linker scripts + "@@rules_distroless++apt+bookworm_libbsd-dev-amd64_0.11.7-2//:usr/lib/x86_64-linux-gnu/libbsd.a", + "@@rules_distroless++apt+bookworm_libmd-dev-amd64_1.0.4-2//:usr/lib/x86_64-linux-gnu/libmd.a", + ], + linkshared = True, + linkopts = [ + "-static-libstdc++", + "-static-libgcc", + # Allow undefined symbols from external runtime deps (hiredis, zmq, etc.) + # These are resolved at runtime when the .so is loaded + "-Wl,--allow-shlib-undefined", + "-Wl,--undefined-version", + # Exclude libbsd from dynamic linking - we use static .a files above + "-Wl,--exclude-libs,libbsd.a", + "-Wl,--exclude-libs,libmd.a", + ], + deps = [":common"], +) + +# If we're building the consolidated binary, we always want to link it. +alwayslink_cc_binary( + name = "libswsscommon_consolidated.so", + binary = "libswsscommon_consolidated_base", +) + +# Alias for compatibility with existing references +alias( + name = "swsscommon_base", + actual = ":common", +) + +# Filegroups for swig bindings and other assets +filegroup( + name = "all_hdrs", + srcs = swss_common_hdrs, +) + +filegroup( + name = "swig_template", + srcs = ["//pyext:swsscommon.i"], +) + +filegroup( + name = "all_luas", + srcs = ["//common:luas"], +) + +# TODO(bazel-ready): CFLAGS_COMMON is not respected yet in many of these targets. + +cc_binary( + name = "swssloglevel", + srcs = ["//common:loglevel_srcs"], + deps = ["libswsscommon"], + linkopts = [ + "-Wl,-z,now", + ], + cxxopts = CXXFLAGS_COMMON, + linkstatic = True, +) + +# libswsscommon deb-style distribution package +# Matches debian/libswsscommon.install: +# usr/lib/*/lib*.so.* +# usr/share/swss/*.lua +# var/run/redis/sonic-db/database_config.json +# usr/bin/swssloglevel + +mtree_spec( + name = "dist_lua_mtree_base", + srcs = [":all_luas"], +) + +mtree_mutate( + name = "dist_lua_mtree", + mtree = ":dist_lua_mtree_base", + strip_prefix = "common", + package_dir = "usr/share/swss", +) + +tar( + name = "dist_lua", + srcs = [":all_luas"], + mtree = ":dist_lua_mtree", +) + +# Loose files: shared library, config, and binaries +tar( + name = "dist_loose", + srcs = [ + ":libswsscommon_consolidated.so", + "//common:database_config.json", + ":swssloglevel", + ], + mtree = [ + "usr/lib/x86_64-linux-gnu/libswsscommon.so type=file content=$(location :libswsscommon_consolidated.so)", + "var/run/redis/sonic-db/database_config.json type=file content=$(location //common:database_config.json)", + "usr/bin/swssloglevel type=file mode=0755 content=$(location :swssloglevel)", + ], +) + +flatten( + name = "libswsscommon_pkg", + tars = [ + ":dist_lua", + ":dist_loose", + ], + visibility = ["//visibility:public"], +) + +tar( + name = "sonic-db-cli_pkg", + srcs = [ + "//sonic-db-cli:sonic-db-cli", + ], + mtree = [ + "usr/bin/sonic-db-cli type=file mode=0755 content=$(location //sonic-db-cli:sonic-db-cli)", + ], + visibility = ["//visibility:public"], +) diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 000000000..7b27b6226 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,73 @@ +# Keep bazel_compatibility in sync with sonic-buildimage's .bazelversion. +module( + name = "sonic-swss-common", + version = "0.0.0", + bazel_compatibility = [">=8.5.1", "<=8.5.1"], +) + +bazel_dep(name = "googletest", version = "1.11.0", repo_name = "com_google_googletest") +bazel_dep(name = "glog", version = "0.5.0", repo_name = "com_github_google_glog") +bazel_dep(name = "rules_python", version = "1.7.0") +bazel_dep(name = "rules_go", version = "0.60.0.sonic-patched", repo_name = "io_bazel_rules_go") +bazel_dep(name = "rules_cc", version = "0.2.8") +bazel_dep(name = "rules_pkg", version = "1.1.0") +bazel_dep(name = "bazel_skylib", version = "1.8.2") +bazel_dep(name = "platforms", version = "1.1.0") + +bazel_dep(name = "tar.bzl", version = "0.8.1.commit-9b76e660363ab2465d89d3aadbbc1f7afa02e65c") +bazel_dep(name = "rules_distroless", version = "0.0.0.commit-ebfd74a2e07b83235e064bb63ace98a1a5223e02") + +bazel_dep(name = "sonic-build-infra", version = "0.0.0", repo_name = "sonic_build_infra") +bazel_dep(name = "libnl3", version = "3.7.0") + +register_toolchains("@sonic_build_infra//toolchains/gcc:host_gcc_toolchain") + +apt = use_extension("@rules_distroless//apt:extensions.bzl", "apt") + +apt.sources_list( + architectures = ["amd64"], + components = ["main"], + suites = ["bookworm", "bookworm-updates"], + uris = ["https://snapshot.debian.org/archive/debian/20251001T023456Z"], +) + +apt.sources_list( + architectures = ["amd64"], + components = ["main"], + suites = ["bookworm-security"], + uris = ["https://snapshot.debian.org/archive/debian-security/20251001T023456Z"], +) + +# sudo apt-get install -y +# make libtool m4 autoconf dh-exec debhelper cmake pkg-config +# nlohmann-json3-dev libhiredis-dev swig3.0 libpython2.7-dev libboost-dev +# libboost-serialization-dev uuid-dev libzmq3-dev +# libnl-3-dev libnl-genl-3-dev libnl-route-3-dev libnl-nf-3-dev -> @libnl3 module + +apt.install( + dependency_set = "bookworm", + suites = [ + "bookworm", + "bookworm-security", + "bookworm-updates", + ], + packages = [ + "nlohmann-json3-dev", + "libhiredis-dev", + "libboost-dev", + "libboost-serialization-dev", + "uuid-dev", + "libzmq3-dev", + "libyang2-dev", + "swig", + "python3-dev" + + # These seem to be unused + # "swig3.0", + # "libpython2.7-dev", + # "libgtest-dev", + # "libgmock-dev", + ] +) + +use_repo(apt, "bookworm") diff --git a/bazel/BUILD.bazel b/bazel/BUILD.bazel new file mode 100644 index 000000000..1a808ed20 --- /dev/null +++ b/bazel/BUILD.bazel @@ -0,0 +1,22 @@ +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") + +package(default_visibility = ["//visibility:public"]) + +# Toggle for YANG-driven generation of cfg_schema.h, mirroring configure.ac's +# --enable/--disable-yangmodules. Defaults on, matching the production deb build +# (rules/swss-common.mk builds libswsscommon without the noyangmod profile). +bool_flag( + name = "yang_modules", + build_setting_default = True, + visibility = ["//visibility:public"], +) + +config_setting( + name = "yang_modules_enabled", + flag_values = {":yang_modules": "True"}, +) + +config_setting( + name = "yang_modules_disabled", + flag_values = {":yang_modules": "False"}, +) diff --git a/bazel/always_link_transition.bzl b/bazel/always_link_transition.bzl new file mode 100644 index 000000000..89636f0b9 --- /dev/null +++ b/bazel/always_link_transition.bzl @@ -0,0 +1,60 @@ +load("@bazel_skylib//lib:paths.bzl", "paths") + +def _alwayslink_transition_impl(settings, attr): + return {"//:alwayslink": True} + +alwayslink_transition = transition( + implementation = _alwayslink_transition_impl, + inputs = [], + outputs = ["//:alwayslink"], +) + +# This implementation is shamelessly stolen from bazel-lib +# https://github.com/bazel-contrib/bazel-lib/blob/main/lib/transitions.bzl +def _transitioned_cc_binary_impl(ctx): + result = [] + + binary = ctx.attr.binary[0] + default_info = binary[DefaultInfo] + files = default_info.files.to_list() + if len(files) != 1: + fail("Please make sure that target {} produces exactly one file".format(ctx.label)) + + original_file = files[0] + + new_file_name = original_file.basename + new_file = ctx.actions.declare_file(paths.join(ctx.label.name, new_file_name)) + + ctx.actions.run_shell( + inputs = [original_file], + outputs = [new_file], + command = "cp {} {}".format(original_file.path, new_file.path), + ) + files = depset(direct = [new_file]) + + result.append( + DefaultInfo( + files = files, + ), + ) + + return result + +alwayslink_cc_binary = rule( + implementation = _transitioned_cc_binary_impl, + doc = """ +Temporary rule to transition a binary with the `alwayslink_transition`. +When building `sonic-swss`, we want _most_ of the build to be built with `--@sonic_swss_common//:alwayslink=False`, +because otherwise we end up with symbols from libcommon all over the codebase. + +However, we want the actual shared library of libswsscommon_consolidated.so to be built with alwayslink=True, +so we must transition that particular target to change the setting. + +This is a temporary rule, which should go away as soon as we have a `cc_shared_library` implementation of libswsscommon. + """, + attrs = { + "binary": attr.label( + cfg = alwayslink_transition, + ), + }, +) diff --git a/bazel/flags.bzl b/bazel/flags.bzl new file mode 100644 index 000000000..e645178ae --- /dev/null +++ b/bazel/flags.bzl @@ -0,0 +1,61 @@ +# CXXFLAGS that we need for Bazel specifically. Not present in the Makefile +CXXFLAGS_COMMON_BAZEL = [ + # TODO(bazel-ready): rules_distroless introduces a bunch of include directories that don't exist + # so we need to disable that warning or else -Werror will fail the build. + "-Wno-missing-include-dirs", +] + +# CFLAGS_COMMON from configure.ac, which is used both for C and C++ +CXXFLAGS_COMMON_MAKEFILE = [ + "-ansi", + "-fPIC", + "-std=c++14", + "-Wall", + "-Wcast-align", + "-Wcast-qual", + "-Wconversion", + "-Wdisabled-optimization", + "-Werror", + "-Wextra", + "-Wfloat-equal", + "-Wformat=2", + "-Wformat-nonliteral", + "-Wformat-security", + "-Wformat-y2k", + "-Wimport", + "-Winit-self", + "-Winvalid-pch", + "-Wlong-long", + "-Wmissing-field-initializers", + "-Wmissing-format-attribute", + "-Wmissing-include-dirs", + "-Wmissing-noreturn", + "-Wno-aggregate-return", + "-Wno-padded", + "-Wno-switch-enum", + "-Wno-unused-parameter", + "-Wpacked", + "-Wpointer-arith", + "-Wredundant-decls", + "-Wshadow", + "-Wstack-protector", + "-Wstrict-aliasing=3", + "-Wswitch", + "-Wswitch-default", + "-Wunreachable-code", + "-Wunused", + "-Wvariadic-macros", + "-Wno-write-strings", + "-Wno-missing-format-attribute", + "-Wno-long-long", + "-fstack-protector-strong", +] + +CXXFLAGS_COMMON = CXXFLAGS_COMMON_MAKEFILE + CXXFLAGS_COMMON_BAZEL + +DBGFLAGS = select({ + "@sonic_build_infra//:debug_enabled": [ + "-ggdb", "-gdwarf-5", + ], + "//conditions:default": ["-g"], +}) diff --git a/common/BUILD.bazel b/common/BUILD.bazel new file mode 100644 index 000000000..c785a1535 --- /dev/null +++ b/common/BUILD.bazel @@ -0,0 +1,66 @@ +package(default_visibility = ["//visibility:public"]) + +# Generated cfg_schema.h lives here in common/, matching common/Makefile.am +# (BUILT_SOURCES = common/cfg_schema.h). schema.h #includes it unconditionally. +# +# When YANG modules are disabled (--//bazel:yang_modules=False) we emit the same +# minimal stub that `./configure --disable-yangmodules` produces. The +# CFG_*_TABLE_NAME macros the library itself uses remain defined inline in schema.h. +# +# TODO BL: real YANG-driven generation (gen_cfg_schema.py) is not wired into the +# Bazel build yet — it needs the YANG toolchain (sonic-yang-mgmt / sonic-yang-models +# / libyang3) in the sonic-buildimage registry plus a python toolchain. Until then, +# building with YANG modules enabled (the default) is deliberately incompatible so it +# fails loudly instead of silently shipping the stub. To build today, pass +# --//bazel:yang_modules=False. +genrule( + name = "cfg_schema_h", + outs = ["cfg_schema.h"], + cmd = """{ +echo '#ifndef CFG_SCHEMA_H' +echo '#define CFG_SCHEMA_H' +echo '' +echo '// Minimal cfg_schema.h generated when YANG modules are disabled' +echo '#ifdef __cplusplus' +echo 'namespace swss {' +echo '#endif' +echo '' +echo '#ifdef __cplusplus' +echo '}' +echo '#endif' +echo '#endif' +} > $@""", + target_compatible_with = select({ + "//bazel:yang_modules_enabled": ["@platforms//:incompatible"], + "//conditions:default": [], + }), +) + +# Library sources, excluding the standalone swssloglevel tool sources. +filegroup( + name = "srcs", + srcs = glob( + ["*.cpp"], + exclude = ["loglevel.cpp", "loglevel_util.cpp"], + ), +) + +# All library headers, including the generated cfg_schema.h. +filegroup( + name = "hdrs", + srcs = glob(["*.h", "*.hpp"], allow_empty = True) + [":cfg_schema_h"], +) + +# swssloglevel standalone tool sources. +filegroup( + name = "loglevel_srcs", + srcs = ["loglevel.cpp", "loglevel_util.cpp", "loglevel.h"], +) + +# Redis Lua scripts shipped in the libswsscommon package. +filegroup( + name = "luas", + srcs = glob(["*.lua"]), +) + +exports_files(["database_config.json"]) diff --git a/goext/BUILD.bazel b/goext/BUILD.bazel new file mode 100644 index 000000000..0b316d830 --- /dev/null +++ b/goext/BUILD.bazel @@ -0,0 +1,87 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("@sonic_build_infra//swig:defs.bzl", "swig_gen_go", "swig_lib_deb") + +# Extract SWIG library files from apt package for hermetic builds +swig_lib_deb( + name = "swig_lib", + data = "@bookworm//swig", + strip_prefix = "usr/share/swig4.0", +) + +swig_gen_go( + name = "swsscommon_go_gen", + interface = "swsscommon.i", + go_out = "swsscommon.go", + cxx_out = "swsscommon_wrap.cxx", + hdr_out = "swsscommon_wrap.h", + hdrs = [ + "//:hdrs", + ], + swig_lib = ":swig_lib", +) + +# Wrapper cc_library that links the consolidated shared library and exposes +# swsscommon headers at their bare names (e.g. "dbconnector.h") for CGO. +cc_library( + name = "swsscommon_cgo_wrapper", + srcs = ["//:libswsscommon_consolidated.so"], + hdrs = ["//:all_hdrs"], + # Strip the "common/" path prefix so CGO can #include "dbconnector.h" etc. + strip_include_prefix = "/common", + deps = [ + "@libnl3//:libnl_3", + "@libnl3//:libnl_route_3", + "@libnl3//:libnl_nf_3", + "@bookworm//libhiredis-dev:libhiredis", + "@bookworm//libzmq3-dev:libzmq3", + "@bookworm//uuid-dev:uuid", + "@bookworm//libboost-dev:libboost", + "@bookworm//libboost-serialization-dev:libboost-serialization", + ], + linkopts = [ + # Allow undefined symbols resolved at runtime from the consolidated .so + "-Wl,--allow-shlib-undefined", + ], + visibility = ["//visibility:public"], +) + +# Compile the SWIG-generated C++ wrapper as a cc_library so that transitive +# include paths (hiredis, boost, etc.) propagate correctly. rules_go CGO does +# not propagate include paths from cc_library deps, so compiling the .cxx +# inside go_library would fail to find transitive headers. +cc_library( + name = "swsscommon_wrap_cc", + srcs = [ + "swsscommon_wrap.cxx", # Generated by swig_gen_go #keep + ], + hdrs = [ + "swsscommon_wrap.h", # Generated by swig_gen_go + ], + deps = [":swsscommon_cgo_wrapper"], + copts = ["-fPIC"], + linkopts = [ + "-Wl,--allow-shlib-undefined", + ], +) + +go_library( + name = "swsscommon", + srcs = [ + "swsscommon.go", # Generated by swig_gen_go + ], + cdeps = [ + ":swsscommon_wrap_cc", + ":swsscommon_cgo_wrapper", + ], + cgo = True, + clinkopts = ["-fuse-ld=bfd"], + importpath = "github.com/sonic-net/sonic-swss-common/goext", + visibility = ["//visibility:public"], +) + +go_test( + name = "swsscommon_test", + srcs = ["swsscommon_test.go"], + embed = [":swsscommon"], + tags = ["manual"], +) diff --git a/pyext/BUILD.bazel b/pyext/BUILD.bazel new file mode 100644 index 000000000..3f9ce14c2 --- /dev/null +++ b/pyext/BUILD.bazel @@ -0,0 +1,92 @@ +load("@rules_pkg//:pkg.bzl", "pkg_deb", "pkg_tar") +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_shared_library") +load("@sonic_build_infra//swig:defs.bzl", "swig_gen", "swig_lib_deb") +load("@sonic_build_infra//python:py_native_library.bzl", "py_native_library") + +# Extract SWIG library files from apt package for hermetic builds +swig_lib_deb( + name = "swig_lib", + data = "@bookworm//swig", + strip_prefix = "usr/share/swig4.0", +) + +exports_files(["swsscommon.i"]) + +swig_gen( + name = "swsscommon_pyy", + interface = "swsscommon.i", + cpp_out = "py3/swsscommon_wrap.cpp", + python_out = "py3/swsscommon.py", + hdrs = [ + "//:hdrs", + ], + swig_lib = ":swig_lib", +) + +cc_library( + name = "swsscommonwrap", + srcs = ["py3/swsscommon_wrap.cpp"], # Generated by SWIG + deps = [ + "//:libswsscommon", + "@bookworm//python3-dev:python3", + ], +) + +cc_binary( + name = "_swsscommon", + srcs = ["py3/swsscommon_wrap.cpp"], # Generated by SWIG + deps = [ + "//:libswsscommon", + "@bookworm//python3-dev:python3", + ], + copts = ["-fvisibility=hidden", "-fPIC"], + linkstatic = 1, + linkshared = 1, +) + + +# Python library wrapping the native swsscommon extension +py_native_library( + name = "swsscommon", + native_so = ":_swsscommon", + native_py = "py3/swsscommon.py", + cc_deps = [ + "//:common", + "@bookworm//python3-dev:python3", + ], + visibility = ["//visibility:public"], +) + +# Backwards compatibility alias +alias( + name = "pyext", + actual = ":swsscommon", + visibility = ["//visibility:public"], +) + + +pkg_tar( + name = "swsscommon_pkg", + srcs = [ + ":swsscommon", + ":_swsscommon" + ], + symlinks = { + # __init__.py looks for _swsscommon.so in the same directory + "_swsscommon.so": "lib_swsscommon.so" + }, + # TODO: exclude lib_swsscommon_lib.a + extension = "tar.gz", + mode = "0755", + package_dir = "/usr/lib/python3/dist-packages/swsscommon", + visibility = ["//visibility:public"] +) + +filegroup( + name = "swsscommon_dist", + srcs = [ + ":swsscommon_pkg", + "@bookworm//python3-dev" + ], + visibility = ["//visibility:public"] +) diff --git a/pyext/swsscommon.i b/pyext/swsscommon.i index 7ad1cba14..ad1a632fd 100644 --- a/pyext/swsscommon.i +++ b/pyext/swsscommon.i @@ -268,6 +268,41 @@ T castSelectableObj(swss::Selectable *temp) %newobject swss::DBConnector::newConnector; %include "schema.h" +// Fix SWIG-generated wrapper code for static constexpr const char* members. +// SWIG generates `char *result` for const char* variables, causing a const +// correctness error in C++. +// - Python: %naturalvar wraps by value (std::string), preserving class attributes. +// - Go: %naturalvar doesn't help, so use %extend/_get() + %ignore instead. +#ifdef SWIGGO +%extend swss::SonicDBConfig { + static const char *DEFAULT_SONIC_DB_CONFIG_FILE_get() { + return swss::SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE; + } + static const char *DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE_get() { + return swss::SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE; + } +} +%ignore swss::SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE; +%ignore swss::SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE; +%extend swss::RedisContext { + static const char *DEFAULT_UNIXSOCKET_get() { + return swss::RedisContext::DEFAULT_UNIXSOCKET; + } +} +%ignore swss::RedisContext::DEFAULT_UNIXSOCKET; +%extend swss::DBConnector { + static const char *DEFAULT_UNIXSOCKET_get() { + return swss::DBConnector::DEFAULT_UNIXSOCKET; + } +} +%ignore swss::DBConnector::DEFAULT_UNIXSOCKET; +#else +%naturalvar swss::SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE; +%naturalvar swss::SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE; +%naturalvar swss::RedisContext::DEFAULT_UNIXSOCKET; +%naturalvar swss::DBConnector::DEFAULT_UNIXSOCKET; +#endif + %include "dbconnector.h" #ifdef ENABLE_YANG_MODULES %include "cfg_schema.h" @@ -290,6 +325,17 @@ T castSelectableObj(swss::Selectable *temp) %include "redisreply.h" %include "redisselect.h" %include "redistran.h" +// See comment on swss::SonicDBConfig for context +#ifdef SWIGGO +%extend swss::ConfigDBConnector_Native { + static const char *INIT_INDICATOR_get() { + return swss::ConfigDBConnector_Native::INIT_INDICATOR; + } +} +%ignore swss::ConfigDBConnector_Native::INIT_INDICATOR; +#else +%naturalvar swss::ConfigDBConnector_Native::INIT_INDICATOR; +#endif %include "configdb.h" %include "zmqserver.h" %include "zmqclient.h" diff --git a/sonic-db-cli/BUILD.bazel b/sonic-db-cli/BUILD.bazel new file mode 100644 index 000000000..8cbb13754 --- /dev/null +++ b/sonic-db-cli/BUILD.bazel @@ -0,0 +1,17 @@ +load("//bazel:flags.bzl", "DBGFLAGS", "CXXFLAGS_COMMON") + +cc_library( + name = "sonicdbcli", + srcs = ["sonic-db-cli.cpp"], + hdrs = ["sonic-db-cli.h"], + deps = ["//:libswsscommon"], + cxxopts = DBGFLAGS + CXXFLAGS_COMMON, +) + +cc_binary( + name = "sonic-db-cli", + srcs = ["main.cpp"], + deps = [":sonicdbcli"], + visibility = ["//visibility:public"], + cxxopts = DBGFLAGS + CXXFLAGS_COMMON, +) From 84638d2cc3aad7f8ea006332a150fbf726e38e63 Mon Sep 17 00:00:00 2001 From: Borja Lorente Date: Sat, 20 Jun 2026 13:38:59 +0100 Subject: [PATCH 2/7] build: Upgrade tar.bzl to 0.10.5 Signed-off-by: Borja Lorente --- MODULE.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MODULE.bazel b/MODULE.bazel index 7b27b6226..f435f5f59 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -14,7 +14,7 @@ bazel_dep(name = "rules_pkg", version = "1.1.0") bazel_dep(name = "bazel_skylib", version = "1.8.2") bazel_dep(name = "platforms", version = "1.1.0") -bazel_dep(name = "tar.bzl", version = "0.8.1.commit-9b76e660363ab2465d89d3aadbbc1f7afa02e65c") +bazel_dep(name = "tar.bzl", version = "0.10.5") bazel_dep(name = "rules_distroless", version = "0.0.0.commit-ebfd74a2e07b83235e064bb63ace98a1a5223e02") bazel_dep(name = "sonic-build-infra", version = "0.0.0", repo_name = "sonic_build_infra") From 3bcf2205c60ef327cfbf8406be026ced29da4996 Mon Sep 17 00:00:00 2001 From: Borja Lorente Date: Thu, 25 Jun 2026 17:54:44 +0100 Subject: [PATCH 3/7] build: Add Bazel build with sonic-bazel-registry Signed-off-by: Borja Lorente --- .bazelrc | 8 ++- BUILD.bazel | 108 +++++++++++++++++------------ MODULE.bazel | 69 ++++-------------- bazel/always_link_transition.bzl | 12 ++-- bazel/flags.bzl | 5 +- common/BUILD.bazel | 28 +++++--- goext/BUILD.bazel | 43 ++++++------ pyext/BUILD.bazel | 56 +++++++-------- sonic-db-cli/BUILD.bazel | 24 ++++--- tests/BUILD | 2 + tools/bazel/buildifier/BUILD.bazel | 22 ++++++ 11 files changed, 201 insertions(+), 176 deletions(-) create mode 100644 tools/bazel/buildifier/BUILD.bazel diff --git a/.bazelrc b/.bazelrc index 7aa64918f..4a5bfbccc 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,6 +1,10 @@ common --check_direct_dependencies=off common --noincompatible_disallow_empty_glob -# TODO(bazel-ready): This assumes this repo is a submodule of sonic-buildimage. -common --registry=file://%workspace%/../../tools/bazel/registry +# Try to read from sonic-bazel-registry first +common --registry=https://raw.githubusercontent.com/blorente/sonic-bazel-registry/main +# If a dependency is not found there, use the Bazel Central Registry common --registry=https://bcr.bazel.build/ + +# Import the per-module override configs shared across sonic-buildimage submodules. +try-import %workspace%/../../tools/bazel/submodule-config.bazelrc diff --git a/BUILD.bazel b/BUILD.bazel index 65153ae46..a60cb7de0 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,8 +1,10 @@ load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +load("@rules_cc//cc:cc_binary.bzl", "cc_binary") +load("@rules_cc//cc:cc_library.bzl", "cc_library") load("@rules_distroless//distroless:defs.bzl", "flatten") load("@tar.bzl", "mtree_mutate", "mtree_spec", "tar") load("//bazel:always_link_transition.bzl", "alwayslink_cc_binary") -load("//bazel:flags.bzl", "CXXFLAGS_COMMON", "DBGFLAGS") +load("//bazel:flags.bzl", "CXXFLAGS_COMMON") package(default_visibility = ["//visibility:public"]) @@ -11,16 +13,16 @@ exports_files(["LICENSE"]) swss_common_hdrs = ["//common:hdrs"] swss_common_deps = [ - "@bookworm//libhiredis-dev:libhiredis", - "@bookworm//nlohmann-json3-dev:nlohmann-json3", + "@trixie//libhiredis-dev:libhiredis", + "@trixie//nlohmann-json3-dev:nlohmann-json3", "@libnl3//:libnl_3", "@libnl3//:libnl_route_3", "@libnl3//:libnl_nf_3", - "@bookworm//libyang2-dev:libyang2", - "@bookworm//libzmq3-dev:libzmq3", - "@bookworm//uuid-dev:uuid", - "@bookworm//libboost-dev:libboost", - "@bookworm//libboost-serialization-dev:libboost-serialization", + "@trixie//libyang-dev:libyang", + "@trixie//libzmq3-dev:libzmq3", + "@trixie//uuid-dev:uuid", + "@trixie//libboost-dev:libboost", + "@trixie//libboost-serialization-dev:libboost-serialization", ] filegroup( @@ -33,8 +35,8 @@ filegroup( # The symbols will come from the prebuilt .so file. # TODO Remove once the cc_shared_library version of swss_common works with rules_distroless. cc_library( - name = "common_deps", - deps = swss_common_deps, + name = "common_deps", + deps = swss_common_deps, ) # Necessary for when we import swss_common as a dynamic .so file. @@ -46,13 +48,13 @@ bool_flag( ) config_setting( - name = "alwayslink_false", - flag_values = { ":alwayslink": "false" } + name = "alwayslink_false", + flag_values = {":alwayslink": "false"}, ) config_setting( - name = "alwayslink_true", - flag_values = { ":alwayslink": "true" } + name = "alwayslink_true", + flag_values = {":alwayslink": "true"}, ) cc_library( @@ -67,8 +69,6 @@ cc_library( "common", ], linkopts = ["-lboost_serialization"], - # Approach 1: - deps = swss_common_deps, # Approach 2: BCR entries compiled from source # deps = [ # "@boost.algorithm", @@ -78,13 +78,15 @@ cc_library( # "@swig//:swig", # ], visibility = ["//visibility:public"], + # Approach 1: + deps = swss_common_deps, # Force all symbols to be included when linking into shared library # This is required for the consolidated .so to export all symbols needed by SWIG bindings # However, some builds like `sonic-swss` will refuse to link if they are always linked, # because symbols in `libcommon.a` will conflict with test mocks. Hence, the select() alwayslink = select({ - ":alwayslink_true": True, - ":alwayslink_false": False, + ":alwayslink_true": True, + ":alwayslink_false": False, }), ) @@ -100,12 +102,6 @@ cc_library( # The :common library has alwayslink=True to ensure all symbols are exported cc_binary( name = "libswsscommon_consolidated_base", - srcs = [ - # Include static libraries directly to force static linking and bypass linker scripts - "@@rules_distroless++apt+bookworm_libbsd-dev-amd64_0.11.7-2//:usr/lib/x86_64-linux-gnu/libbsd.a", - "@@rules_distroless++apt+bookworm_libmd-dev-amd64_1.0.4-2//:usr/lib/x86_64-linux-gnu/libmd.a", - ], - linkshared = True, linkopts = [ "-static-libstdc++", "-static-libgcc", @@ -113,17 +109,15 @@ cc_binary( # These are resolved at runtime when the .so is loaded "-Wl,--allow-shlib-undefined", "-Wl,--undefined-version", - # Exclude libbsd from dynamic linking - we use static .a files above - "-Wl,--exclude-libs,libbsd.a", - "-Wl,--exclude-libs,libmd.a", ], + linkshared = True, deps = [":common"], ) # If we're building the consolidated binary, we always want to link it. alwayslink_cc_binary( - name = "libswsscommon_consolidated.so", - binary = "libswsscommon_consolidated_base", + name = "libswsscommon_consolidated.so", + binary = "libswsscommon_consolidated_base", ) # Alias for compatibility with existing references @@ -153,12 +147,12 @@ filegroup( cc_binary( name = "swssloglevel", srcs = ["//common:loglevel_srcs"], - deps = ["libswsscommon"], + cxxopts = CXXFLAGS_COMMON, linkopts = [ "-Wl,-z,now", ], - cxxopts = CXXFLAGS_COMMON, linkstatic = True, + deps = ["libswsscommon"], ) # libswsscommon deb-style distribution package @@ -176,8 +170,8 @@ mtree_spec( mtree_mutate( name = "dist_lua_mtree", mtree = ":dist_lua_mtree_base", - strip_prefix = "common", package_dir = "usr/share/swss", + strip_prefix = "common", ) tar( @@ -186,19 +180,43 @@ tar( mtree = ":dist_lua_mtree", ) -# Loose files: shared library, config, and binaries -tar( +# Loose files: shared library, config, and binaries. +# +# The library lands in a multiarch directory, and tar()'s mtree attribute must be a +# literal list rather than a select(). So we declare one tar per architecture and +# select between them in the :dist_loose alias below. +DIST_LOOSE_SRCS = [ + ":libswsscommon_consolidated.so", + "//common:database_config.json", + ":swssloglevel", +] + +DIST_LOOSE_COMMON_MTREE = [ + "var/run/redis/sonic-db/database_config.json type=file content=$(location //common:database_config.json)", + "usr/bin/swssloglevel type=file mode=0755 content=$(location :swssloglevel)", +] + +[ + tar( + name = "dist_loose_" + cpu, + srcs = DIST_LOOSE_SRCS, + mtree = [ + "usr/lib/{}/libswsscommon.so type=file content=$(location :libswsscommon_consolidated.so)".format(multiarch), + ] + DIST_LOOSE_COMMON_MTREE, + target_compatible_with = ["@platforms//cpu:" + cpu], + ) + for cpu, multiarch in [ + ("x86_64", "x86_64-linux-gnu"), + ("arm64", "aarch64-linux-gnu"), + ] +] + +alias( name = "dist_loose", - srcs = [ - ":libswsscommon_consolidated.so", - "//common:database_config.json", - ":swssloglevel", - ], - mtree = [ - "usr/lib/x86_64-linux-gnu/libswsscommon.so type=file content=$(location :libswsscommon_consolidated.so)", - "var/run/redis/sonic-db/database_config.json type=file content=$(location //common:database_config.json)", - "usr/bin/swssloglevel type=file mode=0755 content=$(location :swssloglevel)", - ], + actual = select({ + "@platforms//cpu:x86_64": ":dist_loose_x86_64", + "@platforms//cpu:arm64": ":dist_loose_arm64", + }), ) flatten( @@ -213,7 +231,7 @@ flatten( tar( name = "sonic-db-cli_pkg", srcs = [ - "//sonic-db-cli:sonic-db-cli", + "//sonic-db-cli", ], mtree = [ "usr/bin/sonic-db-cli type=file mode=0755 content=$(location //sonic-db-cli:sonic-db-cli)", diff --git a/MODULE.bazel b/MODULE.bazel index f435f5f59..7bde60a28 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -2,72 +2,29 @@ module( name = "sonic-swss-common", version = "0.0.0", - bazel_compatibility = [">=8.5.1", "<=8.5.1"], + bazel_compatibility = [ + ">=8.5.1", + "<=8.5.1", + ], ) +bazel_dep(name = "buildifier_prebuilt", version = "8.5.1.3", dev_dependency = True) + bazel_dep(name = "googletest", version = "1.11.0", repo_name = "com_google_googletest") bazel_dep(name = "glog", version = "0.5.0", repo_name = "com_github_google_glog") bazel_dep(name = "rules_python", version = "1.7.0") bazel_dep(name = "rules_go", version = "0.60.0.sonic-patched", repo_name = "io_bazel_rules_go") bazel_dep(name = "rules_cc", version = "0.2.8") bazel_dep(name = "rules_pkg", version = "1.1.0") -bazel_dep(name = "bazel_skylib", version = "1.8.2") +bazel_dep(name = "bazel_skylib", version = "1.9.0") bazel_dep(name = "platforms", version = "1.1.0") - bazel_dep(name = "tar.bzl", version = "0.10.5") -bazel_dep(name = "rules_distroless", version = "0.0.0.commit-ebfd74a2e07b83235e064bb63ace98a1a5223e02") - -bazel_dep(name = "sonic-build-infra", version = "0.0.0", repo_name = "sonic_build_infra") -bazel_dep(name = "libnl3", version = "3.7.0") +bazel_dep(name = "rules_distroless", version = "0.0.0.commit-01bb79d7eef4eb3ce9511d3dfaf0ab386bd8d12d") +bazel_dep(name = "libnl3", version = "3.7.0.sonic-buildimage") +bazel_dep(name = "sonic-build-infra", version = "0.0.0-d2283ad0aebb0eb78821920635e7f9ab54c6f146", repo_name = "sonic_build_infra") -register_toolchains("@sonic_build_infra//toolchains/gcc:host_gcc_toolchain") +register_toolchains("@sonic_build_infra//toolchains/gcc:all") +# The actual dependencies are listed in sonic-build-infra, and re-exported. apt = use_extension("@rules_distroless//apt:extensions.bzl", "apt") - -apt.sources_list( - architectures = ["amd64"], - components = ["main"], - suites = ["bookworm", "bookworm-updates"], - uris = ["https://snapshot.debian.org/archive/debian/20251001T023456Z"], -) - -apt.sources_list( - architectures = ["amd64"], - components = ["main"], - suites = ["bookworm-security"], - uris = ["https://snapshot.debian.org/archive/debian-security/20251001T023456Z"], -) - -# sudo apt-get install -y -# make libtool m4 autoconf dh-exec debhelper cmake pkg-config -# nlohmann-json3-dev libhiredis-dev swig3.0 libpython2.7-dev libboost-dev -# libboost-serialization-dev uuid-dev libzmq3-dev -# libnl-3-dev libnl-genl-3-dev libnl-route-3-dev libnl-nf-3-dev -> @libnl3 module - -apt.install( - dependency_set = "bookworm", - suites = [ - "bookworm", - "bookworm-security", - "bookworm-updates", - ], - packages = [ - "nlohmann-json3-dev", - "libhiredis-dev", - "libboost-dev", - "libboost-serialization-dev", - "uuid-dev", - "libzmq3-dev", - "libyang2-dev", - "swig", - "python3-dev" - - # These seem to be unused - # "swig3.0", - # "libpython2.7-dev", - # "libgtest-dev", - # "libgmock-dev", - ] -) - -use_repo(apt, "bookworm") +use_repo(apt, "trixie") diff --git a/bazel/always_link_transition.bzl b/bazel/always_link_transition.bzl index 89636f0b9..ae5a3bcff 100644 --- a/bazel/always_link_transition.bzl +++ b/bazel/always_link_transition.bzl @@ -1,6 +1,8 @@ +"""Transition to force `//:alwayslink` on for a single `cc_binary`.""" + load("@bazel_skylib//lib:paths.bzl", "paths") -def _alwayslink_transition_impl(settings, attr): +def _alwayslink_transition_impl(_settings, _attr): return {"//:alwayslink": True} alwayslink_transition = transition( @@ -18,7 +20,7 @@ def _transitioned_cc_binary_impl(ctx): default_info = binary[DefaultInfo] files = default_info.files.to_list() if len(files) != 1: - fail("Please make sure that target {} produces exactly one file".format(ctx.label)) + fail("Please make sure that target {} produces exactly one file".format(ctx.label)) original_file = files[0] @@ -53,8 +55,8 @@ so we must transition that particular target to change the setting. This is a temporary rule, which should go away as soon as we have a `cc_shared_library` implementation of libswsscommon. """, attrs = { - "binary": attr.label( - cfg = alwayslink_transition, - ), + "binary": attr.label( + cfg = alwayslink_transition, + ), }, ) diff --git a/bazel/flags.bzl b/bazel/flags.bzl index e645178ae..b606ff443 100644 --- a/bazel/flags.bzl +++ b/bazel/flags.bzl @@ -1,3 +1,5 @@ +"""Compiler and linker flags shared across the sonic-swss-common build.""" + # CXXFLAGS that we need for Bazel specifically. Not present in the Makefile CXXFLAGS_COMMON_BAZEL = [ # TODO(bazel-ready): rules_distroless introduces a bunch of include directories that don't exist @@ -55,7 +57,8 @@ CXXFLAGS_COMMON = CXXFLAGS_COMMON_MAKEFILE + CXXFLAGS_COMMON_BAZEL DBGFLAGS = select({ "@sonic_build_infra//:debug_enabled": [ - "-ggdb", "-gdwarf-5", + "-ggdb", + "-gdwarf-5", ], "//conditions:default": ["-g"], }) diff --git a/common/BUILD.bazel b/common/BUILD.bazel index c785a1535..d6c3e573d 100644 --- a/common/BUILD.bazel +++ b/common/BUILD.bazel @@ -7,12 +7,11 @@ package(default_visibility = ["//visibility:public"]) # minimal stub that `./configure --disable-yangmodules` produces. The # CFG_*_TABLE_NAME macros the library itself uses remain defined inline in schema.h. # -# TODO BL: real YANG-driven generation (gen_cfg_schema.py) is not wired into the +# TODO(bazel-ready): real YANG-driven generation (gen_cfg_schema.py) is not wired into the # Bazel build yet — it needs the YANG toolchain (sonic-yang-mgmt / sonic-yang-models -# / libyang3) in the sonic-buildimage registry plus a python toolchain. Until then, -# building with YANG modules enabled (the default) is deliberately incompatible so it -# fails loudly instead of silently shipping the stub. To build today, pass -# --//bazel:yang_modules=False. +# / libyang3) in the sonic-buildimage registry plus a python toolchain. +# Until then, building with YANG modules enabled (the default) is deliberately incompatible so it +# fails loudly. To build, pass --//bazel:yang_modules=False. genrule( name = "cfg_schema_h", outs = ["cfg_schema.h"], @@ -41,20 +40,33 @@ filegroup( name = "srcs", srcs = glob( ["*.cpp"], - exclude = ["loglevel.cpp", "loglevel_util.cpp"], + exclude = [ + "loglevel.cpp", + "loglevel_util.cpp", + ], ), ) # All library headers, including the generated cfg_schema.h. filegroup( name = "hdrs", - srcs = glob(["*.h", "*.hpp"], allow_empty = True) + [":cfg_schema_h"], + srcs = glob( + [ + "*.h", + "*.hpp", + ], + allow_empty = True, + ) + [":cfg_schema_h"], ) # swssloglevel standalone tool sources. filegroup( name = "loglevel_srcs", - srcs = ["loglevel.cpp", "loglevel_util.cpp", "loglevel.h"], + srcs = [ + "loglevel.cpp", + "loglevel.h", + "loglevel_util.cpp", + ], ) # Redis Lua scripts shipped in the libswsscommon package. diff --git a/goext/BUILD.bazel b/goext/BUILD.bazel index 0b316d830..b6e1bb953 100644 --- a/goext/BUILD.bazel +++ b/goext/BUILD.bazel @@ -1,22 +1,23 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("@rules_cc//cc:cc_library.bzl", "cc_library") load("@sonic_build_infra//swig:defs.bzl", "swig_gen_go", "swig_lib_deb") # Extract SWIG library files from apt package for hermetic builds swig_lib_deb( name = "swig_lib", - data = "@bookworm//swig", - strip_prefix = "usr/share/swig4.0", + data = "@trixie//swig", + strip_prefix = "usr/share/swig/4.3.0", ) swig_gen_go( name = "swsscommon_go_gen", - interface = "swsscommon.i", - go_out = "swsscommon.go", - cxx_out = "swsscommon_wrap.cxx", - hdr_out = "swsscommon_wrap.h", hdrs = [ "//:hdrs", ], + cxx_out = "swsscommon_wrap.cxx", + go_out = "swsscommon.go", + hdr_out = "swsscommon_wrap.h", + interface = "swsscommon.i", swig_lib = ":swig_lib", ) @@ -26,23 +27,23 @@ cc_library( name = "swsscommon_cgo_wrapper", srcs = ["//:libswsscommon_consolidated.so"], hdrs = ["//:all_hdrs"], + linkopts = [ + # Allow undefined symbols resolved at runtime from the consolidated .so + "-Wl,--allow-shlib-undefined", + ], # Strip the "common/" path prefix so CGO can #include "dbconnector.h" etc. strip_include_prefix = "/common", + visibility = ["//visibility:public"], deps = [ "@libnl3//:libnl_3", - "@libnl3//:libnl_route_3", "@libnl3//:libnl_nf_3", - "@bookworm//libhiredis-dev:libhiredis", - "@bookworm//libzmq3-dev:libzmq3", - "@bookworm//uuid-dev:uuid", - "@bookworm//libboost-dev:libboost", - "@bookworm//libboost-serialization-dev:libboost-serialization", - ], - linkopts = [ - # Allow undefined symbols resolved at runtime from the consolidated .so - "-Wl,--allow-shlib-undefined", + "@libnl3//:libnl_route_3", + "@trixie//libboost-dev:libboost", + "@trixie//libboost-serialization-dev:libboost-serialization", + "@trixie//libhiredis-dev:libhiredis", + "@trixie//libzmq3-dev:libzmq3", + "@trixie//uuid-dev:uuid", ], - visibility = ["//visibility:public"], ) # Compile the SWIG-generated C++ wrapper as a cc_library so that transitive @@ -52,22 +53,22 @@ cc_library( cc_library( name = "swsscommon_wrap_cc", srcs = [ - "swsscommon_wrap.cxx", # Generated by swig_gen_go #keep + "swsscommon_wrap.cxx", # Generated by swig_gen_go #keep ], hdrs = [ - "swsscommon_wrap.h", # Generated by swig_gen_go + "swsscommon_wrap.h", # Generated by swig_gen_go ], - deps = [":swsscommon_cgo_wrapper"], copts = ["-fPIC"], linkopts = [ "-Wl,--allow-shlib-undefined", ], + deps = [":swsscommon_cgo_wrapper"], ) go_library( name = "swsscommon", srcs = [ - "swsscommon.go", # Generated by swig_gen_go + "swsscommon.go", # Generated by swig_gen_go ], cdeps = [ ":swsscommon_wrap_cc", diff --git a/pyext/BUILD.bazel b/pyext/BUILD.bazel index 3f9ce14c2..3d7a72d70 100644 --- a/pyext/BUILD.bazel +++ b/pyext/BUILD.bazel @@ -1,25 +1,26 @@ -load("@rules_pkg//:pkg.bzl", "pkg_deb", "pkg_tar") -load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_shared_library") -load("@sonic_build_infra//swig:defs.bzl", "swig_gen", "swig_lib_deb") +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("@rules_cc//cc:defs.bzl", "cc_binary") +load("@rules_pkg//:pkg.bzl", "pkg_tar") load("@sonic_build_infra//python:py_native_library.bzl", "py_native_library") +load("@sonic_build_infra//swig:defs.bzl", "swig_gen", "swig_lib_deb") # Extract SWIG library files from apt package for hermetic builds swig_lib_deb( name = "swig_lib", - data = "@bookworm//swig", - strip_prefix = "usr/share/swig4.0", + data = "@trixie//swig", + strip_prefix = "usr/share/swig/4.3.0", ) exports_files(["swsscommon.i"]) swig_gen( name = "swsscommon_pyy", - interface = "swsscommon.i", - cpp_out = "py3/swsscommon_wrap.cpp", - python_out = "py3/swsscommon.py", hdrs = [ - "//:hdrs", + "//:hdrs", ], + cpp_out = "py3/swsscommon_wrap.cpp", + interface = "swsscommon.i", + python_out = "py3/swsscommon.py", swig_lib = ":swig_lib", ) @@ -28,32 +29,34 @@ cc_library( srcs = ["py3/swsscommon_wrap.cpp"], # Generated by SWIG deps = [ "//:libswsscommon", - "@bookworm//python3-dev:python3", + "@trixie//python3-dev:python3", ], ) cc_binary( name = "_swsscommon", srcs = ["py3/swsscommon_wrap.cpp"], # Generated by SWIG + copts = [ + "-fvisibility=hidden", + "-fPIC", + ], + linkshared = 1, + linkstatic = 1, deps = [ "//:libswsscommon", - "@bookworm//python3-dev:python3", + "@trixie//python3-dev:python3", ], - copts = ["-fvisibility=hidden", "-fPIC"], - linkstatic = 1, - linkshared = 1, ) - # Python library wrapping the native swsscommon extension py_native_library( name = "swsscommon", - native_so = ":_swsscommon", - native_py = "py3/swsscommon.py", cc_deps = [ "//:common", - "@bookworm//python3-dev:python3", + "@trixie//python3-dev:python3", ], + native_py = "py3/swsscommon.py", + native_so = ":_swsscommon", visibility = ["//visibility:public"], ) @@ -64,29 +67,28 @@ alias( visibility = ["//visibility:public"], ) - pkg_tar( name = "swsscommon_pkg", srcs = [ + ":_swsscommon", ":swsscommon", - ":_swsscommon" ], - symlinks = { - # __init__.py looks for _swsscommon.so in the same directory - "_swsscommon.so": "lib_swsscommon.so" - }, # TODO: exclude lib_swsscommon_lib.a extension = "tar.gz", mode = "0755", package_dir = "/usr/lib/python3/dist-packages/swsscommon", - visibility = ["//visibility:public"] + symlinks = { + # __init__.py looks for _swsscommon.so in the same directory + "_swsscommon.so": "lib_swsscommon.so", + }, + visibility = ["//visibility:public"], ) filegroup( name = "swsscommon_dist", srcs = [ ":swsscommon_pkg", - "@bookworm//python3-dev" + "@trixie//python3-dev", ], - visibility = ["//visibility:public"] + visibility = ["//visibility:public"], ) diff --git a/sonic-db-cli/BUILD.bazel b/sonic-db-cli/BUILD.bazel index 8cbb13754..f447dcc46 100644 --- a/sonic-db-cli/BUILD.bazel +++ b/sonic-db-cli/BUILD.bazel @@ -1,17 +1,19 @@ -load("//bazel:flags.bzl", "DBGFLAGS", "CXXFLAGS_COMMON") +load("@rules_cc//cc:cc_binary.bzl", "cc_binary") +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//bazel:flags.bzl", "CXXFLAGS_COMMON", "DBGFLAGS") cc_library( - name = "sonicdbcli", - srcs = ["sonic-db-cli.cpp"], - hdrs = ["sonic-db-cli.h"], - deps = ["//:libswsscommon"], - cxxopts = DBGFLAGS + CXXFLAGS_COMMON, + name = "sonicdbcli", + srcs = ["sonic-db-cli.cpp"], + hdrs = ["sonic-db-cli.h"], + cxxopts = DBGFLAGS + CXXFLAGS_COMMON, + deps = ["//:libswsscommon"], ) cc_binary( - name = "sonic-db-cli", - srcs = ["main.cpp"], - deps = [":sonicdbcli"], - visibility = ["//visibility:public"], - cxxopts = DBGFLAGS + CXXFLAGS_COMMON, + name = "sonic-db-cli", + srcs = ["main.cpp"], + cxxopts = DBGFLAGS + CXXFLAGS_COMMON, + visibility = ["//visibility:public"], + deps = [":sonicdbcli"], ) diff --git a/tests/BUILD b/tests/BUILD index d9799b163..08d38ccb9 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -1,3 +1,5 @@ +load("@rules_cc//cc:cc_test.bzl", "cc_test") + package(default_visibility = ["//visibility:public"]) cc_test( diff --git a/tools/bazel/buildifier/BUILD.bazel b/tools/bazel/buildifier/BUILD.bazel new file mode 100644 index 000000000..785087862 --- /dev/null +++ b/tools/bazel/buildifier/BUILD.bazel @@ -0,0 +1,22 @@ +# buildifier lives in its own package so the root package does not have to +# load @buildifier_prebuilt. +# Otherwise everyone would have to depend on buildifier_prebuilt. +load("@buildifier_prebuilt//:rules.bzl", "buildifier") + +buildifier( + name = "buildifier", + exclude_patterns = [ + "./.git/*", + ], + lint_mode = "fix", + mode = "fix", +) + +buildifier( + name = "buildifier.check", + exclude_patterns = [ + "./.git/*", + ], + lint_mode = "warn", + mode = "diff", +) From 4b0cc52b134d1ef249e082aa0eb7b4738b5fe07c Mon Sep 17 00:00:00 2001 From: Borja Lorente Date: Thu, 6 Aug 2026 16:21:37 +0100 Subject: [PATCH 4/7] fix: move //bazel to //tools/bazel Signed-off-by: Borja Lorente --- BUILD.bazel | 4 ++-- common/BUILD.bazel | 6 +++--- sonic-db-cli/BUILD.bazel | 2 +- {bazel => tools/bazel}/BUILD.bazel | 0 {bazel => tools/bazel}/always_link_transition.bzl | 0 {bazel => tools/bazel}/flags.bzl | 0 6 files changed, 6 insertions(+), 6 deletions(-) rename {bazel => tools/bazel}/BUILD.bazel (100%) rename {bazel => tools/bazel}/always_link_transition.bzl (100%) rename {bazel => tools/bazel}/flags.bzl (100%) diff --git a/BUILD.bazel b/BUILD.bazel index a60cb7de0..f870690d3 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -3,8 +3,8 @@ load("@rules_cc//cc:cc_binary.bzl", "cc_binary") load("@rules_cc//cc:cc_library.bzl", "cc_library") load("@rules_distroless//distroless:defs.bzl", "flatten") load("@tar.bzl", "mtree_mutate", "mtree_spec", "tar") -load("//bazel:always_link_transition.bzl", "alwayslink_cc_binary") -load("//bazel:flags.bzl", "CXXFLAGS_COMMON") +load("//tools/bazel:always_link_transition.bzl", "alwayslink_cc_binary") +load("//tools/bazel:flags.bzl", "CXXFLAGS_COMMON") package(default_visibility = ["//visibility:public"]) diff --git a/common/BUILD.bazel b/common/BUILD.bazel index d6c3e573d..03bc6ca9c 100644 --- a/common/BUILD.bazel +++ b/common/BUILD.bazel @@ -3,7 +3,7 @@ package(default_visibility = ["//visibility:public"]) # Generated cfg_schema.h lives here in common/, matching common/Makefile.am # (BUILT_SOURCES = common/cfg_schema.h). schema.h #includes it unconditionally. # -# When YANG modules are disabled (--//bazel:yang_modules=False) we emit the same +# When YANG modules are disabled (--//tools/bazel:yang_modules=False) we emit the same # minimal stub that `./configure --disable-yangmodules` produces. The # CFG_*_TABLE_NAME macros the library itself uses remain defined inline in schema.h. # @@ -11,7 +11,7 @@ package(default_visibility = ["//visibility:public"]) # Bazel build yet — it needs the YANG toolchain (sonic-yang-mgmt / sonic-yang-models # / libyang3) in the sonic-buildimage registry plus a python toolchain. # Until then, building with YANG modules enabled (the default) is deliberately incompatible so it -# fails loudly. To build, pass --//bazel:yang_modules=False. +# fails loudly. To build, pass --//tools/bazel:yang_modules=False. genrule( name = "cfg_schema_h", outs = ["cfg_schema.h"], @@ -30,7 +30,7 @@ echo '#endif' echo '#endif' } > $@""", target_compatible_with = select({ - "//bazel:yang_modules_enabled": ["@platforms//:incompatible"], + "//tools/bazel:yang_modules_enabled": ["@platforms//:incompatible"], "//conditions:default": [], }), ) diff --git a/sonic-db-cli/BUILD.bazel b/sonic-db-cli/BUILD.bazel index f447dcc46..7d04c2e9b 100644 --- a/sonic-db-cli/BUILD.bazel +++ b/sonic-db-cli/BUILD.bazel @@ -1,6 +1,6 @@ load("@rules_cc//cc:cc_binary.bzl", "cc_binary") load("@rules_cc//cc:cc_library.bzl", "cc_library") -load("//bazel:flags.bzl", "CXXFLAGS_COMMON", "DBGFLAGS") +load("//tools/bazel:flags.bzl", "CXXFLAGS_COMMON", "DBGFLAGS") cc_library( name = "sonicdbcli", diff --git a/bazel/BUILD.bazel b/tools/bazel/BUILD.bazel similarity index 100% rename from bazel/BUILD.bazel rename to tools/bazel/BUILD.bazel diff --git a/bazel/always_link_transition.bzl b/tools/bazel/always_link_transition.bzl similarity index 100% rename from bazel/always_link_transition.bzl rename to tools/bazel/always_link_transition.bzl diff --git a/bazel/flags.bzl b/tools/bazel/flags.bzl similarity index 100% rename from bazel/flags.bzl rename to tools/bazel/flags.bzl From 9d07b96270c0af8f9c4913c7da4ddcec67da7165 Mon Sep 17 00:00:00 2001 From: Borja Lorente Date: Thu, 6 Aug 2026 17:10:24 +0100 Subject: [PATCH 5/7] chore: Bump sonic-build-infra Signed-off-by: Borja Lorente --- MODULE.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MODULE.bazel b/MODULE.bazel index 7bde60a28..d828c2960 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -21,7 +21,7 @@ bazel_dep(name = "platforms", version = "1.1.0") bazel_dep(name = "tar.bzl", version = "0.10.5") bazel_dep(name = "rules_distroless", version = "0.0.0.commit-01bb79d7eef4eb3ce9511d3dfaf0ab386bd8d12d") bazel_dep(name = "libnl3", version = "3.7.0.sonic-buildimage") -bazel_dep(name = "sonic-build-infra", version = "0.0.0-d2283ad0aebb0eb78821920635e7f9ab54c6f146", repo_name = "sonic_build_infra") +bazel_dep(name = "sonic-build-infra", version = "0.0.1-5b8535804cae4eade881b378016a6f297039ee82", repo_name = "sonic_build_infra") register_toolchains("@sonic_build_infra//toolchains/gcc:all") From 6951ca0d3bb85911ff8c7beb501b1ffcfda077ed Mon Sep 17 00:00:00 2001 From: Borja Lorente Date: Thu, 6 Aug 2026 18:20:21 +0100 Subject: [PATCH 6/7] fix: Add sonic-build-infra platforms Signed-off-by: Borja Lorente --- .bazelrc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.bazelrc b/.bazelrc index 4a5bfbccc..b427bd1e5 100644 --- a/.bazelrc +++ b/.bazelrc @@ -8,3 +8,9 @@ common --registry=https://bcr.bazel.build/ # Import the per-module override configs shared across sonic-buildimage submodules. try-import %workspace%/../../tools/bazel/submodule-config.bazelrc + +# Build all release binaries for `trixie`. +# These are host toolchains (exec == target == $cpu), +# so --config=aarch64 only resolves when Bazel itself is running on an aarch64 machine. +common --platforms=//platforms:x86_64_trixie +common:aarch64 --platforms=//platforms:aarch64_trixie From 394fb30d1e8ed5807885011b73cd32564272dfe8 Mon Sep 17 00:00:00 2001 From: Borja Lorente Date: Thu, 6 Aug 2026 18:21:24 +0100 Subject: [PATCH 7/7] chore: Bump sonic-build-infra Signed-off-by: Borja Lorente --- .bazelrc | 4 ++-- MODULE.bazel | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.bazelrc b/.bazelrc index b427bd1e5..df6877aec 100644 --- a/.bazelrc +++ b/.bazelrc @@ -12,5 +12,5 @@ try-import %workspace%/../../tools/bazel/submodule-config.bazelrc # Build all release binaries for `trixie`. # These are host toolchains (exec == target == $cpu), # so --config=aarch64 only resolves when Bazel itself is running on an aarch64 machine. -common --platforms=//platforms:x86_64_trixie -common:aarch64 --platforms=//platforms:aarch64_trixie +common --platforms=@sonic_build_infra//platforms:x86_64_trixie +common:aarch64 --platforms=@sonic_build_infra//platforms:aarch64_trixie diff --git a/MODULE.bazel b/MODULE.bazel index d828c2960..b41e53fec 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -21,7 +21,7 @@ bazel_dep(name = "platforms", version = "1.1.0") bazel_dep(name = "tar.bzl", version = "0.10.5") bazel_dep(name = "rules_distroless", version = "0.0.0.commit-01bb79d7eef4eb3ce9511d3dfaf0ab386bd8d12d") bazel_dep(name = "libnl3", version = "3.7.0.sonic-buildimage") -bazel_dep(name = "sonic-build-infra", version = "0.0.1-5b8535804cae4eade881b378016a6f297039ee82", repo_name = "sonic_build_infra") +bazel_dep(name = "sonic-build-infra", version = "0.0.2-8192ea3f8bbecd9982a529e547252b67dcb60804", repo_name = "sonic_build_infra") register_toolchains("@sonic_build_infra//toolchains/gcc:all")