Commit f52a2088 authored by Russ Cox's avatar Russ Cox

go/build: add new +build tags 'cgo' and 'nocgo'

This lets us mark net's cgo_stub.go as only to be
built when cgo is disabled.

R=golang-dev, ality, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/5489100
parent 43b8f68c
...@@ -242,11 +242,9 @@ func allPackages(what string) []string { ...@@ -242,11 +242,9 @@ func allPackages(what string) []string {
have := map[string]bool{ have := map[string]bool{
"builtin": true, // ignore pseudo-package that exists only for documentation "builtin": true, // ignore pseudo-package that exists only for documentation
} }
/* if !build.DefaultContext.CgoEnabled {
if !build.DefaultContext.CgoEnabled { have["runtime/cgo"] = true // ignore during walk
have["runtime/cgo"] = true // ignore during walk }
}
*/
var pkgs []string var pkgs []string
// Commands // Commands
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build plan9 // +build plan9 darwin/nocgo
package tls package tls
......
// +build ignore
// Empty include file to generate z symbols // Empty include file to generate z symbols
......
// +build ignore
TEXT linefrompc(SB),7,$0 // Each byte stores its line delta TEXT linefrompc(SB),7,$0 // Each byte stores its line delta
BYTE $2; BYTE $2;
BYTE $1; BYTE $1;
......
...@@ -46,7 +46,7 @@ var buildPkgs = []struct { ...@@ -46,7 +46,7 @@ var buildPkgs = []struct {
{ {
"go/build/cgotest", "go/build/cgotest",
&DirInfo{ &DirInfo{
CgoFiles: []string{"cgotest.go"}, CgoFiles: ifCgo([]string{"cgotest.go"}),
CFiles: []string{"cgotest.c"}, CFiles: []string{"cgotest.c"},
HFiles: []string{"cgotest.h"}, HFiles: []string{"cgotest.h"},
Imports: []string{"C", "unsafe"}, Imports: []string{"C", "unsafe"},
...@@ -56,6 +56,13 @@ var buildPkgs = []struct { ...@@ -56,6 +56,13 @@ var buildPkgs = []struct {
}, },
} }
func ifCgo(x []string) []string {
if DefaultContext.CgoEnabled {
return x
}
return nil
}
const cmdtestOutput = "3" const cmdtestOutput = "3"
func TestBuild(t *testing.T) { func TestBuild(t *testing.T) {
...@@ -72,6 +79,10 @@ func TestBuild(t *testing.T) { ...@@ -72,6 +79,10 @@ func TestBuild(t *testing.T) {
continue continue
} }
if tt.dir == "go/build/cgotest" && len(info.CgoFiles) == 0 {
continue
}
s, err := Build(tree, tt.dir, info) s, err := Build(tree, tt.dir, info)
if err != nil { if err != nil {
t.Errorf("Build(%#q): %v", tt.dir, err) t.Errorf("Build(%#q): %v", tt.dir, err)
......
...@@ -26,9 +26,9 @@ import ( ...@@ -26,9 +26,9 @@ import (
// A Context specifies the supporting context for a build. // A Context specifies the supporting context for a build.
type Context struct { type Context struct {
GOARCH string // target architecture GOARCH string // target architecture
GOOS string // target operating system GOOS string // target operating system
// TODO(rsc,adg): GOPATH CgoEnabled bool // whether cgo can be used
// By default, ScanDir uses the operating system's // By default, ScanDir uses the operating system's
// file system calls to read directories and files. // file system calls to read directories and files.
...@@ -75,9 +75,34 @@ func (ctxt *Context) readFile(dir, file string) (string, []byte, error) { ...@@ -75,9 +75,34 @@ func (ctxt *Context) readFile(dir, file string) (string, []byte, error) {
// The DefaultContext is the default Context for builds. // The DefaultContext is the default Context for builds.
// It uses the GOARCH and GOOS environment variables // It uses the GOARCH and GOOS environment variables
// if set, or else the compiled code's GOARCH and GOOS. // if set, or else the compiled code's GOARCH and GOOS.
var DefaultContext = Context{ var DefaultContext = defaultContext()
GOARCH: envOr("GOARCH", runtime.GOARCH),
GOOS: envOr("GOOS", runtime.GOOS), var cgoEnabled = map[string]bool{
"darwin/386": true,
"darwin/amd64": true,
"linux/386": true,
"linux/amd64": true,
"freebsd/386": true,
"freebsd/amd64": true,
}
func defaultContext() Context {
var c Context
c.GOARCH = envOr("GOARCH", runtime.GOARCH)
c.GOOS = envOr("GOOS", runtime.GOOS)
s := os.Getenv("CGO_ENABLED")
switch s {
case "1":
c.CgoEnabled = true
case "0":
c.CgoEnabled = false
default:
c.CgoEnabled = cgoEnabled[c.GOOS+"/"+c.GOARCH]
}
return c
} }
func envOr(name, def string) string { func envOr(name, def string) string {
...@@ -264,7 +289,9 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) { ...@@ -264,7 +289,9 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) {
} }
} }
if isCgo { if isCgo {
di.CgoFiles = append(di.CgoFiles, name) if ctxt.CgoEnabled {
di.CgoFiles = append(di.CgoFiles, name)
}
} else if isTest { } else if isTest {
if pkg == string(pf.Name.Name) { if pkg == string(pf.Name.Name) {
di.TestGoFiles = append(di.TestGoFiles, name) di.TestGoFiles = append(di.TestGoFiles, name)
...@@ -306,7 +333,6 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) { ...@@ -306,7 +333,6 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) {
} }
var slashslash = []byte("//") var slashslash = []byte("//")
var plusBuild = []byte("+build")
// shouldBuild reports whether it is okay to use this file, // shouldBuild reports whether it is okay to use this file,
// The rule is that in the file's leading run of // comments // The rule is that in the file's leading run of // comments
...@@ -527,14 +553,22 @@ func splitQuoted(s string) (r []string, err error) { ...@@ -527,14 +553,22 @@ func splitQuoted(s string) (r []string, err error) {
// //
// $GOOS // $GOOS
// $GOARCH // $GOARCH
// $GOOS/$GOARCH // cgo (if cgo is enabled)
// nocgo (if cgo is disabled)
// a slash-separated list of any of these
// //
func (ctxt *Context) matchOSArch(name string) bool { func (ctxt *Context) matchOSArch(name string) bool {
if ctxt.CgoEnabled && name == "cgo" {
return true
}
if !ctxt.CgoEnabled && name == "nocgo" {
return true
}
if name == ctxt.GOOS || name == ctxt.GOARCH { if name == ctxt.GOOS || name == ctxt.GOARCH {
return true return true
} }
i := strings.Index(name, "/") i := strings.Index(name, "/")
return i >= 0 && name[:i] == ctxt.GOOS && name[i+1:] == ctxt.GOARCH return i >= 0 && ctxt.matchOSArch(name[:i]) && ctxt.matchOSArch(name[i+1:])
} }
// goodOSArchFile returns false if the name contains a $GOOS or $GOARCH // goodOSArchFile returns false if the name contains a $GOOS or $GOARCH
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build netbsd openbsd // +build nocgo
// Stub cgo routines for systems that do not use cgo to do network lookups. // Stub cgo routines for systems that do not use cgo to do network lookups.
......
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