blob: 0acd0f66564b8c605339cf9f65ff240f1c188d64 [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 Albertf5415d72017-08-17 16:19:59 -070020 "strconv"
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) {
33 ctx.Strict("MIN_SUPPORTED_SDK_VERSION", strconv.Itoa(ctx.Config().MinSupportedSdkVersion()))
34}
35
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080036///////////////////////////////////////////////////////////////////////////////
37// Interface for other packages to use to declare make variables
38type MakeVarsContext interface {
39 Config() Config
Dan Willemsen3fb1fae2018-03-12 15:30:26 -070040 DeviceConfig() DeviceConfig
Colin Cross65494b92019-02-07 14:25:51 -080041 AddNinjaFileDeps(deps ...string)
Colin Cross65494b92019-02-07 14:25:51 -080042
43 ModuleName(module blueprint.Module) string
44 ModuleDir(module blueprint.Module) string
45 ModuleSubDir(module blueprint.Module) string
46 ModuleType(module blueprint.Module) string
47 BlueprintFile(module blueprint.Module) string
48
49 ModuleErrorf(module blueprint.Module, format string, args ...interface{})
50 Errorf(format string, args ...interface{})
51 Failed() bool
52
53 VisitAllModules(visit func(Module))
54 VisitAllModulesIf(pred func(Module) bool, visit func(Module))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080055
56 // Verify the make variable matches the Soong version, fail the build
57 // if it does not. If the make variable is empty, just set it.
58 Strict(name, ninjaStr string)
59 // Check to see if the make variable matches the Soong version, warn if
60 // it does not. If the make variable is empty, just set it.
61 Check(name, ninjaStr string)
62
63 // These are equivalent to the above, but sort the make and soong
64 // variables before comparing them. They also show the unique entries
65 // in each list when displaying the difference, instead of the entire
66 // string.
67 StrictSorted(name, ninjaStr string)
68 CheckSorted(name, ninjaStr string)
Dan Willemsen558e5172016-05-19 16:58:46 -070069
70 // Evaluates a ninja string and returns the result. Used if more
71 // complicated modification needs to happen before giving it to Make.
72 Eval(ninjaStr string) (string, error)
73
74 // These are equivalent to Strict and Check, but do not attempt to
75 // evaluate the values before writing them to the Makefile. They can
76 // be used when all ninja variables have already been evaluated through
77 // Eval().
78 StrictRaw(name, value string)
79 CheckRaw(name, value string)
Colin Cross8177ad22019-11-04 10:27:48 -080080
81 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
82 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
83 // builder whenever a file matching the pattern as added or removed, without rerunning if a
84 // file that does not match the pattern is added to a searched directory.
85 GlobWithDeps(pattern string, excludes []string) ([]string, error)
Colin Crossc3d87d32020-06-04 13:25:17 -070086
87 // Phony creates a phony rule in Make, which will allow additional DistForGoal
88 // dependencies to be added to it. Phony can be called on the same name multiple
89 // times to add additional dependencies.
90 Phony(names string, deps ...Path)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080091}
92
Colin Cross65494b92019-02-07 14:25:51 -080093var _ PathContext = MakeVarsContext(nil)
94
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080095type MakeVarsProvider func(ctx MakeVarsContext)
96
Colin Cross0875c522017-11-28 17:34:01 -080097func RegisterMakeVarsProvider(pctx PackageContext, provider MakeVarsProvider) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080098 makeVarsProviders = append(makeVarsProviders, makeVarsProvider{pctx, provider})
99}
100
Colin Crossed023ec2019-02-19 12:38:45 -0800101// SingletonMakeVarsProvider is a Singleton with an extra method to provide extra values to be exported to Make.
102type SingletonMakeVarsProvider interface {
103 Singleton
104
105 // MakeVars uses a MakeVarsContext to provide extra values to be exported to Make.
106 MakeVars(ctx MakeVarsContext)
107}
108
109// registerSingletonMakeVarsProvider adds a singleton that implements SingletonMakeVarsProvider to the list of
110// MakeVarsProviders to run.
111func registerSingletonMakeVarsProvider(singleton SingletonMakeVarsProvider) {
112 makeVarsProviders = append(makeVarsProviders, makeVarsProvider{pctx, SingletonmakeVarsProviderAdapter(singleton)})
113}
114
115// SingletonmakeVarsProviderAdapter converts a SingletonMakeVarsProvider to a MakeVarsProvider.
116func SingletonmakeVarsProviderAdapter(singleton SingletonMakeVarsProvider) MakeVarsProvider {
117 return func(ctx MakeVarsContext) { singleton.MakeVars(ctx) }
118}
119
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800120///////////////////////////////////////////////////////////////////////////////
121
Colin Cross0875c522017-11-28 17:34:01 -0800122func makeVarsSingletonFunc() Singleton {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800123 return &makeVarsSingleton{}
124}
125
126type makeVarsSingleton struct{}
127
128type makeVarsProvider struct {
Colin Cross0875c522017-11-28 17:34:01 -0800129 pctx PackageContext
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800130 call MakeVarsProvider
131}
132
133var makeVarsProviders []makeVarsProvider
134
135type makeVarsContext struct {
Colin Cross65494b92019-02-07 14:25:51 -0800136 SingletonContext
Colin Crossc3d87d32020-06-04 13:25:17 -0700137 config Config
138 pctx PackageContext
139 vars []makeVarsVariable
140 phonies []phony
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800141}
142
143var _ MakeVarsContext = &makeVarsContext{}
144
145type makeVarsVariable struct {
146 name string
147 value string
148 sort bool
149 strict bool
150}
151
Colin Crossc3d87d32020-06-04 13:25:17 -0700152type phony struct {
153 name string
154 deps []string
155}
156
Colin Cross0875c522017-11-28 17:34:01 -0800157func (s *makeVarsSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800158 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800159 return
160 }
161
Colin Cross988414c2020-01-11 01:11:46 +0000162 outFile := absolutePath(PathForOutput(ctx,
163 "make_vars"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800164
Colin Crossc3d87d32020-06-04 13:25:17 -0700165 lateOutFile := absolutePath(PathForOutput(ctx,
166 "late"+proptools.String(ctx.Config().productVariables.Make_suffix)+".mk").String())
167
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800168 if ctx.Failed() {
169 return
170 }
171
172 vars := []makeVarsVariable{}
Colin Crossc3d87d32020-06-04 13:25:17 -0700173 var phonies []phony
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800174 for _, provider := range makeVarsProviders {
175 mctx := &makeVarsContext{
Colin Cross65494b92019-02-07 14:25:51 -0800176 SingletonContext: ctx,
177 pctx: provider.pctx,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800178 }
179
180 provider.call(mctx)
181
182 vars = append(vars, mctx.vars...)
Colin Crossc3d87d32020-06-04 13:25:17 -0700183 phonies = append(phonies, mctx.phonies...)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800184 }
185
186 if ctx.Failed() {
187 return
188 }
189
190 outBytes := s.writeVars(vars)
191
Colin Crossc3d87d32020-06-04 13:25:17 -0700192 if err := pathtools.WriteFileIfChanged(outFile, outBytes, 0666); err != nil {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800193 ctx.Errorf(err.Error())
194 }
Colin Crossc3d87d32020-06-04 13:25:17 -0700195
196 lateOutBytes := s.writeLate(phonies)
197
198 if err := pathtools.WriteFileIfChanged(lateOutFile, lateOutBytes, 0666); err != nil {
199 ctx.Errorf(err.Error())
200 }
201
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800202}
203
204func (s *makeVarsSingleton) writeVars(vars []makeVarsVariable) []byte {
205 buf := &bytes.Buffer{}
206
Dan Willemsen59339a22018-07-22 21:18:45 -0700207 fmt.Fprint(buf, `# Autogenerated file
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800208
209# Compares SOONG_$(1) against $(1), and warns if they are not equal.
210#
211# If the original variable is empty, then just set it to the SOONG_ version.
212#
213# $(1): Name of the variable to check
214# $(2): If not-empty, sort the values before comparing
215# $(3): Extra snippet to run if it does not match
216define soong-compare-var
217ifneq ($$($(1)),)
Dan Willemsen558e5172016-05-19 16:58:46 -0700218 my_val_make := $$(strip $(if $(2),$$(sort $$($(1))),$$($(1))))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800219 my_val_soong := $(if $(2),$$(sort $$(SOONG_$(1))),$$(SOONG_$(1)))
220 ifneq ($$(my_val_make),$$(my_val_soong))
221 $$(warning $(1) does not match between Make and Soong:)
222 $(if $(2),$$(warning Make adds: $$(filter-out $$(my_val_soong),$$(my_val_make))),$$(warning Make : $$(my_val_make)))
223 $(if $(2),$$(warning Soong adds: $$(filter-out $$(my_val_make),$$(my_val_soong))),$$(warning Soong: $$(my_val_soong)))
224 $(3)
225 endif
226 my_val_make :=
227 my_val_soong :=
228else
229 $(1) := $$(SOONG_$(1))
230endif
Dan Willemsende18f472016-09-30 10:16:38 -0700231.KATI_READONLY := $(1) SOONG_$(1)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800232endef
233
234my_check_failed := false
235
236`)
237
238 // Write all the strict checks out first so that if one of them errors,
239 // we get all of the strict errors printed, but not the non-strict
240 // warnings.
241 for _, v := range vars {
242 if !v.strict {
243 continue
244 }
245
246 sort := ""
247 if v.sort {
248 sort = "true"
249 }
250
251 fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value)
252 fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s,my_check_failed := true))\n\n", v.name, sort)
253 }
254
Dan Willemsen59339a22018-07-22 21:18:45 -0700255 fmt.Fprint(buf, `
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800256ifneq ($(my_check_failed),false)
257 $(error Soong variable check failed)
258endif
259my_check_failed :=
260
261
262`)
263
264 for _, v := range vars {
265 if v.strict {
266 continue
267 }
268
269 sort := ""
270 if v.sort {
271 sort = "true"
272 }
273
274 fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value)
275 fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s))\n\n", v.name, sort)
276 }
277
278 fmt.Fprintln(buf, "\nsoong-compare-var :=")
279
Colin Crossc3d87d32020-06-04 13:25:17 -0700280 fmt.Fprintln(buf)
281
282 return buf.Bytes()
283}
284
285func (s *makeVarsSingleton) writeLate(phonies []phony) []byte {
286 buf := &bytes.Buffer{}
287
288 fmt.Fprint(buf, `# Autogenerated file
289
290# Values written by Soong read after parsing all Android.mk files.
291
292
293`)
294
295 for _, phony := range phonies {
296 fmt.Fprintf(buf, ".PHONY: %s\n", phony.name)
297 fmt.Fprintf(buf, "%s: %s\n", phony.name, strings.Join(phony.deps, "\\\n "))
298 }
299
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800300 return buf.Bytes()
301}
302
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700303func (c *makeVarsContext) DeviceConfig() DeviceConfig {
Colin Cross65494b92019-02-07 14:25:51 -0800304 return DeviceConfig{c.Config().deviceConfig}
Jiyong Park374510b2018-03-19 18:23:01 +0900305}
306
Colin Cross31656952018-05-24 16:11:20 -0700307var ninjaDescaper = strings.NewReplacer("$$", "$")
308
Dan Willemsen558e5172016-05-19 16:58:46 -0700309func (c *makeVarsContext) Eval(ninjaStr string) (string, error) {
Colin Cross65494b92019-02-07 14:25:51 -0800310 s, err := c.SingletonContext.Eval(c.pctx, ninjaStr)
Colin Cross31656952018-05-24 16:11:20 -0700311 if err != nil {
312 return "", err
313 }
314 // SingletonContext.Eval returns an exapnded string that is valid for a ninja file, de-escape $$ to $ for use
315 // in a Makefile
316 return ninjaDescaper.Replace(s), nil
Dan Willemsen558e5172016-05-19 16:58:46 -0700317}
318
319func (c *makeVarsContext) addVariableRaw(name, value string, strict, sort bool) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800320 c.vars = append(c.vars, makeVarsVariable{
321 name: name,
322 value: value,
323 strict: strict,
324 sort: sort,
325 })
326}
327
Dan Willemsen558e5172016-05-19 16:58:46 -0700328func (c *makeVarsContext) addVariable(name, ninjaStr string, strict, sort bool) {
329 value, err := c.Eval(ninjaStr)
330 if err != nil {
Colin Cross65494b92019-02-07 14:25:51 -0800331 c.SingletonContext.Errorf(err.Error())
Dan Willemsen558e5172016-05-19 16:58:46 -0700332 }
333 c.addVariableRaw(name, value, strict, sort)
334}
335
Colin Crossc3d87d32020-06-04 13:25:17 -0700336func (c *makeVarsContext) addPhony(name string, deps []string) {
337 c.phonies = append(c.phonies, phony{name, deps})
338}
339
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800340func (c *makeVarsContext) Strict(name, ninjaStr string) {
341 c.addVariable(name, ninjaStr, true, false)
342}
343func (c *makeVarsContext) StrictSorted(name, ninjaStr string) {
344 c.addVariable(name, ninjaStr, true, true)
345}
Dan Willemsen558e5172016-05-19 16:58:46 -0700346func (c *makeVarsContext) StrictRaw(name, value string) {
347 c.addVariableRaw(name, value, true, false)
348}
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800349
350func (c *makeVarsContext) Check(name, ninjaStr string) {
351 c.addVariable(name, ninjaStr, false, false)
352}
353func (c *makeVarsContext) CheckSorted(name, ninjaStr string) {
354 c.addVariable(name, ninjaStr, false, true)
355}
Dan Willemsen558e5172016-05-19 16:58:46 -0700356func (c *makeVarsContext) CheckRaw(name, value string) {
357 c.addVariableRaw(name, value, false, false)
358}
Colin Crossc3d87d32020-06-04 13:25:17 -0700359
360func (c *makeVarsContext) Phony(name string, deps ...Path) {
361 c.addPhony(name, Paths(deps).Strings())
362}