/
context.go
55 lines (48 loc) · 957 Bytes
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2011-2018 visualfc <visualfc@gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package buildctx
import (
"go/build"
"os"
)
var (
fnLookupEnv = os.LookupEnv
)
func SetLookupEnv(fn func(key string) (string, bool)) {
if fn != nil {
fnLookupEnv = fn
} else {
fnLookupEnv = os.LookupEnv
}
}
func Default() *build.Context {
return &build.Default
}
func System() *build.Context {
return NewContext(fnLookupEnv)
}
func NewContext(env func(key string) (string, bool)) *build.Context {
c := build.Default
if v, ok := env("GOARCH"); ok {
c.GOARCH = v
}
if v, ok := env("GOOS"); ok {
c.GOOS = v
}
if v, ok := env("GOROOT"); ok {
c.GOROOT = v
}
if v, ok := env("GOPATH"); ok {
c.GOPATH = v
}
if v, ok := env("CGO_ENABLED"); ok {
switch v {
case "1":
c.CgoEnabled = true
case "0":
c.CgoEnabled = false
}
}
return &c
}