blob: 2dba6c10dba0fa652e829bd760cd174c885bf939 [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 }
Colin Crosseb050832015-06-22 17:26:12 -070036 return fmt.Sprintf("%s%s",
Colin Crossff3b7952015-06-22 15:39:35 -070037 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 {
Colin Crosseb050832015-06-22 17:26:12 -070091 lines = append(lines, fmt.Sprintf(" %s", valueToString(tok)))
Andres Moralesda8706f2015-04-29 12:46:49 -070092 }
93
94 return strings.Join(lines, " \\\n")
95}
96
Andres Moralesaf11df12015-04-30 12:14:34 -070097func translateTargetConditionals(props []*bpparser.Property,
98 disabledBuilds map[string]bool, isHostRule bool) (computedProps []string) {
99 for _, target := range props {
100 conditionals := targetScopedPropertyConditionals
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700101 altConditionals := hostScopedPropertyConditionals
Andres Moralesaf11df12015-04-30 12:14:34 -0700102 if isHostRule {
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700103 conditionals, altConditionals = altConditionals, conditionals
Andres Moralesaf11df12015-04-30 12:14:34 -0700104 }
105
106 conditional, ok := conditionals[target.Name.Name]
107 if !ok {
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700108 if _, ok := altConditionals[target.Name.Name]; ok {
109 // This is only for the other build type
110 continue
111 } else {
112 // not found
113 conditional = fmt.Sprintf(
114 "ifeq(true, true) # ERROR: unsupported conditional [%s]",
115 target.Name.Name)
116 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700117 }
118
119 var scopedProps []string
120 for _, targetScopedProp := range target.Value.MapValue {
121 if mkProp, ok := standardProperties[targetScopedProp.Name.Name]; ok {
122 scopedProps = append(scopedProps, fmt.Sprintf("%s += %s",
123 mkProp.string, valueToString(targetScopedProp.Value)))
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700124 } else if rwProp, ok := rewriteProperties[targetScopedProp.Name.Name]; ok {
125 scopedProps = append(scopedProps, rwProp.f(rwProp.string, targetScopedProp, nil)...)
Andres Moralesaf11df12015-04-30 12:14:34 -0700126 } else if "disabled" == targetScopedProp.Name.Name {
127 if targetScopedProp.Value.BoolValue {
128 disabledBuilds[target.Name.Name] = true
129 } else {
130 delete(disabledBuilds, target.Name.Name)
131 }
132 }
133 }
134
135 if len(scopedProps) > 0 {
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700136 if conditional != "" {
137 computedProps = append(computedProps, conditional)
138 computedProps = append(computedProps, scopedProps...)
139 computedProps = append(computedProps, "endif")
140 } else {
141 computedProps = append(computedProps, scopedProps...)
142 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700143 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700144 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700145
146 return
147}
148
149func translateSuffixProperties(suffixProps []*bpparser.Property,
150 suffixMap map[string]string) (computedProps []string) {
151 for _, suffixProp := range suffixProps {
152 if suffix, ok := suffixMap[suffixProp.Name.Name]; ok {
153 for _, stdProp := range suffixProp.Value.MapValue {
154 if mkProp, ok := standardProperties[stdProp.Name.Name]; ok {
155 computedProps = append(computedProps, fmt.Sprintf("%s_%s := %s", mkProp.string, suffix, valueToString(stdProp.Value)))
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700156 } else if rwProp, ok := rewriteProperties[stdProp.Name.Name]; ok {
157 computedProps = append(computedProps, rwProp.f(rwProp.string, stdProp, &suffix)...)
Andres Moralesaf11df12015-04-30 12:14:34 -0700158 } else {
159 computedProps = append(computedProps, fmt.Sprintf("# ERROR: unsupported property %s", stdProp.Name.Name))
160 }
161 }
162 }
163 }
164 return
165}
166
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700167func prependLocalPath(name string, prop *bpparser.Property, suffix *string) (computedProps []string) {
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700168 if suffix != nil {
169 name += "_" + *suffix
170 }
Colin Crossff3b7952015-06-22 15:39:35 -0700171 return []string{
172 fmt.Sprintf("%s := $(addprefix $(LOCAL_PATH)/,%s)\n", name, valueToString(prop.Value)),
173 }
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700174}
175
Dan Willemsen1d9f2792015-06-22 15:40:14 -0700176func prependLocalModule(name string, prop *bpparser.Property, suffix *string) (computedProps []string) {
177 if suffix != nil {
178 name += "_" + *suffix
179 }
180 return []string {
Dan Willemsend7b11dd2015-06-22 16:25:39 -0700181 fmt.Sprintf("%s := $(LOCAL_MODULE)%s\n", name, valueToString(prop.Value)),
Dan Willemsen1d9f2792015-06-22 15:40:14 -0700182 }
183}
184
Andres Moralesaf11df12015-04-30 12:14:34 -0700185func (w *androidMkWriter) lookupMap(parent bpparser.Value) (mapValue []*bpparser.Property) {
186 if parent.Variable != "" {
187 mapValue = w.mapScope[parent.Variable]
188 } else {
189 mapValue = parent.MapValue
190 }
191 return
Andres Moralesda8706f2015-04-29 12:46:49 -0700192}
193
194func (w *androidMkWriter) handleComment(comment *bpparser.Comment) {
195 for _, c := range comment.Comment {
Andres Moralesaf11df12015-04-30 12:14:34 -0700196 fmt.Fprintf(w, "#%s\n", c)
Andres Moralesda8706f2015-04-29 12:46:49 -0700197 }
198}
199
Andres Moralesaf11df12015-04-30 12:14:34 -0700200func (w *androidMkWriter) writeModule(moduleRule string, props []string,
201 disabledBuilds map[string]bool, isHostRule bool) {
202 disabledConditionals := disabledTargetConditionals
203 if isHostRule {
204 disabledConditionals = disabledHostConditionals
205 }
206 for build, _ := range disabledBuilds {
207 if conditional, ok := disabledConditionals[build]; ok {
208 fmt.Fprintf(w, "%s\n", conditional)
209 defer fmt.Fprintf(w, "endif\n")
Andres Moralesda8706f2015-04-29 12:46:49 -0700210 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700211 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700212
Andres Moralesaf11df12015-04-30 12:14:34 -0700213 fmt.Fprintf(w, "include $(CLEAR_VARS)\n")
214 fmt.Fprintf(w, "%s\n", strings.Join(props, "\n"))
215 fmt.Fprintf(w, "include $(%s)\n\n", moduleRule)
216}
Andres Moralesda8706f2015-04-29 12:46:49 -0700217
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700218func (w *androidMkWriter) parsePropsAndWriteModule(moduleRule string, isHostRule bool, module *bpparser.Module) (hostSupported bool) {
Andres Moralesaf11df12015-04-30 12:14:34 -0700219 standardProps := make([]string, 0, len(module.Properties))
220 disabledBuilds := make(map[string]bool)
221 for _, prop := range module.Properties {
222 if mkProp, ok := standardProperties[prop.Name.Name]; ok {
223 standardProps = append(standardProps, fmt.Sprintf("%s := %s", mkProp.string, valueToString(prop.Value)))
Dan Willemsen57ad08c2015-06-10 16:20:14 -0700224 } else if rwProp, ok := rewriteProperties[prop.Name.Name]; ok {
225 standardProps = append(standardProps, rwProp.f(rwProp.string, prop, nil)...)
Andres Moralesaf11df12015-04-30 12:14:34 -0700226 } else if suffixMap, ok := suffixProperties[prop.Name.Name]; ok {
227 suffixProps := w.lookupMap(prop.Value)
228 standardProps = append(standardProps, translateSuffixProperties(suffixProps, suffixMap)...)
229 } else if "target" == prop.Name.Name {
230 props := w.lookupMap(prop.Value)
231 standardProps = append(standardProps, translateTargetConditionals(props, disabledBuilds, isHostRule)...)
232 } else if "host_supported" == prop.Name.Name {
233 hostSupported = prop.Value.BoolValue
234 } else {
235 standardProps = append(standardProps, fmt.Sprintf("# ERROR: Unsupported property %s", prop.Name.Name))
236 }
237 }
238
239 // write out target build
240 w.writeModule(moduleRule, standardProps, disabledBuilds, isHostRule)
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700241 return
242}
243
244func (w *androidMkWriter) handleModule(module *bpparser.Module) {
245 moduleRule := fmt.Sprintf(module.Type.Name)
246 if translation, ok := moduleTypeToRule[module.Type.Name]; ok {
247 moduleRule = translation
248 }
249
250 isHostRule := strings.Contains(moduleRule, "HOST")
251 hostSupported := w.parsePropsAndWriteModule(moduleRule, isHostRule, module)
252
253 if !isHostRule && hostSupported {
Andres Moralesaf11df12015-04-30 12:14:34 -0700254 hostModuleRule := "NO CORRESPONDING HOST RULE" + moduleRule
255 if trans, ok := targetToHostModuleRule[moduleRule]; ok {
256 hostModuleRule = trans
257 }
Dan Willemsen68fdfcc2015-06-11 14:05:01 -0700258
259 w.parsePropsAndWriteModule(hostModuleRule, true, module)
Andres Moralesaf11df12015-04-30 12:14:34 -0700260 }
261}
262
263func (w *androidMkWriter) handleSubdirs(value bpparser.Value) {
Andres Morales8ae47de2015-05-11 12:26:07 -0700264 subdirs := make([]string, 0, len(value.ListValue))
265 for _, tok := range value.ListValue {
266 subdirs = append(subdirs, tok.StringValue)
Andres Moralesda8706f2015-04-29 12:46:49 -0700267 }
Ying Wang38284902015-06-02 18:44:59 -0700268 // The current makefile may be generated to outside the source tree (such as the out directory), with a different structure.
269 fmt.Fprintf(w, "# Uncomment the following line if you really want to include subdir Android.mks.\n")
270 fmt.Fprintf(w, "# include $(wildcard $(addsuffix $(LOCAL_PATH)/%s/, Android.mk))\n", strings.Join(subdirs, " "))
Andres Moralesda8706f2015-04-29 12:46:49 -0700271}
272
273func (w *androidMkWriter) handleAssignment(assignment *bpparser.Assignment) {
Andres Moralesaf11df12015-04-30 12:14:34 -0700274 if "subdirs" == assignment.Name.Name {
275 w.handleSubdirs(assignment.OrigValue)
276 } else if assignment.OrigValue.Type == bpparser.Map {
277 // maps may be assigned in Soong, but can only be translated to .mk
278 // in the context of the module
279 w.mapScope[assignment.Name.Name] = assignment.OrigValue.MapValue
280 } else {
281 assigner := ":="
282 if assignment.Assigner != "=" {
283 assigner = assignment.Assigner
284 }
285 fmt.Fprintf(w, "%s %s %s\n", assignment.Name.Name, assigner,
286 valueToString(assignment.OrigValue))
Andres Moralesda8706f2015-04-29 12:46:49 -0700287 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700288}
289
290func (w *androidMkWriter) iter() <-chan interface{} {
Andres Moralesaf11df12015-04-30 12:14:34 -0700291 ch := make(chan interface{}, len(w.blueprint.Comments)+len(w.blueprint.Defs))
Andres Moralesda8706f2015-04-29 12:46:49 -0700292 go func() {
293 commIdx := 0
294 defsIdx := 0
Andres Moralesaf11df12015-04-30 12:14:34 -0700295 for defsIdx < len(w.blueprint.Defs) || commIdx < len(w.blueprint.Comments) {
296 if defsIdx == len(w.blueprint.Defs) {
297 ch <- w.blueprint.Comments[commIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700298 commIdx++
Andres Moralesaf11df12015-04-30 12:14:34 -0700299 } else if commIdx == len(w.blueprint.Comments) {
300 ch <- w.blueprint.Defs[defsIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700301 defsIdx++
302 } else {
303 commentsPos := 0
304 defsPos := 0
305
Andres Moralesaf11df12015-04-30 12:14:34 -0700306 def := w.blueprint.Defs[defsIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700307 switch def := def.(type) {
308 case *bpparser.Module:
309 defsPos = def.LbracePos.Line
310 case *bpparser.Assignment:
311 defsPos = def.Pos.Line
312 }
313
Andres Moralesaf11df12015-04-30 12:14:34 -0700314 comment := w.blueprint.Comments[commIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700315 commentsPos = comment.Pos.Line
316
317 if commentsPos < defsPos {
318 commIdx++
319 ch <- comment
320 } else {
321 defsIdx++
322 ch <- def
323 }
324 }
325 }
326 close(ch)
327 }()
328 return ch
329}
330
Andres Morales8ae47de2015-05-11 12:26:07 -0700331func (w *androidMkWriter) handleLocalPath() error {
Dan Willemsen360a39c2015-06-11 14:34:50 -0700332 if w.printedLocalPath {
333 return nil
334 }
335 w.printedLocalPath = true
336
Ying Wang38284902015-06-02 18:44:59 -0700337 localPath, err := filepath.Abs(w.path)
Andres Morales8ae47de2015-05-11 12:26:07 -0700338 if err != nil {
339 return err
340 }
341
Ying Wang38284902015-06-02 18:44:59 -0700342 top, err := getTopOfAndroidTree(localPath)
Andres Morales8ae47de2015-05-11 12:26:07 -0700343 if err != nil {
344 return err
345 }
346
Ying Wang38284902015-06-02 18:44:59 -0700347 rel, err := filepath.Rel(top, localPath)
Andres Morales8ae47de2015-05-11 12:26:07 -0700348 if err != nil {
349 return err
350 }
351
352 w.WriteString("LOCAL_PATH := " + rel + "\n")
Dan Willemsenc2666e62015-06-10 16:18:58 -0700353 w.WriteString("LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))\n\n")
Andres Morales8ae47de2015-05-11 12:26:07 -0700354 return nil
355}
356
Ying Wang38284902015-06-02 18:44:59 -0700357func (w *androidMkWriter) write(androidMk string) error {
358 fmt.Printf("Writing %s\n", androidMk)
Andres Moralesda8706f2015-04-29 12:46:49 -0700359
Ying Wang38284902015-06-02 18:44:59 -0700360 f, err := os.Create(androidMk)
Andres Moralesda8706f2015-04-29 12:46:49 -0700361 if err != nil {
362 panic(err)
363 }
364
Andres Moralesaf11df12015-04-30 12:14:34 -0700365 defer f.Close()
Andres Moralesda8706f2015-04-29 12:46:49 -0700366
367 w.Writer = bufio.NewWriter(f)
368
369 for block := range w.iter() {
370 switch block := block.(type) {
371 case *bpparser.Module:
Dan Willemsen360a39c2015-06-11 14:34:50 -0700372 if err := w.handleLocalPath(); err != nil {
373 return err
374 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700375 w.handleModule(block)
376 case *bpparser.Assignment:
Dan Willemsen360a39c2015-06-11 14:34:50 -0700377 if err := w.handleLocalPath(); err != nil {
378 return err
379 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700380 w.handleAssignment(block)
381 case bpparser.Comment:
382 w.handleComment(&block)
383 }
384 }
385
386 if err = w.Flush(); err != nil {
387 panic(err)
388 }
Ying Wang38284902015-06-02 18:44:59 -0700389 return nil
Andres Moralesda8706f2015-04-29 12:46:49 -0700390}
391
392func main() {
393 if len(os.Args) < 2 {
394 fmt.Println("No filename supplied")
Ying Wang38284902015-06-02 18:44:59 -0700395 os.Exit(1)
Andres Moralesda8706f2015-04-29 12:46:49 -0700396 }
397
Ying Wang38284902015-06-02 18:44:59 -0700398 androidBp := os.Args[1]
399 var androidMk string
400 if len(os.Args) >= 3 {
401 androidMk = os.Args[2]
402 } else {
403 androidMk = androidBp + ".mk"
404 }
405
406 reader, err := os.Open(androidBp)
Andres Moralesda8706f2015-04-29 12:46:49 -0700407 if err != nil {
408 fmt.Println(err.Error())
Ying Wang38284902015-06-02 18:44:59 -0700409 os.Exit(1)
Andres Moralesda8706f2015-04-29 12:46:49 -0700410 }
411
412 scope := bpparser.NewScope(nil)
Ying Wang38284902015-06-02 18:44:59 -0700413 blueprint, errs := bpparser.Parse(androidBp, reader, scope)
Andres Moralesda8706f2015-04-29 12:46:49 -0700414 if len(errs) > 0 {
Ying Wang38284902015-06-02 18:44:59 -0700415 fmt.Println("%d errors parsing %s", len(errs), androidBp)
Andres Moralesda8706f2015-04-29 12:46:49 -0700416 fmt.Println(errs)
Ying Wang38284902015-06-02 18:44:59 -0700417 os.Exit(1)
Andres Moralesda8706f2015-04-29 12:46:49 -0700418 }
419
420 writer := &androidMkWriter{
Andres Moralesaf11df12015-04-30 12:14:34 -0700421 blueprint: blueprint,
Ying Wang38284902015-06-02 18:44:59 -0700422 path: path.Dir(androidBp),
Andres Moralesaf11df12015-04-30 12:14:34 -0700423 mapScope: make(map[string][]*bpparser.Property),
Andres Moralesda8706f2015-04-29 12:46:49 -0700424 }
425
Ying Wang38284902015-06-02 18:44:59 -0700426 err = writer.write(androidMk)
427 if err != nil {
428 fmt.Println(err.Error())
429 os.Exit(1)
430 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700431}