Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
common --check_direct_dependencies=off
common --noincompatible_disallow_empty_glob

# 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

# 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=@sonic_build_infra//platforms:x86_64_trixie
common:aarch64 --platforms=@sonic_build_infra//platforms:aarch64_trixie
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.0
8.5.1
32 changes: 0 additions & 32 deletions BUILD

This file was deleted.

240 changes: 240 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
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("//tools/bazel:always_link_transition.bzl", "alwayslink_cc_binary")
load("//tools/bazel:flags.bzl", "CXXFLAGS_COMMON")

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

exports_files(["LICENSE"])

swss_common_hdrs = ["//common:hdrs"]

swss_common_deps = [
"@trixie//libhiredis-dev:libhiredis",
"@trixie//nlohmann-json3-dev:nlohmann-json3",
"@libnl3//:libnl_3",
"@libnl3//:libnl_route_3",
"@libnl3//:libnl_nf_3",
"@trixie//libyang-dev:libyang",
"@trixie//libzmq3-dev:libzmq3",
"@trixie//uuid-dev:uuid",
"@trixie//libboost-dev:libboost",
"@trixie//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 2: BCR entries compiled from source
# deps = [
# "@boost.algorithm",
# "@boost.serialization",
# "@nlohmann_json//:json",
# "@libuuid//:libuuid",
# "@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,
}),
)

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",
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",
],
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",
)

# 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"],
cxxopts = CXXFLAGS_COMMON,
linkopts = [
"-Wl,-z,now",
],
linkstatic = True,
deps = ["libswsscommon"],
)

# 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",
package_dir = "usr/share/swss",
strip_prefix = "common",
)

tar(
name = "dist_lua",
srcs = [":all_luas"],
mtree = ":dist_lua_mtree",
)

# 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",
actual = select({
"@platforms//cpu:x86_64": ":dist_loose_x86_64",
"@platforms//cpu:arm64": ":dist_loose_arm64",
}),
)

flatten(
name = "libswsscommon_pkg",
tars = [
":dist_lua",
":dist_loose",
],
visibility = ["//visibility:public"],
)

tar(
name = "sonic-db-cli_pkg",
srcs = [
"//sonic-db-cli",
],
mtree = [
"usr/bin/sonic-db-cli type=file mode=0755 content=$(location //sonic-db-cli:sonic-db-cli)",
],
visibility = ["//visibility:public"],
)
30 changes: 30 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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 = "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.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-01bb79d7eef4eb3ce9511d3dfaf0ab386bd8d12d")
bazel_dep(name = "libnl3", version = "3.7.0.sonic-buildimage")
bazel_dep(name = "sonic-build-infra", version = "0.0.2-8192ea3f8bbecd9982a529e547252b67dcb60804", repo_name = "sonic_build_infra")

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")
use_repo(apt, "trixie")
Loading