Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linker error if go_library with cgo references unresolved symbol #3174

Merged
Merged
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
25 changes: 24 additions & 1 deletion go/tools/builders/cgo2.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,30 @@ func cgo2(goenv *env, goSrcs, cgoSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSr
args = append([]string{cc, "-o", mainBin, mainObj}, cObjs...)
args = append(args, combinedLdFlags...)
if err := goenv.runCommand(args); err != nil {
return "", nil, nil, err
// If linking the binary for cgo fails, this is usually because the
// object files reference external symbols that can't be resolved yet.
// Since the binary is only produced to have its symbols read by the cgo
// command, there is no harm in trying to build it allowing unresolved
// symbols - the real link that happens at the end will fail if they
// rightfully can't be resolved.
var allowUnresolvedSymbolsLdFlag string
switch os.Getenv("GOOS") {
case "windows":
// MinGW's linker doesn't seem to support --unresolved-symbols
// and MSVC isn't supported at all.
return "", nil, nil, err
case "darwin", "ios":
allowUnresolvedSymbolsLdFlag = "-Wl,-undefined,dynamic_lookup"
default:
allowUnresolvedSymbolsLdFlag = "-Wl,--unresolved-symbols=ignore-all"
}
if err2 := goenv.runCommand(append(args, allowUnresolvedSymbolsLdFlag)); err2 != nil {
// Return the original error if we can't link the binary with the
// additional linker flags as they may simply be incorrect for the
// particular compiler/linker pair and would obscure the true reason
// for the failure of the original command.
return "", nil, nil, err
}
}

cgoImportsGo := filepath.Join(workDir, "_cgo_imports.go")
Expand Down
20 changes: 20 additions & 0 deletions tests/core/cgo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,23 @@ go_bazel_test(
name = "external_includes_test",
srcs = ["external_includes_test.go"],
)

go_library(
name = "use_external_symbol",
srcs = ["use_external_symbol.go"],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/core/cgo/use_external_symbol",
tags = ["manual"],
)

go_binary(
name = "provide_external_symbol",
srcs = ["provide_external_symbol.go"],
cgo = True,
target_compatible_with = select({
"@platforms//os:osx": [],
"@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [":use_external_symbol"],
)
12 changes: 12 additions & 0 deletions tests/core/cgo/provide_external_symbol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import "C"

import "github.com/bazelbuild/rules_go/tests/core/cgo/use_external_symbol"

//export external_symbol
func external_symbol() {}

func main() {
use_external_symbol.UseExternalSymbol()
}
10 changes: 10 additions & 0 deletions tests/core/cgo/use_external_symbol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package use_external_symbol

/*
void external_symbol();
*/
import "C"

func UseExternalSymbol() {
C.external_symbol()
}