mirror of
https://github.com/golang/go
synced 2024-11-18 15:34:53 -07:00
go.tools/importer: negate "cgo" build tag to avoid native code in "net".
This removes the need for the caller to specify CGO_ENABLED=0 in the environment. R=crawshaw CC=golang-dev https://golang.org/cl/13464045
This commit is contained in:
parent
f9e325b575
commit
b21b4e8c88
@ -51,11 +51,6 @@ Examples:
|
||||
|
||||
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
||||
|
||||
// TODO(adonovan): the caller must---before go/build.init
|
||||
// runs---specify CGO_ENABLED=0, which entails the "!cgo" go/build
|
||||
// tag, preferring (dummy) Go to native C implementations of
|
||||
// cgoLookupHost et al.
|
||||
|
||||
func init() {
|
||||
// If $GOMAXPROCS isn't set, use the full capacity of the machine.
|
||||
// For small machines, use at least 4 threads.
|
||||
@ -66,16 +61,6 @@ func init() {
|
||||
}
|
||||
runtime.GOMAXPROCS(n)
|
||||
}
|
||||
|
||||
// For now, caller must---before go/build.init runs---specify
|
||||
// CGO_ENABLED=0, which entails the "!cgo" go/build tag,
|
||||
// preferring (dummy) Go to native C implementations of
|
||||
// cgoLookupHost et al.
|
||||
// TODO(adonovan): make the importer do this.
|
||||
if os.Getenv("CGO_ENABLED") != "0" {
|
||||
fmt.Fprint(os.Stderr, "Warning: CGO_ENABLED=0 not specified; "+
|
||||
"analysis of cgo code may be less precise.\n")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -114,14 +99,14 @@ func main() {
|
||||
|
||||
// -format flag
|
||||
if *formatFlag != "json" && *formatFlag != "plain" {
|
||||
fmt.Fprintf(os.Stderr, "illegal -format value: %q", *formatFlag)
|
||||
fmt.Fprintf(os.Stderr, "Error: illegal -format value: %q", *formatFlag)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Ask the oracle.
|
||||
res, err := oracle.Query(args, *modeFlag, *posFlag, ptalog, &build.Default)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
@ -101,7 +101,7 @@ result."
|
||||
|
||||
(message "Running oracle...")
|
||||
;; Use dynamic binding to modify/restore the environment
|
||||
(let ((process-environment (list* goroot-env gopath-env "CGO_ENABLED=0" process-environment)))
|
||||
(let ((process-environment (list* goroot-env gopath-env process-environment)))
|
||||
(apply #'call-process args)))
|
||||
(insert "\n")
|
||||
(compilation-mode)
|
||||
|
@ -70,9 +70,14 @@ func init() {
|
||||
// loadPackage ascertains which files belong to package path, then
|
||||
// loads, parses and returns them.
|
||||
func loadPackage(ctxt *build.Context, fset *token.FileSet, path string) (files []*ast.File, err error) {
|
||||
// Set the "!cgo" go/build tag, preferring (dummy) Go to
|
||||
// native C implementations of net.cgoLookupHost et al.
|
||||
ctxt2 := *ctxt
|
||||
ctxt2.CgoEnabled = false
|
||||
|
||||
// TODO(adonovan): fix: Do we need cwd? Shouldn't
|
||||
// ImportDir(path) / $GOROOT suffice?
|
||||
bp, err := ctxt.Import(path, cwd, 0)
|
||||
bp, err := ctxt2.Import(path, cwd, 0)
|
||||
if _, ok := err.(*build.NoGoError); ok {
|
||||
return nil, nil // empty directory
|
||||
}
|
||||
|
@ -7,9 +7,7 @@ package ssa_test
|
||||
// This file runs the SSA builder in sanity-checking mode on all
|
||||
// packages beneath $GOROOT and prints some summary information.
|
||||
//
|
||||
// Run test with GOMAXPROCS=8 and CGO_ENABLED=0. The latter cannot be
|
||||
// set from the test because it's too late to stop go/build.init()
|
||||
// from picking up the value from the parent's environment.
|
||||
// Run test with GOMAXPROCS=8.
|
||||
|
||||
import (
|
||||
"go/build"
|
||||
@ -52,9 +50,7 @@ func allPackages() []string {
|
||||
}
|
||||
|
||||
func TestStdlib(t *testing.T) {
|
||||
ctxt := build.Default
|
||||
ctxt.CgoEnabled = false // mutating a global!
|
||||
impctx := importer.Config{Build: &ctxt}
|
||||
impctx := importer.Config{Build: &build.Default}
|
||||
|
||||
// Load, parse and type-check the program.
|
||||
t0 := time.Now()
|
||||
|
Loading…
Reference in New Issue
Block a user