| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 1 | // Copyright 2016 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 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 16 |  | 
 | 17 | import ( | 
 | 18 | 	"bytes" | 
| Colin Cross | d9bbf4b | 2023-11-17 16:23:48 -0800 | [diff] [blame] | 19 | 	"cmp" | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 20 | 	"fmt" | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 21 | 	"path/filepath" | 
 | 22 | 	"runtime" | 
| Colin Cross | d9bbf4b | 2023-11-17 16:23:48 -0800 | [diff] [blame] | 23 | 	"slices" | 
| Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 24 | 	"sort" | 
| Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 25 | 	"strings" | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 26 |  | 
| Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 27 | 	"github.com/google/blueprint" | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 28 | 	"github.com/google/blueprint/pathtools" | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 29 | 	"github.com/google/blueprint/proptools" | 
 | 30 | ) | 
 | 31 |  | 
| Dan Albert | f5415d7 | 2017-08-17 16:19:59 -0700 | [diff] [blame] | 32 | func init() { | 
 | 33 | 	RegisterMakeVarsProvider(pctx, androidMakeVarsProvider) | 
 | 34 | } | 
 | 35 |  | 
 | 36 | func androidMakeVarsProvider(ctx MakeVarsContext) { | 
| Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 37 | 	ctx.Strict("MIN_SUPPORTED_SDK_VERSION", ctx.Config().MinSupportedSdkVersion().String()) | 
| Dan Albert | f5415d7 | 2017-08-17 16:19:59 -0700 | [diff] [blame] | 38 | } | 
 | 39 |  | 
| Paul Duffin | 8eb4573 | 2022-10-04 19:03:31 +0100 | [diff] [blame] | 40 | // ///////////////////////////////////////////////////////////////////////////// | 
| Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 41 |  | 
 | 42 | // BaseMakeVarsContext contains the common functions for other packages to use | 
 | 43 | // to declare make variables | 
 | 44 | type BaseMakeVarsContext interface { | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 45 | 	Config() Config | 
| Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 46 | 	DeviceConfig() DeviceConfig | 
| Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 47 | 	AddNinjaFileDeps(deps ...string) | 
| Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 48 |  | 
| Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 49 | 	Failed() bool | 
 | 50 |  | 
| Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 51 | 	// These are equivalent to Strict and Check, but do not attempt to | 
 | 52 | 	// evaluate the values before writing them to the Makefile. They can | 
 | 53 | 	// be used when all ninja variables have already been evaluated through | 
 | 54 | 	// Eval(). | 
 | 55 | 	StrictRaw(name, value string) | 
 | 56 | 	CheckRaw(name, value string) | 
| Colin Cross | 8177ad2 | 2019-11-04 10:27:48 -0800 | [diff] [blame] | 57 |  | 
 | 58 | 	// GlobWithDeps returns a list of files that match the specified pattern but do not match any | 
 | 59 | 	// of the patterns in excludes.  It also adds efficient dependencies to rerun the primary | 
 | 60 | 	// builder whenever a file matching the pattern as added or removed, without rerunning if a | 
 | 61 | 	// file that does not match the pattern is added to a searched directory. | 
 | 62 | 	GlobWithDeps(pattern string, excludes []string) ([]string, error) | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 63 |  | 
 | 64 | 	// Phony creates a phony rule in Make, which will allow additional DistForGoal | 
 | 65 | 	// dependencies to be added to it.  Phony can be called on the same name multiple | 
 | 66 | 	// times to add additional dependencies. | 
 | 67 | 	Phony(names string, deps ...Path) | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 68 |  | 
 | 69 | 	// DistForGoal creates a rule to copy one or more Paths to the artifacts | 
 | 70 | 	// directory on the build server when the specified goal is built. | 
 | 71 | 	DistForGoal(goal string, paths ...Path) | 
 | 72 |  | 
 | 73 | 	// DistForGoalWithFilename creates a rule to copy a Path to the artifacts | 
 | 74 | 	// directory on the build server with the given filename when the specified | 
 | 75 | 	// goal is built. | 
 | 76 | 	DistForGoalWithFilename(goal string, path Path, filename string) | 
 | 77 |  | 
 | 78 | 	// DistForGoals creates a rule to copy one or more Paths to the artifacts | 
 | 79 | 	// directory on the build server when any of the specified goals are built. | 
 | 80 | 	DistForGoals(goals []string, paths ...Path) | 
 | 81 |  | 
 | 82 | 	// DistForGoalsWithFilename creates a rule to copy a Path to the artifacts | 
 | 83 | 	// directory on the build server with the given filename when any of the | 
 | 84 | 	// specified goals are built. | 
 | 85 | 	DistForGoalsWithFilename(goals []string, path Path, filename string) | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 86 | } | 
 | 87 |  | 
| Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 88 | // MakeVarsContext contains the set of functions available for MakeVarsProvider | 
 | 89 | // and SingletonMakeVarsProvider implementations. | 
 | 90 | type MakeVarsContext interface { | 
 | 91 | 	BaseMakeVarsContext | 
 | 92 |  | 
 | 93 | 	ModuleName(module blueprint.Module) string | 
 | 94 | 	ModuleDir(module blueprint.Module) string | 
 | 95 | 	ModuleSubDir(module blueprint.Module) string | 
 | 96 | 	ModuleType(module blueprint.Module) string | 
| Yu Liu | 663e450 | 2024-08-12 18:23:59 +0000 | [diff] [blame] | 97 | 	otherModuleProvider(module blueprint.Module, key blueprint.AnyProviderKey) (any, bool) | 
| Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 98 | 	BlueprintFile(module blueprint.Module) string | 
 | 99 |  | 
 | 100 | 	ModuleErrorf(module blueprint.Module, format string, args ...interface{}) | 
| Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 101 | 	OtherModulePropertyErrorf(module Module, property, format string, args ...interface{}) | 
| Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 102 | 	Errorf(format string, args ...interface{}) | 
 | 103 |  | 
 | 104 | 	VisitAllModules(visit func(Module)) | 
 | 105 | 	VisitAllModulesIf(pred func(Module) bool, visit func(Module)) | 
 | 106 |  | 
 | 107 | 	// Verify the make variable matches the Soong version, fail the build | 
 | 108 | 	// if it does not. If the make variable is empty, just set it. | 
 | 109 | 	Strict(name, ninjaStr string) | 
 | 110 | 	// Check to see if the make variable matches the Soong version, warn if | 
 | 111 | 	// it does not. If the make variable is empty, just set it. | 
 | 112 | 	Check(name, ninjaStr string) | 
 | 113 |  | 
 | 114 | 	// These are equivalent to the above, but sort the make and soong | 
 | 115 | 	// variables before comparing them. They also show the unique entries | 
 | 116 | 	// in each list when displaying the difference, instead of the entire | 
 | 117 | 	// string. | 
 | 118 | 	StrictSorted(name, ninjaStr string) | 
 | 119 | 	CheckSorted(name, ninjaStr string) | 
 | 120 |  | 
 | 121 | 	// Evaluates a ninja string and returns the result. Used if more | 
 | 122 | 	// complicated modification needs to happen before giving it to Make. | 
 | 123 | 	Eval(ninjaStr string) (string, error) | 
 | 124 | } | 
 | 125 |  | 
 | 126 | // MakeVarsModuleContext contains the set of functions available for modules | 
 | 127 | // implementing the ModuleMakeVarsProvider interface. | 
 | 128 | type MakeVarsModuleContext interface { | 
 | 129 | 	BaseMakeVarsContext | 
 | 130 | } | 
 | 131 |  | 
| Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 132 | var _ PathContext = MakeVarsContext(nil) | 
 | 133 |  | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 134 | type MakeVarsProvider func(ctx MakeVarsContext) | 
 | 135 |  | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 136 | func RegisterMakeVarsProvider(pctx PackageContext, provider MakeVarsProvider) { | 
| Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 137 | 	makeVarsInitProviders = append(makeVarsInitProviders, makeVarsProvider{pctx, provider}) | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 138 | } | 
 | 139 |  | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 140 | // SingletonMakeVarsProvider is a Singleton with an extra method to provide extra values to be exported to Make. | 
 | 141 | type SingletonMakeVarsProvider interface { | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 142 | 	// MakeVars uses a MakeVarsContext to provide extra values to be exported to Make. | 
 | 143 | 	MakeVars(ctx MakeVarsContext) | 
 | 144 | } | 
 | 145 |  | 
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 146 | var singletonMakeVarsProvidersKey = NewOnceKey("singletonMakeVarsProvidersKey") | 
 | 147 |  | 
| Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 148 | func getSingletonMakevarsProviders(config Config) *[]makeVarsProvider { | 
 | 149 | 	return config.Once(singletonMakeVarsProvidersKey, func() interface{} { | 
 | 150 | 		return &[]makeVarsProvider{} | 
 | 151 | 	}).(*[]makeVarsProvider) | 
 | 152 | } | 
 | 153 |  | 
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 154 | // registerSingletonMakeVarsProvider adds a singleton that implements SingletonMakeVarsProvider to | 
 | 155 | // the list of MakeVarsProviders to run. | 
 | 156 | func registerSingletonMakeVarsProvider(config Config, singleton SingletonMakeVarsProvider) { | 
 | 157 | 	// Singletons are registered on the Context and may be different between different Contexts, | 
 | 158 | 	// for example when running multiple tests.  Store the SingletonMakeVarsProviders in the | 
 | 159 | 	// Config so they are attached to the Context. | 
| Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 160 | 	singletonMakeVarsProviders := getSingletonMakevarsProviders(config) | 
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 161 |  | 
 | 162 | 	*singletonMakeVarsProviders = append(*singletonMakeVarsProviders, | 
 | 163 | 		makeVarsProvider{pctx, singletonMakeVarsProviderAdapter(singleton)}) | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 164 | } | 
 | 165 |  | 
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 166 | // singletonMakeVarsProviderAdapter converts a SingletonMakeVarsProvider to a MakeVarsProvider. | 
 | 167 | func singletonMakeVarsProviderAdapter(singleton SingletonMakeVarsProvider) MakeVarsProvider { | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 168 | 	return func(ctx MakeVarsContext) { singleton.MakeVars(ctx) } | 
 | 169 | } | 
 | 170 |  | 
| Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 171 | // ModuleMakeVarsProvider is a Module with an extra method to provide extra values to be exported to Make. | 
 | 172 | type ModuleMakeVarsProvider interface { | 
 | 173 | 	Module | 
 | 174 |  | 
 | 175 | 	// MakeVars uses a MakeVarsModuleContext to provide extra values to be exported to Make. | 
 | 176 | 	MakeVars(ctx MakeVarsModuleContext) | 
 | 177 | } | 
 | 178 |  | 
| Paul Duffin | 8eb4573 | 2022-10-04 19:03:31 +0100 | [diff] [blame] | 179 | // ///////////////////////////////////////////////////////////////////////////// | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 180 |  | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 181 | func makeVarsSingletonFunc() Singleton { | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 182 | 	return &makeVarsSingleton{} | 
 | 183 | } | 
 | 184 |  | 
| Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 185 | type makeVarsSingleton struct { | 
| Paul Duffin | 8eb4573 | 2022-10-04 19:03:31 +0100 | [diff] [blame] | 186 | 	varsForTesting     []makeVarsVariable | 
| Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 187 | 	installsForTesting []byte | 
 | 188 | } | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 189 |  | 
 | 190 | type makeVarsProvider struct { | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 191 | 	pctx PackageContext | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 192 | 	call MakeVarsProvider | 
 | 193 | } | 
 | 194 |  | 
| Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 195 | // Collection of makevars providers that are registered in init() methods. | 
 | 196 | var makeVarsInitProviders []makeVarsProvider | 
 | 197 |  | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 198 | type makeVarsContext struct { | 
| Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 199 | 	SingletonContext | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 200 | 	config  Config | 
 | 201 | 	pctx    PackageContext | 
 | 202 | 	vars    []makeVarsVariable | 
 | 203 | 	phonies []phony | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 204 | 	dists   []dist | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 205 | } | 
 | 206 |  | 
 | 207 | var _ MakeVarsContext = &makeVarsContext{} | 
 | 208 |  | 
 | 209 | type makeVarsVariable struct { | 
 | 210 | 	name   string | 
 | 211 | 	value  string | 
 | 212 | 	sort   bool | 
 | 213 | 	strict bool | 
 | 214 | } | 
 | 215 |  | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 216 | type phony struct { | 
 | 217 | 	name string | 
 | 218 | 	deps []string | 
 | 219 | } | 
 | 220 |  | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 221 | type dist struct { | 
 | 222 | 	goals []string | 
 | 223 | 	paths []string | 
 | 224 | } | 
 | 225 |  | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 226 | func (s *makeVarsSingleton) GenerateBuildActions(ctx SingletonContext) { | 
| Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 227 | 	if !ctx.Config().KatiEnabled() { | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 228 | 		return | 
 | 229 | 	} | 
 | 230 |  | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 231 | 	outFile := absolutePath(PathForOutput(ctx, | 
 | 232 | 		"make_vars"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String()) | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 233 |  | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 234 | 	lateOutFile := absolutePath(PathForOutput(ctx, | 
 | 235 | 		"late"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String()) | 
 | 236 |  | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 237 | 	installsFile := absolutePath(PathForOutput(ctx, | 
 | 238 | 		"installs"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String()) | 
 | 239 |  | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 240 | 	if ctx.Failed() { | 
 | 241 | 		return | 
 | 242 | 	} | 
 | 243 |  | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 244 | 	var vars []makeVarsVariable | 
 | 245 | 	var dists []dist | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 246 | 	var phonies []phony | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 247 | 	var katiInstalls []katiInstall | 
| Colin Cross | d9bbf4b | 2023-11-17 16:23:48 -0800 | [diff] [blame] | 248 | 	var katiInitRcInstalls []katiInstall | 
 | 249 | 	var katiVintfManifestInstalls []katiInstall | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 250 | 	var katiSymlinks []katiInstall | 
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 251 |  | 
 | 252 | 	providers := append([]makeVarsProvider(nil), makeVarsInitProviders...) | 
| Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 253 | 	providers = append(providers, *getSingletonMakevarsProviders(ctx.Config())...) | 
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 254 |  | 
 | 255 | 	for _, provider := range providers { | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 256 | 		mctx := &makeVarsContext{ | 
| Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 257 | 			SingletonContext: ctx, | 
 | 258 | 			pctx:             provider.pctx, | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 259 | 		} | 
 | 260 |  | 
 | 261 | 		provider.call(mctx) | 
 | 262 |  | 
 | 263 | 		vars = append(vars, mctx.vars...) | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 264 | 		phonies = append(phonies, mctx.phonies...) | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 265 | 		dists = append(dists, mctx.dists...) | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 266 | 	} | 
 | 267 |  | 
| Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 268 | 	ctx.VisitAllModules(func(m Module) { | 
| Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 269 | 		if provider, ok := m.(ModuleMakeVarsProvider); ok && m.Enabled(ctx) { | 
| Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 270 | 			mctx := &makeVarsContext{ | 
 | 271 | 				SingletonContext: ctx, | 
 | 272 | 			} | 
 | 273 |  | 
 | 274 | 			provider.MakeVars(mctx) | 
 | 275 |  | 
 | 276 | 			vars = append(vars, mctx.vars...) | 
 | 277 | 			phonies = append(phonies, mctx.phonies...) | 
 | 278 | 			dists = append(dists, mctx.dists...) | 
 | 279 | 		} | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 280 |  | 
 | 281 | 		if m.ExportedToMake() { | 
| Yu Liu | d46e5ae | 2024-08-15 18:46:17 +0000 | [diff] [blame] | 282 | 			info := OtherModuleProviderOrDefault(ctx, m, InstallFilesProvider) | 
 | 283 | 			katiInstalls = append(katiInstalls, info.KatiInstalls...) | 
| Colin Cross | d9bbf4b | 2023-11-17 16:23:48 -0800 | [diff] [blame] | 284 | 			katiInitRcInstalls = append(katiInitRcInstalls, m.base().katiInitRcInstalls...) | 
 | 285 | 			katiVintfManifestInstalls = append(katiVintfManifestInstalls, m.base().katiVintfInstalls...) | 
| Yu Liu | d46e5ae | 2024-08-15 18:46:17 +0000 | [diff] [blame] | 286 | 			katiSymlinks = append(katiSymlinks, info.KatiSymlinks...) | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 287 | 		} | 
| Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 288 | 	}) | 
 | 289 |  | 
| Colin Cross | d9bbf4b | 2023-11-17 16:23:48 -0800 | [diff] [blame] | 290 | 	compareKatiInstalls := func(a, b katiInstall) int { | 
 | 291 | 		aTo, bTo := a.to.String(), b.to.String() | 
 | 292 | 		if cmpTo := cmp.Compare(aTo, bTo); cmpTo != 0 { | 
 | 293 | 			return cmpTo | 
 | 294 | 		} | 
 | 295 |  | 
 | 296 | 		aFrom, bFrom := a.from.String(), b.from.String() | 
 | 297 | 		return cmp.Compare(aFrom, bFrom) | 
 | 298 | 	} | 
 | 299 |  | 
 | 300 | 	slices.SortFunc(katiInitRcInstalls, compareKatiInstalls) | 
 | 301 | 	katiInitRcInstalls = slices.CompactFunc(katiInitRcInstalls, func(a, b katiInstall) bool { | 
 | 302 | 		return compareKatiInstalls(a, b) == 0 | 
 | 303 | 	}) | 
 | 304 | 	katiInstalls = append(katiInstalls, katiInitRcInstalls...) | 
 | 305 |  | 
 | 306 | 	slices.SortFunc(katiVintfManifestInstalls, compareKatiInstalls) | 
 | 307 | 	katiVintfManifestInstalls = slices.CompactFunc(katiVintfManifestInstalls, func(a, b katiInstall) bool { | 
 | 308 | 		return compareKatiInstalls(a, b) == 0 | 
 | 309 | 	}) | 
 | 310 |  | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 311 | 	if ctx.Failed() { | 
 | 312 | 		return | 
 | 313 | 	} | 
 | 314 |  | 
| Dan Willemsen | 6a6478d | 2020-07-17 19:28:53 -0700 | [diff] [blame] | 315 | 	sort.Slice(vars, func(i, j int) bool { | 
 | 316 | 		return vars[i].name < vars[j].name | 
 | 317 | 	}) | 
 | 318 | 	sort.Slice(phonies, func(i, j int) bool { | 
 | 319 | 		return phonies[i].name < phonies[j].name | 
 | 320 | 	}) | 
 | 321 | 	lessArr := func(a, b []string) bool { | 
 | 322 | 		if len(a) == len(b) { | 
 | 323 | 			for i := range a { | 
 | 324 | 				if a[i] < b[i] { | 
 | 325 | 					return true | 
 | 326 | 				} | 
 | 327 | 			} | 
 | 328 | 			return false | 
 | 329 | 		} | 
 | 330 | 		return len(a) < len(b) | 
 | 331 | 	} | 
 | 332 | 	sort.Slice(dists, func(i, j int) bool { | 
 | 333 | 		return lessArr(dists[i].goals, dists[j].goals) || lessArr(dists[i].paths, dists[j].paths) | 
 | 334 | 	}) | 
 | 335 |  | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 336 | 	outBytes := s.writeVars(vars) | 
 | 337 |  | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 338 | 	if err := pathtools.WriteFileIfChanged(outFile, outBytes, 0666); err != nil { | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 339 | 		ctx.Errorf(err.Error()) | 
 | 340 | 	} | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 341 |  | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 342 | 	lateOutBytes := s.writeLate(phonies, dists) | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 343 |  | 
 | 344 | 	if err := pathtools.WriteFileIfChanged(lateOutFile, lateOutBytes, 0666); err != nil { | 
 | 345 | 		ctx.Errorf(err.Error()) | 
 | 346 | 	} | 
 | 347 |  | 
| Colin Cross | d9bbf4b | 2023-11-17 16:23:48 -0800 | [diff] [blame] | 348 | 	installsBytes := s.writeInstalls(katiInstalls, katiSymlinks, katiVintfManifestInstalls) | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 349 | 	if err := pathtools.WriteFileIfChanged(installsFile, installsBytes, 0666); err != nil { | 
 | 350 | 		ctx.Errorf(err.Error()) | 
 | 351 | 	} | 
| Colin Cross | 6ac9576 | 2021-11-09 13:17:44 -0800 | [diff] [blame] | 352 |  | 
| Paul Duffin | 8eb4573 | 2022-10-04 19:03:31 +0100 | [diff] [blame] | 353 | 	// Only save state for tests when testing. | 
 | 354 | 	if ctx.Config().RunningInsideUnitTest() { | 
 | 355 | 		s.varsForTesting = vars | 
 | 356 | 		s.installsForTesting = installsBytes | 
 | 357 | 	} | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 358 | } | 
 | 359 |  | 
 | 360 | func (s *makeVarsSingleton) writeVars(vars []makeVarsVariable) []byte { | 
 | 361 | 	buf := &bytes.Buffer{} | 
 | 362 |  | 
| Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 363 | 	fmt.Fprint(buf, `# Autogenerated file | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 364 |  | 
 | 365 | # Compares SOONG_$(1) against $(1), and warns if they are not equal. | 
 | 366 | # | 
 | 367 | # If the original variable is empty, then just set it to the SOONG_ version. | 
 | 368 | # | 
 | 369 | # $(1): Name of the variable to check | 
 | 370 | # $(2): If not-empty, sort the values before comparing | 
 | 371 | # $(3): Extra snippet to run if it does not match | 
 | 372 | define soong-compare-var | 
 | 373 | ifneq ($$($(1)),) | 
| Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 374 |   my_val_make := $$(strip $(if $(2),$$(sort $$($(1))),$$($(1)))) | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 375 |   my_val_soong := $(if $(2),$$(sort $$(SOONG_$(1))),$$(SOONG_$(1))) | 
 | 376 |   ifneq ($$(my_val_make),$$(my_val_soong)) | 
 | 377 |     $$(warning $(1) does not match between Make and Soong:) | 
 | 378 |     $(if $(2),$$(warning Make  adds: $$(filter-out $$(my_val_soong),$$(my_val_make))),$$(warning Make : $$(my_val_make))) | 
 | 379 |     $(if $(2),$$(warning Soong adds: $$(filter-out $$(my_val_make),$$(my_val_soong))),$$(warning Soong: $$(my_val_soong))) | 
 | 380 |     $(3) | 
 | 381 |   endif | 
 | 382 |   my_val_make := | 
 | 383 |   my_val_soong := | 
 | 384 | else | 
 | 385 |   $(1) := $$(SOONG_$(1)) | 
 | 386 | endif | 
| Dan Willemsen | de18f47 | 2016-09-30 10:16:38 -0700 | [diff] [blame] | 387 | .KATI_READONLY := $(1) SOONG_$(1) | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 388 | endef | 
 | 389 |  | 
 | 390 | my_check_failed := false | 
 | 391 |  | 
 | 392 | `) | 
 | 393 |  | 
 | 394 | 	// Write all the strict checks out first so that if one of them errors, | 
 | 395 | 	// we get all of the strict errors printed, but not the non-strict | 
 | 396 | 	// warnings. | 
 | 397 | 	for _, v := range vars { | 
 | 398 | 		if !v.strict { | 
 | 399 | 			continue | 
 | 400 | 		} | 
 | 401 |  | 
 | 402 | 		sort := "" | 
 | 403 | 		if v.sort { | 
 | 404 | 			sort = "true" | 
 | 405 | 		} | 
 | 406 |  | 
 | 407 | 		fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value) | 
 | 408 | 		fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s,my_check_failed := true))\n\n", v.name, sort) | 
 | 409 | 	} | 
 | 410 |  | 
| Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 411 | 	fmt.Fprint(buf, ` | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 412 | ifneq ($(my_check_failed),false) | 
 | 413 |   $(error Soong variable check failed) | 
 | 414 | endif | 
 | 415 | my_check_failed := | 
 | 416 |  | 
 | 417 |  | 
 | 418 | `) | 
 | 419 |  | 
 | 420 | 	for _, v := range vars { | 
 | 421 | 		if v.strict { | 
 | 422 | 			continue | 
 | 423 | 		} | 
 | 424 |  | 
 | 425 | 		sort := "" | 
 | 426 | 		if v.sort { | 
 | 427 | 			sort = "true" | 
 | 428 | 		} | 
 | 429 |  | 
 | 430 | 		fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value) | 
 | 431 | 		fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s))\n\n", v.name, sort) | 
 | 432 | 	} | 
 | 433 |  | 
 | 434 | 	fmt.Fprintln(buf, "\nsoong-compare-var :=") | 
 | 435 |  | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 436 | 	fmt.Fprintln(buf) | 
 | 437 |  | 
 | 438 | 	return buf.Bytes() | 
 | 439 | } | 
 | 440 |  | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 441 | func (s *makeVarsSingleton) writeLate(phonies []phony, dists []dist) []byte { | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 442 | 	buf := &bytes.Buffer{} | 
 | 443 |  | 
 | 444 | 	fmt.Fprint(buf, `# Autogenerated file | 
 | 445 |  | 
 | 446 | # Values written by Soong read after parsing all Android.mk files. | 
 | 447 |  | 
 | 448 |  | 
 | 449 | `) | 
 | 450 |  | 
 | 451 | 	for _, phony := range phonies { | 
 | 452 | 		fmt.Fprintf(buf, ".PHONY: %s\n", phony.name) | 
 | 453 | 		fmt.Fprintf(buf, "%s: %s\n", phony.name, strings.Join(phony.deps, "\\\n  ")) | 
 | 454 | 	} | 
 | 455 |  | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 456 | 	fmt.Fprintln(buf) | 
 | 457 |  | 
 | 458 | 	for _, dist := range dists { | 
| Colin Cross | b5399c8 | 2021-11-11 16:33:24 -0800 | [diff] [blame] | 459 | 		fmt.Fprintf(buf, ".PHONY: %s\n", strings.Join(dist.goals, " ")) | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 460 | 		fmt.Fprintf(buf, "$(call dist-for-goals,%s,%s)\n", | 
 | 461 | 			strings.Join(dist.goals, " "), strings.Join(dist.paths, " ")) | 
 | 462 | 	} | 
 | 463 |  | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 464 | 	return buf.Bytes() | 
 | 465 | } | 
 | 466 |  | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 467 | // writeInstalls writes the list of install rules generated by Soong to a makefile.  The rules | 
 | 468 | // are exported to Make instead of written directly to the ninja file so that main.mk can add | 
 | 469 | // the dependencies from the `required` property that are hard to resolve in Soong. | 
