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: 11 additions & 5 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,17 @@ def _merge_embed(source, embed):
if source["cgo"]:
fail("multiple libraries with cgo enabled")
source["cgo"] = s.cgo
source["cdeps"] = s.cdeps
source["cppopts"] = s.cppopts
source["copts"] = s.copts
source["cxxopts"] = s.cxxopts
source["clinkopts"] = s.clinkopts

# Merge cgo-related attributes if the embedded library has them,
# even if it doesn't have cgo=True itself. This is necessary because
# a library can have cdeps without cgo=True if it embeds another library
# that has cgo=True. The cdeps must propagate through the embed chain.
if s.cdeps or s.cppopts or s.copts or s.cxxopts or s.clinkopts:
source["cdeps"] = source["cdeps"] or s.cdeps
source["cppopts"] = source["cppopts"] or s.cppopts
source["copts"] = source["copts"] or s.copts
source["cxxopts"] = source["cxxopts"] or s.cxxopts
source["clinkopts"] = source["clinkopts"] or s.clinkopts

def _dedup_archives(archives):
"""Returns a list of archives without duplicate import paths.
Expand Down
28 changes: 28 additions & 0 deletions tests/core/cgo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ cc_library(
hdrs = ["native_dep.h"],
# Force static linking to ensure that the build doesn't succeed by
# accidentally picking up the shared library in the search path.
alwayslink = True,
linkstatic = True,
)

Expand Down Expand Up @@ -556,3 +557,30 @@ go_bazel_test(
"//conditions:default": ["@platforms//:incompatible"],
}),
)

# Regression test for cdeps propagation through embed chains.
# Tests that when a go_library with cdeps embeds another go_library with cgo=True,
# and a go_test embeds the library with cdeps, the cdeps are properly propagated.
# This was broken in rules_go v0.51.0+ and fixed in this commit.
go_library(
name = "cdeps_embed_base",
srcs = [
"cdeps_embed_base.go",
"cdeps_embed.h",
],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/core/cgo/cdeps_embed",
)

go_library(
name = "cdeps_embed_with_cdeps",
cdeps = [":native_dep"],
embed = [":cdeps_embed_base"],
importpath = "github.com/bazelbuild/rules_go/tests/core/cgo/cdeps_embed",
)

go_test(
name = "cdeps_embed_test",
srcs = ["cdeps_embed_test.go"],
embed = [":cdeps_embed_with_cdeps"],
)
6 changes: 6 additions & 0 deletions tests/core/cgo/cdeps_embed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef CDEPS_EMBED_H
#define CDEPS_EMBED_H

void native_greeting(void);

#endif
8 changes: 8 additions & 0 deletions tests/core/cgo/cdeps_embed_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cdeps_embed

// #include "cdeps_embed.h"
import "C"

func CallNative() {
C.native_greeting()
}
15 changes: 15 additions & 0 deletions tests/core/cgo/cdeps_embed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cdeps_embed

import "testing"

// TestCdepsEmbedPropagation tests that cdeps are properly propagated
// when a go_library with cdeps embeds another go_library with cgo=True,
// and a go_test embeds the library with cdeps.
//
// This is a regression test for the issue where cdeps were not being
// propagated through embed chains in rules_go v0.51.0+.
func TestCdepsEmbedPropagation(t *testing.T) {
// If cdeps are properly propagated, this will link successfully
// and call the C function from native_dep.
CallNative()
}