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"
|
2023-06-20 11:29:19 +02:00
|
|
|
"errors"
|
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
|
|
|
"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
|
2021-04-12 12:07:02 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 02:02:05 +02:00
|
|
|
// RegisterGoModuleTypes adds module types to build tools written in golang
|
|
|
|
func RegisterGoModuleTypes(ctx *blueprint.Context) {
|
|
|
|
ctx.RegisterModuleType("bootstrap_go_package", newGoPackageModuleFactory())
|
|
|
|
ctx.RegisterModuleType("blueprint_go_binary", newGoBinaryModuleFactory())
|
|
|
|
}
|
|
|
|
|
2022-09-17 03:27:28 +02:00
|
|
|
// RunBlueprint emits `args.OutFile` (a Ninja file) and returns the list of
|
|
|
|
// its dependencies. These can be written to a `${args.OutFile}.d` file
|
|
|
|
// so that it is correctly rebuilt when needed in case Blueprint is itself
|
|
|
|
// invoked from Ninja
|
2023-06-20 11:29:19 +02:00
|
|
|
func RunBlueprint(args Args, stopBefore StopBefore, ctx *blueprint.Context, config interface{}) ([]string, error) {
|
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 {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fmt.Errorf("error opening cpuprofile: %s", err)
|
2014-09-25 02:51:52 +02:00
|
|
|
}
|
|
|
|
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 {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fmt.Errorf("error opening trace: %s", err)
|
2016-08-05 22:59:43 +02:00
|
|
|
}
|
|
|
|
trace.Start(f)
|
|
|
|
defer f.Close()
|
|
|
|
defer trace.Stop()
|
|
|
|
}
|
|
|
|
|
2022-09-17 04:10:14 +02:00
|
|
|
if args.ModuleListFile == "" {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fmt.Errorf("-l <moduleListFile> is required and must be nonempty")
|
2017-08-10 00:13:12 +02:00
|
|
|
}
|
2022-09-17 04:10:14 +02:00
|
|
|
ctx.SetModuleListFile(args.ModuleListFile)
|
|
|
|
|
|
|
|
var ninjaDeps []string
|
|
|
|
ninjaDeps = append(ninjaDeps, args.ModuleListFile)
|
|
|
|
|
2022-03-25 05:56:02 +01:00
|
|
|
ctx.BeginEvent("list_modules")
|
2022-09-17 04:10:14 +02:00
|
|
|
var filesToParse []string
|
|
|
|
if f, err := ctx.ListModulePaths("."); err != nil {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fmt.Errorf("could not enumerate files: %v\n", err.Error())
|
2022-09-17 04:10:14 +02:00
|
|
|
} else {
|
|
|
|
filesToParse = f
|
2017-08-10 00:13:12 +02:00
|
|
|
}
|
2022-09-17 04:10:14 +02:00
|
|
|
ctx.EndEvent("list_modules")
|
2017-08-10 00:13:12 +02:00
|
|
|
|
2015-07-25 01:53:27 +02:00
|
|
|
ctx.RegisterBottomUpMutator("bootstrap_plugin_deps", pluginDeps)
|
2023-05-15 22:59:49 +02:00
|
|
|
ctx.RegisterSingletonType("bootstrap", newSingletonFactory(), false)
|
2023-06-08 02:02:05 +02:00
|
|
|
RegisterGoModuleTypes(ctx)
|
2022-11-08 20:44:01 +01:00
|
|
|
blueprint.RegisterPackageIncludesModuleType(ctx)
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2022-03-25 05:56:02 +01:00
|
|
|
ctx.BeginEvent("parse_bp")
|
2022-09-17 04:10:14 +02:00
|
|
|
if blueprintFiles, errs := ctx.ParseFileList(".", filesToParse, config); len(errs) > 0 {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fatalErrors(errs)
|
2022-09-17 04:10:14 +02:00
|
|
|
} else {
|
|
|
|
ctx.EndEvent("parse_bp")
|
|
|
|
ninjaDeps = append(ninjaDeps, blueprintFiles...)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
2014-06-27 00:34:06 +02:00
|
|
|
|
2022-09-17 04:10:14 +02:00
|
|
|
if resolvedDeps, errs := ctx.ResolveDependencies(config); len(errs) > 0 {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fatalErrors(errs)
|
2022-09-17 04:10:14 +02:00
|
|
|
} else {
|
|
|
|
ninjaDeps = append(ninjaDeps, resolvedDeps...)
|
2015-05-13 23:36:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 17:54:32 +02:00
|
|
|
if stopBefore == StopBeforePrepareBuildActions {
|
2023-06-20 11:29:19 +02:00
|
|
|
return ninjaDeps, nil
|
2017-12-12 00:03:11 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 19:46:40 +02:00
|
|
|
if ctx.BeforePrepareBuildActionsHook != nil {
|
2022-09-17 04:10:14 +02:00
|
|
|
if err := ctx.BeforePrepareBuildActionsHook(); err != nil {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fatalErrors([]error{err})
|
2022-05-10 19:46:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 04:10:14 +02:00
|
|
|
if buildActionsDeps, errs := ctx.PrepareBuildActions(config); len(errs) > 0 {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fatalErrors(errs)
|
2022-09-17 04:10:14 +02:00
|
|
|
} else {
|
|
|
|
ninjaDeps = append(ninjaDeps, buildActionsDeps...)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
2014-06-26 02:21:54 +02:00
|
|
|
|
2021-09-07 17:54:32 +02:00
|
|
|
if stopBefore == StopBeforeWriteNinja {
|
2023-06-20 11:29:19 +02:00
|
|
|
return ninjaDeps, nil
|
2020-11-05 17:17:32 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 22:23:00 +01:00
|
|
|
const outFilePermissions = 0666
|
2023-10-31 23:15:30 +01:00
|
|
|
var out blueprint.StringWriterWriter
|
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 {
|
2022-09-17 03:27:28 +02:00
|
|
|
if err := os.WriteFile(joinPath(ctx.SrcDir(), args.OutFile), []byte(nil), outFilePermissions); err != nil {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fmt.Errorf("error writing empty Ninja file: %s", err)
|
2020-04-19 21:50:48 +02:00
|
|
|
}
|
2023-10-31 23:15:30 +01:00
|
|
|
out = io.Discard.(blueprint.StringWriterWriter)
|
2023-02-17 23:52:47 +01:00
|
|
|
} else {
|
2022-09-17 04:10:14 +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 {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fmt.Errorf("error opening Ninja file: %s", err)
|
2019-01-23 22:23:00 +01:00
|
|
|
}
|
2023-02-09 05:33:56 +01:00
|
|
|
defer f.Close()
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-17 04:10:14 +02:00
|
|
|
if err := ctx.WriteBuildFile(out); err != nil {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fmt.Errorf("error writing Ninja file contents: %s", err)
|
2020-01-16 19:32:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if buf != nil {
|
2022-09-17 04:10:14 +02:00
|
|
|
if err := buf.Flush(); err != nil {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fmt.Errorf("error flushing Ninja file contents: %s", err)
|
2020-01-16 19:32:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f != nil {
|
2022-09-17 04:10:14 +02:00
|
|
|
if err := f.Close(); err != nil {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fmt.Errorf("error closing Ninja file: %s", err)
|
2020-01-16 19:32:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-06-20 11:29:19 +02:00
|
|
|
return nil, fmt.Errorf("error opening memprofile: %s", err)
|
2016-05-17 22:56:21 +02:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
pprof.WriteHeapProfile(f)
|
|
|
|
}
|
2021-04-14 13:47:52 +02:00
|
|
|
|
2023-06-20 11:29:19 +02:00
|
|
|
return ninjaDeps, nil
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2023-06-20 11:29:19 +02:00
|
|
|
func fatalErrors(errs []error) 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
|
|
|
}
|
|
|
|
}
|
2023-06-20 11:29:19 +02:00
|
|
|
|
|
|
|
return errors.New("fatal errors encountered")
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
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
|
|
|
}
|