GOPATH=$(pwd) go build -buildmode=plugin plugin2
GOPATH=$(pwd)/altpath go build -buildmode=plugin plugin-mismatch
GOPATH=$(pwd) go build -buildmode=plugin -o=sub/plugin1.so sub/plugin1
-GOPATH=$(pwd) go build -buildmode=plugin unnamed1.go
-GOPATH=$(pwd) go build -buildmode=plugin unnamed2.go
+GOPATH=$(pwd) go build -buildmode=plugin -o=unnamed1.so unnamed1/main.go
+GOPATH=$(pwd) go build -buildmode=plugin -o=unnamed2.so unnamed2/main.go
GOPATH=$(pwd) go build host
LD_LIBRARY_PATH=$(pwd) ./host
return false, ""
}
-// computeBuildID computes the build ID for p, leaving it in p.Internal.BuildID.
-// Build ID is a hash of the information we want to detect changes in.
-// See the long comment in isStale for details.
-func computeBuildID(p *Package) {
- h := sha1.New()
-
- // Include the list of files compiled as part of the package.
- // This lets us detect removed files. See issue 3895.
- inputFiles := str.StringList(
+func pkgInputFiles(p *Package) []string {
+ return str.StringList(
p.GoFiles,
p.CgoFiles,
p.CFiles,
p.SwigFiles,
p.SwigCXXFiles,
)
+}
+
+// PluginPath computes the package path for a plugin main package.
+//
+// This is typically the import path of the main package p, unless the
+// plugin is being built directly from source files. In that case we
+// combine the package build ID with the contents of the main package
+// source files. This allows us to identify two different plugins
+// built from two source files with the same name.
+func PluginPath(p *Package) string {
+ if p.ImportPath != "command-line-arguments" {
+ return p.ImportPath
+ }
+ h := sha1.New()
+ fmt.Fprintf(h, "build ID: %s\n", p.Internal.BuildID)
+ for _, file := range str.StringList(p.GoFiles, p.CgoFiles, p.SFiles) {
+ data, err := ioutil.ReadFile(filepath.Join(p.Dir, file))
+ if err != nil {
+ base.Fatalf("go: %s", err)
+ }
+ h.Write(data)
+ }
+ return fmt.Sprintf("plugin/unnamed-%x", h.Sum(nil))
+}
+
+// computeBuildID computes the build ID for p, leaving it in p.Internal.BuildID.
+// Build ID is a hash of the information we want to detect changes in.
+// See the long comment in isStale for details.
+func computeBuildID(p *Package) {
+ h := sha1.New()
+
+ // Include the list of files compiled as part of the package.
+ // This lets us detect removed files. See issue 3895.
+ inputFiles := pkgInputFiles(p)
for _, file := range inputFiles {
fmt.Fprintf(h, "file %s\n", file)
}
pkgpath := p.ImportPath
if cfg.BuildBuildmode == "plugin" {
- if pkgpath == "command-line-arguments" {
- pkgpath = "plugin/unnamed-" + p.Internal.BuildID
- }
+ pkgpath = load.PluginPath(p)
} else if p.Name == "main" {
pkgpath = "main"
}
ldflags = append(ldflags, "-s", "-w")
}
if cfg.BuildBuildmode == "plugin" {
- pluginpath := root.Package.ImportPath
- if pluginpath == "command-line-arguments" {
- pluginpath = "plugin/unnamed-" + root.Package.Internal.BuildID
- }
- ldflags = append(ldflags, "-pluginpath", pluginpath)
+ ldflags = append(ldflags, "-pluginpath", load.PluginPath(root.Package))
}
// If the user has not specified the -extld option, then specify the