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

Append path into trimpath if option already exists #2994

Merged
merged 2 commits into from
Nov 30, 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
6 changes: 6 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ tasks:
- "-//tests/core/go_plugin:go_plugin"
- "-//tests/core/go_plugin:go_default_test"
- "-//tests/core/go_plugin:plugin"
- "-//tests/core/go_plugin_with_proto_library:go_plugin_with_proto_library"
- "-//tests/core/go_plugin_with_proto_library:go_default_test"
- "-//tests/core/go_plugin_with_proto_library:plugin"
- "-//tests/core/go_proto_library:all"
- "-//tests/core/go_proto_library_importmap:foo_go_proto"
- "-//tests/core/go_proto_library_importmap:foo_proto"
Expand Down Expand Up @@ -299,6 +302,9 @@ tasks:
- "-//tests/core/go_plugin:go_plugin"
- "-//tests/core/go_plugin:go_default_test"
- "-//tests/core/go_plugin:plugin"
- "-//tests/core/go_plugin_with_proto_library:go_plugin_with_proto_library"
- "-//tests/core/go_plugin_with_proto_library:go_default_test"
- "-//tests/core/go_plugin_with_proto_library:plugin"
- "-//tests/core/go_proto_library:all"
- "-//tests/core/go_proto_library_importmap:importmap_test"
- "-//tests/core/go_test:data_test"
Expand Down
14 changes: 12 additions & 2 deletions go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ func compileArchive(
return err
}

gcFlags = append(gcFlags, "-trimpath="+srcDir)
gcFlags = append(gcFlags, createTrimPath(gcFlags, srcDir))
} else {
if cgoExportHPath != "" {
if err := ioutil.WriteFile(cgoExportHPath, nil, 0666); err != nil {
return err
}
}
gcFlags = append(gcFlags, "-trimpath=.")
gcFlags = append(gcFlags, createTrimPath(gcFlags, "."))
}

// Check that the filtered sources don't import anything outside of
Expand Down Expand Up @@ -511,6 +511,16 @@ func runNogo(ctx context.Context, workDir string, nogoPath string, srcs []string
return nil
}

func createTrimPath(gcFlags []string, path string) string {
for _, flag := range gcFlags {
if strings.HasPrefix(flag, "-trimpath=") {
return flag + ":" + path
}
}

return "-trimpath=" + path
}

func sanitizePathForIdentifier(path string) string {
return strings.Map(func(r rune) rune {
if 'A' <= r && r <= 'Z' ||
Expand Down
31 changes: 31 additions & 0 deletions tests/core/go_plugin_with_proto_library/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

test_suite(name = "go_plugin_with_proto_library")

go_test(
name = "go_default_test",
srcs = ["all_test.go"],
data = [":plugin"],
deps = [":validate"],
)

go_binary(
name = "plugin",
srcs = ["plugin.go"],
out = "plugin.so",
deps = [":validate"],
linkmode = "plugin",
)

proto_library(
name = "validate_proto",
srcs = ["validate.proto"],
)

go_proto_library(
name = "validate",
importpath = "go_plugin_with_proto_library/validate",
proto = ":validate_proto",
gc_goopts = ["-trimpath=$(BINDIR)=>."],
)
16 changes: 16 additions & 0 deletions tests/core/go_plugin_with_proto_library/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Go Plugin supporting protobufs
==============================

.. _go_binary: /go/core.rst#_go_binary

Tests to ensure a protobuf can be included into a plugin and host.

all_test
--------

1. Test that a go_binary_ rule can write its shared object file with a custom
name in the package directory (not the mode directory) when the plugin
depends on a protobuf.

2. Test that a plugin with a protobuf dependency built using a go_binary_ rule
can be loaded by a Go program and that its symbols are working as expected.
36 changes: 36 additions & 0 deletions tests/core/go_plugin_with_proto_library/all_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main_test

import (
"go_plugin_with_proto_library/validate"
"os"
"plugin"
"testing"
)

const RuleName = "test"

func TestPluginCreated(t *testing.T) {
_, err := os.Stat("plugin.so")
if err != nil {
t.Error(err)
}
}

func TestPluginWorks(t *testing.T) {
p, err := plugin.Open("plugin.so")
if err != nil {
t.Error(err)
return
}

symProto, err := p.Lookup("SomeProto")
if err != nil {
t.Error(err)
return
}

proto := symProto.(*validate.MessageRules)
if *proto.Name != RuleName {
t.Errorf("expected %#v, got %#v", RuleName, proto.Name)
}
}
7 changes: 7 additions & 0 deletions tests/core/go_plugin_with_proto_library/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "go_plugin_with_proto_library/validate"

var testValue = "test"

var SomeProto = validate.MessageRules{Name: &testValue}
6 changes: 6 additions & 0 deletions tests/core/go_plugin_with_proto_library/validate.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto2";
package validate;

message MessageRules {
required string name = 1;
}