blob: ba45f91d27ce18795f54517f097e329f8a192140 [file] [log] [blame]
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// The makefuchsia.go script builds a GOROOT in the out/ build directory
// using sources from third_party/go.
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
var (
flagBootstrapGoroot = flag.String("bootstrap-goroot", "", "Path to go root used for bootstrap")
flagGoroot = flag.String("goroot", "", "Path to the output GOROOT")
flagDepfilePath = flag.String("depfile", "", "depfile to write into")
flagDepfileTarget = flag.String("depfile-target", "", "depfile `make` target")
flagDepfileRoot = flag.String("depfile-root", "", "Path from which dependency paths should be rooted")
flagStampPath = flag.String("stamp-file", "", "Path of a file to create upon completion")
)
func join(path ...string) string { return filepath.Join(path...) }
func main() {
log.SetFlags(0)
log.SetPrefix("makefuchsia.go: ")
flag.Parse()
versionFile := join(*flagGoroot, "VERSION")
origVersion, err := ioutil.ReadFile(versionFile)
if err != nil {
log.Fatal(err)
}
// Do not repeat the updating of the VERSION file if it was previously performed.
if bytes.Index(origVersion, []byte("fuchsia")) == -1 {
if err := ioutil.WriteFile(versionFile, []byte(fmt.Sprintf("devel(%s) third_party/go fuchsia", bytes.TrimSpace(origVersion))), os.ModePerm); err != nil {
log.Fatal(err)
}
}
env := os.Environ()
if *flagBootstrapGoroot == "" {
log.Fatal("-bootstrap-goroot is required")
}
env = append(env, "GOROOT_BOOTSTRAP="+strings.TrimSpace(*flagBootstrapGoroot))
env = append(env, "CC_FOR_fuchsia_amd64="+*flagGoroot+"/misc/fuchsia/clangwrap.sh")
env = append(env, "CC_FOR_fuchsia_arm64="+*flagGoroot+"/misc/fuchsia/clangwrap.sh")
abs, err := filepath.Abs(join(*flagGoroot, "src", "make.bash"))
if err != nil {
log.Fatal(err)
}
cmd := exec.Command(abs)
cmd.Args = []string{"--no-clean"}
cmd.Dir = join(*flagGoroot, "src")
cmd.Env = env
buf := new(bytes.Buffer)
cmd.Stdout = buf
cmd.Stderr = buf
if err := cmd.Run(); err != nil {
log.Fatalf("make.bash failed: %v\n%s", err, buf.String())
}
depfile, err := os.Create(*flagDepfilePath)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(depfile, "%s:", *flagStampPath)
if err := filepath.Walk(*flagGoroot, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
fmt.Fprintf(depfile, " %s", path)
return err
}); err != nil {
log.Fatal(err)
}
fmt.Fprint(depfile, "\n")
if err := ioutil.WriteFile(*flagStampPath, []byte(fmt.Sprintf("%s", time.Now())), os.ModePerm); err != nil {
log.Fatal(err)
}
}