blob: ba2eda4927a683cb897b2d9c270e655dad8fb7a6 [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
Andres Moralesda8706f2015-04-29 12:46:49 -070018type androidMkWriter struct {
19 *bufio.Writer
20
Andres Moralesaf11df12015-04-30 12:14:34 -070021 blueprint *bpparser.File
22 path string
23
Dan Willemsen360a39c2015-06-11 14:34:50 -070024 printedLocalPath bool
25
Andres Moralesaf11df12015-04-30 12:14:34 -070026 mapScope map[string][]*bpparser.Property
Andres Moralesda8706f2015-04-29 12:46:49 -070027}
28
Andres Moralesaf11df12015-04-30 12:14:34 -070029func valueToString(value bpparser.Value) string {
Andres Moralesda8706f2015-04-29 12:46:49 -070030 if value.Variable != "" {
31 return fmt.Sprintf("$(%s)", value.Variable)
Colin Crossff3b7952015-06-22 15:39:35 -070032 } else if value.Expression != nil {
33 if value.Expression.Operator != '+' {
34 panic(fmt.Errorf("unexpected operator '%c'", value.Expression.Operator))
35 }
36 return fmt.Sprintf("%s %s",
37 valueToString(value.Expression.Args[0]),
38 valueToString(value.Expression.Args[1]))
Andres Moralesda8706f2015-04-29 12:46:49 -070039 } else {
40 switch value.Type {
41 case bpparser.Bool:
Ying Wang38284902015-06-02 18:44:59 -070042 return fmt.Sprintf("%t", value.BoolValue)
Andres Moralesda8706f2015-04-29 12:46:49 -070043 case bpparser.String:
Ying Wang38284902015-06-02 18:44:59 -070044 return fmt.Sprintf("%s", processWildcards(value.StringValue))
Andres Moralesda8706f2015-04-29 12:46:49 -070045 case bpparser.List:
Colin Crossff3b7952015-06-22 15:39:35 -070046 return fmt.Sprintf("\\\n%s", listToMkString(value.ListValue))
Andres Moralesda8706f2015-04-29 12:46:49 -070047 case bpparser.Map:
Andres Moralesaf11df12015-04-30 12:14:34 -070048 return fmt.Sprintf("ERROR can't convert map to string")
49 default:
50 return fmt.Sprintf("ERROR: unsupported type %d", value.Type)
Andres Moralesda8706f2015-04-29 12:46:49 -070051 }
52 }
Andres Moralesda8706f2015-04-29 12:46:49 -070053}
54
Andres Morales8ae47de2015-05-11 12:26:07 -070055func getTopOfAndroidTree(wd string) (string, error) {
56 if !filepath.IsAbs(wd) {
57 return "", errors.New("path must be absolute: " + wd)
58 }
59
60 topfile := "build/soong/bootstrap.bash"
61
62 for "/" != wd {
63 expected := filepath.Join(wd, topfile)
64
65 if _, err := os.Stat(expected); err == nil {
66 // Found the top
67 return wd, nil
68 }
69
70 wd = filepath.Join(wd, "..")
71 }
72
73 return "", errors.New("couldn't find top of tree from " + wd)
74}
75
Andres Moralesaf11df12015-04-30 12:14:34 -070076// TODO: handle non-recursive wildcards?
77func processWildcards(s string) string {
Andres Morales8ae47de2015-05-11 12:26:07 -070078 submatches := recursiveSubdirRegex.FindStringSubmatch(s)
79 if len(submatches) > 2 {
Andres Moralesaf11df12015-04-30 12:14:34 -070080 // Found a wildcard rule
81 return fmt.Sprintf("$(call find-files-in-subdirs, $(LOCAL_PATH), %s, %s)",
Andres Morales8ae47de2015-05-11 12:26:07 -070082 submatches[2], submatches[1])
Andres Moralesaf11df12015-04-30 12:14:34 -070083 }
84
85 return s
86}
87
88func listToMkString(list []bpparser.Value) string {
Andres Moralesda8706f2015-04-29 12:46:49 -070089 lines := make([]string, 0, len(list))
90 for _, tok := range list {
Andres Moralesaf11df12015-04-30 12:14:34 -070091 if tok.Type == bpparser.String {
Ying Wang38284902015-06-02 18:44:59 -070092 lines = append(lines, fmt.Sprintf(" %s", processWildcards(tok.StringValue)))
Andres Moralesaf11df12015-04-30 12:14:34 -070093 } else {
94 lines = append(lines, fmt.Sprintf("# ERROR: unsupported type %s in list",
95 tok.Type.String()))
96 }
Andres Moralesda8706f2015-04-29 12:46:49 -070097 }
98
99 return strings.Join(lines, " \\\n")
100}
101
Andres Moralesaf11df12015-04-30 12:14:34 -0700102func translateTargetConditionals(props []*bpparser.Property,
103 disabledBuilds map[string]bool, isHostRule bool) (computedProps []string) {
104 for _, target := range props {
105 conditionals := targetScopedPropertyConditionals
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700106 altConditionals := hostScopedPropertyConditionals
Andres Moralesaf11df12015-04-30 12:14:34 -0700107 if isHostRule {
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700108 conditionals, altConditionals = altConditionals, conditionals
Andres Moralesaf11df12015-04-30 12:14:34 -0700109 }
110
111 conditional, ok := conditionals[target.Name.Name]
112 if !ok {
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700113 if _, ok := altConditionals[target.Name.Name]; ok {
114 // This is only for the other build type
115 continue
116 } else {
117 // not found
118 conditional = fmt.Sprintf(
119 "ifeq(true, true) # ERROR: unsupported conditional [%s]",
120 target.Name.Name)
121 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700122 }
123
124 var scopedProps []string
125 for _, targetScopedProp := range target.Value.MapValue {
126 if mkProp, ok := standardProperties[targetScopedProp.Name.Name]; ok {
127 scopedProps = append(scopedProps, fmt.Sprintf("%s += %s",
128 mkProp.string, valueToString(targetScopedProp.Value)))
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700129 } else if rwProp, ok := rewriteProperties[targetScopedProp.Name.Name]; ok {
130 scopedProps = append(scopedProps, rwProp.f(rwProp.string, targetScopedProp, nil)...)
Andres Moralesaf11df12015-04-30 12:14:34 -0700131 } else if "disabled" == targetScopedProp.Name.Name {
132 if targetScopedProp.Value.BoolValue {
133 disabledBuilds[target.Name.Name] = true
134 } else {
135 delete(disabledBuilds, target.Name.Name)
136 }
137 }
138 }
139
140 if len(scopedProps) > 0 {
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700141 if conditional != "" {
142 computedProps = append(computedProps, conditional)
143 computedProps = append(computedProps, scopedProps...)
144 computedProps = append(computedProps, "endif")
145 } else {
146 computedProps = append(computedProps, scopedProps...)
147 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700148 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700149 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700150
151 return
152}
153
154func translateSuffixProperties(suffixProps []*bpparser.Property,
155 suffixMap map[string]string) (computedProps []string) {
156 for _, suffixProp := range suffixProps {
157 if suffix, ok := suffixMap[suffixProp.Name.Name]; ok {
158 for _, stdProp := range suffixProp.Value.MapValue {
159 if mkProp, ok := standardProperties[stdProp.Name.Name]; ok {
160 computedProps = append(computedProps, fmt.Sprintf("%s_%s := %s", mkProp.string, suffix, valueToString(stdProp.Value)))
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700161 } else if rwProp, ok := rewriteProperties[stdProp.Name.Name]; ok {
162 computedProps = append(computedProps, rwProp.f(rwProp.string, stdProp, &suffix)...)
Andres Moralesaf11df12015-04-30 12:14:34 -0700163 } else {
164 computedProps = append(computedProps, fmt.Sprintf("# ERROR: unsupported property %s", stdProp.Name.Name))
165 }
166 }
167 }
168 }
169 return
170}
171
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700172func prependLocalPath(name string, prop *bpparser.Property, suffix *string) (computedProps []string) {
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700173 if suffix != nil {
174 name += "_" + *suffix
175 }
Colin Crossff3b7952015-06-22 15:39:35 -0700176 return []string{
177 fmt.Sprintf("%s := $(addprefix $(LOCAL_PATH)/,%s)\n", name, valueToString(prop.Value)),
178 }
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700179}
180
Andres Moralesaf11df12015-04-30 12:14:34 -0700181func (w *androidMkWriter) lookupMap(parent bpparser.Value) (mapValue []*bpparser.Property) {
182 if parent.Variable != "" {
183 mapValue = w.mapScope[parent.Variable]
184 } else {
185 mapValue = parent.MapValue
186 }
187 return
Andres Moralesda8706f2015-04-29 12:46:49 -0700188}
189
190func (w *androidMkWriter) handleComment(comment *bpparser.Comment) {
191 for _, c := range comment.Comment {
Andres Moralesaf11df12015-04-30 12:14:34 -0700192 fmt.Fprintf(w, "#%s\n", c)
Andres Moralesda8706f2015-04-29 12:46:49 -0700193 }
194}
195
Andres Moralesaf11df12015-04-30 12:14:34 -0700196func (w *androidMkWriter) writeModule(moduleRule string, props []string,
197 disabledBuilds map[string]bool, isHostRule bool) {
198 disabledConditionals := disabledTargetConditionals
199 if isHostRule {
200 disabledConditionals = disabledHostConditionals
201 }
202 for build, _ := range disabledBuilds {
203 if conditional, ok := disabledConditionals[build]; ok {
204 fmt.Fprintf(w, "%s\n", conditional)
205 defer fmt.Fprintf(w, "endif\n")
Andres Moralesda8706f2015-04-29 12:46:49 -0700206 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700207 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700208
Andres Moralesaf11df12015-04-30 12:14:34 -0700209 fmt.Fprintf(w, "include $(CLEAR_VARS)\n")
210 fmt.Fprintf(w, "%s\n", strings.Join(props, "\n"))
211 fmt.Fprintf(w, "include $(%s)\n\n", moduleRule)
212}
Andres Moralesda8706f2015-04-29 12:46:49 -0700213
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700214func (w *androidMkWriter) parsePropsAndWriteModule(moduleRule string, isHostRule bool, module *bpparser.Module) (hostSupported bool) {
Andres Moralesaf11df12015-04-30 12:14:34 -0700215 standardProps := make([]string, 0, len(module.Properties))
216 disabledBuilds := make(map[string]bool)
217 for _, prop := range module.Properties {
218 if mkProp, ok := standardProperties[prop.Name.Name]; ok {
219 standardProps = append(standardProps, fmt.Sprintf("%s := %s", mkProp.string, valueToString(prop.Value)))
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700220 } else if rwProp, ok := rewriteProperties[prop.Name.Name]; ok {
221 standardProps = append(standardProps, rwProp.f(rwProp.string, prop, nil)...)
Andres Moralesaf11df12015-04-30 12:14:34 -0700222 } else if suffixMap, ok := suffixProperties[prop.Name.Name]; ok {
223 suffixProps := w.lookupMap(prop.Value)
224 standardProps = append(standardProps, translateSuffixProperties(suffixProps, suffixMap)...)
225 } else if "target" == prop.Name.Name {
226 props := w.lookupMap(prop.Value)
227 standardProps = append(standardProps, translateTargetConditionals(props, disabledBuilds, isHostRule)...)
228 } else if "host_supported" == prop.Name.Name {
229 hostSupported = prop.Value.BoolValue
230 } else {
231 standardProps = append(standardProps, fmt.Sprintf("# ERROR: Unsupported property %s", prop.Name.Name))
232 }
233 }
234
235 // write out target build
236 w.writeModule(moduleRule, standardProps, disabledBuilds, isHostRule)
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700237 return
238}
239
240func (w *androidMkWriter) handleModule(module *bpparser.Module) {
241 moduleRule := fmt.Sprintf(module.Type.Name)
242 if translation, ok := moduleTypeToRule[module.Type.Name]; ok {
243 moduleRule = translation
244 }
245
246 isHostRule := strings.Contains(moduleRule, "HOST")
247 hostSupported := w.parsePropsAndWriteModule(moduleRule, isHostRule, module)
248
249 if !isHostRule && hostSupported {
Andres Moralesaf11df12015-04-30 12:14:34 -0700250 hostModuleRule := "NO CORRESPONDING HOST RULE" + moduleRule
251 if trans, ok := targetToHostModuleRule[moduleRule]; ok {
252 hostModuleRule = trans
253 }
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700254
255 w.parsePropsAndWriteModule(hostModuleRule, true, module)
Andres Moralesaf11df12015-04-30 12:14:34 -0700256 }
257}
258
259func (w *androidMkWriter) handleSubdirs(value bpparser.Value) {
Andres Morales8ae47de2015-05-11 12:26:07 -0700260 subdirs := make([]string, 0, len(value.ListValue))
261 for _, tok := range value.ListValue {
262 subdirs = append(subdirs, tok.StringValue)
Andres Moralesda8706f2015-04-29 12:46:49 -0700263 }
Ying Wang38284902015-06-02 18:44:59 -0700264 // The current makefile may be generated to outside the source tree (such as the out directory), with a different structure.
265 fmt.Fprintf(w, "# Uncomment the following line if you really want to include subdir Android.mks.\n")
266 fmt.Fprintf(w, "# include $(wildcard $(addsuffix $(LOCAL_PATH)/%s/, Android.mk))\n", strings.Join(subdirs, " "))
Andres Moralesda8706f2015-04-29 12:46:49 -0700267}
268
269func (w *androidMkWriter) handleAssignment(assignment *bpparser.Assignment) {
Andres Moralesaf11df12015-04-30 12:14:34 -0700270 if "subdirs" == assignment.Name.Name {
271 w.handleSubdirs(assignment.OrigValue)
272 } else if assignment.OrigValue.Type == bpparser.Map {
273 // maps may be assigned in Soong, but can only be translated to .mk
274 // in the context of the module
275 w.mapScope[assignment.Name.Name] = assignment.OrigValue.MapValue
276 } else {
277 assigner := ":="
278 if assignment.Assigner != "=" {
279 assigner = assignment.Assigner
280 }
281 fmt.Fprintf(w, "%s %s %s\n", assignment.Name.Name, assigner,
282 valueToString(assignment.OrigValue))
Andres Moralesda8706f2015-04-29 12:46:49 -0700283 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700284}
285
286func (w *androidMkWriter) iter() <-chan interface{} {
Andres Moralesaf11df12015-04-30 12:14:34 -0700287 ch := make(chan interface{}, len(w.blueprint.Comments)+len(w.blueprint.Defs))
Andres Moralesda8706f2015-04-29 12:46:49 -0700288 go func() {
289 commIdx := 0
290 defsIdx := 0
Andres Moralesaf11df12015-04-30 12:14:34 -0700291 for defsIdx < len(w.blueprint.Defs) || commIdx < len(w.blueprint.Comments) {
292 if defsIdx == len(w.blueprint.Defs) {
293 ch <- w.blueprint.Comments[commIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700294 commIdx++
Andres Moralesaf11df12015-04-30 12:14:34 -0700295 } else if commIdx == len(w.blueprint.Comments) {
296 ch <- w.blueprint.Defs[defsIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700297 defsIdx++
298 } else {
299 commentsPos := 0
300 defsPos := 0
301
Andres Moralesaf11df12015-04-30 12:14:34 -0700302 def := w.blueprint.Defs[defsIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700303 switch def := def.(type) {
304 case *bpparser.Module:
305 defsPos = def.LbracePos.Line
306 case *bpparser.Assignment:
307 defsPos = def.Pos.Line
308 }
309
Andres Moralesaf11df12015-04-30 12:14:34 -0700310 comment := w.blueprint.Comments[commIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700311 commentsPos = comment.Pos.Line
312
313 if commentsPos < defsPos {
314 commIdx++
315 ch <- comment
316 } else {
317 defsIdx++
318 ch <- def
319 }
320 }
321 }
322 close(ch)
323 }()
324 return ch
325}
326
Andres Morales8ae47de2015-05-11 12:26:07 -0700327func (w *androidMkWriter) handleLocalPath() error {
Dan Willemsen360a39c2015-06-11 14:34:50 -0700328 if w.printedLocalPath {
329 return nil
330 }
331 w.printedLocalPath = true
332
Ying Wang38284902015-06-02 18:44:59 -0700333 localPath, err := filepath.Abs(w.path)
Andres Morales8ae47de2015-05-11 12:26:07 -0700334 if err != nil {
335 return err
336 }
337
Ying Wang38284902015-06-02 18:44:59 -0700338 top, err := getTopOfAndroidTree(localPath)
Andres Morales8ae47de2015-05-11 12:26:07 -0700339 if err != nil {
340 return err
341 }
342
Ying Wang38284902015-06-02 18:44:59 -0700343 rel, err := filepath.Rel(top, localPath)
Andres Morales8ae47de2015-05-11 12:26:07 -0700344 if err != nil {
345 return err
346 }
347
348 w.WriteString("LOCAL_PATH := " + rel + "\n")
Dan Willemsenc2666e62015-06-10 16:18:58 -0700349 w.WriteString("LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))\n\n")
Andres Morales8ae47de2015-05-11 12:26:07 -0700350 return nil
351}
352
Ying Wang38284902015-06-02 18:44:59 -0700353func (w *androidMkWriter) write(androidMk string) error {
354 fmt.Printf("Writing %s\n", androidMk)
Andres Moralesda8706f2015-04-29 12:46:49 -0700355
Ying Wang38284902015-06-02 18:44:59 -0700356 f, err := os.Create(androidMk)
Andres Moralesda8706f2015-04-29 12:46:49 -0700357 if err != nil {
358 panic(err)
359 }
360
Andres Moralesaf11df12015-04-30 12:14:34 -0700361 defer f.Close()
Andres Moralesda8706f2015-04-29 12:46:49 -0700362
363 w.Writer = bufio.NewWriter(f)
364
365 for block := range w.iter() {
366 switch block := block.(type) {
367 case *bpparser.Module:
Dan Willemsen360a39c2015-06-11 14:34:50 -0700368 if err := w.handleLocalPath(); err != nil {
369 return err
370 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700371 w.handleModule(block)
372 case *bpparser.Assignment:
Dan Willemsen360a39c2015-06-11 14:34:50 -0700373 if err := w.handleLocalPath(); err != nil {
374 return err
375 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700376 w.handleAssignment(block)
377 case bpparser.Comment:
378 w.handleComment(&block)
379 }
380 }
381
382 if err = w.Flush(); err != nil {
383 panic(err)
384 }
Ying Wang38284902015-06-02 18:44:59 -0700385 return nil
Andres Moralesda8706f2015-04-29 12:46:49 -0700386}
387
388func main() {
389 if len(os.Args) < 2 {
390 fmt.Println("No filename supplied")
Ying Wang38284902015-06-02 18:44:59 -0700391 os.Exit(1)
Andres Moralesda8706f2015-04-29 12:46:49 -0700392 }
393
Ying Wang38284902015-06-02 18:44:59 -0700394 androidBp := os.Args[1]
395 var androidMk string
396 if len(os.Args) >= 3 {
397 androidMk = os.Args[2]
398 } else {
399 androidMk = androidBp + ".mk"
400 }
401
402 reader, err := os.Open(androidBp)
Andres Moralesda8706f2015-04-29 12:46:49 -0700403 if err != nil {
404 fmt.Println(err.Error())
Ying Wang38284902015-06-02 18:44:59 -0700405 os.Exit(1)
Andres Moralesda8706f2015-04-29 12:46:49 -0700406 }
407
408 scope := bpparser.NewScope(nil)
Ying Wang38284902015-06-02 18:44:59 -0700409 blueprint, errs := bpparser.Parse(androidBp, reader, scope)
Andres Moralesda8706f2015-04-29 12:46:49 -0700410 if len(errs) > 0 {
Ying Wang38284902015-06-02 18:44:59 -0700411 fmt.Println("%d errors parsing %s", len(errs), androidBp)
Andres Moralesda8706f2015-04-29 12:46:49 -0700412 fmt.Println(errs)
Ying Wang38284902015-06-02 18:44:59 -0700413 os.Exit(1)
Andres Moralesda8706f2015-04-29 12:46:49 -0700414 }
415
416 writer := &androidMkWriter{
Andres Moralesaf11df12015-04-30 12:14:34 -0700417 blueprint: blueprint,
Ying Wang38284902015-06-02 18:44:59 -0700418 path: path.Dir(androidBp),
Andres Moralesaf11df12015-04-30 12:14:34 -0700419 mapScope: make(map[string][]*bpparser.Property),
Andres Moralesda8706f2015-04-29 12:46:49 -0700420 }
421
Ying Wang38284902015-06-02 18:44:59 -0700422 err = writer.write(androidMk)
423 if err != nil {
424 fmt.Println(err.Error())
425 os.Exit(1)
426 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700427}