blob: 3ca7792d4a0a3129f2f9e9cfe46ef8768f81c97e [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
142// registerSingletonMakeVarsProvider adds a singleton that implements SingletonMakeVarsProvider to the list of
143// MakeVarsProviders to run.
144func registerSingletonMakeVarsProvider(singleton SingletonMakeVarsProvider) {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400145 singletonMakeVarsProviders = append(singletonMakeVarsProviders,
146 makeVarsProvider{pctx, SingletonmakeVarsProviderAdapter(singleton)})
Colin Crossed023ec2019-02-19 12:38:45 -0800147}
148
149// SingletonmakeVarsProviderAdapter converts a SingletonMakeVarsProvider to a MakeVarsProvider.
150func SingletonmakeVarsProviderAdapter(singleton SingletonMakeVarsProvider) MakeVarsProvider {
151 return func(ctx MakeVarsContext) { singleton.MakeVars(ctx) }
152}
153
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700154// ModuleMakeVarsProvider is a Module with an extra method to provide extra values to be exported to Make.
155type ModuleMakeVarsProvider interface {
156 Module
157
158 // MakeVars uses a MakeVarsModuleContext to provide extra values to be exported to Make.
159 MakeVars(ctx MakeVarsModuleContext)
160}
161
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800162///////////////////////////////////////////////////////////////////////////////
163
Colin Cross0875c522017-11-28 17:34:01 -0800164func makeVarsSingletonFunc() Singleton {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800165 return &makeVarsSingleton{}
166}
167
168type makeVarsSingleton struct{}
169
170type makeVarsProvider struct {
Colin Cross0875c522017-11-28 17:34:01 -0800171 pctx PackageContext
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800172 call MakeVarsProvider
173}
174
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400175// Collection of makevars providers that are registered in init() methods.
176var makeVarsInitProviders []makeVarsProvider
177
178// Collection of singleton makevars providers that are not registered as part of init() methods.
179var singletonMakeVarsProviders []makeVarsProvider
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800180
181type makeVarsContext struct {
Colin Cross65494b92019-02-07 14:25:51 -0800182 SingletonContext
Colin Crossc3d87d32020-06-04 13:25:17 -0700183 config Config
184 pctx PackageContext
185 vars []makeVarsVariable
186 phonies []phony
Colin Cross3cda0d82019-09-24 13:40:07 -0700187 dists []dist
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800188}
189
190var _ MakeVarsContext = &makeVarsContext{}
191
192type makeVarsVariable struct {
193 name string
194 value string
195 sort bool
196 strict bool
197}
198
Colin Crossc3d87d32020-06-04 13:25:17 -0700199type phony struct {
200 name string
201 deps []string
202}
203
Colin Cross3cda0d82019-09-24 13:40:07 -0700204type dist struct {
205 goals []string
206 paths []string
207}
208
Colin Cross0875c522017-11-28 17:34:01 -0800209func (s *makeVarsSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800210 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800211 return
212 }
213
Colin Cross988414c2020-01-11 01:11:46 +0000214 outFile := absolutePath(PathForOutput(ctx,
215 "make_vars"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800216
Colin Crossc3d87d32020-06-04 13:25:17 -0700217 lateOutFile := absolutePath(PathForOutput(ctx,
218 "late"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String())
219
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800220 if ctx.Failed() {
221 return
222 }
223
Colin Cross3cda0d82019-09-24 13:40:07 -0700224 var vars []makeVarsVariable
225 var dists []dist
Colin Crossc3d87d32020-06-04 13:25:17 -0700226 var phonies []phony
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400227 for _, provider := range append(makeVarsInitProviders) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800228 mctx := &makeVarsContext{
Colin Cross65494b92019-02-07 14:25:51 -0800229 SingletonContext: ctx,
230 pctx: provider.pctx,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800231 }
232
233 provider.call(mctx)
234
235 vars = append(vars, mctx.vars...)
Colin Crossc3d87d32020-06-04 13:25:17 -0700236 phonies = append(phonies, mctx.phonies...)
Colin Cross3cda0d82019-09-24 13:40:07 -0700237 dists = append(dists, mctx.dists...)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800238 }
239
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400240 for _, provider := range append(singletonMakeVarsProviders) {
241 mctx := &makeVarsContext{
242 SingletonContext: ctx,
243 pctx: provider.pctx,
244 }
245
246 provider.call(mctx)
247
248 vars = append(vars, mctx.vars...)
249 phonies = append(phonies, mctx.phonies...)
250 dists = append(dists, mctx.dists...)
251 }
252
253 // Clear singleton makevars providers after use. Since these are in-memory
254 // singletons, this ensures state is reset if the build tree is processed
255 // multiple times.
256 // TODO(cparsons): Clean up makeVarsProviders to be part of the context.
257 singletonMakeVarsProviders = nil
258
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700259 ctx.VisitAllModules(func(m Module) {
Jiyong Parkf78531b2020-09-09 17:14:28 +0900260 if provider, ok := m.(ModuleMakeVarsProvider); ok && m.Enabled() {
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700261 mctx := &makeVarsContext{
262 SingletonContext: ctx,
263 }
264
265 provider.MakeVars(mctx)
266
267 vars = append(vars, mctx.vars...)
268 phonies = append(phonies, mctx.phonies...)
269 dists = append(dists, mctx.dists...)
270 }
271 })
272
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800273 if ctx.Failed() {
274 return
275 }
276
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700277 sort.Slice(vars, func(i, j int) bool {
278 return vars[i].name < vars[j].name
279 })
280 sort.Slice(phonies, func(i, j int) bool {
281 return phonies[i].name < phonies[j].name
282 })
283 lessArr := func(a, b []string) bool {
284 if len(a) == len(b) {
285 for i := range a {
286 if a[i] < b[i] {
287 return true
288 }
289 }
290 return false
291 }
292 return len(a) < len(b)
293 }
294 sort.Slice(dists, func(i, j int) bool {
295 return lessArr(dists[i].goals, dists[j].goals) || lessArr(dists[i].paths, dists[j].paths)
296 })
297
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800298 outBytes := s.writeVars(vars)
299
Colin Crossc3d87d32020-06-04 13:25:17 -0700300 if err := pathtools.WriteFileIfChanged(outFile, outBytes, 0666); err != nil {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800301 ctx.Errorf(err.Error())
302 }
Colin Crossc3d87d32020-06-04 13:25:17 -0700303
Colin Cross3cda0d82019-09-24 13:40:07 -0700304 lateOutBytes := s.writeLate(phonies, dists)
Colin Crossc3d87d32020-06-04 13:25:17 -0700305
306 if err := pathtools.WriteFileIfChanged(lateOutFile, lateOutBytes, 0666); err != nil {
307 ctx.Errorf(err.Error())
308 }
309
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800310}
311
312func (s *makeVarsSingleton) writeVars(vars []makeVarsVariable) []byte {
313 buf := &bytes.Buffer{}
314
Dan Willemsen59339a22018-07-22 21:18:45 -0700315 fmt.Fprint(buf, `# Autogenerated file
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800316
317# Compares SOONG_$(1) against $(1), and warns if they are not equal.
318#
319# If the original variable is empty, then just set it to the SOONG_ version.
320#
321# $(1): Name of the variable to check
322# $(2): If not-empty, sort the values before comparing
323# $(3): Extra snippet to run if it does not match
324define soong-compare-var
325ifneq ($$($(1)),)
Dan Willemsen558e5172016-05-19 16:58:46 -0700326 my_val_make := $$(strip $(if $(2),$$(sort $$($(1))),$$($(1))))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800327 my_val_soong := $(if $(2),$$(sort $$(SOONG_$(1))),$$(SOONG_$(1)))
328 ifneq ($$(my_val_make),$$(my_val_soong))
329 $$(warning $(1) does not match between Make and Soong:)
330 $(if $(2),$$(warning Make adds: $$(filter-out $$(my_val_soong),$$(my_val_make))),$$(warning Make : $$(my_val_make)))
331 $(if $(2),$$(warning Soong adds: $$(filter-out $$(my_val_make),$$(my_val_soong))),$$(warning Soong: $$(my_val_soong)))
332 $(3)
333 endif
334 my_val_make :=
335 my_val_soong :=
336else
337 $(1) := $$(SOONG_$(1))
338endif
Dan Willemsende18f472016-09-30 10:16:38 -0700339.KATI_READONLY := $(1) SOONG_$(1)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800340endef
341
342my_check_failed := false
343
344`)
345
346 // Write all the strict checks out first so that if one of them errors,
347 // we get all of the strict errors printed, but not the non-strict
348 // warnings.
349 for _, v := range vars {
350 if !v.strict {
351 continue
352 }
353
354 sort := ""
355 if v.sort {
356 sort = "true"
357 }
358
359 fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value)
360 fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s,my_check_failed := true))\n\n", v.name, sort)
361 }
362
Dan Willemsen59339a22018-07-22 21:18:45 -0700363 fmt.Fprint(buf, `
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800364ifneq ($(my_check_failed),false)
365 $(error Soong variable check failed)
366endif
367my_check_failed :=
368
369
370`)
371
372 for _, v := range vars {
373 if v.strict {
374 continue
375 }
376
377 sort := ""
378 if v.sort {
379 sort = "true"
380 }
381
382 fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value)
383 fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s))\n\n", v.name, sort)
384 }
385
386 fmt.Fprintln(buf, "\nsoong-compare-var :=")
387
Colin Crossc3d87d32020-06-04 13:25:17 -0700388 fmt.Fprintln(buf)
389
390 return buf.Bytes()
391}
392
Colin Cross3cda0d82019-09-24 13:40:07 -0700393func (s *makeVarsSingleton) writeLate(phonies []phony, dists []dist) []byte {
Colin Crossc3d87d32020-06-04 13:25:17 -0700394 buf := &bytes.Buffer{}
395
396 fmt.Fprint(buf, `# Autogenerated file
397
398# Values written by Soong read after parsing all Android.mk files.
399
400
401`)
402
403 for _, phony := range phonies {
404 fmt.Fprintf(buf, ".PHONY: %s\n", phony.name)
405 fmt.Fprintf(buf, "%s: %s\n", phony.name, strings.Join(phony.deps, "\\\n "))
406 }
407
Colin Cross3cda0d82019-09-24 13:40:07 -0700408 fmt.Fprintln(buf)
409
410 for _, dist := range dists {
411 fmt.Fprintf(buf, "$(call dist-for-goals,%s,%s)\n",
412 strings.Join(dist.goals, " "), strings.Join(dist.paths, " "))
413 }
414
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800415 return buf.Bytes()
416}
417
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700418func (c *makeVarsContext) DeviceConfig() DeviceConfig {
Colin Cross65494b92019-02-07 14:25:51 -0800419 return DeviceConfig{c.Config().deviceConfig}
Jiyong Park374510b2018-03-19 18:23:01 +0900420}
421
Colin Cross31656952018-05-24 16:11:20 -0700422var ninjaDescaper = strings.NewReplacer("$$", "$")
423
Dan Willemsen558e5172016-05-19 16:58:46 -0700424func (c *makeVarsContext) Eval(ninjaStr string) (string, error) {
Colin Cross65494b92019-02-07 14:25:51 -0800425 s, err := c.SingletonContext.Eval(c.pctx, ninjaStr)
Colin Cross31656952018-05-24 16:11:20 -0700426 if err != nil {
427 return "", err
428 }
429 // SingletonContext.Eval returns an exapnded string that is valid for a ninja file, de-escape $$ to $ for use
430 // in a Makefile
431 return ninjaDescaper.Replace(s), nil
Dan Willemsen558e5172016-05-19 16:58:46 -0700432}
433
434func (c *makeVarsContext) addVariableRaw(name, value string, strict, sort bool) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800435 c.vars = append(c.vars, makeVarsVariable{
436 name: name,
437 value: value,
438 strict: strict,
439 sort: sort,
440 })
441}
442
Dan Willemsen558e5172016-05-19 16:58:46 -0700443func (c *makeVarsContext) addVariable(name, ninjaStr string, strict, sort bool) {
444 value, err := c.Eval(ninjaStr)
445 if err != nil {
Colin Cross65494b92019-02-07 14:25:51 -0800446 c.SingletonContext.Errorf(err.Error())
Dan Willemsen558e5172016-05-19 16:58:46 -0700447 }
448 c.addVariableRaw(name, value, strict, sort)
449}
450
Colin Crossc3d87d32020-06-04 13:25:17 -0700451func (c *makeVarsContext) addPhony(name string, deps []string) {
452 c.phonies = append(c.phonies, phony{name, deps})
453}
454
Colin Cross3cda0d82019-09-24 13:40:07 -0700455func (c *makeVarsContext) addDist(goals []string, paths []string) {
456 c.dists = append(c.dists, dist{
457 goals: goals,
458 paths: paths,
459 })
460}
461
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800462func (c *makeVarsContext) Strict(name, ninjaStr string) {
463 c.addVariable(name, ninjaStr, true, false)
464}
465func (c *makeVarsContext) StrictSorted(name, ninjaStr string) {
466 c.addVariable(name, ninjaStr, true, true)
467}
Dan Willemsen558e5172016-05-19 16:58:46 -0700468func (c *makeVarsContext) StrictRaw(name, value string) {
469 c.addVariableRaw(name, value, true, false)
470}
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800471
472func (c *makeVarsContext) Check(name, ninjaStr string) {
473 c.addVariable(name, ninjaStr, false, false)
474}
475func (c *makeVarsContext) CheckSorted(name, ninjaStr string) {
476 c.addVariable(name, ninjaStr, false, true)
477}
Dan Willemsen558e5172016-05-19 16:58:46 -0700478func (c *makeVarsContext) CheckRaw(name, value string) {
479 c.addVariableRaw(name, value, false, false)
480}
Colin Crossc3d87d32020-06-04 13:25:17 -0700481
482func (c *makeVarsContext) Phony(name string, deps ...Path) {
483 c.addPhony(name, Paths(deps).Strings())
484}
Colin Cross3cda0d82019-09-24 13:40:07 -0700485
486func (c *makeVarsContext) DistForGoal(goal string, paths ...Path) {
487 c.DistForGoals([]string{goal}, paths...)
488}
489
490func (c *makeVarsContext) DistForGoalWithFilename(goal string, path Path, filename string) {
491 c.DistForGoalsWithFilename([]string{goal}, path, filename)
492}
493
494func (c *makeVarsContext) DistForGoals(goals []string, paths ...Path) {
495 c.addDist(goals, Paths(paths).Strings())
496}
497
498func (c *makeVarsContext) DistForGoalsWithFilename(goals []string, path Path, filename string) {
499 c.addDist(goals, []string{path.String() + ":" + filename})
500}