-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.go
142 lines (127 loc) · 4.41 KB
/
stdlib.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// stdlib builds the standard library in the appropriate mode into a new goroot.
package main
import (
"flag"
"fmt"
"go/build"
"log"
"os"
"path/filepath"
"strings"
)
func run(args []string) error {
// process the args
args, err := readParamsFiles(args)
if err != nil {
return err
}
flags := flag.NewFlagSet("stdlib", flag.ExitOnError)
goenv := envFlags(flags)
filterBuildid := flags.String("filter_buildid", "", "Path to filter_buildid tool")
out := flags.String("out", "", "Path to output go root")
race := flags.Bool("race", false, "Build in race mode")
shared := flags.Bool("shared", false, "Build in shared mode")
dynlink := flags.Bool("dynlink", false, "Build in dynlink mode")
if err := flags.Parse(args); err != nil {
return err
}
if err := goenv.checkFlags(); err != nil {
return err
}
goroot := os.Getenv("GOROOT")
if goroot == "" {
return fmt.Errorf("GOROOT not set")
}
output := abs(*out)
// Link in the bare minimum needed to the new GOROOT
if err := replicate(goroot, output, replicatePaths("src", "pkg/tool", "pkg/include")); err != nil {
return err
}
output, err = processPath(output)
if err != nil {
return err
}
// Now switch to the newly created GOROOT
os.Setenv("GOROOT", output)
// Create a temporary cache directory. "go build" requires this starting
// in Go 1.12.
cachePath := filepath.Join(output, ".gocache")
os.Setenv("GOCACHE", cachePath)
defer os.RemoveAll(cachePath)
// Make sure we have an absolute path to the C compiler.
// TODO(#1357): also take absolute paths of includes and other paths in flags.
os.Setenv("CC", abs(os.Getenv("CC")))
// Ensure paths are absolute.
absPaths := []string{}
for _, path := range filepath.SplitList(os.Getenv("PATH")) {
absPaths = append(absPaths, abs(path))
}
os.Setenv("PATH", strings.Join(absPaths, string(os.PathListSeparator)))
// Strip path prefix from source files in debug information.
os.Setenv("CGO_CFLAGS", os.Getenv("CGO_CFLAGS")+" -fdebug-prefix-map="+abs(".")+string(os.PathSeparator)+"=")
// Build the commands needed to build the std library in the right mode
installArgs := goenv.goCmd("install", "-toolexec", abs(*filterBuildid))
if len(build.Default.BuildTags) > 0 {
installArgs = append(installArgs, "-tags", strings.Join(build.Default.BuildTags, " "))
}
gcflags := []string{}
ldflags := []string{"-trimpath", abs(".")}
asmflags := []string{"-trimpath", abs(".")}
if *race {
installArgs = append(installArgs, "-race")
}
if *shared {
gcflags = append(gcflags, "-shared")
ldflags = append(ldflags, "-shared")
asmflags = append(asmflags, "-shared")
}
if *dynlink {
gcflags = append(gcflags, "-dynlink")
ldflags = append(ldflags, "-dynlink")
asmflags = append(asmflags, "-dynlink")
}
// Since Go 1.10, an all= prefix indicates the flags should apply to the package
// and its dependencies, rather than just the package itself. This was the
// default behavior before Go 1.10.
allSlug := ""
for _, t := range build.Default.ReleaseTags {
if t == "go1.10" {
allSlug = "all="
break
}
}
installArgs = append(installArgs, "-gcflags="+allSlug+strings.Join(gcflags, " "))
installArgs = append(installArgs, "-ldflags="+allSlug+strings.Join(ldflags, " "))
installArgs = append(installArgs, "-asmflags="+allSlug+strings.Join(asmflags, " "))
// Modifying CGO flags to use only absolute path
// because go is having its own sandbox, all CGO flags must use absolute path
if err := absEnv(cgoEnvVars, cgoAbsEnvFlags); err != nil {
return fmt.Errorf("error modifying cgo environment to absolute path: %v", err)
}
for _, target := range []string{"std", "runtime/cgo"} {
if err := goenv.runCommand(append(installArgs, target)); err != nil {
return err
}
}
return nil
}
func main() {
log.SetFlags(0)
log.SetPrefix("GoStdlib: ")
if err := run(os.Args[1:]); err != nil {
log.Fatal(err)
}
}