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	"os"
19	"path/filepath"
20	"runtime"
21	"strings"
22
23	"github.com/google/blueprint"
24)
25
26func bootstrapVariable(name string, value func() string) blueprint.Variable {
27	return pctx.VariableFunc(name, func(config interface{}) (string, error) {
28		return value(), nil
29	})
30}
31
32var (
33	// These variables are the only configuration needed by the bootstrap
34	// modules.
35	srcDir = bootstrapVariable("srcDir", func() string {
36		return SrcDir
37	})
38	buildDir = bootstrapVariable("buildDir", func() string {
39		return BuildDir
40	})
41	ninjaBuildDir = bootstrapVariable("ninjaBuildDir", func() string {
42		return NinjaBuildDir
43	})
44	goRoot = bootstrapVariable("goRoot", func() string {
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
55	})
56	compileCmd = bootstrapVariable("compileCmd", func() string {
57		return "$goRoot/pkg/tool/" + runtime.GOOS + "_" + runtime.GOARCH + "/compile"
58	})
59	linkCmd = bootstrapVariable("linkCmd", func() string {
60		return "$goRoot/pkg/tool/" + runtime.GOOS + "_" + runtime.GOARCH + "/link"
61	})
62)
63
64type ConfigInterface interface {
65	// GeneratingPrimaryBuilder should return true if this build invocation is
66	// creating a .bootstrap/build.ninja file to be used to build the
67	// primary builder
68	GeneratingPrimaryBuilder() bool
69}
70
71type ConfigRemoveAbandonedFilesUnder interface {
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)
77}
78
79type 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
86type StopBefore int
87
88const (
89	StopBeforePrepareBuildActions StopBefore = 1
90)
91
92type ConfigStopBefore interface {
93	StopBefore() StopBefore
94}
95
96type Stage int
97
98const (
99	StagePrimary Stage = iota
100	StageMain
101)
102
103type Config struct {
104	stage Stage
105
106	topLevelBlueprintsFile string
107
108	emptyNinjaFile bool
109	runGoTests     bool
110	useValidations bool
111	moduleListFile string
112}
113