Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 15 | package bootstrap |
| 16 | |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 17 | import ( |
Lukacs T. Berki | 7ea1c16 | 2021-03-16 08:54:33 +0100 | [diff] [blame] | 18 | "fmt" |
Dan Willemsen | a1e6eee | 2018-03-02 14:54:17 -0800 | [diff] [blame] | 19 | "os" |
| 20 | "path/filepath" |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 21 | "runtime" |
Dan Willemsen | a1e6eee | 2018-03-02 14:54:17 -0800 | [diff] [blame] | 22 | "strings" |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint" |
| 25 | ) |
| 26 | |
Lukacs T. Berki | 7ea1c16 | 2021-03-16 08:54:33 +0100 | [diff] [blame] | 27 | func bootstrapVariable(name string, value func(BootstrapConfig) string) blueprint.Variable { |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 28 | return pctx.VariableFunc(name, func(config interface{}) (string, error) { |
Lukacs T. Berki | 7ea1c16 | 2021-03-16 08:54:33 +0100 | [diff] [blame] | 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 |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 35 | }) |
| 36 | } |
| 37 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 38 | var ( |
Colin Cross | c5fa50e | 2019-12-17 13:12:35 -0800 | [diff] [blame] | 39 | // These variables are the only configuration needed by the bootstrap |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 40 | // modules. |
Lukacs T. Berki | 7ea1c16 | 2021-03-16 08:54:33 +0100 | [diff] [blame] | 41 | srcDirVariable = bootstrapVariable("srcDir", func(c BootstrapConfig) string { |
| 42 | return c.SrcDir() |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 43 | }) |
Lukacs T. Berki | 7ea1c16 | 2021-03-16 08:54:33 +0100 | [diff] [blame] | 44 | buildDirVariable = bootstrapVariable("buildDir", func(c BootstrapConfig) string { |
| 45 | return c.BuildDir() |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 46 | }) |
Lukacs T. Berki | 7ea1c16 | 2021-03-16 08:54:33 +0100 | [diff] [blame] | 47 | ninjaBuildDirVariable = bootstrapVariable("ninjaBuildDir", func(c BootstrapConfig) string { |
| 48 | return c.NinjaBuildDir() |
Dan Willemsen | cd4e0ce | 2017-07-19 22:43:30 -0700 | [diff] [blame] | 49 | }) |
Lukacs T. Berki | 7ea1c16 | 2021-03-16 08:54:33 +0100 | [diff] [blame] | 50 | goRootVariable = bootstrapVariable("goRoot", func(c BootstrapConfig) string { |
Dan Willemsen | a1e6eee | 2018-03-02 14:54:17 -0800 | [diff] [blame] | 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 |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 61 | }) |
Lukacs T. Berki | 7ea1c16 | 2021-03-16 08:54:33 +0100 | [diff] [blame] | 62 | compileCmdVariable = bootstrapVariable("compileCmd", func(c BootstrapConfig) string { |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 63 | return "$goRoot/pkg/tool/" + runtime.GOOS + "_" + runtime.GOARCH + "/compile" |
| 64 | }) |
Lukacs T. Berki | 7ea1c16 | 2021-03-16 08:54:33 +0100 | [diff] [blame] | 65 | linkCmdVariable = bootstrapVariable("linkCmd", func(c BootstrapConfig) string { |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 66 | return "$goRoot/pkg/tool/" + runtime.GOOS + "_" + runtime.GOARCH + "/link" |
| 67 | }) |
Lukacs T. Berki | 07a91f0 | 2021-03-17 15:01:51 +0100 | [diff] [blame] | 68 | debugFlagsVariable = bootstrapVariable("debugFlags", func(c BootstrapConfig) string { |
| 69 | if c.DebugCompilation() { |
Lukacs T. Berki | a4c074b | 2021-03-18 10:03:39 +0100 | [diff] [blame] | 70 | // -N: disable optimizations, -l: disable inlining |
Lukacs T. Berki | 07a91f0 | 2021-03-17 15:01:51 +0100 | [diff] [blame] | 71 | return "-N -l" |
| 72 | } else { |
| 73 | return "" |
| 74 | } |
| 75 | }) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 76 | ) |
| 77 | |
Lukacs T. Berki | 5353744 | 2021-03-12 08:31:19 +0100 | [diff] [blame] | 78 | type 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. Berki | 07a91f0 | 2021-03-17 15:01:51 +0100 | [diff] [blame] | 88 | |
| 89 | // Whether to compile Go code in such a way that it can be debugged |
| 90 | DebugCompilation() bool |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 91 | } |
Dan Willemsen | 30a80c3 | 2015-06-24 19:21:21 -0700 | [diff] [blame] | 92 | |
Dan Willemsen | cd4e0ce | 2017-07-19 22:43:30 -0700 | [diff] [blame] | 93 | type ConfigRemoveAbandonedFilesUnder interface { |
Dan Willemsen | ab223a5 | 2018-07-05 21:56:59 -0700 | [diff] [blame] | 94 | // 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. Berki | 7ea1c16 | 2021-03-16 08:54:33 +0100 | [diff] [blame] | 98 | RemoveAbandonedFilesUnder(buildDir string) (under, except []string) |
Colin Cross | 6d529f0 | 2015-11-17 16:16:58 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Dan Willemsen | be27523 | 2016-05-26 10:07:59 -0700 | [diff] [blame] | 101 | type 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 Cross | 28b2843 | 2017-12-11 15:03:11 -0800 | [diff] [blame] | 108 | type StopBefore int |
| 109 | |
| 110 | const ( |
| 111 | StopBeforePrepareBuildActions StopBefore = 1 |
Chris Parsons | 5e83426 | 2020-11-05 11:17:32 -0500 | [diff] [blame] | 112 | StopBeforeWriteNinja StopBefore = 2 |
Colin Cross | 28b2843 | 2017-12-11 15:03:11 -0800 | [diff] [blame] | 113 | ) |
| 114 | |
| 115 | type ConfigStopBefore interface { |
| 116 | StopBefore() StopBefore |
| 117 | } |
| 118 | |
Dan Willemsen | 91a657e | 2015-07-22 17:05:59 -0700 | [diff] [blame] | 119 | type Stage int |
| 120 | |
| 121 | const ( |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 122 | StagePrimary Stage = iota |
Dan Willemsen | 91a657e | 2015-07-22 17:05:59 -0700 | [diff] [blame] | 123 | StageMain |
| 124 | ) |
| 125 | |
Lukacs T. Berki | 77ef79b | 2021-04-12 12:07:02 +0200 | [diff] [blame] | 126 | type PrimaryBuilderInvocation struct { |
| 127 | Inputs []string |
| 128 | Outputs []string |
| 129 | Args []string |
| 130 | } |
| 131 | |
Dan Willemsen | 30a80c3 | 2015-06-24 19:21:21 -0700 | [diff] [blame] | 132 | type Config struct { |
Dan Willemsen | 91a657e | 2015-07-22 17:05:59 -0700 | [diff] [blame] | 133 | stage Stage |
Dan Willemsen | 30a80c3 | 2015-06-24 19:21:21 -0700 | [diff] [blame] | 134 | |
| 135 | topLevelBlueprintsFile string |
Lukacs T. Berki | 77ef79b | 2021-04-12 12:07:02 +0200 | [diff] [blame] | 136 | globFile string |
Dan Willemsen | 87ba294 | 2015-06-23 17:21:00 -0700 | [diff] [blame] | 137 | |
Jeff Gaston | c3e2844 | 2017-08-09 15:13:12 -0700 | [diff] [blame] | 138 | runGoTests bool |
Colin Cross | 0cdec99 | 2020-07-09 14:24:56 -0700 | [diff] [blame] | 139 | useValidations bool |
Lukacs T. Berki | 77ef79b | 2021-04-12 12:07:02 +0200 | [diff] [blame] | 140 | |
| 141 | primaryBuilderInvocations []PrimaryBuilderInvocation |
Dan Willemsen | 30a80c3 | 2015-06-24 19:21:21 -0700 | [diff] [blame] | 142 | } |