]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: add new +build tags 'cgo' and 'nocgo'
authorRuss Cox <rsc@golang.org>
Wed, 21 Dec 2011 13:51:18 +0000 (08:51 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 21 Dec 2011 13:51:18 +0000 (08:51 -0500)
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

src/cmd/go/main.go
src/pkg/crypto/tls/root_stub.go
src/pkg/debug/gosym/pclinetest.h
src/pkg/debug/gosym/pclinetest.s
src/pkg/go/build/build_test.go
src/pkg/go/build/dir.go
src/pkg/net/cgo_stub.go

index 2709750fcce67f914ddc69df36e50c4134d0ce18..7b7f4a450dd77b07f585f20f070bb21ca8696065 100644 (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
index 18dcb02043a6c8cbebb34ea35c14580c7a6758f8..d00493a5736d466291e3fc9684e5308b94db5571 100644 (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
 
index a6c40e76cd5ae0ec305660c9c44e97ef37eea64c..156c0b87b00a0f6ccb9271424bb75957b6d0c8c0 100644 (file)
@@ -1,3 +1,5 @@
+// +build ignore
+
 // Empty include file to generate z symbols
 
 
index 6305435b09e1308b6a77c3682fca7e6f2469a9d1..c1d4818d402494d55b18d786ab6db8061d128091 100644 (file)
@@ -1,3 +1,5 @@
+// +build ignore
+
 TEXT linefrompc(SB),7,$0       // Each byte stores its line delta
 BYTE $2;
 BYTE $1;
index e86cfc012ed33c271f945be74f2c791e59fb7d1e..fd4030632a9d8dc77cc9e898fdc6d3ef1f756501 100644 (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)
index 29d7c4c7d32274f521373b621d37ee068a2418ef..b710bc18da7854d8271840a6ea395ff4c9b8bbf7 100644 (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
index 4c49e63184c2748942bea1744e7ee7deb26e8af5..66aff837d0a412fc2529b874152600fd46e6b5d2 100644 (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.