func GcToolchain(opts ...func(*gcoption)) func(c *Context) error { defaults := []func(*gcoption){ func(opt *gcoption) { opt.goos = runtime.GOOS }, func(opt *gcoption) { opt.goarch = runtime.GOARCH }, } var options gcoption for _, opt := range append(defaults, opts...) { opt(&options) } return func(c *Context) error { goroot := runtime.GOROOT() goos := options.goos goarch := options.goarch archchar, err := build.ArchChar(goarch) if err != nil { return err } tooldir := filepath.Join(goroot, "pkg", "tool", goos+"_"+goarch) c.tc = &gcToolchain{ goos: goos, goarch: goarch, gc: filepath.Join(tooldir, archchar+"g"), ld: filepath.Join(tooldir, archchar+"l"), as: filepath.Join(tooldir, archchar+"a"), cc: filepath.Join(tooldir, archchar+"c"), pack: filepath.Join(tooldir, "pack"), } return nil } }
func main() { gochar, err := build.ArchChar(runtime.GOARCH) if err != nil { log.Fatal(err) } f, err := os.Create("builtin.go") if err != nil { log.Fatal(err) } defer f.Close() w := bufio.NewWriter(f) fmt.Fprintln(w, "// AUTO-GENERATED by mkbuiltin.go; DO NOT EDIT") fmt.Fprintln(w, "") fmt.Fprintln(w, "package gc") for _, name := range os.Args[1:] { mkbuiltin(w, gochar, name) } if err := w.Flush(); err != nil { log.Fatal(err) } }
func (b *builder) init() { var err error b.actionCache = make(map[cacheKey]*action) b.mkdirCache = make(map[string]bool) b.goarch = buildContext.GOARCH b.goos = buildContext.GOOS b.goroot = build.Path[0].Path b.gobin = build.Path[0].BinDir() if b.goos == "windows" { b.exe = ".exe" } b.gcflags = strings.Fields(os.Getenv("GCFLAGS")) b.arch, err = build.ArchChar(b.goarch) if err != nil { fatalf("%s", err) } if buildN { b.work = "$WORK" } else { b.work, err = ioutil.TempDir("", "go-build") if err != nil { fatalf("%s", err) } if buildX { fmt.Printf("WORK=%s\n", b.work) } atexit(func() { os.RemoveAll(b.work) }) } }
func main() { if runtime.Compiler != "gc" || runtime.GOOS == "nacl" { return } a, err := build.ArchChar(runtime.GOARCH) check(err) err = os.Chdir(filepath.Join("fixedbugs", "issue9355.dir")) check(err) out := run("go", "tool", a+"g", "-S", "a.go") os.Remove("a." + a) // 6g/8g print the offset as dec, but 5g/9g print the offset as hex. patterns := []string{ `rel 0\+\d t=1 \"\"\.x\+8\r?\n`, // y = &x.b `rel 0\+\d t=1 \"\"\.x\+(28|1c)\r?\n`, // z = &x.d.q `rel 0\+\d t=1 \"\"\.b\+5\r?\n`, // c = &b[5] `rel 0\+\d t=1 \"\"\.x\+(88|58)\r?\n`, // w = &x.f[3].r } for _, p := range patterns { if ok, err := regexp.Match(p, out); !ok || err != nil { println(string(out)) panic("can't find pattern " + p) } } }
// setEnvironment assembles the configuration for gotest and its subcommands. func setEnvironment() { // Basic environment. GOROOT = runtime.GOROOT() addEnv("GOROOT", GOROOT) GOARCH = build.DefaultContext.GOARCH addEnv("GOARCH", GOARCH) var err error O, err = build.ArchChar(GOARCH) if err != nil { Fatalf("unknown architecture: %s", err) } // Commands and their flags. gc := os.Getenv("GC") if gc == "" { gc = O + "g" } var gcflags []string if gf := strings.TrimSpace(os.Getenv("GCFLAGS")); gf != "" { gcflags = strings.Fields(gf) } XGC = append([]string{gc, "-I", "_test", "-o", "_xtest_." + O}, gcflags...) GC = append(append([]string{gc, "-I", "_test"}, gcflags...), "_testmain.go") gl := os.Getenv("GL") if gl == "" { gl = O + "l" } GL = []string{gl, "-L", "_test", "_testmain." + O} // Silence make on Linux addEnv("MAKEFLAGS", "") addEnv("MAKELEVEL", "") }
func init() { if char, err := build.ArchChar(runtime.GOARCH); err == nil { gcPath = filepath.Join(build.ToolDir, char+"g") return } gcPath = "unknown-GOARCH-compiler" }
func GcToolchain(opts ...func(*gcoption)) func(c *Context) error { return func(c *Context) error { // TODO(dfc) this should come from the context, not the runtime. goroot := runtime.GOROOT() // cross-compliation is not supported yet #31 if c.gohostos != c.gotargetos || c.gohostarch != c.gotargetarch { return fmt.Errorf("cross compilation from host %s/%s to target %s/%s not supported with Go 1.4", c.gohostos, c.gohostarch, c.gotargetos, c.gotargetarch) } archchar, err := build.ArchChar(c.gotargetarch) if err != nil { return err } tooldir := filepath.Join(goroot, "pkg", "tool", c.gohostos+"_"+c.gohostarch) c.tc = &gcToolchain{ gc: filepath.Join(tooldir, archchar+"g"), ld: filepath.Join(tooldir, archchar+"l"), as: filepath.Join(tooldir, archchar+"a"), cc: filepath.Join(tooldir, archchar+"c"), pack: filepath.Join(tooldir, "pack"), } return nil } }
func compile(t *testing.T, dirname, filename string) string { cmd := exec.Command(gcPath, filename) cmd.Dir = dirname out, err := cmd.CombinedOutput() if err != nil { t.Logf("%s", out) t.Fatalf("%s %s failed: %s", gcPath, filename, err) } archCh, _ := build.ArchChar(runtime.GOARCH) // filename should end with ".go" return filepath.Join(dirname, filename[:len(filename)-2]+archCh) }
func init() { goarch = buildContext.GOARCH goos = buildContext.GOOS if goos == "windows" { exeSuffix = ".exe" } var err error archChar, err = build.ArchChar(goarch) if err != nil { fatalf("%s", err) } }
func GcToolchain(opts ...func(*gcoption)) func(c *Context) error { envor := func(key, def string) string { if v := os.Getenv(key); v != "" { return v } else { return def } } defaults := []func(*gcoption){ func(opt *gcoption) { opt.goos = envor("GOOS", runtime.GOOS) }, func(opt *gcoption) { opt.goarch = envor("GOARCH", runtime.GOARCH) }, } var options gcoption for _, opt := range append(defaults, opts...) { opt(&options) } return func(c *Context) error { goroot := runtime.GOROOT() goos := options.goos goarch := options.goarch // cross-compliation is not supported yet #31 if goos != runtime.GOOS || goarch != runtime.GOARCH { return fmt.Errorf("cross compilation from host %s/%s to target %s/%s not supported. See issue #31", runtime.GOOS, runtime.GOARCH, goos, goarch) } archchar, err := build.ArchChar(goarch) if err != nil { return err } tooldir := filepath.Join(goroot, "pkg", "tool", goos+"_"+goarch) c.tc = &gcToolchain{ goos: goos, goarch: goarch, gc: filepath.Join(tooldir, archchar+"g"), ld: filepath.Join(tooldir, archchar+"l"), as: filepath.Join(tooldir, archchar+"a"), cc: filepath.Join(tooldir, archchar+"c"), pack: filepath.Join(tooldir, "pack"), } return nil } }
func main() { a, err := build.ArchChar(runtime.GOARCH) if err != nil { fmt.Println("BUG:", err) os.Exit(1) } run("go", "tool", a+"g", filepath.Join("fixedbugs", "bug302.dir", "p.go")) run("go", "tool", "pack", "grc", "pp.a", "p."+a) run("go", "tool", a+"g", "-I", ".", filepath.Join("fixedbugs", "bug302.dir", "main.go")) os.Remove("p." + a) os.Remove("pp.a") os.Remove("main." + a) }
func main() { flag.Parse() // source of unique numbers go func() { for i := 0; ; i++ { uniq <- i } }() // set archChar var err os.Error archChar, err = build.ArchChar(runtime.GOARCH) if err != nil { log.Fatal(err) } // find and serve the go tour files t, _, err := build.FindTree(basePkg) if err != nil { log.Fatalf("Couldn't find tour files: %v", err) } root := filepath.Join(t.SrcDir(), basePkg) root := basePkg log.Println("Serving content from", root) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/favicon.ico" || r.URL.Path == "/" { fn := filepath.Join(root, "static", r.URL.Path[1:]) http.ServeFile(w, r, fn) return } http.Error(w, "not found", 404) }) http.Handle("/static/", http.FileServer(http.Dir(root))) http.HandleFunc("/kill", kill) // set include path for ld and gc pkgDir = t.PkgDir() if !strings.HasPrefix(*httpListen, "127.0.0.1") && !strings.HasPrefix(*httpListen, "localhost") { log.Print(localhostWarning) } log.Printf("Serving at http://%s/", *httpListen) log.Fatal(http.ListenAndServe(*httpListen, nil)) }
func main() { a, err := build.ArchChar(build.Default.GOARCH) check(err) err = os.Chdir(filepath.Join(".", "fixedbugs", "bug369.dir")) check(err) run("go", "tool", a+"g", "-N", "-o", "slow."+a, "pkg.go") run("go", "tool", a+"g", "-o", "fast."+a, "pkg.go") run("go", "tool", a+"g", "-o", "main."+a, "main.go") run("go", "tool", a+"l", "-o", "a.exe", "main."+a) run("." + string(filepath.Separator) + "a.exe") os.Remove("slow." + a) os.Remove("fast." + a) os.Remove("main." + a) os.Remove("a.exe") }
func GcToolchain() func(c *Context) error { return func(c *Context) error { // TODO(dfc) this should come from the context, not the runtime. goroot := runtime.GOROOT() if gc14 && (c.gohostos != c.gotargetos || c.gohostarch != c.gotargetarch) { // cross-compliation is not supported yet #31 return fmt.Errorf("cross compilation from host %s/%s to target %s/%s not supported with Go 1.4", c.gohostos, c.gohostarch, c.gotargetos, c.gotargetarch) } tooldir := filepath.Join(goroot, "pkg", "tool", c.gohostos+"_"+c.gohostarch) exe := "" if c.gohostos == "windows" { exe += ".exe" } switch { case gc14: archchar, err := build.ArchChar(c.gotargetarch) if err != nil { return err } c.tc = &gcToolchain{ gc: filepath.Join(tooldir, archchar+"g"+exe), ld: filepath.Join(tooldir, archchar+"l"+exe), as: filepath.Join(tooldir, archchar+"a"+exe), cc: filepath.Join(tooldir, archchar+"c"+exe), pack: filepath.Join(tooldir, "pack"+exe), } case gc15: c.tc = &gcToolchain{ gc: filepath.Join(tooldir, "compile"+exe), ld: filepath.Join(tooldir, "link"+exe), as: filepath.Join(tooldir, "asm"+exe), pack: filepath.Join(tooldir, "pack"+exe), } default: return fmt.Errorf("unsupported Go version: %v", runtime.Version) } return nil } }
func main() { letter, err := build.ArchChar(build.Default.GOARCH) if err != nil { fmt.Println(err) os.Exit(1) } cmd := exec.Command("go", "tool", letter+"g", "-S", "sinit.go") out, err := cmd.CombinedOutput() if err != nil { fmt.Println(string(out)) fmt.Println(err) os.Exit(1) } os.Remove("sinit." + letter) if bytes.Contains(out, []byte("initdone")) { fmt.Println("sinit generated an init function") os.Exit(1) } }
func main() { a, err := build.ArchChar(build.Default.GOARCH) check(err) // TODO: If we get rid of errchk, re-enable this test on Windows. errchk, err := filepath.Abs("errchk") check(err) err = os.Chdir(filepath.Join("fixedbugs", "bug248.dir")) check(err) run("go", "tool", a+"g", "bug0.go") run("go", "tool", a+"g", "bug1.go") run("go", "tool", a+"g", "bug2.go") run(errchk, "go", "tool", a+"g", "-e", "bug3.go") run("go", "tool", a+"l", "bug2."+a) run(fmt.Sprintf(".%c%s.out", filepath.Separator, a)) os.Remove("bug0." + a) os.Remove("bug1." + a) os.Remove("bug2." + a) os.Remove(a + ".out") }
func main() { flag.Parse() goos = getenv("GOOS", runtime.GOOS) goarch = getenv("GOARCH", runtime.GOARCH) findExecCmd() // Disable parallelism if printing or if using a simulator. if *verbose || len(findExecCmd()) > 0 { *numParallel = 1 } ratec = make(chan bool, *numParallel) rungatec = make(chan bool, *runoutputLimit) var err error letter, err = build.ArchChar(build.Default.GOARCH) check(err) gc = letter + "g" ld = letter + "l" var tests []*test if flag.NArg() > 0 { for _, arg := range flag.Args() { if arg == "-" || arg == "--" { // Permit running: // $ go run run.go - env.go // $ go run run.go -- env.go // $ go run run.go - ./fixedbugs // $ go run run.go -- ./fixedbugs continue } if fi, err := os.Stat(arg); err == nil && fi.IsDir() { for _, baseGoFile := range goFiles(arg) { tests = append(tests, startTest(arg, baseGoFile)) } } else if strings.HasSuffix(arg, ".go") { dir, file := filepath.Split(arg) tests = append(tests, startTest(dir, file)) } else { log.Fatalf("can't yet deal with non-directory and non-go file %q", arg) } } } else { for _, dir := range dirs { for _, baseGoFile := range goFiles(dir) { tests = append(tests, startTest(dir, baseGoFile)) } } } failed := false resCount := map[string]int{} for _, test := range tests { <-test.donec status := "ok " errStr := "" if _, isSkip := test.err.(skipError); isSkip { test.err = nil errStr = "unexpected skip for " + path.Join(test.dir, test.gofile) + ": " + errStr status = "FAIL" } if test.err != nil { status = "FAIL" errStr = test.err.Error() } if status == "FAIL" { failed = true } resCount[status]++ if status == "skip" && !*verbose && !*showSkips { continue } dt := fmt.Sprintf("%.3fs", test.dt.Seconds()) if status == "FAIL" { fmt.Printf("# go run run.go -- %s\n%s\nFAIL\t%s\t%s\n", path.Join(test.dir, test.gofile), errStr, test.goFileName(), dt) continue } if !*verbose { continue } fmt.Printf("%s\t%s\t%s\n", status, test.goFileName(), dt) } if *summary { for k, v := range resCount { fmt.Printf("%5d %s\n", v, k) } } if failed { os.Exit(1) } }
func CompileBinary(scriptPath string, binaryPath string, goBuild bool) { scriptDir := filepath.Dir(scriptPath) binaryDir := filepath.Dir(binaryPath) // Open source file for modifications file, err := os.OpenFile(scriptPath, os.O_RDWR, 0) if err != nil { log.Fatalf("Could not open file: %s", err) } defer func() { if err := file.Close(); err != nil { log.Fatalf("Could not close file: %s", err) } }() // Comment hashbang line in source file hasHashbang := CheckForHashbang(file) if hasHashbang { CommentHashbang(file, "//") } defer func() { // Restore hashbang line in source file if hasHashbang { CommentHashbang(file, "#!") } // Recover build panic and use it for log.Fatal after hashbang has been restored if r := recover(); r != nil { log.Fatal(r) } }() // Use "go build" if goBuild { // Get current directory currentDir, err := os.Getwd() if err != nil { panic(err) } currentDir, err = filepath.Abs(currentDir) if err != nil { panic(err) } if currentDir != scriptDir { // Change into scripts directory if err := os.Chdir(scriptDir); err != nil { panic(err) } defer func() { // Go back to previous directory if err := os.Chdir(currentDir); err != nil { panic(err) } }() } // Build current/scripts directory out, err := exec.Command("go", "build", "-o", binaryPath).CombinedOutput() if err != nil { panic(fmt.Errorf("%s\n%s\n", err, out)) } } else { // Set toolchain archChar, err := build.ArchChar(runtime.GOARCH) if err != nil { panic(err) } // Compile source file objectPath := filepath.Join(binaryDir, "_go_."+archChar) cmd := exec.Command(filepath.Join(build.ToolDir, archChar+"g"), "-o", objectPath, scriptPath) out, err := cmd.CombinedOutput() if err != nil { panic(fmt.Errorf("%s\n%s", cmd.Args, out)) } // Link executable out, err = exec.Command(filepath.Join(build.ToolDir, archChar+"l"), "-o", binaryPath, objectPath).CombinedOutput() if err != nil { panic(fmt.Errorf("Linker failed: %s\n%s", err, out)) } // Cleaning if err := os.Remove(objectPath); err != nil { panic(fmt.Errorf("Could not remove object file: %s", err)) } } }
func main() { flag.Parse() // Disable parallelism if printing if *verbose { *numParallel = 1 } ratec = make(chan bool, *numParallel) rungatec = make(chan bool, *runoutputLimit) var err error letter, err = build.ArchChar(build.Default.GOARCH) check(err) gc = letter + "g" ld = letter + "l" var tests []*test if flag.NArg() > 0 { for _, arg := range flag.Args() { if arg == "-" || arg == "--" { // Permit running: // $ go run run.go - env.go // $ go run run.go -- env.go // $ go run run.go - ./fixedbugs // $ go run run.go -- ./fixedbugs continue } if fi, err := os.Stat(arg); err == nil && fi.IsDir() { for _, baseGoFile := range goFiles(arg) { tests = append(tests, startTest(arg, baseGoFile)) } } else if strings.HasSuffix(arg, ".go") { dir, file := filepath.Split(arg) tests = append(tests, startTest(dir, file)) } else { log.Fatalf("can't yet deal with non-directory and non-go file %q", arg) } } } else { for _, dir := range dirs { for _, baseGoFile := range goFiles(dir) { tests = append(tests, startTest(dir, baseGoFile)) } } } failed := false resCount := map[string]int{} for _, test := range tests { <-test.donec _, isSkip := test.err.(skipError) errStr := "pass" if test.err != nil { errStr = test.err.Error() if !isSkip { failed = true } } if isSkip && !skipOkay[path.Join(test.dir, test.gofile)] { errStr = "unexpected skip for " + path.Join(test.dir, test.gofile) + ": " + errStr isSkip = false failed = true } resCount[errStr]++ if isSkip && !*verbose && !*showSkips { continue } if !*verbose && test.err == nil { continue } fmt.Printf("# go run run.go -- %s\n%-20s %-20s: %s\n", path.Join(test.dir, test.gofile), test.action, test.goFileName(), errStr) } if *summary { for k, v := range resCount { fmt.Printf("%5d %s\n", v, k) } } if failed { os.Exit(1) } }
func main() { flag.Parse() if *verbose { *numParallel = 1 } ratec = make(chan bool, *numParallel) var err error letter, err = build.ArchChar(build.DefaultContext.GOARCH) check(err) gc = letter + "g" ld = letter + "l" var tests []*test if flag.NArg() > 0 { for _, arg := range flag.Args() { if arg == "-" || arg == "--" { // Permit running either: // $ go run run.go - env.go // $ go run run.go -- env.go continue } if !strings.HasSuffix(arg, ".go") { log.Fatalf("can't yet deal with non-go file %q", arg) } dir, file := filepath.Split(arg) tests = append(tests, startTest(dir, file)) } } else { for _, dir := range dirs { for _, baseGoFile := range goFiles(dir) { tests = append(tests, startTest(dir, baseGoFile)) } } } failed := false resCount := map[string]int{} for _, test := range tests { <-test.donec _, isSkip := test.err.(skipError) errStr := "pass" if isSkip { errStr = "skip" } if test.err != nil { errStr = test.err.Error() if !isSkip { failed = true } } resCount[errStr]++ if isSkip && !*verbose && !*showSkips { continue } if !*verbose && test.err == nil { continue } fmt.Printf("%-10s %-20s: %s\n", test.action, test.goFileName(), errStr) } if *summary { for k, v := range resCount { fmt.Printf("%5d %s\n", v, k) } } if failed { os.Exit(1) } }
func main() { var binaryDir, binaryPath string // == Flags force := flag.Bool("f", false, "force compilation") flag.Usage = usage flag.Parse() if flag.NArg() == 0 { usage() } log.SetFlags(0) log.SetPrefix("ERROR: ") // == Paths pkg, err := build.Import("", build.Default.GOROOT, build.FindOnly) if err != nil { log.Fatalf("GOROOT is not set: %s", err) } scriptPath := flag.Args()[0] scriptDir, scriptName := filepath.Split(scriptPath) ext := filepath.Ext(scriptName) // Global directory if exist(pkg.BinDir) { // "GOROOT" could be into a directory not mounted // Absolute path to calculate its hash. scriptDirAbs, err := filepath.Abs(scriptDir) if err != nil { log.Fatalf("Could not get absolute path: %s", err) } // generates a hash for the file crc := adler32.Checksum([]byte(scriptDirAbs)) binaryDir = filepath.Join(pkg.PkgRoot, filepath.Base(build.ToolDir), SUBDIR, strconv.FormatUint(uint64(crc), 10)) } else { // Local directory; ready to work in shared filesystems binaryDir = filepath.Join(SUBDIR, filepath.Base(build.ToolDir)) } binaryPath = filepath.Join(binaryDir, strings.Replace(scriptName, ext, "", 1)) // Windows does not like running binaries without the ".exe" extension if runtime.GOOS == "windows" { binaryPath += ".exe" } // Check directory if !exist(binaryDir) { if err := os.MkdirAll(binaryDir, 0750); err != nil { log.Fatalf("Could not make directory: %s", err) } } // == Run and exit if !*force && exist(binaryPath) { scriptMtime := getTime(scriptPath) binaryMtime := getTime(binaryPath) // If the script was not modified if scriptMtime.Equal(binaryMtime) || scriptMtime.Before(binaryMtime) { RunAndExit(binaryPath) } } // == Compile and link file, err := os.OpenFile(scriptPath, os.O_RDWR, 0) if err != nil { log.Fatalf("Could not open file: %s", err) } defer file.Close() hasInterpreter := checkInterpreter(file) if hasInterpreter { // comment interpreter line file.Seek(0, 0) if _, err = file.Write([]byte("//")); err != nil { log.Fatalf("could not comment the line interpreter: %s", err) } } // Set toolchain archChar, err := build.ArchChar(runtime.GOARCH) if err != nil { log.Fatal(err) } // == Compile source file objectPath := filepath.Join(binaryDir, "_go_."+archChar) cmd := exec.Command(filepath.Join(build.ToolDir, archChar+"g"), "-o", objectPath, scriptPath) out, err := cmd.CombinedOutput() if hasInterpreter { // comment out interpreter line file.Seek(0, 0) if _, err := file.Write([]byte("#!")); err != nil { log.Fatalf("could not comment out the line interpreter: %s", err) } } if err != nil { log.Fatalf("%s\n%s", cmd.Args, out) } // == Link executable out, err = exec.Command(filepath.Join(build.ToolDir, archChar+"l"), "-o", binaryPath, objectPath).CombinedOutput() if err != nil { log.Fatalf("Linker failed: %s\n%s", err, out) } // == Cleaning if err := os.Remove(objectPath); err != nil { log.Fatalf("Could not remove object file: %s", err) } RunAndExit(binaryPath) }