blob: b5efa4d99e53fb28c9ed0305bf31af1e2bf62b9c [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) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800131 makeVarsProviders = append(makeVarsProviders, makeVarsProvider{pctx, provider})
132}
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) {
145 makeVarsProviders = append(makeVarsProviders, makeVarsProvider{pctx, SingletonmakeVarsProviderAdapter(singleton)})
146}
147
148// SingletonmakeVarsProviderAdapter converts a SingletonMakeVarsProvider to a MakeVarsProvider.
149func SingletonmakeVarsProviderAdapter(singleton SingletonMakeVarsProvider) MakeVarsProvider {
150 return func(ctx MakeVarsContext) { singleton.MakeVars(ctx) }
151}
152
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700153// ModuleMakeVarsProvider is a Module with an extra method to provide extra values to be exported to Make.
154type ModuleMakeVarsProvider interface {
155 Module
156
157 // MakeVars uses a MakeVarsModuleContext to provide extra values to be exported to Make.
158 MakeVars(ctx MakeVarsModuleContext)
159}
160
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800161///////////////////////////////////////////////////////////////////////////////
162
Colin Cross0875c522017-11-28 17:34:01 -0800163func makeVarsSingletonFunc() Singleton {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800164 return &makeVarsSingleton{}
165}
166
167type makeVarsSingleton struct{}
168
169type makeVarsProvider struct {
Colin Cross0875c522017-11-28 17:34:01 -0800170 pctx PackageContext
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800171 call MakeVarsProvider
172}
173
174var makeVarsProviders []makeVarsProvider
175
176type makeVarsContext struct {
Colin Cross65494b92019-02-07 14:25:51 -0800177 SingletonContext
Colin Crossc3d87d32020-06-04 13:25:17 -0700178 config Config
179 pctx PackageContext
180 vars []makeVarsVariable
181 phonies []phony
Colin Cross3cda0d82019-09-24 13:40:07 -0700182 dists []dist
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800183}
184
185var _ MakeVarsContext = &makeVarsContext{}
186
187type makeVarsVariable struct {
188 name string
189 value string
190 sort bool
191 strict bool
192}
193
Colin Crossc3d87d32020-06-04 13:25:17 -0700194type phony struct {
195 name string
196 deps []string
197}
198
Colin Cross3cda0d82019-09-24 13:40:07 -0700199type dist struct {
200 goals []string
201 paths []string
202}
203
Colin Cross0875c522017-11-28 17:34:01 -0800204func (s *makeVarsSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800205 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800206 return
207 }
208
Colin Cross988414c2020-01-11 01:11:46 +0000209 outFile := absolutePath(PathForOutput(ctx,
210 "make_vars"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800211
Colin Crossc3d87d32020-06-04 13:25:17 -0700212 lateOutFile := absolutePath(PathForOutput(ctx,
213 "late"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String())
214
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800215 if ctx.Failed() {
216 return
217 }
218
Colin Cross3cda0d82019-09-24 13:40:07 -0700219 var vars []makeVarsVariable
220 var dists []dist
Colin Crossc3d87d32020-06-04 13:25:17 -0700221 var phonies []phony
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800222 for _, provider := range makeVarsProviders {
223 mctx := &makeVarsContext{
Colin Cross65494b92019-02-07 14:25:51 -0800224 SingletonContext: ctx,
225 pctx: provider.pctx,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800226 }
227
228 provider.call(mctx)
229
230 vars = append(vars, mctx.vars...)
Colin Crossc3d87d32020-06-04 13:25:17 -0700231 phonies = append(phonies, mctx.phonies...)
Colin Cross3cda0d82019-09-24 13:40:07 -0700232 dists = append(dists, mctx.dists...)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800233 }
234
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700235 ctx.VisitAllModules(func(m Module) {
236 if provider, ok := m.(ModuleMakeVarsProvider); ok {
237 mctx := &makeVarsContext{
238 SingletonContext: ctx,
239 }
240
241 provider.MakeVars(mctx)
242
243 vars = append(vars, mctx.vars...)
244 phonies = append(phonies, mctx.phonies...)
245 dists = append(dists, mctx.dists...)
246 }
247 })
248
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800249 if ctx.Failed() {
250 return
251 }
252
Dan Willemsen6a6478d2020-07-17 19:28:53 -0700253 sort.Slice(vars, func(i, j int) bool {
254 return vars[i].name < vars[j].name
255 })
256 sort.Slice(phonies, func(i, j int) bool {
257 return phonies[i].name < phonies[j].name
258 })
259 lessArr := func(a, b []string) bool {
260 if len(a) == len(b) {
261 for i := range a {
262 if a[i] < b[i] {
263 return true
264 }
265 }
266 return false
267 }
268 return len(a) < len(b)
269 }
270 sort.Slice(dists, func(i, j int) bool {
271 return lessArr(dists[i].goals, dists[j].goals) || lessArr(dists[i].paths, dists[j].paths)
272 })
273
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800274 outBytes := s.writeVars(vars)
275
Colin Crossc3d87d32020-06-04 13:25:17 -0700276 if err := pathtools.WriteFileIfChanged(outFile, outBytes, 0666); err != nil {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800277 ctx.Errorf(err.Error())
278 }
Colin Crossc3d87d32020-06-04 13:25:17 -0700279
Colin Cross3cda0d82019-09-24 13:40:07 -0700280 lateOutBytes := s.writeLate(phonies, dists)
Colin Crossc3d87d32020-06-04 13:25:17 -0700281
282 if err := pathtools.WriteFileIfChanged(lateOutFile, lateOutBytes, 0666); err != nil {
283 ctx.Errorf(err.Error())
284 }
285
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800286}
287
288func (s *makeVarsSingleton) writeVars(vars []makeVarsVariable) []byte {
289 buf := &bytes.Buffer{}
290
Dan Willemsen59339a22018-07-22 21:18:45 -0700291 fmt.Fprint(buf, `# Autogenerated file
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800292
293# Compares SOONG_$(1) against $(1), and warns if they are not equal.
294#
295# If the original variable is empty, then just set it to the SOONG_ version.
296#
297# $(1): Name of the variable to check
298# $(2): If not-empty, sort the values before comparing
299# $(3): Extra snippet to run if it does not match
300define soong-compare-var
301ifneq ($$($(1)),)
Dan Willemsen558e5172016-05-19 16:58:46 -0700302 my_val_make := $$(strip $(if $(2),$$(sort $$($(1))),$$($(1))))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800303 my_val_soong := $(if $(2),$$(sort $$(SOONG_$(1))),$$(SOONG_$(1)))
304 ifneq ($$(my_val_make),$$(my_val_soong))
305 $$(warning $(1) does not match between Make and Soong:)
306 $(if $(2),$$(warning Make adds: $$(filter-out $$(my_val_soong),$$(my_val_make))),$$(warning Make : $$(my_val_make)))
307 $(if $(2),$$(warning Soong adds: $$(filter-out $$(my_val_make),$$(my_val_soong))),$$(warning Soong: $$(my_val_soong)))
308 $(3)
309 endif
310 my_val_make :=
311 my_val_soong :=
312else
313 $(1) := $$(SOONG_$(1))
314endif
Dan Willemsende18f472016-09-30 10:16:38 -0700315.KATI_READONLY := $(1) SOONG_$(1)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800316endef
317
318my_check_failed := false
319
320`)
321
322 // Write all the strict checks out first so that if one of them errors,
323 // we get all of the strict errors printed, but not the non-strict
324 // warnings.
325 for _, v := range vars {
326 if !v.strict {
327 continue
328 }
329
330 sort := ""
331 if v.sort {
332 sort = "true"
333 }
334
335 fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value)
336 fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s,my_check_failed := true))\n\n", v.name, sort)
337 }
338
Dan Willemsen59339a22018-07-22 21:18:45 -0700339 fmt.Fprint(buf, `
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800340ifneq ($(my_check_failed),false)
341 $(error Soong variable check failed)
342endif
343my_check_failed :=
344
345
346`)
347
348 for _, v := range vars {
349 if v.strict {
350 continue
351 }
352
353 sort := ""
354 if v.sort {
355 sort = "true"
356 }
357
358 fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value)
359 fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s))\n\n", v.name, sort)
360 }
361
362 fmt.Fprintln(buf, "\nsoong-compare-var :=")
363
Colin Crossc3d87d32020-06-04 13:25:17 -0700364 fmt.Fprintln(buf)
365
366 return buf.Bytes()
367}
368
Colin Cross3cda0d82019-09-24 13:40:07 -0700369func (s *makeVarsSingleton) writeLate(phonies []phony, dists []dist) []byte {
Colin Crossc3d87d32020-06-04 13:25:17 -0700370 buf := &bytes.Buffer{}
371
372 fmt.Fprint(buf, `# Autogenerated file
373
374# Values written by Soong read after parsing all Android.mk files.
375
376
377`)
378
379 for _, phony := range phonies {
380 fmt.Fprintf(buf, ".PHONY: %s\n", phony.name)
381 fmt.Fprintf(buf, "%s: %s\n", phony.name, strings.Join(phony.deps, "\\\n "))
382 }
383
Colin Cross3cda0d82019-09-24 13:40:07 -0700384 fmt.Fprintln(buf)
385
386 for _, dist := range dists {
387 fmt.Fprintf(buf, "$(call dist-for-goals,%s,%s)\n",
388 strings.Join(dist.goals, " "), strings.Join(dist.paths, " "))
389 }
390
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800391 return buf.Bytes()
392}
393
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700394func (c *makeVarsContext) DeviceConfig() DeviceConfig {
Colin Cross65494b92019-02-07 14:25:51 -0800395 return DeviceConfig{c.Config().deviceConfig}
Jiyong Park374510b2018-03-19 18:23:01 +0900396}
397
Colin Cross31656952018-05-24 16:11:20 -0700398var ninjaDescaper = strings.NewReplacer("$$", "$")
399
Dan Willemsen558e5172016-05-19 16:58:46 -0700400func (c *makeVarsContext) Eval(ninjaStr string) (string, error) {
Colin Cross65494b92019-02-07 14:25:51 -0800401 s, err := c.SingletonContext.Eval(c.pctx, ninjaStr)
Colin Cross31656952018-05-24 16:11:20 -0700402 if err != nil {
403 return "", err
404 }
405 // SingletonContext.Eval returns an exapnded string that is valid for a ninja file, de-escape $$ to $ for use
406 // in a Makefile
407 return ninjaDescaper.Replace(s), nil
Dan Willemsen558e5172016-05-19 16:58:46 -0700408}
409
410func (c *makeVarsContext) addVariableRaw(name, value string, strict, sort bool) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800411 c.vars = append(c.vars, makeVarsVariable{
412 name: name,
413 value: value,
414 strict: strict,
415 sort: sort,
416 })
417}
418
Dan Willemsen558e5172016-05-19 16:58:46 -0700419func (c *makeVarsContext) addVariable(name, ninjaStr string, strict, sort bool) {
420 value, err := c.Eval(ninjaStr)
421 if err != nil {
Colin Cross65494b92019-02-07 14:25:51 -0800422 c.SingletonContext.Errorf(err.Error())
Dan Willemsen558e5172016-05-19 16:58:46 -0700423 }
424 c.addVariableRaw(name, value, strict, sort)
425}
426
Colin Crossc3d87d32020-06-04 13:25:17 -0700427func (c *makeVarsContext) addPhony(name string, deps []string) {
428 c.phonies = append(c.phonies, phony{name, deps})
429}
430
Colin Cross3cda0d82019-09-24 13:40:07 -0700431func (c *makeVarsContext) addDist(goals []string, paths []string) {
432 c.dists = append(c.dists, dist{
433 goals: goals,
434 paths: paths,
435 })
436}
437
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800438func (c *makeVarsContext) Strict(name, ninjaStr string) {
439 c.addVariable(name, ninjaStr, true, false)
440}
441func (c *makeVarsContext) StrictSorted(name, ninjaStr string) {
442 c.addVariable(name, ninjaStr, true, true)
443}
Dan Willemsen558e5172016-05-19 16:58:46 -0700444func (c *makeVarsContext) StrictRaw(name, value string) {
445 c.addVariableRaw(name, value, true, false)
446}
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800447
448func (c *makeVarsContext) Check(name, ninjaStr string) {
449 c.addVariable(name, ninjaStr, false, false)
450}
451func (c *makeVarsContext) CheckSorted(name, ninjaStr string) {
452 c.addVariable(name, ninjaStr, false, true)
453}
Dan Willemsen558e5172016-05-19 16:58:46 -0700454func (c *makeVarsContext) CheckRaw(name, value string) {
455 c.addVariableRaw(name, value, false, false)
456}
Colin Crossc3d87d32020-06-04 13:25:17 -0700457
458func (c *makeVarsContext) Phony(name string, deps ...Path) {
459 c.addPhony(name, Paths(deps).Strings())
460}
Colin Cross3cda0d82019-09-24 13:40:07 -0700461
462func (c *makeVarsContext) DistForGoal(goal string, paths ...Path) {
463 c.DistForGoals([]string{goal}, paths...)
464}
465
466func (c *makeVarsContext) DistForGoalWithFilename(goal string, path Path, filename string) {
467 c.DistForGoalsWithFilename([]string{goal}, path, filename)
468}
469
470func (c *makeVarsContext) DistForGoals(goals []string, paths ...Path) {
471 c.addDist(goals, Paths(paths).Strings())
472}
473
474func (c *makeVarsContext) DistForGoalsWithFilename(goals []string, path Path, filename string) {
475 c.addDist(goals, []string{path.String() + ":" + filename})
476}