| Colin Cross | d9bbf4b | 2023-11-17 16:23:48 -0800 | [diff] [blame] | 470 | func (s *makeVarsSingleton) writeInstalls(installs, symlinks, katiVintfManifestInstalls []katiInstall) []byte { | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 471 | 	buf := &bytes.Buffer{} | 
 | 472 |  | 
 | 473 | 	fmt.Fprint(buf, `# Autogenerated file | 
 | 474 |  | 
 | 475 | # Values written by Soong to generate install rules that can be amended by Kati. | 
 | 476 |  | 
| Cole Faust | 99bec75 | 2024-05-09 11:07:20 -0700 | [diff] [blame] | 477 | EXTRA_INSTALL_ZIPS := | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 478 | `) | 
 | 479 |  | 
 | 480 | 	preserveSymlinksFlag := "-d" | 
 | 481 | 	if runtime.GOOS == "darwin" { | 
 | 482 | 		preserveSymlinksFlag = "-R" | 
 | 483 | 	} | 
 | 484 |  | 
 | 485 | 	for _, install := range installs { | 
 | 486 | 		// Write a rule for each install request in the form: | 
 | 487 | 		//  to: from [ deps ] [ | order only deps ] | 
 | 488 | 		//       cp -f -d $< $@ [ && chmod +x $@ ] | 
 | 489 | 		fmt.Fprintf(buf, "%s: %s", install.to.String(), install.from.String()) | 
 | 490 | 		for _, dep := range install.implicitDeps { | 
 | 491 | 			fmt.Fprintf(buf, " %s", dep.String()) | 
 | 492 | 		} | 
| Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 493 | 		if extraFiles := install.extraFiles; extraFiles != nil { | 
 | 494 | 			fmt.Fprintf(buf, " %s", extraFiles.zip.String()) | 
 | 495 | 		} | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 496 | 		if len(install.orderOnlyDeps) > 0 { | 
 | 497 | 			fmt.Fprintf(buf, " |") | 
 | 498 | 		} | 
 | 499 | 		for _, dep := range install.orderOnlyDeps { | 
 | 500 | 			fmt.Fprintf(buf, " %s", dep.String()) | 
 | 501 | 		} | 
 | 502 | 		fmt.Fprintln(buf) | 
| Alessandro Astone | 2b17a23 | 2022-10-25 11:44:59 +0200 | [diff] [blame] | 503 | 		fmt.Fprintln(buf, "\t@echo \"Install: $@\"") | 
| Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 504 | 		fmt.Fprintf(buf, "\trm -f $@ && cp -f %s $< $@\n", preserveSymlinksFlag) | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 505 | 		if install.executable { | 
| Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 506 | 			fmt.Fprintf(buf, "\tchmod +x $@\n") | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 507 | 		} | 
| Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 508 | 		if extraFiles := install.extraFiles; extraFiles != nil { | 
| Romain Jobredeaux | 1cef629 | 2022-05-19 11:11:51 -0400 | [diff] [blame] | 509 | 			fmt.Fprintf(buf, "\t( unzip -qDD -d '%s' '%s' 2>&1 | grep -v \"zipfile is empty\"; exit $${PIPESTATUS[0]} ) || \\\n", extraFiles.dir.String(), extraFiles.zip.String()) | 
 | 510 | 			fmt.Fprintf(buf, "\t  ( code=$$?; if [ $$code -ne 0 -a $$code -ne 1 ]; then exit $$code; fi )\n") | 
| Cole Faust | 07c4537 | 2024-05-10 12:39:05 -0700 | [diff] [blame] | 511 | 			fmt.Fprintf(buf, "EXTRA_INSTALL_ZIPS += %s:%s:%s\n", install.to.String(), extraFiles.dir.String(), extraFiles.zip.String()) | 
| Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 512 | 		} | 
| Cole Faust | 99bec75 | 2024-05-09 11:07:20 -0700 | [diff] [blame] | 513 |  | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 514 | 		fmt.Fprintln(buf) | 
 | 515 | 	} | 
| Cole Faust | 99bec75 | 2024-05-09 11:07:20 -0700 | [diff] [blame] | 516 | 	fmt.Fprintf(buf, ".KATI_READONLY := EXTRA_INSTALL_ZIPS\n") | 
| Cole Faust | b23bae7 | 2024-05-15 15:44:21 -0700 | [diff] [blame] | 517 | 	fmt.Fprintf(buf, "$(KATI_visibility_prefix EXTRA_INSTALL_ZIPS,build/make/core/Makefile)\n") | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 518 |  | 
 | 519 | 	for _, symlink := range symlinks { | 
 | 520 | 		fmt.Fprintf(buf, "%s:", symlink.to.String()) | 
| Colin Cross | 64002af | 2021-11-09 16:37:52 -0800 | [diff] [blame] | 521 | 		if symlink.from != nil { | 
| Colin Cross | d9bbf4b | 2023-11-17 16:23:48 -0800 | [diff] [blame] | 522 | 			// The katiVintfManifestInstall doesn't need updating when the target is modified, but we sometimes | 
 | 523 | 			// have a dependency on a katiVintfManifestInstall to a binary instead of to the binary directly, and | 
 | 524 | 			// the mtime of the katiVintfManifestInstall must be updated when the binary is modified, so use a | 
| Colin Cross | 64002af | 2021-11-09 16:37:52 -0800 | [diff] [blame] | 525 | 			// normal dependency here instead of an order-only dependency. | 
 | 526 | 			fmt.Fprintf(buf, " %s", symlink.from.String()) | 
 | 527 | 		} | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 528 | 		for _, dep := range symlink.implicitDeps { | 
 | 529 | 			fmt.Fprintf(buf, " %s", dep.String()) | 
 | 530 | 		} | 
| Colin Cross | 64002af | 2021-11-09 16:37:52 -0800 | [diff] [blame] | 531 | 		if len(symlink.orderOnlyDeps) > 0 { | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 532 | 			fmt.Fprintf(buf, " |") | 
 | 533 | 		} | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 534 | 		for _, dep := range symlink.orderOnlyDeps { | 
 | 535 | 			fmt.Fprintf(buf, " %s", dep.String()) | 
 | 536 | 		} | 
 | 537 | 		fmt.Fprintln(buf) | 
 | 538 |  | 
 | 539 | 		fromStr := "" | 
 | 540 | 		if symlink.from != nil { | 
 | 541 | 			rel, err := filepath.Rel(filepath.Dir(symlink.to.String()), symlink.from.String()) | 
 | 542 | 			if err != nil { | 
| Colin Cross | d9bbf4b | 2023-11-17 16:23:48 -0800 | [diff] [blame] | 543 | 				panic(fmt.Errorf("failed to find relative path for katiVintfManifestInstall from %q to %q: %w", | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 544 | 					symlink.from.String(), symlink.to.String(), err)) | 
 | 545 | 			} | 
 | 546 | 			fromStr = rel | 
 | 547 | 		} else { | 
 | 548 | 			fromStr = symlink.absFrom | 
 | 549 | 		} | 
 | 550 |  | 
| Alessandro Astone | 2b17a23 | 2022-10-25 11:44:59 +0200 | [diff] [blame] | 551 | 		fmt.Fprintln(buf, "\t@echo \"Symlink: $@\"") | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 552 | 		fmt.Fprintf(buf, "\trm -f $@ && ln -sfn %s $@", fromStr) | 
 | 553 | 		fmt.Fprintln(buf) | 
 | 554 | 		fmt.Fprintln(buf) | 
 | 555 | 	} | 
 | 556 |  | 
| Colin Cross | d9bbf4b | 2023-11-17 16:23:48 -0800 | [diff] [blame] | 557 | 	for _, install := range katiVintfManifestInstalls { | 
 | 558 | 		// Write a rule for each vintf install request that calls the copy-vintf-manifest-chedk make function. | 
 | 559 | 		fmt.Fprintf(buf, "$(eval $(call copy-vintf-manifest-checked, %s, %s))\n", install.from.String(), install.to.String()) | 
 | 560 |  | 
 | 561 | 		if len(install.implicitDeps) > 0 { | 
 | 562 | 			panic(fmt.Errorf("unsupported implicitDeps %q in vintf install rule %q", install.implicitDeps, install.to)) | 
 | 563 | 		} | 
 | 564 | 		if len(install.orderOnlyDeps) > 0 { | 
 | 565 | 			panic(fmt.Errorf("unsupported orderOnlyDeps %q in vintf install rule %q", install.orderOnlyDeps, install.to)) | 
 | 566 | 		} | 
 | 567 |  | 
 | 568 | 		fmt.Fprintln(buf) | 
 | 569 | 	} | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 570 | 	return buf.Bytes() | 
 | 571 | } | 
 | 572 |  | 
| Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 573 | func (c *makeVarsContext) DeviceConfig() DeviceConfig { | 
| Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 574 | 	return DeviceConfig{c.Config().deviceConfig} | 
| Jiyong Park | 374510b | 2018-03-19 18:23:01 +0900 | [diff] [blame] | 575 | } | 
 | 576 |  | 
| Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 577 | var ninjaDescaper = strings.NewReplacer("$$", "$") | 
 | 578 |  | 
| Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 579 | func (c *makeVarsContext) Eval(ninjaStr string) (string, error) { | 
| Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 580 | 	s, err := c.SingletonContext.Eval(c.pctx, ninjaStr) | 
| Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 581 | 	if err != nil { | 
 | 582 | 		return "", err | 
 | 583 | 	} | 
 | 584 | 	// SingletonContext.Eval returns an exapnded string that is valid for a ninja file, de-escape $$ to $ for use | 
 | 585 | 	// in a Makefile | 
 | 586 | 	return ninjaDescaper.Replace(s), nil | 
| Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 587 | } | 
 | 588 |  | 
 | 589 | func (c *makeVarsContext) addVariableRaw(name, value string, strict, sort bool) { | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 590 | 	c.vars = append(c.vars, makeVarsVariable{ | 
 | 591 | 		name:   name, | 
 | 592 | 		value:  value, | 
 | 593 | 		strict: strict, | 
 | 594 | 		sort:   sort, | 
 | 595 | 	}) | 
 | 596 | } | 
 | 597 |  | 
| Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 598 | func (c *makeVarsContext) addVariable(name, ninjaStr string, strict, sort bool) { | 
 | 599 | 	value, err := c.Eval(ninjaStr) | 
 | 600 | 	if err != nil { | 
| Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 601 | 		c.SingletonContext.Errorf(err.Error()) | 
| Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 602 | 	} | 
 | 603 | 	c.addVariableRaw(name, value, strict, sort) | 
 | 604 | } | 
 | 605 |  | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 606 | func (c *makeVarsContext) addPhony(name string, deps []string) { | 
 | 607 | 	c.phonies = append(c.phonies, phony{name, deps}) | 
 | 608 | } | 
 | 609 |  | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 610 | func (c *makeVarsContext) addDist(goals []string, paths []string) { | 
 | 611 | 	c.dists = append(c.dists, dist{ | 
 | 612 | 		goals: goals, | 
 | 613 | 		paths: paths, | 
 | 614 | 	}) | 
 | 615 | } | 
 | 616 |  | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 617 | func (c *makeVarsContext) Strict(name, ninjaStr string) { | 
 | 618 | 	c.addVariable(name, ninjaStr, true, false) | 
 | 619 | } | 
 | 620 | func (c *makeVarsContext) StrictSorted(name, ninjaStr string) { | 
 | 621 | 	c.addVariable(name, ninjaStr, true, true) | 
 | 622 | } | 
| Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 623 | func (c *makeVarsContext) StrictRaw(name, value string) { | 
 | 624 | 	c.addVariableRaw(name, value, true, false) | 
 | 625 | } | 
| Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 626 |  | 
 | 627 | func (c *makeVarsContext) Check(name, ninjaStr string) { | 
 | 628 | 	c.addVariable(name, ninjaStr, false, false) | 
 | 629 | } | 
 | 630 | func (c *makeVarsContext) CheckSorted(name, ninjaStr string) { | 
 | 631 | 	c.addVariable(name, ninjaStr, false, true) | 
 | 632 | } | 
| Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 633 | func (c *makeVarsContext) CheckRaw(name, value string) { | 
 | 634 | 	c.addVariableRaw(name, value, false, false) | 
 | 635 | } | 
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 636 |  | 
 | 637 | func (c *makeVarsContext) Phony(name string, deps ...Path) { | 
 | 638 | 	c.addPhony(name, Paths(deps).Strings()) | 
 | 639 | } | 
| Colin Cross | 3cda0d8 | 2019-09-24 13:40:07 -0700 | [diff] [blame] | 640 |  | 
 | 641 | func (c *makeVarsContext) DistForGoal(goal string, paths ...Path) { | 
 | 642 | 	c.DistForGoals([]string{goal}, paths...) | 
 | 643 | } | 
 | 644 |  | 
 | 645 | func (c *makeVarsContext) DistForGoalWithFilename(goal string, path Path, filename string) { | 
 | 646 | 	c.DistForGoalsWithFilename([]string{goal}, path, filename) | 
 | 647 | } | 
 | 648 |  | 
 | 649 | func (c *makeVarsContext) DistForGoals(goals []string, paths ...Path) { | 
 | 650 | 	c.addDist(goals, Paths(paths).Strings()) | 
 | 651 | } | 
 | 652 |  | 
 | 653 | func (c *makeVarsContext) DistForGoalsWithFilename(goals []string, path Path, filename string) { | 
 | 654 | 	c.addDist(goals, []string{path.String() + ":" + filename}) | 
 | 655 | } |