Commit c1938231 authored by David Crawshaw's avatar David Crawshaw

cmd/go: support -buildmode=plugin on linux

Change-Id: I0c8a04457db28c55c35c9a186b63c40f40730e39
Reviewed-on: https://go-review.googlesource.com/27824Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 0cbb12f0
...@@ -866,6 +866,10 @@ ...@@ -866,6 +866,10 @@
// position independent executables (PIE). Packages not named // position independent executables (PIE). Packages not named
// main are ignored. // main are ignored.
// //
// -buildmode=plugin
// Build the listed main packages, plus all packages that they
// import, into a Go plugin. Packages not named main are ignored.
//
// //
// File types // File types
// //
......
...@@ -406,6 +406,21 @@ func buildModeInit() { ...@@ -406,6 +406,21 @@ func buildModeInit() {
fatalf("-buildmode=shared and -o not supported together") fatalf("-buildmode=shared and -o not supported together")
} }
ldBuildmode = "shared" ldBuildmode = "shared"
case "plugin":
pkgsFilter = pkgsMain
if gccgo {
codegenArg = "-fPIC"
} else {
switch platform {
case "linux/amd64", "linux/arm", "linux/arm64", "linux/386",
"android/amd64", "android/arm", "android/arm64", "android/386":
default:
fatalf("-buildmode=plugin not supported on %s\n", platform)
}
codegenArg = "-dynlink"
}
exeSuffix = ".so"
ldBuildmode = "plugin"
default: default:
fatalf("buildmode=%s not supported", buildBuildmode) fatalf("buildmode=%s not supported", buildBuildmode)
} }
...@@ -1665,7 +1680,7 @@ func (b *builder) install(a *action) (err error) { ...@@ -1665,7 +1680,7 @@ func (b *builder) install(a *action) (err error) {
perm := os.FileMode(0666) perm := os.FileMode(0666)
if a1.link { if a1.link {
switch buildBuildmode { switch buildBuildmode {
case "c-archive", "c-shared": case "c-archive", "c-shared", "plugin":
default: default:
perm = 0777 perm = 0777
} }
...@@ -2959,7 +2974,7 @@ func (tools gccgoToolchain) cc(b *builder, p *Package, objdir, ofile, cfile stri ...@@ -2959,7 +2974,7 @@ func (tools gccgoToolchain) cc(b *builder, p *Package, objdir, ofile, cfile stri
// maybePIC adds -fPIC to the list of arguments if needed. // maybePIC adds -fPIC to the list of arguments if needed.
func (tools gccgoToolchain) maybePIC(args []string) []string { func (tools gccgoToolchain) maybePIC(args []string) []string {
switch buildBuildmode { switch buildBuildmode {
case "c-shared", "shared": case "c-shared", "shared", "plugin":
args = append(args, "-fPIC") args = append(args, "-fPIC")
} }
return args return args
......
...@@ -577,5 +577,9 @@ are: ...@@ -577,5 +577,9 @@ are:
Build the listed main packages and everything they import into Build the listed main packages and everything they import into
position independent executables (PIE). Packages not named position independent executables (PIE). Packages not named
main are ignored. main are ignored.
-buildmode=plugin
Build the listed main packages, plus all packages that they
import, into a Go plugin. Packages not named main are ignored.
`, `,
} }
...@@ -775,7 +775,7 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package ...@@ -775,7 +775,7 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
useBindir := p.Name == "main" useBindir := p.Name == "main"
if !p.Standard { if !p.Standard {
switch buildBuildmode { switch buildBuildmode {
case "c-archive", "c-shared": case "c-archive", "c-shared", "plugin":
useBindir = false useBindir = false
} }
} }
...@@ -846,11 +846,11 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package ...@@ -846,11 +846,11 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
importPaths = append(importPaths, "syscall") importPaths = append(importPaths, "syscall")
} }
// Currently build modes c-shared, pie, and -linkshared force // Currently build modes c-shared, pie, plugin, and -linkshared force
// external linking mode, and external linking mode forces an // external linking mode, and external linking mode forces an
// import of runtime/cgo. // import of runtime/cgo.
pieCgo := buildBuildmode == "pie" && (buildContext.GOOS != "linux" || buildContext.GOARCH != "amd64") pieCgo := buildBuildmode == "pie" && (buildContext.GOOS != "linux" || buildContext.GOARCH != "amd64")
if p.Name == "main" && !p.Goroot && (buildBuildmode == "c-shared" || pieCgo || buildLinkshared) { if p.Name == "main" && !p.Goroot && (buildBuildmode == "c-shared" || buildBuildmode == "plugin" || pieCgo || buildLinkshared) {
importPaths = append(importPaths, "runtime/cgo") importPaths = append(importPaths, "runtime/cgo")
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment