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

CGO merged directory should have usable name. #2930

Merged
merged 4 commits into from
Sep 23, 2021
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
9 changes: 9 additions & 0 deletions go/tools/builders/cgo2.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ func cgo2(goenv *env, goSrcs, cgoSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSr
}
defer cleanup()

// cgo2 will gather sources into a single temporary directory, since nogo
// scanners might want to include or exclude these sources we need to ensure
// that a fragment of the path is stable and human friendly enough to be
// referenced in nogo configuration.
workDir = filepath.Join(workDir, "cgo", packagePath)
if err := os.MkdirAll(workDir, 0700); err != nil {
return "", nil, nil, err
}

// Filter out -lstdc++ and -lc++ from ldflags if we don't have C++ sources,
// and set CGO_LDFLAGS. These flags get written as special comments into cgo
// generated sources. The compiler encodes those flags in the compiled .a
Expand Down
41 changes: 40 additions & 1 deletion tests/core/nogo/custom/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ go_library(
deps = [":dep"],
)

go_library(
name = "uses_cgo_with_errors",
srcs = [
"examplepkg/uses_cgo_clean.go",
"examplepkg/pure_src_with_err_calling_native.go",
],
importpath = "examplepkg",
cgo = True,
)

go_library(
name = "no_errors",
srcs = ["no_errors.go"],
Expand Down Expand Up @@ -336,6 +346,26 @@ package dep
func D() {
}

-- examplepkg/uses_cgo_clean.go --
package examplepkg

// #include <stdlib.h>
import "C"

func Bar() bool {
if C.rand() > 10 {
return true
}
return false
}

-- examplepkg/pure_src_with_err_calling_native.go --
package examplepkg

func Foo() bool { // This should fail foofuncname
return Bar()
}

`,
})
}
Expand Down Expand Up @@ -388,6 +418,15 @@ func Test(t *testing.T) {
excludes: []string{
`importfmt`,
},
}, {
desc: "uses_cgo_with_errors",
config: "config.json",
target: "//:uses_cgo_with_errors",
wantSuccess: false,
includes: []string{
// note the cross platform regex :)
`.*[\\/]cgo[\\/]examplepkg[\\/]pure_src_with_err_calling_native.go:.*function must not be named Foo \(foofuncname\)`,
},
}, {
desc: "no_errors",
target: "//:no_errors",
Expand Down Expand Up @@ -417,7 +456,7 @@ func Test(t *testing.T) {
if matched, err := regexp.Match(pattern, stderr.Bytes()); err != nil {
t.Fatal(err)
} else if !matched {
t.Errorf("output did not contain pattern: %s", pattern)
t.Errorf("got output:\n %s\n which does not contain pattern: %s", string(stderr.Bytes()), pattern)
}
}
for _, pattern := range test.excludes {
Expand Down