blob: a29ba7603d2940f05a804c0bd392f2e2151e19f5 [file] [log] [blame]
Colin Cross8e0c5112015-01-23 14:15:10 -08001// 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
Jamie Gennis1bc967e2014-05-27 16:34:41 -070015package bootstrap
16
Dan Willemsen7d0dddd2016-08-13 12:42:11 -070017import (
Lukacs T. Berki7ea1c162021-03-16 08:54:33 +010018 "fmt"
Dan Willemsena1e6eee2018-03-02 14:54:17 -080019 "os"
20 "path/filepath"
Dan Willemsen7d0dddd2016-08-13 12:42:11 -070021 "runtime"
Dan Willemsena1e6eee2018-03-02 14:54:17 -080022 "strings"
Dan Willemsen7d0dddd2016-08-13 12:42:11 -070023
24 "github.com/google/blueprint"
25)
26
Lukacs T. Berki7ea1c162021-03-16 08:54:33 +010027func bootstrapVariable(name string, value func(BootstrapConfig) string) blueprint.Variable {
Dan Willemsen7d0dddd2016-08-13 12:42:11 -070028 return pctx.VariableFunc(name, func(config interface{}) (string, error) {
Lukacs T. Berki7ea1c162021-03-16 08:54:33 +010029 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
Dan Willemsen7d0dddd2016-08-13 12:42:11 -070035 })
36}
37
Jamie Gennis1bc967e2014-05-27 16:34:41 -070038var (
Colin Crossc5fa50e2019-12-17 13:12:35 -080039 // These variables are the only configuration needed by the bootstrap
Dan Willemsen1e723212017-07-18 19:37:37 -070040 // modules.
Lukacs T. Berki7ea1c162021-03-16 08:54:33 +010041 srcDirVariable = bootstrapVariable("srcDir", func(c BootstrapConfig) string {
42 return c.SrcDir()
Dan Willemsen7d0dddd2016-08-13 12:42:11 -070043 })
Lukacs T. Berki7ea1c162021-03-16 08:54:33 +010044 buildDirVariable = bootstrapVariable("buildDir", func(c BootstrapConfig) string {
45 return c.BuildDir()
Dan Willemsen7d0dddd2016-08-13 12:42:11 -070046 })
Lukacs T. Berki7ea1c162021-03-16 08:54:33 +010047 ninjaBuildDirVariable = bootstrapVariable("ninjaBuildDir", func(c BootstrapConfig) string {
48 return c.NinjaBuildDir()
Dan Willemsencd4e0ce2017-07-19 22:43:30 -070049 })
Lukacs T. Berki7ea1c162021-03-16 08:54:33 +010050 goRootVariable = bootstrapVariable("goRoot", func(c BootstrapConfig) string {
Dan Willemsena1e6eee2018-03-02 14:54:17 -080051 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
Dan Willemsen7d0dddd2016-08-13 12:42:11 -070061 })
Lukacs T. Berki7ea1c162021-03-16 08:54:33 +010062 compileCmdVariable = bootstrapVariable("compileCmd", func(c BootstrapConfig) string {
Dan Willemsen7d0dddd2016-08-13 12:42:11 -070063 return "$goRoot/pkg/tool/" + runtime.GOOS + "_" + runtime.GOARCH + "/compile"
64 })
Lukacs T. Berki7ea1c162021-03-16 08:54:33 +010065 linkCmdVariable = bootstrapVariable("linkCmd", func(c BootstrapConfig) string {
Dan Willemsen7d0dddd2016-08-13 12:42:11 -070066 return "$goRoot/pkg/tool/" + runtime.GOOS + "_" + runtime.GOARCH + "/link"
67 })
Lukacs T. Berki07a91f02021-03-17 15:01:51 +010068 debugFlagsVariable = bootstrapVariable("debugFlags", func(c BootstrapConfig) string {
69 if c.DebugCompilation() {
Lukacs T. Berkia4c074b2021-03-18 10:03:39 +010070 // -N: disable optimizations, -l: disable inlining
Lukacs T. Berki07a91f02021-03-17 15:01:51 +010071 return "-N -l"
72 } else {
73 return ""
74 }
75 })
Jamie Gennis1bc967e2014-05-27 16:34:41 -070076)
77
Lukacs T. Berki53537442021-03-12 08:31:19 +010078type BootstrapConfig interface {
79 // The top-level directory of the source tree
80 SrcDir() string
81
82 // The directory where files emitted during bootstrapping are located.
83 // Usually NinjaBuildDir() + "/soong".
84 BuildDir() string
85
86 // The output directory for the build.
87 NinjaBuildDir() string
Lukacs T. Berki07a91f02021-03-17 15:01:51 +010088
89 // Whether to compile Go code in such a way that it can be debugged
90 DebugCompilation() bool
Jamie Gennis1bc967e2014-05-27 16:34:41 -070091}
Dan Willemsen30a80c32015-06-24 19:21:21 -070092
Dan Willemsencd4e0ce2017-07-19 22:43:30 -070093type ConfigRemoveAbandonedFilesUnder interface {
Dan Willemsenab223a52018-07-05 21:56:59 -070094 // RemoveAbandonedFilesUnder should return two slices:
95 // - a slice of path prefixes that will be cleaned of files that are no
96 // longer active targets, but are listed in the .ninja_log.
97 // - a slice of paths that are exempt from cleaning
Lukacs T. Berki7ea1c162021-03-16 08:54:33 +010098 RemoveAbandonedFilesUnder(buildDir string) (under, except []string)
Colin Cross6d529f02015-11-17 16:16:58 -080099}
100
Dan Willemsenbe275232016-05-26 10:07:59 -0700101type ConfigBlueprintToolLocation interface {
102 // BlueprintToolLocation can return a path name to install blueprint tools
103 // designed for end users (bpfmt, bpmodify, and anything else using
104 // blueprint_go_binary).
105 BlueprintToolLocation() string
106}
107
Colin Cross28b28432017-12-11 15:03:11 -0800108type StopBefore int
109
110const (
111 StopBeforePrepareBuildActions StopBefore = 1
Chris Parsons5e834262020-11-05 11:17:32 -0500112 StopBeforeWriteNinja StopBefore = 2
Colin Cross28b28432017-12-11 15:03:11 -0800113)
114
115type ConfigStopBefore interface {
116 StopBefore() StopBefore
117}
118
Dan Willemsen91a657e2015-07-22 17:05:59 -0700119type Stage int
120
121const (
Dan Willemsen1e723212017-07-18 19:37:37 -0700122 StagePrimary Stage = iota
Dan Willemsen91a657e2015-07-22 17:05:59 -0700123 StageMain
124)
125
Lukacs T. Berki77ef79b2021-04-12 12:07:02 +0200126type PrimaryBuilderInvocation struct {
127 Inputs []string
128 Outputs []string
129 Args []string
130}
131
Dan Willemsen30a80c32015-06-24 19:21:21 -0700132type Config struct {
Dan Willemsen91a657e2015-07-22 17:05:59 -0700133 stage Stage
Dan Willemsen30a80c32015-06-24 19:21:21 -0700134
135 topLevelBlueprintsFile string
Lukacs T. Berki77ef79b2021-04-12 12:07:02 +0200136 globFile string
Dan Willemsen87ba2942015-06-23 17:21:00 -0700137
Jeff Gastonc3e28442017-08-09 15:13:12 -0700138 runGoTests bool
Colin Cross0cdec992020-07-09 14:24:56 -0700139 useValidations bool
Lukacs T. Berki77ef79b2021-04-12 12:07:02 +0200140
141 primaryBuilderInvocations []PrimaryBuilderInvocation
Dan Willemsen30a80c32015-06-24 19:21:21 -0700142}