blob: f78439596db898e9b0b0d9eba39f40e0c89a5db6 [file] [log] [blame]
Dan Willemsen4b7d5de2016-01-12 23:20:28 -08001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080016
17import (
18 "bytes"
19 "fmt"
Dan Willemsen6a6478d2020-07-17 19:28:53 -070020 "sort"
Colin Cross31656952018-05-24 16:11:20 -070021 "strings"
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080022
Colin Cross65494b92019-02-07 14:25:51 -080023 "github.com/google/blueprint"
Colin Crossc3d87d32020-06-04 13:25:17 -070024 "github.com/google/blueprint/pathtools"
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080025 "github.com/google/blueprint/proptools"
26)
27
Dan Albertf5415d72017-08-17 16:19:59 -070028func init() {
29 RegisterMakeVarsProvider(pctx, androidMakeVarsProvider)
30}
31
32func androidMakeVarsProvider(ctx MakeVarsContext) {
Dan Albert1a246272020-07-06 14:49:35 -070033 ctx.Strict("MIN_SUPPORTED_SDK_VERSION", ctx.Config().MinSupportedSdkVersion().String())
Dan Albertf5415d72017-08-17 16:19:59 -070034}
35
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080036///////////////////////////////////////////////////////////////////////////////
Dan Willemsen6a6478d2020-07-17 19:28:53 -070037
38// BaseMakeVarsContext contains the common functions for other packages to use
39// to declare make variables
40type BaseMakeVarsContext interface {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080041 Config() Config
Dan Willemsen3fb1fae2018-03-12 15:30:26 -070042 DeviceConfig() DeviceConfig
Colin Cross65494b92019-02-07 14:25:51 -080043 AddNinjaFileDeps(deps ...string)
Colin Cross65494b92019-02-07 14:25:51 -080044
Colin Cross65494b92019-02-07 14:25:51 -080045 Failed() bool
46
Dan Willemsen558e5172016-05-19 16:58:46 -070047 // These are equivalent to Strict and Check, but do not attempt to
48 // evaluate the values before writing them to the Makefile. They can
49 // be used when all ninja variables have already been evaluated through
50 // Eval().
51 StrictRaw(name, value string)
52 CheckRaw(name, value string)
Colin Cross8177ad22019-11-04 10:27:48 -080053
54 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
55 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
56 // builder whenever a file matching the pattern as added or removed, without rerunning if a
57 // file that does not match the pattern is added to a searched directory.
58 GlobWithDeps(pattern string, excludes []string) ([]string, error)
Colin Crossc3d87d32020-06-04 13:25:17 -070059
60 // Phony creates a phony rule in Make, which will allow additional DistForGoal
61 // dependencies to be added to it. Phony can be called on the same name multiple
62 // times to add additional dependencies.
63 Phony(names string, deps ...Path)
Colin Cross3cda0d82019-09-24 13:40:07 -070064
65 // DistForGoal creates a rule to copy one or more Paths to the artifacts
66 // directory on the build server when the specified goal is built.
67 DistForGoal(goal string, paths ...Path)
68
69 // DistForGoalWithFilename creates a rule to copy a Path to the artifacts
70 // directory on the build server with the given filename when the specified
71 // goal is built.
72 DistForGoalWithFilename(goal string, path Path, filename string)
73
74 // DistForGoals creates a rule to copy one or more Paths to the artifacts
75 // directory on the build server when any of the specified goals are built.
76 DistForGoals(goals []string, paths ...Path)
77
78 // DistForGoalsWithFilename creates a rule to copy a Path to the artifacts
79 // directory on the build server with the given filename when any of the
80 // specified goals are built.
81 DistForGoalsWithFilename(goals []string, path Path, filename string)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080082}
83
Dan Willemsen6a6478d2020-07-17 19:28:53 -070084// MakeVarsContext contains the set of functions available for MakeVarsProvider
85// and SingletonMakeVarsProvider implementations.
86type MakeVarsContext interface {
87 BaseMakeVarsContext
88
89 ModuleName(module blueprint.Module) string
90 ModuleDir(module blueprint.Module) string
91 ModuleSubDir(module blueprint.Module) string
92 ModuleType(module blueprint.Module) string
93 BlueprintFile(module blueprint.Module) string
94
95 ModuleErrorf(module blueprint.Module, format string, args ...interface{})
96 Errorf(format string, args ...interface{})
97
98 VisitAllModules(visit func(Module))
99 VisitAllModulesIf(pred func(Module) bool, visit func(Module))
100
101 // Verify the make variable matches the Soong version, fail the build
102 // if it does not. If the make variable is empty, just set it.
103 Strict(name, ninjaStr string)
104 // Check to see if the make variable matches the Soong version, warn if
105 // it does not. If the make variable is empty, just set it.
106 Check(name, ninjaStr string)
107
108 // These are equivalent to the above, but sort the make and soong
109 // variables before comparing them. They also show the unique entries
110 // in each list when displaying the difference, instead of the entire
111 // string.
112 StrictSorted(name, ninjaStr string)
113 CheckSorted(name, ninjaStr string)
114
115 // Evaluates a ninja string and returns the result. Used if more
116 // complicated modification needs to happen before giving it to Make.
117 Eval(ninjaStr string) (string, error)
118}
119
120// MakeVarsModuleContext contains the set of functions available for modules
121// implementing the ModuleMakeVarsProvider interface.
122type MakeVarsModuleContext interface {
123 BaseMakeVarsContext
124}
125
Colin Cross65494b92019-02-07 14:25:51 -0800126var _ PathContext = MakeVarsContext(nil)
127
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800128type MakeVarsProvider func(ctx MakeVarsContext)
129
Colin Cross0875c522017-11-28 17:34:01 -0800130func RegisterMakeVarsProvider(pctx PackageContext, provider MakeVarsProvider) {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400131 makeVarsInitProviders = append(makeVarsInitProviders, makeVarsProvider{pctx, provider})
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800132}
133
Colin Crossed023ec2019-02-19 12:38:45 -0800134// SingletonMakeVarsProvider is a Singleton with an extra method to provide extra values to be exported to Make.
135type SingletonMakeVarsProvider interface {
136 Singleton
137
138 // MakeVars uses a MakeVarsContext to provide extra values to be exported to Make.
139 MakeVars(ctx MakeVarsContext)
140}
141
Colin Cross06fa5882020-10-29 18:21:38 -0700142var singletonMakeVarsProvidersKey = NewOnceKey("singletonMakeVarsProvidersKey")
143
144// registerSingletonMakeVarsProvider adds a singleton that implements SingletonMakeVarsProvider to
145// the list of MakeVarsProviders to run.
146func registerSingletonMakeVarsProvider(config Config, singleton SingletonMakeVarsProvider) {
147 // Singletons are registered on the Context and may be different between different Contexts,
148 // for example when running multiple tests. Store the SingletonMakeVarsProviders in the
149 // Config so they are attached to the Context.
150 singletonMakeVarsProviders := config.Once(singletonMakeVarsProvidersKey, func() interface{} {
151 return &[]makeVarsProvider{}
152 }).(*[]makeVarsProvider)
153
154 *singletonMakeVarsProviders = append(*singletonMakeVarsProviders,
155 makeVarsProvider{pctx, singletonMakeVarsProviderAdapter(singleton)})
Colin Crossed023ec2019-02-19 12:38:45 -0800156}
157
Colin Cross06fa5882020-10-29 18:21:38 -0700158// singletonMakeVarsProviderAdapter converts a SingletonMakeVarsProvider to a MakeVarsProvider.
159func singletonMakeVarsProviderAdapter(singleton SingletonMakeVarsProvider) MakeVarsProvider {
Colin Crossed023ec2019-02-19 12:38:45 -0800160 return func(ctx MakeVarsContext) { singleton.MakeVars(ctx) }
161}
162
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700163// ModuleMakeVarsProvider is a Module with an extra method to provide extra values to be exported to Make.
164type ModuleMakeVarsProvider interface {
165 Module
166
167 // MakeVars uses a MakeVarsModuleContext to provide extra values to be exported to Make.
168 MakeVars(ctx MakeVarsModuleContext)
169}
170
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800171///////////////////////////////////////////////////////////////////////////////
172
Colin Cross0875c522017-11-28 17:34:01 -0800173func makeVarsSingletonFunc() Singleton {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800174 return &makeVarsSingleton{}
175}
176
177type makeVarsSingleton struct{}
178
179type makeVarsProvider struct {
Colin Cross0875c522017-11-28 17:34:01 -0800180 pctx PackageContext
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800181 call MakeVarsProvider
182}
183
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400184// Collection of makevars providers that are registered in init() methods.
185var makeVarsInitProviders []makeVarsProvider
186
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800187type makeVarsContext struct {
Colin Cross65494b92019-02-07 14:25:51 -0800188 SingletonContext
Colin Crossc3d87d32020-06-04 13:25:17 -0700189 config Config
190 pctx PackageContext
191 vars []makeVarsVariable
192 phonies []phony
Colin Cross3cda0d82019-09-24 13:40:07 -0700193 dists []dist
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800194}
195
196var _ MakeVarsContext = &makeVarsContext{}
197
198type makeVarsVariable struct {
199 name string
200 value string
201 sort bool
202 strict bool
203}
204
Colin Crossc3d87d32020-06-04 13:25:17 -0700205type phony struct {
206 name string
207 deps []string
208}
209
Colin Cross3cda0d82019-09-24 13:40:07 -0700210type dist struct {
211 goals []string
212 paths []string
213}
214
Colin Cross0875c522017-11-28 17:34:01 -0800215func (s *makeVarsSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800216 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800217 return
218 }
219
Colin Cross988414c2020-01-11 01:11:46 +0000220 outFile := absolutePath(PathForOutput(ctx,
221 "make_vars"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800222
Colin Crossc3d87d32020-06-04 13:25:17 -0700223 lateOutFile := absolutePath(PathForOutput(ctx,
224 "late"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String())
225
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800226 if ctx.Failed() {
227 return
228 }
229
Colin Cross3cda0d82019-09-24 13:40:07 -0700230 var vars []makeVarsVariable
231 var dists []dist
Colin Crossc3d87d32020-06-04 13:25:17 -0700232 var phonies []phony
Colin Cross06fa5882020-10-29 18:21:38 -0700233
234 providers := append([]makeVarsProvider(nil), makeVarsInitProviders...)
235 providers = append(providers, *ctx.Config().Get(singletonMakeVarsProvidersKey).(*[]makeVarsProvider)...)
236
237 for _, provider := range providers {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800238 mctx := &makeVarsContext{
Colin Cross65494b92019-02-07 14:25:51 -0800239 SingletonContext: ctx,
240 pctx: provider.pctx,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800241 }
242
243 provider.call(mctx)
244
245 vars = append(vars, mctx.vars...)
Colin Crossc3d87d32020-06-04 13:25:17 -0700246 phonies = append(phonies, mctx.phonies...)
Colin Cross3cda0d82019-09-24 13:40:07 -0700247 dists = append(dists, mctx.dists...)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800248 }
249
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700250 ctx.VisitAllModules(func(m Module) {
Jiyong Parkf78531b2020-09-09 17:14:28 +0900251 if provider, ok := m.(ModuleMakeVarsProvider); ok && m.Enabled() {
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700252 mctx := &makeVarsContext{
253 SingletonContext: ctx,
254 }
255
256 provider.MakeVars(mctx)
257
258 vars = append(vars, mctx.vars...)
259 phonies = append(phonies, mctx.phonies...)
260 dists = append(dists, mctx.dists...)
261 }
262 })
263
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800264 if ctx.Failed() {
265 return
266 }
267
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700268 sort.Slice(vars, func(i, j int) bool {
269 return vars[i].name < vars[j].name
270 })
271 sort.Slice(phonies, func(i, j int) bool {
272 return phonies[i].name < phonies[j].name
273 })
274 lessArr := func(a, b []string) bool {
275 if len(a) == len(b) {
276 for i := range a {
277 if a[i] < b[i] {
278 return true
279 }
280 }
281 return false
282 }
283 return len(a) < len(b)
284 }
285 sort.Slice(dists, func(i, j int) bool {
286 return lessArr(dists[i].goals, dists[j].goals) || lessArr(dists[i].paths, dists[j].paths)
287 })
288
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800289 outBytes := s.writeVars(vars)
290
Colin Crossc3d87d32020-06-04 13:25:17 -0700291 if err := pathtools.WriteFileIfChanged(outFile, outBytes, 0666); err != nil {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800292 ctx.Errorf(err.Error())
293 }
Colin Crossc3d87d32020-06-04 13:25:17 -0700294
Colin Cross3cda0d82019-09-24 13:40:07 -0700295 lateOutBytes := s.writeLate(phonies, dists)
Colin Crossc3d87d32020-06-04 13:25:17 -0700296
297 if err := pathtools.WriteFileIfChanged(lateOutFile, lateOutBytes, 0666); err != nil {
298 ctx.Errorf(err.Error())
299 }
300
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800301}
302
303func (s *makeVarsSingleton) writeVars(vars []makeVarsVariable) []byte {
304 buf := &bytes.Buffer{}
305
Dan Willemsen59339a22018-07-22 21:18:45 -0700306 fmt.Fprint(buf, `# Autogenerated file
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800307
308# Compares SOONG_$(1) against $(1), and warns if they are not equal.
309#
310# If the original variable is empty, then just set it to the SOONG_ version.
311#
312# $(1): Name of the variable to check
313# $(2): If not-empty, sort the values before comparing
314# $(3): Extra snippet to run if it does not match
315define soong-compare-var
316ifneq ($$($(1)),)
Dan Willemsen558e5172016-05-19 16:58:46 -0700317 my_val_make := $$(strip $(if $(2),$$(sort $$($(1))),$$($(1))))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800318 my_val_soong := $(if $(2),$$(sort $$(SOONG_$(1))),$$(SOONG_$(1)))
319 ifneq ($$(my_val_make),$$(my_val_soong))
320 $$(warning $(1) does not match between Make and Soong:)
321 $(if $(2),$$(warning Make adds: $$(filter-out $$(my_val_soong),$$(my_val_make))),$$(warning Make : $$(my_val_make)))
322 $(if $(2),$$(warning Soong adds: $$(filter-out $$(my_val_make),$$(my_val_soong))),$$(warning Soong: $$(my_val_soong)))
323 $(3)
324 endif
325 my_val_make :=
326 my_val_soong :=
327else
328 $(1) := $$(SOONG_$(1))
329endif
Dan Willemsende18f472016-09-30 10:16:38 -0700330.KATI_READONLY := $(1) SOONG_$(1)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800331endef
332
333my_check_failed := false
334
335`)
336
337 // Write all the strict checks out first so that if one of them errors,
338 // we get all of the strict errors printed, but not the non-strict
339 // warnings.
340 for _, v := range vars {
341 if !v.strict {
342 continue
343 }
344
345 sort := ""
346 if v.sort {
347 sort = "true"
348 }
349
350 fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value)
351 fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s,my_check_failed := true))\n\n", v.name, sort)
352 }
353
Dan Willemsen59339a22018-07-22 21:18:45 -0700354 fmt.Fprint(buf, `
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800355ifneq ($(my_check_failed),false)
356 $(error Soong variable check failed)
357endif
358my_check_failed :=
359
360
361`)
362
363 for _, v := range vars {
364 if v.strict {
365 continue
366 }
367
368 sort := ""
369 if v.sort {
370 sort = "true"
371 }
372
373 fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value)
374 fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s))\n\n", v.name, sort)
375 }
376
377 fmt.Fprintln(buf, "\nsoong-compare-var :=")
378
Colin Crossc3d87d32020-06-04 13:25:17 -0700379 fmt.Fprintln(buf)
380
381 return buf.Bytes()
382}
383
Colin Cross3cda0d82019-09-24 13:40:07 -0700384func (s *makeVarsSingleton) writeLate(phonies []phony, dists []dist) []byte {
Colin Crossc3d87d32020-06-04 13:25:17 -0700385 buf := &bytes.Buffer{}
386
387 fmt.Fprint(buf, `# Autogenerated file
388
389# Values written by Soong read after parsing all Android.mk files.
390
391
392`)
393
394 for _, phony := range phonies {
395 fmt.Fprintf(buf, ".PHONY: %s\n", phony.name)
396 fmt.Fprintf(buf, "%s: %s\n", phony.name, strings.Join(phony.deps, "\\\n "))
397 }
398
Colin Cross3cda0d82019-09-24 13:40:07 -0700399 fmt.Fprintln(buf)
400
401 for _, dist := range dists {
402 fmt.Fprintf(buf, "$(call dist-for-goals,%s,%s)\n",
403 strings.Join(dist.goals, " "), strings.Join(dist.paths, " "))
404 }
405
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800406 return buf.Bytes()
407}
408
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700409func (c *makeVarsContext) DeviceConfig() DeviceConfig {
Colin Cross65494b92019-02-07 14:25:51 -0800410 return DeviceConfig{c.Config().deviceConfig}
Jiyong Park374510b2018-03-19 18:23:01 +0900411}
412
Colin Cross31656952018-05-24 16:11:20 -0700413var ninjaDescaper = strings.NewReplacer("$$", "$")
414
Dan Willemsen558e5172016-05-19 16:58:46 -0700415func (c *makeVarsContext) Eval(ninjaStr string) (string, error) {
Colin Cross65494b92019-02-07 14:25:51 -0800416 s, err := c.SingletonContext.Eval(c.pctx, ninjaStr)
Colin Cross31656952018-05-24 16:11:20 -0700417 if err != nil {
418 return "", err
419 }
420 // SingletonContext.Eval returns an exapnded string that is valid for a ninja file, de-escape $$ to $ for use
421 // in a Makefile
422 return ninjaDescaper.Replace(s), nil
Dan Willemsen558e5172016-05-19 16:58:46 -0700423}
424
425func (c *makeVarsContext) addVariableRaw(name, value string, strict, sort bool) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800426 c.vars = append(c.vars, makeVarsVariable{
427 name: name,
428 value: value,
429 strict: strict,
430 sort: sort,
431 })
432}
433
Dan Willemsen558e5172016-05-19 16:58:46 -0700434func (c *makeVarsContext) addVariable(name, ninjaStr string, strict, sort bool) {
435 value, err := c.Eval(ninjaStr)
436 if err != nil {
Colin Cross65494b92019-02-07 14:25:51 -0800437 c.SingletonContext.Errorf(err.Error())
Dan Willemsen558e5172016-05-19 16:58:46 -0700438 }
439 c.addVariableRaw(name, value, strict, sort)
440}
441
Colin Crossc3d87d32020-06-04 13:25:17 -0700442func (c *makeVarsContext) addPhony(name string, deps []string) {
443 c.phonies = append(c.phonies, phony{name, deps})
444}
445
Colin Cross3cda0d82019-09-24 13:40:07 -0700446func (c *makeVarsContext) addDist(goals []string, paths []string) {
447 c.dists = append(c.dists, dist{
448 goals: goals,
449 paths: paths,
450 })
451}
452
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800453func (c *makeVarsContext) Strict(name, ninjaStr string) {
454 c.addVariable(name, ninjaStr, true, false)
455}
456func (c *makeVarsContext) StrictSorted(name, ninjaStr string) {
457 c.addVariable(name, ninjaStr, true, true)
458}
Dan Willemsen558e5172016-05-19 16:58:46 -0700459func (c *makeVarsContext) StrictRaw(name, value string) {
460 c.addVariableRaw(name, value, true, false)
461}
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800462
463func (c *makeVarsContext) Check(name, ninjaStr string) {
464 c.addVariable(name, ninjaStr, false, false)
465}
466func (c *makeVarsContext) CheckSorted(name, ninjaStr string) {
467 c.addVariable(name, ninjaStr, false, true)
468}
Dan Willemsen558e5172016-05-19 16:58:46 -0700469func (c *makeVarsContext) CheckRaw(name, value string) {
470 c.addVariableRaw(name, value, false, false)
471}
Colin Crossc3d87d32020-06-04 13:25:17 -0700472
473func (c *makeVarsContext) Phony(name string, deps ...Path) {
474 c.addPhony(name, Paths(deps).Strings())
475}
Colin Cross3cda0d82019-09-24 13:40:07 -0700476
477func (c *makeVarsContext) DistForGoal(goal string, paths ...Path) {
478 c.DistForGoals([]string{goal}, paths...)
479}
480
481func (c *makeVarsContext) DistForGoalWithFilename(goal string, path Path, filename string) {
482 c.DistForGoalsWithFilename([]string{goal}, path, filename)
483}
484
485func (c *makeVarsContext) DistForGoals(goals []string, paths ...Path) {
486 c.addDist(goals, Paths(paths).Strings())
487}
488
489func (c *makeVarsContext) DistForGoalsWithFilename(goals []string, path Path, filename string) {
490 c.addDist(goals, []string{path.String() + ":" + filename})
491}