2015-01-23 23:15:10 +01:00
|
|
|
// Copyright 2014 Google Inc. 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.
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
package bootstrap
|
|
|
|
|
|
|
|
import (
|
2019-01-23 22:23:00 +01:00
|
|
|
"bufio"
|
2014-05-28 01:34:41 +02:00
|
|
|
"fmt"
|
2019-01-23 22:23:00 +01:00
|
|
|
"io"
|
2014-05-28 01:34:41 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-11-09 20:58:40 +01:00
|
|
|
"path/filepath"
|
2015-04-15 02:22:19 +02:00
|
|
|
"runtime"
|
2016-05-17 22:56:21 +02:00
|
|
|
"runtime/debug"
|
2015-04-21 01:41:55 +02:00
|
|
|
"runtime/pprof"
|
2016-08-05 22:59:43 +02:00
|
|
|
"runtime/trace"
|
2015-04-15 02:22:19 +02:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2014-05-28 01:34:41 +02:00
|
|
|
)
|
|
|
|
|
2021-03-16 08:54:33 +01:00
|
|
|
type Args struct {
|
2021-08-31 10:42:29 +02:00
|
|
|
ModuleListFile string
|
2021-09-07 12:36:52 +02:00
|
|
|
OutFile string
|
2021-04-14 10:28:54 +02:00
|
|
|
|
2021-08-31 10:42:29 +02:00
|
|
|
EmptyNinjaFile bool
|
|
|
|
|
2021-09-07 12:36:52 +02:00
|
|
|
NoGC bool
|
|
|
|
Cpuprofile string
|
|
|
|
Memprofile string
|
|
|
|
TraceFile string
|
2022-08-19 17:35:54 +02:00
|
|
|
|
|
|
|
BazelMode bool
|
|
|
|
BazelModeDev bool
|
2021-04-12 12:07:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 15:05:26 +02:00
|
|
|
// Returns the list of dependencies the emitted Ninja files has. These can be
|
|
|
|
// written to the .d file for the output so that it is correctly rebuilt when
|
|
|
|
// needed in case Blueprint is itself invoked from Ninja
|
2021-09-07 17:54:32 +02:00
|
|
|
func RunBlueprint(args Args, stopBefore StopBefore, ctx *blueprint.Context, config interface{}) []string {
|
2015-04-15 02:22:19 +02:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
2021-03-16 08:54:33 +01:00
|
|
|
if args.NoGC {
|
2016-05-17 22:56:21 +02:00
|
|
|
debug.SetGCPercent(-1)
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:54:33 +01:00
|
|
|
if args.Cpuprofile != "" {
|
2021-08-16 15:14:26 +02:00
|
|
|
f, err := os.Create(joinPath(ctx.SrcDir(), args.Cpuprofile))
|
2014-09-25 02:51:52 +02:00
|
|
|
if err != nil {
|
|
|
|
fatalf("error opening cpuprofile: %s", err)
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer f.Close()
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:54:33 +01:00
|
|
|
if args.TraceFile != "" {
|
2021-08-16 15:14:26 +02:00
|
|
|
f, err := os.Create(joinPath(ctx.SrcDir(), args.TraceFile))
|
2016-08-05 22:59:43 +02:00
|
|
|
if err != nil {
|
|
|
|
fatalf("error opening trace: %s", err)
|
|
|
|
}
|
|
|
|
trace.Start(f)
|
|
|
|
defer f.Close()
|
|
|
|
defer trace.Stop()
|
|
|
|
}
|
|
|
|
|
2021-08-31 10:42:29 +02:00
|
|
|
srcDir := "."
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2021-04-15 15:05:26 +02:00
|
|
|
ninjaDeps := make([]string, 0)
|
|
|
|
|
2021-03-16 08:54:33 +01:00
|
|
|
if args.ModuleListFile != "" {
|
|
|
|
ctx.SetModuleListFile(args.ModuleListFile)
|
2021-04-15 15:05:26 +02:00
|
|
|
ninjaDeps = append(ninjaDeps, args.ModuleListFile)
|
2017-08-10 00:13:12 +02:00
|
|
|
} else {
|
|
|
|
fatalf("-l <moduleListFile> is required and must be nonempty")
|
|
|
|
}
|
2022-03-25 05:56:02 +01:00
|
|
|
ctx.BeginEvent("list_modules")
|
2021-03-16 08:54:33 +01:00
|
|
|
filesToParse, err := ctx.ListModulePaths(srcDir)
|
2022-03-25 05:56:02 +01:00
|
|
|
ctx.EndEvent("list_modules")
|
2017-08-10 00:13:12 +02:00
|
|
|
if err != nil {
|
|
|
|
fatalf("could not enumerate files: %v\n", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-07-25 01:53:27 +02:00
|
|
|
ctx.RegisterBottomUpMutator("bootstrap_plugin_deps", pluginDeps)
|
2021-09-02 09:57:30 +02:00
|
|
|
ctx.RegisterModuleType("bootstrap_go_package", newGoPackageModuleFactory())
|
|
|
|
ctx.RegisterModuleType("blueprint_go_binary", newGoBinaryModuleFactory())
|
|
|
|
ctx.RegisterSingletonType("bootstrap", newSingletonFactory())
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2022-03-25 05:56:02 +01:00
|
|
|
ctx.BeginEvent("parse_bp")
|
2021-08-31 10:42:29 +02:00
|
|
|
blueprintFiles, errs := ctx.ParseFileList(".", filesToParse, config)
|
2014-05-28 01:34:41 +02:00
|
|
|
if len(errs) > 0 {
|
|
|
|
fatalErrors(errs)
|
|
|
|
}
|
2022-03-25 05:56:02 +01:00
|
|
|
ctx.EndEvent("parse_bp")
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2014-06-27 00:34:06 +02:00
|
|
|
// Add extra ninja file dependencies
|
2021-04-15 15:05:26 +02:00
|
|
|
ninjaDeps = append(ninjaDeps, blueprintFiles...)
|
2014-06-27 00:34:06 +02:00
|
|
|
|
2017-08-01 02:26:06 +02:00
|
|
|
extraDeps, errs := ctx.ResolveDependencies(config)
|
2015-05-13 23:36:24 +02:00
|
|
|
if len(errs) > 0 {
|
|
|
|
fatalErrors(errs)
|
|
|
|
}
|
2021-04-15 15:05:26 +02:00
|
|
|
ninjaDeps = append(ninjaDeps, extraDeps...)
|
2015-05-13 23:36:24 +02:00
|
|
|
|
2021-09-07 17:54:32 +02:00
|
|
|
if stopBefore == StopBeforePrepareBuildActions {
|
|
|
|
return ninjaDeps
|
2017-12-12 00:03:11 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 19:46:40 +02:00
|
|
|
if ctx.BeforePrepareBuildActionsHook != nil {
|
|
|
|
err := ctx.BeforePrepareBuildActionsHook()
|
|
|
|
if err != nil {
|
|
|
|
fatalErrors([]error{err})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 02:26:06 +02:00
|
|
|
extraDeps, errs = ctx.PrepareBuildActions(config)
|
2014-05-28 01:34:41 +02:00
|
|
|
if len(errs) > 0 {
|
|
|
|
fatalErrors(errs)
|
|
|
|
}
|
2021-04-15 15:05:26 +02:00
|
|
|
ninjaDeps = append(ninjaDeps, extraDeps...)
|
2014-06-26 02:21:54 +02:00
|
|
|
|
2021-09-07 17:54:32 +02:00
|
|
|
if stopBefore == StopBeforeWriteNinja {
|
|
|
|
return ninjaDeps
|
2020-11-05 17:17:32 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 22:23:00 +01:00
|
|
|
const outFilePermissions = 0666
|
2021-01-22 00:26:21 +01:00
|
|
|
var out io.StringWriter
|
2019-01-23 22:23:00 +01:00
|
|
|
var f *os.File
|
|
|
|
var buf *bufio.Writer
|
|
|
|
|
2022-03-25 05:56:02 +01:00
|
|
|
ctx.BeginEvent("write_files")
|
|
|
|
defer ctx.EndEvent("write_files")
|
2021-03-16 08:54:33 +01:00
|
|
|
if args.EmptyNinjaFile {
|
2021-08-16 15:14:26 +02:00
|
|
|
if err := ioutil.WriteFile(joinPath(ctx.SrcDir(), args.OutFile), []byte(nil), outFilePermissions); err != nil {
|
2020-04-19 21:50:48 +02:00
|
|
|
fatalf("error writing empty Ninja file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 10:42:29 +02:00
|
|
|
if !args.EmptyNinjaFile {
|
2021-08-16 15:14:26 +02:00
|
|
|
f, err = os.OpenFile(joinPath(ctx.SrcDir(), args.OutFile), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, outFilePermissions)
|
2019-01-23 22:23:00 +01:00
|
|
|
if err != nil {
|
|
|
|
fatalf("error opening Ninja file: %s", err)
|
|
|
|
}
|
2021-01-22 00:28:04 +01:00
|
|
|
buf = bufio.NewWriterSize(f, 16*1024*1024)
|
2019-01-23 22:23:00 +01:00
|
|
|
out = buf
|
|
|
|
} else {
|
2021-01-22 00:26:21 +01:00
|
|
|
out = ioutil.Discard.(io.StringWriter)
|
2019-01-23 22:23:00 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 19:32:35 +01:00
|
|
|
err = ctx.WriteBuildFile(out)
|
|
|
|
if err != nil {
|
|
|
|
fatalf("error writing Ninja file contents: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf != nil {
|
|
|
|
err = buf.Flush()
|
|
|
|
if err != nil {
|
|
|
|
fatalf("error flushing Ninja file contents: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f != nil {
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
fatalf("error closing Ninja file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:54:33 +01:00
|
|
|
if args.Memprofile != "" {
|
2021-08-16 15:14:26 +02:00
|
|
|
f, err := os.Create(joinPath(ctx.SrcDir(), args.Memprofile))
|
2016-05-17 22:56:21 +02:00
|
|
|
if err != nil {
|
|
|
|
fatalf("error opening memprofile: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
pprof.WriteHeapProfile(f)
|
|
|
|
}
|
2021-04-14 13:47:52 +02:00
|
|
|
|
2021-04-15 15:05:26 +02:00
|
|
|
return ninjaDeps
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func fatalf(format string, args ...interface{}) {
|
2014-06-06 05:00:22 +02:00
|
|
|
fmt.Printf(format, args...)
|
2015-08-21 23:47:06 +02:00
|
|
|
fmt.Print("\n")
|
2014-05-28 01:34:41 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fatalErrors(errs []error) {
|
2016-01-07 20:16:48 +01:00
|
|
|
red := "\x1b[31m"
|
|
|
|
unred := "\x1b[0m"
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
for _, err := range errs {
|
2016-01-07 20:16:48 +01:00
|
|
|
switch err := err.(type) {
|
2016-10-08 02:13:10 +02:00
|
|
|
case *blueprint.BlueprintError,
|
|
|
|
*blueprint.ModuleError,
|
|
|
|
*blueprint.PropertyError:
|
2016-01-07 20:16:48 +01:00
|
|
|
fmt.Printf("%serror:%s %s\n", red, unred, err.Error())
|
2014-05-28 01:34:41 +02:00
|
|
|
default:
|
2016-01-07 20:16:48 +01:00
|
|
|
fmt.Printf("%sinternal error:%s %s\n", red, unred, err)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-12-17 22:12:35 +01:00
|
|
|
|
2021-08-16 15:14:26 +02:00
|
|
|
func joinPath(base, path string) string {
|
2019-12-17 22:12:35 +01:00
|
|
|
if filepath.IsAbs(path) {
|
|
|
|
return path
|
|
|
|
}
|
2021-08-16 15:14:26 +02:00
|
|
|
return filepath.Join(base, path)
|
2019-12-17 22:12:35 +01:00
|
|
|
}
|