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 blueprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-15 00:38:36 +02:00
|
|
|
indentWidth = 4
|
|
|
|
maxIndentDepth = 2
|
|
|
|
lineWidth = 80
|
2014-05-28 01:34:41 +02:00
|
|
|
)
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
var indentString = strings.Repeat(" ", indentWidth*maxIndentDepth)
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
type ninjaWriter struct {
|
|
|
|
writer io.Writer
|
|
|
|
|
|
|
|
justDidBlankLine bool // true if the last operation was a BlankLine
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNinjaWriter(writer io.Writer) *ninjaWriter {
|
|
|
|
return &ninjaWriter{
|
|
|
|
writer: writer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ninjaWriter) Comment(comment string) error {
|
|
|
|
n.justDidBlankLine = false
|
|
|
|
|
|
|
|
const lineHeaderLen = len("# ")
|
|
|
|
const maxLineLen = lineWidth - lineHeaderLen
|
|
|
|
|
|
|
|
var lineStart, lastSplitPoint int
|
|
|
|
for i, r := range comment {
|
|
|
|
if unicode.IsSpace(r) {
|
|
|
|
// We know we can safely split the line here.
|
|
|
|
lastSplitPoint = i + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var line string
|
|
|
|
var writeLine bool
|
|
|
|
switch {
|
|
|
|
case r == '\n':
|
|
|
|
// Output the line without trimming the left so as to allow comments
|
|
|
|
// to contain their own indentation.
|
|
|
|
line = strings.TrimRightFunc(comment[lineStart:i], unicode.IsSpace)
|
|
|
|
writeLine = true
|
|
|
|
|
|
|
|
case (i-lineStart > maxLineLen) && (lastSplitPoint > lineStart):
|
|
|
|
// The line has grown too long and is splittable. Split it at the
|
|
|
|
// last split point.
|
|
|
|
line = strings.TrimSpace(comment[lineStart:lastSplitPoint])
|
|
|
|
writeLine = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if writeLine {
|
|
|
|
line = strings.TrimSpace("# "+line) + "\n"
|
|
|
|
_, err := io.WriteString(n.writer, line)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lineStart = lastSplitPoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if lineStart != len(comment) {
|
|
|
|
line := strings.TrimSpace(comment[lineStart:])
|
|
|
|
_, err := fmt.Fprintf(n.writer, "# %s\n", line)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ninjaWriter) Pool(name string) error {
|
|
|
|
n.justDidBlankLine = false
|
|
|
|
_, err := fmt.Fprintf(n.writer, "pool %s\n", name)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ninjaWriter) Rule(name string) error {
|
|
|
|
n.justDidBlankLine = false
|
|
|
|
_, err := fmt.Fprintf(n.writer, "rule %s\n", name)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-26 06:26:12 +02:00
|
|
|
func (n *ninjaWriter) Build(comment string, rule string, outputs, implicitOuts,
|
|
|
|
explicitDeps, implicitDeps, orderOnlyDeps []string) error {
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
n.justDidBlankLine = false
|
|
|
|
|
|
|
|
const lineWrapLen = len(" $")
|
|
|
|
const maxLineLen = lineWidth - lineWrapLen
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper := ninjaWriterWithWrap{
|
|
|
|
ninjaWriter: n,
|
|
|
|
maxLineLen: maxLineLen,
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 21:21:58 +01:00
|
|
|
if comment != "" {
|
2019-01-23 22:23:00 +01:00
|
|
|
err := wrapper.Comment(comment)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-08 21:21:58 +01:00
|
|
|
}
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteString("build")
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
for _, output := range outputs {
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteStringWithSpace(output)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 06:26:12 +02:00
|
|
|
if len(implicitOuts) > 0 {
|
|
|
|
wrapper.WriteStringWithSpace("|")
|
|
|
|
|
|
|
|
for _, out := range implicitOuts {
|
|
|
|
wrapper.WriteStringWithSpace(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteString(":")
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteStringWithSpace(rule)
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
for _, dep := range explicitDeps {
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteStringWithSpace(dep)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(implicitDeps) > 0 {
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteStringWithSpace("|")
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
for _, dep := range implicitDeps {
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteStringWithSpace(dep)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(orderOnlyDeps) > 0 {
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteStringWithSpace("||")
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
for _, dep := range orderOnlyDeps {
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteStringWithSpace(dep)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
return wrapper.Flush()
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ninjaWriter) Assign(name, value string) error {
|
|
|
|
n.justDidBlankLine = false
|
|
|
|
_, err := fmt.Fprintf(n.writer, "%s = %s\n", name, value)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ninjaWriter) ScopedAssign(name, value string) error {
|
|
|
|
n.justDidBlankLine = false
|
2015-04-15 00:38:36 +02:00
|
|
|
_, err := fmt.Fprintf(n.writer, "%s%s = %s\n", indentString[:indentWidth], name, value)
|
2014-05-28 01:34:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ninjaWriter) Default(targets ...string) error {
|
|
|
|
n.justDidBlankLine = false
|
|
|
|
|
|
|
|
const lineWrapLen = len(" $")
|
|
|
|
const maxLineLen = lineWidth - lineWrapLen
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper := ninjaWriterWithWrap{
|
|
|
|
ninjaWriter: n,
|
|
|
|
maxLineLen: maxLineLen,
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteString("default")
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
for _, target := range targets {
|
2015-04-15 00:38:36 +02:00
|
|
|
wrapper.WriteString(" " + target)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
return wrapper.Flush()
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
Run globs during earlier bootstrap phases
Instead of sometimes re-running minibp/the primary builder during the
next phase, run bpglob earlier to check dependencies.
We've run into issues where the environment is slightly different
between bootstrapping phase and the main build phase. It's also a
problem because our primary builder (Soong) exports information used by
another tool (Kati) that runs in between the bootstrapping phases and
the main phase. When Soong would run in the main phase, it could get out
of sync, and would require the build to be run again.
To do this, add a "subninja" include a build-globs.ninja file to each
build.ninja file. The first time, this will be an empty file, but we'll
always run minibp / the primary builder anyway. When the builder runs,
in addition to writing a dependency file, write out the
build-globs.ninja file with the rules to run bpglob.
Since bpglob may need to be run very early, before it would normally be
built, build it with microfactory.
Change-Id: I89fcd849a8729e892f163d40060ab90b5d4dfa5d
2018-07-06 06:56:59 +02:00
|
|
|
func (n *ninjaWriter) Subninja(file string) error {
|
|
|
|
n.justDidBlankLine = false
|
|
|
|
_, err := fmt.Fprintf(n.writer, "subninja %s\n", file)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
func (n *ninjaWriter) BlankLine() (err error) {
|
|
|
|
// We don't output multiple blank lines in a row.
|
|
|
|
if !n.justDidBlankLine {
|
|
|
|
n.justDidBlankLine = true
|
|
|
|
_, err = io.WriteString(n.writer, "\n")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
type ninjaWriterWithWrap struct {
|
|
|
|
*ninjaWriter
|
|
|
|
maxLineLen int
|
|
|
|
writtenLen int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ninjaWriterWithWrap) writeString(s string, space bool) {
|
|
|
|
if n.err != nil {
|
|
|
|
return
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
spaceLen := 0
|
|
|
|
if space {
|
|
|
|
spaceLen = 1
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
if n.writtenLen+len(s)+spaceLen > n.maxLineLen {
|
|
|
|
_, n.err = io.WriteString(n.writer, " $\n")
|
|
|
|
if n.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, n.err = io.WriteString(n.writer, indentString[:indentWidth*2])
|
|
|
|
if n.err != nil {
|
|
|
|
return
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
2015-04-15 00:38:36 +02:00
|
|
|
n.writtenLen = indentWidth * 2
|
|
|
|
s = strings.TrimLeftFunc(s, unicode.IsSpace)
|
|
|
|
} else if space {
|
2019-01-23 22:23:00 +01:00
|
|
|
_, n.err = io.WriteString(n.writer, " ")
|
|
|
|
if n.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-04-15 00:38:36 +02:00
|
|
|
n.writtenLen++
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 00:38:36 +02:00
|
|
|
_, n.err = io.WriteString(n.writer, s)
|
|
|
|
n.writtenLen += len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ninjaWriterWithWrap) WriteString(s string) {
|
|
|
|
n.writeString(s, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ninjaWriterWithWrap) WriteStringWithSpace(s string) {
|
|
|
|
n.writeString(s, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ninjaWriterWithWrap) Flush() error {
|
|
|
|
if n.err != nil {
|
|
|
|
return n.err
|
|
|
|
}
|
|
|
|
_, err := io.WriteString(n.writer, "\n")
|
|
|
|
return err
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|