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 ( |
Dan Willemsen | a1e6eee | 2018-03-02 14:54:17 -0800 | [diff] [blame] | 18 | "os" |
| 19 | "path/filepath" |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 20 | "runtime" |
Dan Willemsen | a1e6eee | 2018-03-02 14:54:17 -0800 | [diff] [blame] | 21 | "strings" |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | ) |
| 25 | |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 26 | func bootstrapVariable(name string, value func() string) blueprint.Variable { |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 27 | return pctx.VariableFunc(name, func(config interface{}) (string, error) { |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 28 | return value(), nil |
| 29 | }) |
| 30 | } |
| 31 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 32 | var ( |
Colin Cross | c5fa50e | 2019-12-17 13:12:35 -0800 | [diff] [blame] | 33 | // These variables are the only configuration needed by the bootstrap |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 34 | // modules. |
| 35 | srcDir = bootstrapVariable("srcDir", func() string { |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 36 | return SrcDir |
| 37 | }) |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 38 | buildDir = bootstrapVariable("buildDir", func() string { |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 39 | return BuildDir |
| 40 | }) |
Dan Willemsen | cd4e0ce | 2017-07-19 22:43:30 -0700 | [diff] [blame] | 41 | ninjaBuildDir = bootstrapVariable("ninjaBuildDir", func() string { |
| 42 | return NinjaBuildDir |
| 43 | }) |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 44 | goRoot = bootstrapVariable("goRoot", func() string { |
Dan Willemsen | a1e6eee | 2018-03-02 14:54:17 -0800 | [diff] [blame] | 45 | goroot := runtime.GOROOT() |
| 46 | // Prefer to omit absolute paths from the ninja file |
| 47 | if cwd, err := os.Getwd(); err == nil { |
| 48 | if relpath, err := filepath.Rel(cwd, goroot); err == nil { |
| 49 | if !strings.HasPrefix(relpath, "../") { |
| 50 | goroot = relpath |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | return goroot |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 55 | }) |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 56 | compileCmd = bootstrapVariable("compileCmd", func() string { |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 57 | return "$goRoot/pkg/tool/" + runtime.GOOS + "_" + runtime.GOARCH + "/compile" |
| 58 | }) |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 59 | linkCmd = bootstrapVariable("linkCmd", func() string { |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 60 | return "$goRoot/pkg/tool/" + runtime.GOOS + "_" + runtime.GOARCH + "/link" |
| 61 | }) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 62 | ) |
| 63 | |
Dan Willemsen | 30a80c3 | 2015-06-24 19:21:21 -0700 | [diff] [blame] | 64 | type ConfigInterface interface { |
Dan Willemsen | efd2de7 | 2015-07-22 17:06:06 -0700 | [diff] [blame] | 65 | // GeneratingPrimaryBuilder should return true if this build invocation is |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 66 | // creating a .bootstrap/build.ninja file to be used to build the |
| 67 | // primary builder |
Dan Willemsen | efd2de7 | 2015-07-22 17:06:06 -0700 | [diff] [blame] | 68 | GeneratingPrimaryBuilder() bool |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 69 | } |
Dan Willemsen | 30a80c3 | 2015-06-24 19:21:21 -0700 | [diff] [blame] | 70 | |
Dan Willemsen | cd4e0ce | 2017-07-19 22:43:30 -0700 | [diff] [blame] | 71 | type ConfigRemoveAbandonedFilesUnder interface { |
Dan Willemsen | ab223a5 | 2018-07-05 21:56:59 -0700 | [diff] [blame] | 72 | // RemoveAbandonedFilesUnder should return two slices: |
| 73 | // - a slice of path prefixes that will be cleaned of files that are no |
| 74 | // longer active targets, but are listed in the .ninja_log. |
| 75 | // - a slice of paths that are exempt from cleaning |
| 76 | RemoveAbandonedFilesUnder() (under, except []string) |
Colin Cross | 6d529f0 | 2015-11-17 16:16:58 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Dan Willemsen | be27523 | 2016-05-26 10:07:59 -0700 | [diff] [blame] | 79 | type ConfigBlueprintToolLocation interface { |
| 80 | // BlueprintToolLocation can return a path name to install blueprint tools |
| 81 | // designed for end users (bpfmt, bpmodify, and anything else using |
| 82 | // blueprint_go_binary). |
| 83 | BlueprintToolLocation() string |
| 84 | } |
| 85 | |
Colin Cross | 28b2843 | 2017-12-11 15:03:11 -0800 | [diff] [blame] | 86 | type StopBefore int |
| 87 | |
| 88 | const ( |
| 89 | StopBeforePrepareBuildActions StopBefore = 1 |
| 90 | ) |
| 91 | |
| 92 | type ConfigStopBefore interface { |
| 93 | StopBefore() StopBefore |
| 94 | } |
| 95 | |
Dan Willemsen | 91a657e | 2015-07-22 17:05:59 -0700 | [diff] [blame] | 96 | type Stage int |
| 97 | |
| 98 | const ( |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 99 | StagePrimary Stage = iota |
Dan Willemsen | 91a657e | 2015-07-22 17:05:59 -0700 | [diff] [blame] | 100 | StageMain |
| 101 | ) |
| 102 | |
Dan Willemsen | 30a80c3 | 2015-06-24 19:21:21 -0700 | [diff] [blame] | 103 | type Config struct { |
Dan Willemsen | 91a657e | 2015-07-22 17:05:59 -0700 | [diff] [blame] | 104 | stage Stage |
Dan Willemsen | 30a80c3 | 2015-06-24 19:21:21 -0700 | [diff] [blame] | 105 | |
| 106 | topLevelBlueprintsFile string |
Dan Willemsen | 87ba294 | 2015-06-23 17:21:00 -0700 | [diff] [blame] | 107 | |
Dan Willemsen | dac90d3 | 2018-10-25 22:02:10 -0700 | [diff] [blame] | 108 | emptyNinjaFile bool |
Jeff Gaston | c3e2844 | 2017-08-09 15:13:12 -0700 | [diff] [blame] | 109 | runGoTests bool |
| 110 | moduleListFile string |
Dan Willemsen | 30a80c3 | 2015-06-24 19:21:21 -0700 | [diff] [blame] | 111 | } |