1
0
mirror of https://github.com/golang/go synced 2024-11-21 23:04:39 -07:00

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
This commit is contained in:
Russ Cox 2011-12-21 08:51:18 -05:00
parent 43b8f68c3f
commit f52a2088ef
7 changed files with 65 additions and 18 deletions

View File

@ -242,11 +242,9 @@ func allPackages(what string) []string {
have := map[string]bool{
"builtin": true, // ignore pseudo-package that exists only for documentation
}
/*
if !build.DefaultContext.CgoEnabled {
have["runtime/cgo"] = true // ignore during walk
}
*/
if !build.DefaultContext.CgoEnabled {
have["runtime/cgo"] = true // ignore during walk
}
var pkgs []string
// Commands

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build plan9
// +build plan9 darwin/nocgo
package tls

View File

@ -1,3 +1,5 @@
// +build ignore
// Empty include file to generate z symbols

View File

@ -1,3 +1,5 @@
// +build ignore
TEXT linefrompc(SB),7,$0 // Each byte stores its line delta
BYTE $2;
BYTE $1;

View File

@ -46,7 +46,7 @@ var buildPkgs = []struct {
{
"go/build/cgotest",
&DirInfo{
CgoFiles: []string{"cgotest.go"},
CgoFiles: ifCgo([]string{"cgotest.go"}),
CFiles: []string{"cgotest.c"},
HFiles: []string{"cgotest.h"},
Imports: []string{"C", "unsafe"},
@ -56,6 +56,13 @@ var buildPkgs = []struct {
},
}
func ifCgo(x []string) []string {
if DefaultContext.CgoEnabled {
return x
}
return nil
}
const cmdtestOutput = "3"
func TestBuild(t *testing.T) {
@ -72,6 +79,10 @@ func TestBuild(t *testing.T) {
continue
}
if tt.dir == "go/build/cgotest" && len(info.CgoFiles) == 0 {
continue
}
s, err := Build(tree, tt.dir, info)
if err != nil {
t.Errorf("Build(%#q): %v", tt.dir, err)

View File

@ -26,9 +26,9 @@ import (
// A Context specifies the supporting context for a build.
type Context struct {
GOARCH string // target architecture
GOOS string // target operating system
// TODO(rsc,adg): GOPATH
GOARCH string // target architecture
GOOS string // target operating system
CgoEnabled bool // whether cgo can be used
// By default, ScanDir uses the operating system's
// file system calls to read directories and files.
@ -75,9 +75,34 @@ func (ctxt *Context) readFile(dir, file string) (string, []byte, error) {
// The DefaultContext is the default Context for builds.
// It uses the GOARCH and GOOS environment variables
// if set, or else the compiled code's GOARCH and GOOS.
var DefaultContext = Context{
GOARCH: envOr("GOARCH", runtime.GOARCH),
GOOS: envOr("GOOS", runtime.GOOS),
var DefaultContext = defaultContext()
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 {
@ -264,7 +289,9 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) {
}
}
if isCgo {
di.CgoFiles = append(di.CgoFiles, name)
if ctxt.CgoEnabled {
di.CgoFiles = append(di.CgoFiles, name)
}
} else if isTest {
if pkg == string(pf.Name.Name) {
di.TestGoFiles = append(di.TestGoFiles, name)
@ -306,7 +333,6 @@ func (ctxt *Context) ScanDir(dir string) (info *DirInfo, err error) {
}
var slashslash = []byte("//")
var plusBuild = []byte("+build")
// shouldBuild reports whether it is okay to use this file,
// The rule is that in the file's leading run of // comments
@ -527,14 +553,22 @@ func splitQuoted(s string) (r []string, err error) {
//
// $GOOS
// $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 {
if ctxt.CgoEnabled && name == "cgo" {
return true
}
if !ctxt.CgoEnabled && name == "nocgo" {
return true
}
if name == ctxt.GOOS || name == ctxt.GOARCH {
return true
}
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

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// 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.