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