1// Copyright 2014 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bootstrap
16
17import (
18	"fmt"
19	"os"
20	"path/filepath"
21	"runtime"
22	"strings"
23
24	"github.com/google/blueprint"
25)
26
27func bootstrapVariable(name string, value func(BootstrapConfig) string) blueprint.Variable {
28	return pctx.VariableFunc(name, func(ctx blueprint.VariableFuncContext, config interface{}) (string, error) {
29		c, ok := config.(BootstrapConfig)
30		if !ok {
31			panic(fmt.Sprintf("Bootstrap rules were passed a configuration that does not include theirs, config=%q",
32				config))
33		}
34		return value(c), nil
35	})
36}
37
38var (
39	// These variables are the only configuration needed by the bootstrap
40	// modules.
41	srcDirVariable = bootstrapVariable("srcDir", func(c BootstrapConfig) string {
42		return "."
43	})
44	soongOutDirVariable = bootstrapVariable("soongOutDir", func(c BootstrapConfig) string {
45		return c.SoongOutDir()
46	})
47	outDirVariable = bootstrapVariable("outDir", func(c BootstrapConfig) string {
48		return c.OutDir()
49	})
50	goRootVariable = bootstrapVariable("goRoot", func(c BootstrapConfig) string {
51		goroot := runtime.GOROOT()
52		// Prefer to omit absolute paths from the ninja file
53		if cwd, err := os.Getwd(); err == nil {
54			if relpath, err := filepath.Rel(cwd, goroot); err == nil {
55				if !strings.HasPrefix(relpath, "../") {
56					goroot = relpath
57				}
58			}
59		}
60		return goroot
61	})
62	compileCmdVariable = bootstrapVariable("compileCmd", func(c BootstrapConfig) string {
63		return "$goRoot/pkg/tool/" + runtime.GOOS + "_" + runtime.GOARCH + "/compile"
64	})
65	linkCmdVariable = bootstrapVariable("linkCmd", func(c BootstrapConfig) string {
66		return "$goRoot/pkg/tool/" + runtime.GOOS + "_" + runtime.GOARCH + "/link"
67	})
68	debugFlagsVariable = bootstrapVariable("debugFlags", func(c BootstrapConfig) string {
69		if c.DebugCompilation() {
70			// -N: disable optimizations, -l: disable inlining
71			return "-N -l"
72		} else {
73			return ""
74		}
75	})
76)
77
78type BootstrapConfig interface {
79	// The directory where tools run during the build are located.
80	HostToolDir() string
81
82	// The directory where files emitted during bootstrapping are located.
83	// Usually OutDir() + "/soong".
84	SoongOutDir() string
85
86	// The output directory for the build.
87	OutDir() string
88
89	// Whether to compile Go code in such a way that it can be debugged
90	DebugCompilation() bool
91
92	// Whether to run tests for Go code
93	RunGoTests() bool
94
95	Subninjas() []string
96	PrimaryBuilderInvocations() []PrimaryBuilderInvocation
97}
98
99type StopBefore int
100
101const (
102	DoEverything StopBefore = iota
103	StopBeforePrepareBuildActions
104	StopBeforeWriteNinja
105)
106
107type PrimaryBuilderInvocation struct {
108	Inputs          []string
109	Implicits       []string
110	OrderOnlyInputs []string
111	Outputs         []string
112	Args            []string
113	Console         bool
114	Description     string
115	Env             map[string]string
116}
117