blob: 19a9328e409e1e99f40a9595d885759e4f771d0f [file] [log] [blame]
Andres Moralesda8706f2015-04-29 12:46:49 -07001package main
2
3import (
4 "bufio"
Andres Morales8ae47de2015-05-11 12:26:07 -07005 "errors"
Andres Moralesda8706f2015-04-29 12:46:49 -07006 "fmt"
7 "os"
8 "path"
Andres Morales8ae47de2015-05-11 12:26:07 -07009 "path/filepath"
Andres Moralesaf11df12015-04-30 12:14:34 -070010 "regexp"
Andres Moralesda8706f2015-04-29 12:46:49 -070011 "strings"
12
13 bpparser "github.com/google/blueprint/parser"
14)
15
Andres Morales8ae47de2015-05-11 12:26:07 -070016var recursiveSubdirRegex *regexp.Regexp = regexp.MustCompile("(.+)/\\*\\*/(.+)")
17
Dan Willemsen3a4045d2015-06-24 15:37:17 -070018type Module struct {
19 bpmod *bpparser.Module
20 bpname string
21 mkname string
22 isHostRule bool
23}
24
25func newModule(mod *bpparser.Module) *Module {
26 return &Module{
27 bpmod: mod,
28 bpname: mod.Type.Name,
29 }
30}
31
32func (m *Module) translateRuleName() {
33 name := fmt.Sprintf(m.bpname)
34 if translation, ok := moduleTypeToRule[m.bpname]; ok {
35 name = translation
36 }
37
38 if m.isHostRule {
39 if trans, ok := targetToHostModuleRule[name]; ok {
40 name = trans
41 } else {
42 name = "NO CORRESPONDING HOST RULE" + name
43 }
44 } else {
45 m.isHostRule = strings.Contains(name, "HOST")
46 }
47
48 m.mkname = name
49}
50
Andres Moralesda8706f2015-04-29 12:46:49 -070051type androidMkWriter struct {
52 *bufio.Writer
53
Andres Moralesaf11df12015-04-30 12:14:34 -070054 blueprint *bpparser.File
55 path string
56
Dan Willemsen360a39c2015-06-11 14:34:50 -070057 printedLocalPath bool
58
Andres Moralesaf11df12015-04-30 12:14:34 -070059 mapScope map[string][]*bpparser.Property
Andres Moralesda8706f2015-04-29 12:46:49 -070060}
61
Andres Moralesaf11df12015-04-30 12:14:34 -070062func valueToString(value bpparser.Value) string {
Andres Moralesda8706f2015-04-29 12:46:49 -070063 if value.Variable != "" {
64 return fmt.Sprintf("$(%s)", value.Variable)
Colin Crossff3b7952015-06-22 15:39:35 -070065 } else if value.Expression != nil {
66 if value.Expression.Operator != '+' {
67 panic(fmt.Errorf("unexpected operator '%c'", value.Expression.Operator))
68 }
Colin Crosseb050832015-06-22 17:26:12 -070069 return fmt.Sprintf("%s%s",
Colin Crossff3b7952015-06-22 15:39:35 -070070 valueToString(value.Expression.Args[0]),
71 valueToString(value.Expression.Args[1]))
Andres Moralesda8706f2015-04-29 12:46:49 -070072 } else {
73 switch value.Type {
74 case bpparser.Bool:
Ying Wang38284902015-06-02 18:44:59 -070075 return fmt.Sprintf("%t", value.BoolValue)
Andres Moralesda8706f2015-04-29 12:46:49 -070076 case bpparser.String:
Ying Wang38284902015-06-02 18:44:59 -070077 return fmt.Sprintf("%s", processWildcards(value.StringValue))
Andres Moralesda8706f2015-04-29 12:46:49 -070078 case bpparser.List:
Colin Crossff3b7952015-06-22 15:39:35 -070079 return fmt.Sprintf("\\\n%s", listToMkString(value.ListValue))
Andres Moralesda8706f2015-04-29 12:46:49 -070080 case bpparser.Map:
Andres Moralesaf11df12015-04-30 12:14:34 -070081 return fmt.Sprintf("ERROR can't convert map to string")
82 default:
83 return fmt.Sprintf("ERROR: unsupported type %d", value.Type)
Andres Moralesda8706f2015-04-29 12:46:49 -070084 }
85 }
Andres Moralesda8706f2015-04-29 12:46:49 -070086}
87
Andres Morales8ae47de2015-05-11 12:26:07 -070088func getTopOfAndroidTree(wd string) (string, error) {
89 if !filepath.IsAbs(wd) {
90 return "", errors.New("path must be absolute: " + wd)
91 }
92
93 topfile := "build/soong/bootstrap.bash"
94
95 for "/" != wd {
96 expected := filepath.Join(wd, topfile)
97
98 if _, err := os.Stat(expected); err == nil {
99 // Found the top
100 return wd, nil
101 }
102
103 wd = filepath.Join(wd, "..")
104 }
105
106 return "", errors.New("couldn't find top of tree from " + wd)
107}
108
Andres Moralesaf11df12015-04-30 12:14:34 -0700109// TODO: handle non-recursive wildcards?
110func processWildcards(s string) string {
Andres Morales8ae47de2015-05-11 12:26:07 -0700111 submatches := recursiveSubdirRegex.FindStringSubmatch(s)
112 if len(submatches) > 2 {
Andres Moralesaf11df12015-04-30 12:14:34 -0700113 // Found a wildcard rule
114 return fmt.Sprintf("$(call find-files-in-subdirs, $(LOCAL_PATH), %s, %s)",
Andres Morales8ae47de2015-05-11 12:26:07 -0700115 submatches[2], submatches[1])
Andres Moralesaf11df12015-04-30 12:14:34 -0700116 }
117
118 return s
119}
120
121func listToMkString(list []bpparser.Value) string {
Andres Moralesda8706f2015-04-29 12:46:49 -0700122 lines := make([]string, 0, len(list))
123 for _, tok := range list {
Colin Crosseb050832015-06-22 17:26:12 -0700124 lines = append(lines, fmt.Sprintf(" %s", valueToString(tok)))
Andres Moralesda8706f2015-04-29 12:46:49 -0700125 }
126
127 return strings.Join(lines, " \\\n")
128}
129
Andres Moralesaf11df12015-04-30 12:14:34 -0700130func translateTargetConditionals(props []*bpparser.Property,
131 disabledBuilds map[string]bool, isHostRule bool) (computedProps []string) {
132 for _, target := range props {
133 conditionals := targetScopedPropertyConditionals
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700134 altConditionals := hostScopedPropertyConditionals
Andres Moralesaf11df12015-04-30 12:14:34 -0700135 if isHostRule {
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700136 conditionals, altConditionals = altConditionals, conditionals
Andres Moralesaf11df12015-04-30 12:14:34 -0700137 }
138
139 conditional, ok := conditionals[target.Name.Name]
140 if !ok {
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700141 if _, ok := altConditionals[target.Name.Name]; ok {
142 // This is only for the other build type
143 continue
144 } else {
145 // not found
146 conditional = fmt.Sprintf(
147 "ifeq(true, true) # ERROR: unsupported conditional [%s]",
148 target.Name.Name)
149 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700150 }
151
152 var scopedProps []string
153 for _, targetScopedProp := range target.Value.MapValue {
154 if mkProp, ok := standardProperties[targetScopedProp.Name.Name]; ok {
155 scopedProps = append(scopedProps, fmt.Sprintf("%s += %s",
156 mkProp.string, valueToString(targetScopedProp.Value)))
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700157 } else if rwProp, ok := rewriteProperties[targetScopedProp.Name.Name]; ok {
158 scopedProps = append(scopedProps, rwProp.f(rwProp.string, targetScopedProp, nil)...)
Andres Moralesaf11df12015-04-30 12:14:34 -0700159 } else if "disabled" == targetScopedProp.Name.Name {
160 if targetScopedProp.Value.BoolValue {
161 disabledBuilds[target.Name.Name] = true
162 } else {
163 delete(disabledBuilds, target.Name.Name)
164 }
165 }
166 }
167
168 if len(scopedProps) > 0 {
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700169 if conditional != "" {
170 computedProps = append(computedProps, conditional)
171 computedProps = append(computedProps, scopedProps...)
172 computedProps = append(computedProps, "endif")
173 } else {
174 computedProps = append(computedProps, scopedProps...)
175 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700176 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700177 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700178
179 return
180}
181
182func translateSuffixProperties(suffixProps []*bpparser.Property,
183 suffixMap map[string]string) (computedProps []string) {
184 for _, suffixProp := range suffixProps {
185 if suffix, ok := suffixMap[suffixProp.Name.Name]; ok {
186 for _, stdProp := range suffixProp.Value.MapValue {
187 if mkProp, ok := standardProperties[stdProp.Name.Name]; ok {
188 computedProps = append(computedProps, fmt.Sprintf("%s_%s := %s", mkProp.string, suffix, valueToString(stdProp.Value)))
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700189 } else if rwProp, ok := rewriteProperties[stdProp.Name.Name]; ok {
190 computedProps = append(computedProps, rwProp.f(rwProp.string, stdProp, &suffix)...)
Andres Moralesaf11df12015-04-30 12:14:34 -0700191 } else {
192 computedProps = append(computedProps, fmt.Sprintf("# ERROR: unsupported property %s", stdProp.Name.Name))
193 }
194 }
195 }
196 }
197 return
198}
199
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700200func prependLocalPath(name string, prop *bpparser.Property, suffix *string) (computedProps []string) {
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700201 if suffix != nil {
202 name += "_" + *suffix
203 }
Colin Crossff3b7952015-06-22 15:39:35 -0700204 return []string{
205 fmt.Sprintf("%s := $(addprefix $(LOCAL_PATH)/,%s)\n", name, valueToString(prop.Value)),
206 }
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700207}
208
Dan Willemsen1d9f2792015-06-22 15:40:14 -0700209func prependLocalModule(name string, prop *bpparser.Property, suffix *string) (computedProps []string) {
210 if suffix != nil {
211 name += "_" + *suffix
212 }
213 return []string {
Dan Willemsend7b11dd2015-06-22 16:25:39 -0700214 fmt.Sprintf("%s := $(LOCAL_MODULE)%s\n", name, valueToString(prop.Value)),
Dan Willemsen1d9f2792015-06-22 15:40:14 -0700215 }
216}
217
Dan Willemsen3a4045d2015-06-24 15:37:17 -0700218func modulePropBool(module *bpparser.Module, name string) bool {
219 for _, prop := range module.Properties {
220 if name == prop.Name.Name {
221 return prop.Value.BoolValue
222 }
223 }
224 return false
225}
226
Andres Moralesaf11df12015-04-30 12:14:34 -0700227func (w *androidMkWriter) lookupMap(parent bpparser.Value) (mapValue []*bpparser.Property) {
228 if parent.Variable != "" {
229 mapValue = w.mapScope[parent.Variable]
230 } else {
231 mapValue = parent.MapValue
232 }
233 return
Andres Moralesda8706f2015-04-29 12:46:49 -0700234}
235
Andres Moralesaf11df12015-04-30 12:14:34 -0700236func (w *androidMkWriter) writeModule(moduleRule string, props []string,
237 disabledBuilds map[string]bool, isHostRule bool) {
238 disabledConditionals := disabledTargetConditionals
239 if isHostRule {
240 disabledConditionals = disabledHostConditionals
241 }
242 for build, _ := range disabledBuilds {
243 if conditional, ok := disabledConditionals[build]; ok {
244 fmt.Fprintf(w, "%s\n", conditional)
245 defer fmt.Fprintf(w, "endif\n")
Andres Moralesda8706f2015-04-29 12:46:49 -0700246 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700247 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700248
Andres Moralesaf11df12015-04-30 12:14:34 -0700249 fmt.Fprintf(w, "include $(CLEAR_VARS)\n")
250 fmt.Fprintf(w, "%s\n", strings.Join(props, "\n"))
251 fmt.Fprintf(w, "include $(%s)\n\n", moduleRule)
252}
Andres Moralesda8706f2015-04-29 12:46:49 -0700253
Dan Willemsen3a4045d2015-06-24 15:37:17 -0700254func (w *androidMkWriter) parsePropsAndWriteModule(module *Module) {
255 standardProps := make([]string, 0, len(module.bpmod.Properties))
Andres Moralesaf11df12015-04-30 12:14:34 -0700256 disabledBuilds := make(map[string]bool)
Dan Willemsen3a4045d2015-06-24 15:37:17 -0700257 for _, prop := range module.bpmod.Properties {
Andres Moralesaf11df12015-04-30 12:14:34 -0700258 if mkProp, ok := standardProperties[prop.Name.Name]; ok {
259 standardProps = append(standardProps, fmt.Sprintf("%s := %s", mkProp.string, valueToString(prop.Value)))
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700260 } else if rwProp, ok := rewriteProperties[prop.Name.Name]; ok {
261 standardProps = append(standardProps, rwProp.f(rwProp.string, prop, nil)...)
Andres Moralesaf11df12015-04-30 12:14:34 -0700262 } else if suffixMap, ok := suffixProperties[prop.Name.Name]; ok {
263 suffixProps := w.lookupMap(prop.Value)
264 standardProps = append(standardProps, translateSuffixProperties(suffixProps, suffixMap)...)
265 } else if "target" == prop.Name.Name {
266 props := w.lookupMap(prop.Value)
Dan Willemsen3a4045d2015-06-24 15:37:17 -0700267 standardProps = append(standardProps, translateTargetConditionals(props, disabledBuilds, module.isHostRule)...)
Dan Willemsen0a544692015-06-24 15:50:07 -0700268 } else if _, ok := ignoredProperties[prop.Name.Name]; ok {
Andres Moralesaf11df12015-04-30 12:14:34 -0700269 } else {
270 standardProps = append(standardProps, fmt.Sprintf("# ERROR: Unsupported property %s", prop.Name.Name))
271 }
272 }
273
Dan Willemsen3a4045d2015-06-24 15:37:17 -0700274 w.writeModule(module.mkname, standardProps, disabledBuilds, module.isHostRule)
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700275}
276
Dan Willemsen3a4045d2015-06-24 15:37:17 -0700277func (w *androidMkWriter) mutateModule(module *Module) (modules []*Module) {
278 modules = []*Module{module}
Dan Willemsen49f50452015-06-24 14:56:00 -0700279
Dan Willemsen3a4045d2015-06-24 15:37:17 -0700280 if module.bpname == "cc_library" {
281 modules = []*Module{
282 newModule(module.bpmod),
283 newModule(module.bpmod),
284 }
285 modules[0].bpname = "cc_library_shared"
286 modules[1].bpname = "cc_library_static"
287 }
288
289 for _, mod := range modules {
290 mod.translateRuleName()
291 if mod.isHostRule || !modulePropBool(mod.bpmod, "host_supported") {
292 continue
293 }
294
295 m := &Module{
296 bpmod: mod.bpmod,
297 bpname: mod.bpname,
298 isHostRule: true,
299 }
300 m.translateRuleName()
301 modules = append(modules, m)
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700302 }
303
Dan Willemsen49f50452015-06-24 14:56:00 -0700304 return
305}
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700306
Dan Willemsen49f50452015-06-24 14:56:00 -0700307func (w *androidMkWriter) handleModule(inputModule *bpparser.Module) {
Dan Willemsen3a4045d2015-06-24 15:37:17 -0700308 modules := w.mutateModule(newModule(inputModule))
Dan Willemsen49f50452015-06-24 14:56:00 -0700309
310 for _, module := range modules {
Dan Willemsen3a4045d2015-06-24 15:37:17 -0700311 w.parsePropsAndWriteModule(module)
Andres Moralesaf11df12015-04-30 12:14:34 -0700312 }
313}
314
315func (w *androidMkWriter) handleSubdirs(value bpparser.Value) {
Andres Morales8ae47de2015-05-11 12:26:07 -0700316 subdirs := make([]string, 0, len(value.ListValue))
317 for _, tok := range value.ListValue {
318 subdirs = append(subdirs, tok.StringValue)
Andres Moralesda8706f2015-04-29 12:46:49 -0700319 }
Ying Wang38284902015-06-02 18:44:59 -0700320 // The current makefile may be generated to outside the source tree (such as the out directory), with a different structure.
321 fmt.Fprintf(w, "# Uncomment the following line if you really want to include subdir Android.mks.\n")
322 fmt.Fprintf(w, "# include $(wildcard $(addsuffix $(LOCAL_PATH)/%s/, Android.mk))\n", strings.Join(subdirs, " "))
Andres Moralesda8706f2015-04-29 12:46:49 -0700323}
324
325func (w *androidMkWriter) handleAssignment(assignment *bpparser.Assignment) {
Andres Moralesaf11df12015-04-30 12:14:34 -0700326 if "subdirs" == assignment.Name.Name {
327 w.handleSubdirs(assignment.OrigValue)
328 } else if assignment.OrigValue.Type == bpparser.Map {
329 // maps may be assigned in Soong, but can only be translated to .mk
330 // in the context of the module
331 w.mapScope[assignment.Name.Name] = assignment.OrigValue.MapValue
332 } else {
333 assigner := ":="
334 if assignment.Assigner != "=" {
335 assigner = assignment.Assigner
336 }
337 fmt.Fprintf(w, "%s %s %s\n", assignment.Name.Name, assigner,
338 valueToString(assignment.OrigValue))
Andres Moralesda8706f2015-04-29 12:46:49 -0700339 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700340}
341
Andres Morales8ae47de2015-05-11 12:26:07 -0700342func (w *androidMkWriter) handleLocalPath() error {
Dan Willemsen360a39c2015-06-11 14:34:50 -0700343 if w.printedLocalPath {
344 return nil
345 }
346 w.printedLocalPath = true
347
Ying Wang38284902015-06-02 18:44:59 -0700348 localPath, err := filepath.Abs(w.path)
Andres Morales8ae47de2015-05-11 12:26:07 -0700349 if err != nil {
350 return err
351 }
352
Ying Wang38284902015-06-02 18:44:59 -0700353 top, err := getTopOfAndroidTree(localPath)
Andres Morales8ae47de2015-05-11 12:26:07 -0700354 if err != nil {
355 return err
356 }
357
Ying Wang38284902015-06-02 18:44:59 -0700358 rel, err := filepath.Rel(top, localPath)
Andres Morales8ae47de2015-05-11 12:26:07 -0700359 if err != nil {
360 return err
361 }
362
363 w.WriteString("LOCAL_PATH := " + rel + "\n")
Dan Willemsenc2666e62015-06-10 16:18:58 -0700364 w.WriteString("LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))\n\n")
Andres Morales8ae47de2015-05-11 12:26:07 -0700365 return nil
366}
367
Ying Wang38284902015-06-02 18:44:59 -0700368func (w *androidMkWriter) write(androidMk string) error {
369 fmt.Printf("Writing %s\n", androidMk)
Andres Moralesda8706f2015-04-29 12:46:49 -0700370
Ying Wang38284902015-06-02 18:44:59 -0700371 f, err := os.Create(androidMk)
Andres Moralesda8706f2015-04-29 12:46:49 -0700372 if err != nil {
373 panic(err)
374 }
375
Andres Moralesaf11df12015-04-30 12:14:34 -0700376 defer f.Close()
Andres Moralesda8706f2015-04-29 12:46:49 -0700377
378 w.Writer = bufio.NewWriter(f)
379
Colin Cross26478b72015-06-29 13:46:00 -0700380 if err := w.handleLocalPath(); err != nil {
381 return err
382 }
383
384 for _, block := range w.blueprint.Defs {
Andres Moralesda8706f2015-04-29 12:46:49 -0700385 switch block := block.(type) {
386 case *bpparser.Module:
387 w.handleModule(block)
388 case *bpparser.Assignment:
389 w.handleAssignment(block)
Andres Moralesda8706f2015-04-29 12:46:49 -0700390 }
391 }
392
393 if err = w.Flush(); err != nil {
394 panic(err)
395 }
Ying Wang38284902015-06-02 18:44:59 -0700396 return nil
Andres Moralesda8706f2015-04-29 12:46:49 -0700397}
398
399func main() {
400 if len(os.Args) < 2 {
401 fmt.Println("No filename supplied")
Ying Wang38284902015-06-02 18:44:59 -0700402 os.Exit(1)
Andres Moralesda8706f2015-04-29 12:46:49 -0700403 }
404
Ying Wang38284902015-06-02 18:44:59 -0700405 androidBp := os.Args[1]
406 var androidMk string
407 if len(os.Args) >= 3 {
408 androidMk = os.Args[2]
409 } else {
410 androidMk = androidBp + ".mk"
411 }
412
413 reader, err := os.Open(androidBp)
Andres Moralesda8706f2015-04-29 12:46:49 -0700414 if err != nil {
415 fmt.Println(err.Error())
Ying Wang38284902015-06-02 18:44:59 -0700416 os.Exit(1)
Andres Moralesda8706f2015-04-29 12:46:49 -0700417 }
418
419 scope := bpparser.NewScope(nil)
Ying Wang38284902015-06-02 18:44:59 -0700420 blueprint, errs := bpparser.Parse(androidBp, reader, scope)
Andres Moralesda8706f2015-04-29 12:46:49 -0700421 if len(errs) > 0 {
Ying Wang38284902015-06-02 18:44:59 -0700422 fmt.Println("%d errors parsing %s", len(errs), androidBp)
Andres Moralesda8706f2015-04-29 12:46:49 -0700423 fmt.Println(errs)
Ying Wang38284902015-06-02 18:44:59 -0700424 os.Exit(1)
Andres Moralesda8706f2015-04-29 12:46:49 -0700425 }
426
427 writer := &androidMkWriter{
Andres Moralesaf11df12015-04-30 12:14:34 -0700428 blueprint: blueprint,
Ying Wang38284902015-06-02 18:44:59 -0700429 path: path.Dir(androidBp),
Andres Moralesaf11df12015-04-30 12:14:34 -0700430 mapScope: make(map[string][]*bpparser.Property),
Andres Moralesda8706f2015-04-29 12:46:49 -0700431 }
432
Ying Wang38284902015-06-02 18:44:59 -0700433 err = writer.write(androidMk)
434 if err != nil {
435 fmt.Println(err.Error())
436 os.Exit(1)
437 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700438}