Commit 9725f225 authored by David Crawshaw's avatar David Crawshaw

cmd/go: -buildmode=c-archive support

Change-Id: I469254384b0f4e5b5f08a18658934e19259935f9
Reviewed-on: https://go-review.googlesource.com/8718Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 989f0ee8
...@@ -286,22 +286,48 @@ func (v *stringsFlag) String() string { ...@@ -286,22 +286,48 @@ func (v *stringsFlag) String() string {
return "<stringsFlag>" return "<stringsFlag>"
} }
var pkgFilter = func(p *Package) bool { return true } func pkgsMain(pkgs []*Package) (res []*Package) {
for _, p := range pkgs {
if p.Name == "main" {
res = append(res, p)
}
}
return res
}
func pkgsNotMain(pkgs []*Package) (res []*Package) {
for _, p := range pkgs {
if p.Name != "main" {
res = append(res, p)
}
}
return res
}
var pkgsFilter = func(pkgs []*Package) []*Package { return pkgs }
func buildModeInit() { func buildModeInit() {
var codegenArg, ldBuildmode string var codegenArg, ldBuildmode string
switch buildBuildmode { switch buildBuildmode {
case "archive": case "archive":
pkgFilter = func(p *Package) bool { return p.Name != "main" } pkgsFilter = pkgsNotMain
case "c-archive":
pkgsFilter = func(p []*Package) []*Package {
if len(p) != 1 || p[0].Name != "main" {
fatalf("-buildmode=c-archive requires exactly one main package")
}
return p
}
exeSuffix = ".a"
ldBuildmode = "c-archive"
case "c-shared": case "c-shared":
pkgFilter = func(p *Package) bool { return p.Name == "main" } pkgsFilter = pkgsMain
platform := goos + "/" + goarch platform := goos + "/" + goarch
switch platform { switch platform {
case "linux/amd64": case "linux/amd64":
case "android/arm": case "android/arm":
default: default:
fmt.Fprintf(os.Stderr, "go %s: -buildmode=c-shared not supported on %s\n", platform) fatalf("-buildmode=c-shared not supported on %s\n", platform)
os.Exit(2)
} }
if goarch == "amd64" { if goarch == "amd64" {
codegenArg = "-shared" codegenArg = "-shared"
...@@ -310,7 +336,7 @@ func buildModeInit() { ...@@ -310,7 +336,7 @@ func buildModeInit() {
case "default": case "default":
ldBuildmode = "exe" ldBuildmode = "exe"
case "exe": case "exe":
pkgFilter = func(p *Package) bool { return p.Name == "main" } pkgsFilter = pkgsMain
ldBuildmode = "exe" ldBuildmode = "exe"
default: default:
fatalf("buildmode=%s not supported", buildBuildmode) fatalf("buildmode=%s not supported", buildBuildmode)
...@@ -385,10 +411,8 @@ func runBuild(cmd *Command, args []string) { ...@@ -385,10 +411,8 @@ func runBuild(cmd *Command, args []string) {
} }
a := &action{} a := &action{}
for _, p := range packages(args) { for _, p := range pkgsFilter(packages(args)) {
if pkgFilter(p) { a.deps = append(a.deps, b.action(modeBuild, depMode, p))
a.deps = append(a.deps, b.action(modeBuild, depMode, p))
}
} }
b.do(a) b.do(a)
} }
...@@ -409,7 +433,7 @@ See also: go build, go get, go clean. ...@@ -409,7 +433,7 @@ See also: go build, go get, go clean.
func runInstall(cmd *Command, args []string) { func runInstall(cmd *Command, args []string) {
raceInit() raceInit()
pkgs := packagesForBuild(args) pkgs := pkgsFilter(packagesForBuild(args))
for _, p := range pkgs { for _, p := range pkgs {
if p.Target == "" && (!p.Standard || p.ImportPath != "unsafe") { if p.Target == "" && (!p.Standard || p.ImportPath != "unsafe") {
...@@ -429,9 +453,6 @@ func runInstall(cmd *Command, args []string) { ...@@ -429,9 +453,6 @@ func runInstall(cmd *Command, args []string) {
a := &action{} a := &action{}
var tools []*action var tools []*action
for _, p := range pkgs { for _, p := range pkgs {
if !pkgFilter(p) {
continue
}
// If p is a tool, delay the installation until the end of the build. // If p is a tool, delay the installation until the end of the build.
// This avoids installing assemblers/compilers that are being executed // This avoids installing assemblers/compilers that are being executed
// by other steps in the build. // by other steps in the build.
......
...@@ -657,6 +657,12 @@ are: ...@@ -657,6 +657,12 @@ are:
Build the listed non-main packages into .a files. Packages named Build the listed non-main packages into .a files. Packages named
main are ignored. main are ignored.
-buildmode=c-archive
Build the listed main package, plus all packages it imports,
into a C archive file. The only callable symbols will be those
functions marked as exported. Requires exactly one main package
to be listed.
-buildmode=c-shared -buildmode=c-shared
Build the listed main packages, plus all packages that they Build the listed main packages, plus all packages that they
import, into C shared libraries. The only callable symbols will import, into C shared libraries. The only callable symbols will
......
...@@ -376,6 +376,12 @@ are: ...@@ -376,6 +376,12 @@ are:
Build the listed non-main packages into .a files. Packages named Build the listed non-main packages into .a files. Packages named
main are ignored. main are ignored.
-buildmode=c-archive
Build the listed main package, plus all packages it imports,
into a C archive file. The only callable symbols will be those
functions marked as exported. Requires exactly one main package
to be listed.
-buildmode=c-shared -buildmode=c-shared
Build the listed main packages, plus all packages that they Build the listed main packages, plus all packages that they
import, into C shared libraries. The only callable symbols will import, into C shared libraries. The only callable symbols will
......
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