blob: d323613e1cca656ea4b9d441991dbb5fe1e796c4 [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"
20 "io/ioutil"
21 "os"
Dan Albertf5415d72017-08-17 16:19:59 -070022 "strconv"
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080023
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080024 "github.com/google/blueprint/proptools"
25)
26
Dan Albertf5415d72017-08-17 16:19:59 -070027func init() {
28 RegisterMakeVarsProvider(pctx, androidMakeVarsProvider)
29}
30
31func androidMakeVarsProvider(ctx MakeVarsContext) {
32 ctx.Strict("MIN_SUPPORTED_SDK_VERSION", strconv.Itoa(ctx.Config().MinSupportedSdkVersion()))
33}
34
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080035///////////////////////////////////////////////////////////////////////////////
36// Interface for other packages to use to declare make variables
37type MakeVarsContext interface {
38 Config() Config
39
40 // Verify the make variable matches the Soong version, fail the build
41 // if it does not. If the make variable is empty, just set it.
42 Strict(name, ninjaStr string)
43 // Check to see if the make variable matches the Soong version, warn if
44 // it does not. If the make variable is empty, just set it.
45 Check(name, ninjaStr string)
46
47 // These are equivalent to the above, but sort the make and soong
48 // variables before comparing them. They also show the unique entries
49 // in each list when displaying the difference, instead of the entire
50 // string.
51 StrictSorted(name, ninjaStr string)
52 CheckSorted(name, ninjaStr string)
Dan Willemsen558e5172016-05-19 16:58:46 -070053
54 // Evaluates a ninja string and returns the result. Used if more
55 // complicated modification needs to happen before giving it to Make.
56 Eval(ninjaStr string) (string, error)
57
58 // These are equivalent to Strict and Check, but do not attempt to
59 // evaluate the values before writing them to the Makefile. They can
60 // be used when all ninja variables have already been evaluated through
61 // Eval().
62 StrictRaw(name, value string)
63 CheckRaw(name, value string)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080064}
65
66type MakeVarsProvider func(ctx MakeVarsContext)
67
Colin Cross0875c522017-11-28 17:34:01 -080068func RegisterMakeVarsProvider(pctx PackageContext, provider MakeVarsProvider) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080069 makeVarsProviders = append(makeVarsProviders, makeVarsProvider{pctx, provider})
70}
71
72///////////////////////////////////////////////////////////////////////////////
73
74func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070075 RegisterSingletonType("makevars", makeVarsSingletonFunc)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080076}
77
Colin Cross0875c522017-11-28 17:34:01 -080078func makeVarsSingletonFunc() Singleton {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080079 return &makeVarsSingleton{}
80}
81
82type makeVarsSingleton struct{}
83
84type makeVarsProvider struct {
Colin Cross0875c522017-11-28 17:34:01 -080085 pctx PackageContext
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080086 call MakeVarsProvider
87}
88
89var makeVarsProviders []makeVarsProvider
90
91type makeVarsContext struct {
92 config Config
Colin Cross0875c522017-11-28 17:34:01 -080093 ctx SingletonContext
94 pctx PackageContext
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080095 vars []makeVarsVariable
96}
97
98var _ MakeVarsContext = &makeVarsContext{}
99
100type makeVarsVariable struct {
101 name string
102 value string
103 sort bool
104 strict bool
105}
106
Colin Cross0875c522017-11-28 17:34:01 -0800107func (s *makeVarsSingleton) GenerateBuildActions(ctx SingletonContext) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800108 config := ctx.Config().(Config)
109
110 if !config.EmbeddedInMake() {
111 return
112 }
113
114 outFile := PathForOutput(ctx, "make_vars"+proptools.String(config.ProductVariables.Make_suffix)+".mk").String()
115
116 if ctx.Failed() {
117 return
118 }
119
120 vars := []makeVarsVariable{}
121 for _, provider := range makeVarsProviders {
122 mctx := &makeVarsContext{
123 config: config,
124 ctx: ctx,
125 pctx: provider.pctx,
126 }
127
128 provider.call(mctx)
129
130 vars = append(vars, mctx.vars...)
131 }
132
133 if ctx.Failed() {
134 return
135 }
136
137 outBytes := s.writeVars(vars)
138
139 if _, err := os.Stat(outFile); err == nil {
140 if data, err := ioutil.ReadFile(outFile); err == nil {
141 if bytes.Equal(data, outBytes) {
142 return
143 }
144 }
145 }
146
147 if err := ioutil.WriteFile(outFile, outBytes, 0666); err != nil {
148 ctx.Errorf(err.Error())
149 }
150}
151
152func (s *makeVarsSingleton) writeVars(vars []makeVarsVariable) []byte {
153 buf := &bytes.Buffer{}
154
155 fmt.Fprintln(buf, `# Autogenerated file
156
157# Compares SOONG_$(1) against $(1), and warns if they are not equal.
158#
159# If the original variable is empty, then just set it to the SOONG_ version.
160#
161# $(1): Name of the variable to check
162# $(2): If not-empty, sort the values before comparing
163# $(3): Extra snippet to run if it does not match
164define soong-compare-var
165ifneq ($$($(1)),)
Dan Willemsen558e5172016-05-19 16:58:46 -0700166 my_val_make := $$(strip $(if $(2),$$(sort $$($(1))),$$($(1))))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800167 my_val_soong := $(if $(2),$$(sort $$(SOONG_$(1))),$$(SOONG_$(1)))
168 ifneq ($$(my_val_make),$$(my_val_soong))
169 $$(warning $(1) does not match between Make and Soong:)
170 $(if $(2),$$(warning Make adds: $$(filter-out $$(my_val_soong),$$(my_val_make))),$$(warning Make : $$(my_val_make)))
171 $(if $(2),$$(warning Soong adds: $$(filter-out $$(my_val_make),$$(my_val_soong))),$$(warning Soong: $$(my_val_soong)))
172 $(3)
173 endif
174 my_val_make :=
175 my_val_soong :=
176else
177 $(1) := $$(SOONG_$(1))
178endif
Dan Willemsende18f472016-09-30 10:16:38 -0700179.KATI_READONLY := $(1) SOONG_$(1)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800180endef
181
182my_check_failed := false
183
184`)
185
186 // Write all the strict checks out first so that if one of them errors,
187 // we get all of the strict errors printed, but not the non-strict
188 // warnings.
189 for _, v := range vars {
190 if !v.strict {
191 continue
192 }
193
194 sort := ""
195 if v.sort {
196 sort = "true"
197 }
198
199 fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value)
200 fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s,my_check_failed := true))\n\n", v.name, sort)
201 }
202
203 fmt.Fprintln(buf, `
204ifneq ($(my_check_failed),false)
205 $(error Soong variable check failed)
206endif
207my_check_failed :=
208
209
210`)
211
212 for _, v := range vars {
213 if v.strict {
214 continue
215 }
216
217 sort := ""
218 if v.sort {
219 sort = "true"
220 }
221
222 fmt.Fprintf(buf, "SOONG_%s := %s\n", v.name, v.value)
223 fmt.Fprintf(buf, "$(eval $(call soong-compare-var,%s,%s))\n\n", v.name, sort)
224 }
225
226 fmt.Fprintln(buf, "\nsoong-compare-var :=")
227
228 return buf.Bytes()
229}
230
231func (c *makeVarsContext) Config() Config {
232 return c.config
233}
234
Dan Willemsen558e5172016-05-19 16:58:46 -0700235func (c *makeVarsContext) Eval(ninjaStr string) (string, error) {
236 return c.ctx.Eval(c.pctx, ninjaStr)
237}
238
239func (c *makeVarsContext) addVariableRaw(name, value string, strict, sort bool) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800240 c.vars = append(c.vars, makeVarsVariable{
241 name: name,
242 value: value,
243 strict: strict,
244 sort: sort,
245 })
246}
247
Dan Willemsen558e5172016-05-19 16:58:46 -0700248func (c *makeVarsContext) addVariable(name, ninjaStr string, strict, sort bool) {
249 value, err := c.Eval(ninjaStr)
250 if err != nil {
251 c.ctx.Errorf(err.Error())
252 }
253 c.addVariableRaw(name, value, strict, sort)
254}
255
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800256func (c *makeVarsContext) Strict(name, ninjaStr string) {
257 c.addVariable(name, ninjaStr, true, false)
258}
259func (c *makeVarsContext) StrictSorted(name, ninjaStr string) {
260 c.addVariable(name, ninjaStr, true, true)
261}
Dan Willemsen558e5172016-05-19 16:58:46 -0700262func (c *makeVarsContext) StrictRaw(name, value string) {
263 c.addVariableRaw(name, value, true, false)
264}
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800265
266func (c *makeVarsContext) Check(name, ninjaStr string) {
267 c.addVariable(name, ninjaStr, false, false)
268}
269func (c *makeVarsContext) CheckSorted(name, ninjaStr string) {
270 c.addVariable(name, ninjaStr, false, true)
271}
Dan Willemsen558e5172016-05-19 16:58:46 -0700272func (c *makeVarsContext) CheckRaw(name, value string) {
273 c.addVariableRaw(name, value, false, false)
274}