Skip to content
Draft
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
3 changes: 3 additions & 0 deletions bazel/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//bazel:proto_descriptor_set.bzl", "proto_descriptor_set")
load("//bazel:proto_library.bzl", "proto_library")
load(":bazel_proto_library_tests.bzl", "bazel_proto_library_test_suite")
load(":cc_proto_library_tests.bzl", "cc_proto_library_test_suite")
load(":cc_toolchain_tests.bzl", "cc_toolchain_test_suite")
load(":java_proto_library_tests.bzl", "java_proto_library_test_suite")
load(":proto_common_check_collocated_tests.bzl", "proto_common_check_collocated_test_suite")
Expand All @@ -28,6 +29,8 @@ java_proto_library_test_suite(name = "java_proto_library_test_suite")

py_proto_library_test_suite(name = "py_proto_library_test_suite")

cc_proto_library_test_suite(name = "cc_proto_library_tests")

proto_library(
name = "empty_proto_library",
)
Expand Down
126 changes: 126 additions & 0 deletions bazel/tests/cc_proto_library_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Protocol Buffers - Google's data interchange format
# Copyright 2025 Google Inc. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
"""Tests for `cc_proto_library`."""

load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:util.bzl", "util")
load("//bazel:cc_proto_library.bzl", "cc_proto_library")
load("//bazel:proto_library.bzl", "proto_library")

def cc_proto_library_test_suite(name):
test_suite(
name = name,
tests = [
_test_link_order_with_mixed_deps,
_test_link_order_with_mixed_deps_and_intermediate_library,
_test_link_order_with_mixed_deps_and_linkshared,
],
)

def _test_link_order_with_mixed_deps(name):
util.helper_target(cc_library, name = name + "_a", srcs = ["a.cc"])
util.helper_target(proto_library, name = name + "_b", srcs = ["b.proto"])
util.helper_target(cc_proto_library, name = name + "_bc", deps = [":" + name + "_b"])
util.helper_target(cc_library, name = name + "_c", srcs = ["c.cc"])
util.helper_target(
cc_binary,
name = name + "_foo",
srcs = ["foo.cc"],
deps = [
":" + name + "_a",
":" + name + "_bc",
":" + name + "_c",
],
)

analysis_test(
name = name,
target = ":" + name + "_foo",
impl = _test_link_order_with_mixed_deps_impl,
)

def _test_link_order_with_mixed_deps_impl(env, target):
action = env.expect.that_target(target).action_named("CppLink")
action.inputs().contains_at_least_predicates([
matching.file_basename_equals("foo.pic.o"),
matching.file_basename_equals("a.pic.o"),
matching.file_basename_equals("b.pb.pic.o"),
matching.file_basename_equals("c.pic.o"),
]).in_order()

def _test_link_order_with_mixed_deps_and_intermediate_library(name):
util.helper_target(cc_library, name = name + "_a", srcs = ["a.cc"])
util.helper_target(proto_library, name = name + "_b", srcs = ["b.proto"])
util.helper_target(cc_proto_library, name = name + "_bc", deps = [":" + name + "_b"])
util.helper_target(cc_library, name = name + "_c", srcs = ["c.cc"])
util.helper_target(
cc_library,
name = name + "_lib",
srcs = ["lib.cc"],
deps = [
":" + name + "_a",
":" + name + "_bc",
":" + name + "_c",
],
)
util.helper_target(
cc_binary,
name = name + "_foo",
srcs = ["foo.cc"],
deps = [":" + name + "_lib"],
)

analysis_test(
name = name,
target = ":" + name + "_foo",
impl = _test_link_order_with_mixed_deps_and_intermediate_library_impl,
)

def _test_link_order_with_mixed_deps_and_intermediate_library_impl(env, target):
action = env.expect.that_target(target).action_named("CppLink")
action.inputs().contains_at_least_predicates([
matching.file_basename_equals("foo.pic.o"),
matching.file_basename_equals("lib.pic.o"),
matching.file_basename_equals("a.pic.o"),
matching.file_basename_equals("b.pb.pic.o"),
matching.file_basename_equals("c.pic.o"),
]).in_order()

def _test_link_order_with_mixed_deps_and_linkshared(name):
util.helper_target(cc_library, name = name + "_a", srcs = ["a.cc"])
util.helper_target(proto_library, name = name + "_b", srcs = ["b.proto"])
util.helper_target(cc_proto_library, name = name + "_bc", deps = [":" + name + "_b"])
util.helper_target(cc_library, name = name + "_c", srcs = ["c.cc"])
util.helper_target(
cc_binary,
name = name + "_foo",
srcs = ["foo.cc"],
linkshared = 1,
deps = [
":" + name + "_a",
":" + name + "_bc",
":" + name + "_c",
],
)

analysis_test(
name = name,
target = ":" + name + "_foo",
impl = _test_link_order_with_mixed_deps_and_linkshared_impl,
)

def _test_link_order_with_mixed_deps_and_linkshared_impl(env, target):
action = env.expect.that_target(target).action_named("CppLink")
action.inputs().contains_at_least_predicates([
matching.file_basename_equals("foo.pic.o"),
matching.file_basename_equals("a.pic.o"),
matching.file_basename_equals("b.pb.pic.o"),
matching.file_basename_equals("c.pic.o"),
]).in_order()
Loading