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