blob: a68b11c2f30c5cced22d79f223d62120c4cf1f9d [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
24 mapScope map[string][]*bpparser.Property
Andres Moralesda8706f2015-04-29 12:46:49 -070025}
26
Andres Moralesaf11df12015-04-30 12:14:34 -070027func valueToString(value bpparser.Value) string {
Andres Moralesda8706f2015-04-29 12:46:49 -070028 if value.Variable != "" {
29 return fmt.Sprintf("$(%s)", value.Variable)
30 } else {
31 switch value.Type {
32 case bpparser.Bool:
33 return fmt.Sprintf(`"%t"`, value.BoolValue)
34 case bpparser.String:
Andres Moralesaf11df12015-04-30 12:14:34 -070035 return fmt.Sprintf(`"%s"`, processWildcards(value.StringValue))
Andres Moralesda8706f2015-04-29 12:46:49 -070036 case bpparser.List:
Andres Moralesaf11df12015-04-30 12:14:34 -070037 return fmt.Sprintf("\\\n%s\n", listToMkString(value.ListValue))
Andres Moralesda8706f2015-04-29 12:46:49 -070038 case bpparser.Map:
Andres Moralesaf11df12015-04-30 12:14:34 -070039 return fmt.Sprintf("ERROR can't convert map to string")
40 default:
41 return fmt.Sprintf("ERROR: unsupported type %d", value.Type)
Andres Moralesda8706f2015-04-29 12:46:49 -070042 }
43 }
Andres Moralesda8706f2015-04-29 12:46:49 -070044}
45
Andres Morales8ae47de2015-05-11 12:26:07 -070046func getTopOfAndroidTree(wd string) (string, error) {
47 if !filepath.IsAbs(wd) {
48 return "", errors.New("path must be absolute: " + wd)
49 }
50
51 topfile := "build/soong/bootstrap.bash"
52
53 for "/" != wd {
54 expected := filepath.Join(wd, topfile)
55
56 if _, err := os.Stat(expected); err == nil {
57 // Found the top
58 return wd, nil
59 }
60
61 wd = filepath.Join(wd, "..")
62 }
63
64 return "", errors.New("couldn't find top of tree from " + wd)
65}
66
Andres Moralesaf11df12015-04-30 12:14:34 -070067// TODO: handle non-recursive wildcards?
68func processWildcards(s string) string {
Andres Morales8ae47de2015-05-11 12:26:07 -070069 submatches := recursiveSubdirRegex.FindStringSubmatch(s)
70 if len(submatches) > 2 {
Andres Moralesaf11df12015-04-30 12:14:34 -070071 // Found a wildcard rule
72 return fmt.Sprintf("$(call find-files-in-subdirs, $(LOCAL_PATH), %s, %s)",
Andres Morales8ae47de2015-05-11 12:26:07 -070073 submatches[2], submatches[1])
Andres Moralesaf11df12015-04-30 12:14:34 -070074 }
75
76 return s
77}
78
79func listToMkString(list []bpparser.Value) string {
Andres Moralesda8706f2015-04-29 12:46:49 -070080 lines := make([]string, 0, len(list))
81 for _, tok := range list {
Andres Moralesaf11df12015-04-30 12:14:34 -070082 if tok.Type == bpparser.String {
83 lines = append(lines, fmt.Sprintf("\t\"%s\"", processWildcards(tok.StringValue)))
84 } else {
85 lines = append(lines, fmt.Sprintf("# ERROR: unsupported type %s in list",
86 tok.Type.String()))
87 }
Andres Moralesda8706f2015-04-29 12:46:49 -070088 }
89
90 return strings.Join(lines, " \\\n")
91}
92
Andres Moralesaf11df12015-04-30 12:14:34 -070093func translateTargetConditionals(props []*bpparser.Property,
94 disabledBuilds map[string]bool, isHostRule bool) (computedProps []string) {
95 for _, target := range props {
96 conditionals := targetScopedPropertyConditionals
97 if isHostRule {
98 conditionals = hostScopedPropertyConditionals
99 }
100
101 conditional, ok := conditionals[target.Name.Name]
102 if !ok {
103 // not found
104 conditional = fmt.Sprintf(
105 "ifeq(true, true) # ERROR: unsupported conditional host [%s]",
106 target.Name.Name)
107 }
108
109 var scopedProps []string
110 for _, targetScopedProp := range target.Value.MapValue {
111 if mkProp, ok := standardProperties[targetScopedProp.Name.Name]; ok {
112 scopedProps = append(scopedProps, fmt.Sprintf("%s += %s",
113 mkProp.string, valueToString(targetScopedProp.Value)))
114 } else if "disabled" == targetScopedProp.Name.Name {
115 if targetScopedProp.Value.BoolValue {
116 disabledBuilds[target.Name.Name] = true
117 } else {
118 delete(disabledBuilds, target.Name.Name)
119 }
120 }
121 }
122
123 if len(scopedProps) > 0 {
124 computedProps = append(computedProps, conditional)
125 computedProps = append(computedProps, scopedProps...)
126 computedProps = append(computedProps, "endif")
127 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700128 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700129
130 return
131}
132
133func translateSuffixProperties(suffixProps []*bpparser.Property,
134 suffixMap map[string]string) (computedProps []string) {
135 for _, suffixProp := range suffixProps {
136 if suffix, ok := suffixMap[suffixProp.Name.Name]; ok {
137 for _, stdProp := range suffixProp.Value.MapValue {
138 if mkProp, ok := standardProperties[stdProp.Name.Name]; ok {
139 computedProps = append(computedProps, fmt.Sprintf("%s_%s := %s", mkProp.string, suffix, valueToString(stdProp.Value)))
140 } else {
141 computedProps = append(computedProps, fmt.Sprintf("# ERROR: unsupported property %s", stdProp.Name.Name))
142 }
143 }
144 }
145 }
146 return
147}
148
149func (w *androidMkWriter) lookupMap(parent bpparser.Value) (mapValue []*bpparser.Property) {
150 if parent.Variable != "" {
151 mapValue = w.mapScope[parent.Variable]
152 } else {
153 mapValue = parent.MapValue
154 }
155 return
Andres Moralesda8706f2015-04-29 12:46:49 -0700156}
157
158func (w *androidMkWriter) handleComment(comment *bpparser.Comment) {
159 for _, c := range comment.Comment {
Andres Moralesaf11df12015-04-30 12:14:34 -0700160 fmt.Fprintf(w, "#%s\n", c)
Andres Moralesda8706f2015-04-29 12:46:49 -0700161 }
162}
163
Andres Moralesaf11df12015-04-30 12:14:34 -0700164func (w *androidMkWriter) writeModule(moduleRule string, props []string,
165 disabledBuilds map[string]bool, isHostRule bool) {
166 disabledConditionals := disabledTargetConditionals
167 if isHostRule {
168 disabledConditionals = disabledHostConditionals
169 }
170 for build, _ := range disabledBuilds {
171 if conditional, ok := disabledConditionals[build]; ok {
172 fmt.Fprintf(w, "%s\n", conditional)
173 defer fmt.Fprintf(w, "endif\n")
Andres Moralesda8706f2015-04-29 12:46:49 -0700174 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700175 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700176
Andres Moralesaf11df12015-04-30 12:14:34 -0700177 fmt.Fprintf(w, "include $(CLEAR_VARS)\n")
178 fmt.Fprintf(w, "%s\n", strings.Join(props, "\n"))
179 fmt.Fprintf(w, "include $(%s)\n\n", moduleRule)
180}
Andres Moralesda8706f2015-04-29 12:46:49 -0700181
Andres Moralesaf11df12015-04-30 12:14:34 -0700182func (w *androidMkWriter) handleModule(module *bpparser.Module) {
183 moduleRule := fmt.Sprintf(module.Type.Name)
184 if translation, ok := moduleTypeToRule[module.Type.Name]; ok {
185 moduleRule = translation
186 }
187
188 isHostRule := strings.Contains(moduleRule, "HOST")
189 hostSupported := false
190 standardProps := make([]string, 0, len(module.Properties))
191 disabledBuilds := make(map[string]bool)
192 for _, prop := range module.Properties {
193 if mkProp, ok := standardProperties[prop.Name.Name]; ok {
194 standardProps = append(standardProps, fmt.Sprintf("%s := %s", mkProp.string, valueToString(prop.Value)))
195 } else if suffixMap, ok := suffixProperties[prop.Name.Name]; ok {
196 suffixProps := w.lookupMap(prop.Value)
197 standardProps = append(standardProps, translateSuffixProperties(suffixProps, suffixMap)...)
198 } else if "target" == prop.Name.Name {
199 props := w.lookupMap(prop.Value)
200 standardProps = append(standardProps, translateTargetConditionals(props, disabledBuilds, isHostRule)...)
201 } else if "host_supported" == prop.Name.Name {
202 hostSupported = prop.Value.BoolValue
203 } else {
204 standardProps = append(standardProps, fmt.Sprintf("# ERROR: Unsupported property %s", prop.Name.Name))
205 }
206 }
207
208 // write out target build
209 w.writeModule(moduleRule, standardProps, disabledBuilds, isHostRule)
210 if hostSupported {
211 hostModuleRule := "NO CORRESPONDING HOST RULE" + moduleRule
212 if trans, ok := targetToHostModuleRule[moduleRule]; ok {
213 hostModuleRule = trans
214 }
215 w.writeModule(hostModuleRule, standardProps,
216 disabledBuilds, true)
217 }
218}
219
220func (w *androidMkWriter) handleSubdirs(value bpparser.Value) {
Andres Morales8ae47de2015-05-11 12:26:07 -0700221 subdirs := make([]string, 0, len(value.ListValue))
222 for _, tok := range value.ListValue {
223 subdirs = append(subdirs, tok.StringValue)
Andres Moralesda8706f2015-04-29 12:46:49 -0700224 }
Andres Morales8ae47de2015-05-11 12:26:07 -0700225 fmt.Fprintf(w, "include $(wildcard $(addsuffix %s, Android.mk))\n", strings.Join(subdirs, " "))
Andres Moralesda8706f2015-04-29 12:46:49 -0700226}
227
228func (w *androidMkWriter) handleAssignment(assignment *bpparser.Assignment) {
Andres Moralesaf11df12015-04-30 12:14:34 -0700229 if "subdirs" == assignment.Name.Name {
230 w.handleSubdirs(assignment.OrigValue)
231 } else if assignment.OrigValue.Type == bpparser.Map {
232 // maps may be assigned in Soong, but can only be translated to .mk
233 // in the context of the module
234 w.mapScope[assignment.Name.Name] = assignment.OrigValue.MapValue
235 } else {
236 assigner := ":="
237 if assignment.Assigner != "=" {
238 assigner = assignment.Assigner
239 }
240 fmt.Fprintf(w, "%s %s %s\n", assignment.Name.Name, assigner,
241 valueToString(assignment.OrigValue))
Andres Moralesda8706f2015-04-29 12:46:49 -0700242 }
Andres Moralesda8706f2015-04-29 12:46:49 -0700243}
244
245func (w *androidMkWriter) iter() <-chan interface{} {
Andres Moralesaf11df12015-04-30 12:14:34 -0700246 ch := make(chan interface{}, len(w.blueprint.Comments)+len(w.blueprint.Defs))
Andres Moralesda8706f2015-04-29 12:46:49 -0700247 go func() {
248 commIdx := 0
249 defsIdx := 0
Andres Moralesaf11df12015-04-30 12:14:34 -0700250 for defsIdx < len(w.blueprint.Defs) || commIdx < len(w.blueprint.Comments) {
251 if defsIdx == len(w.blueprint.Defs) {
252 ch <- w.blueprint.Comments[commIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700253 commIdx++
Andres Moralesaf11df12015-04-30 12:14:34 -0700254 } else if commIdx == len(w.blueprint.Comments) {
255 ch <- w.blueprint.Defs[defsIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700256 defsIdx++
257 } else {
258 commentsPos := 0
259 defsPos := 0
260
Andres Moralesaf11df12015-04-30 12:14:34 -0700261 def := w.blueprint.Defs[defsIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700262 switch def := def.(type) {
263 case *bpparser.Module:
264 defsPos = def.LbracePos.Line
265 case *bpparser.Assignment:
266 defsPos = def.Pos.Line
267 }
268
Andres Moralesaf11df12015-04-30 12:14:34 -0700269 comment := w.blueprint.Comments[commIdx]
Andres Moralesda8706f2015-04-29 12:46:49 -0700270 commentsPos = comment.Pos.Line
271
272 if commentsPos < defsPos {
273 commIdx++
274 ch <- comment
275 } else {
276 defsIdx++
277 ch <- def
278 }
279 }
280 }
281 close(ch)
282 }()
283 return ch
284}
285
Andres Morales8ae47de2015-05-11 12:26:07 -0700286func (w *androidMkWriter) handleLocalPath() error {
287 androidMkDir, err := filepath.Abs(w.path)
288 if err != nil {
289 return err
290 }
291
292 top, err := getTopOfAndroidTree(androidMkDir)
293 if err != nil {
294 return err
295 }
296
297 rel, err := filepath.Rel(top, androidMkDir)
298 if err != nil {
299 return err
300 }
301
302 w.WriteString("LOCAL_PATH := " + rel + "\n")
303 return nil
304}
305
Andres Moralesda8706f2015-04-29 12:46:49 -0700306func (w *androidMkWriter) write() {
Andres Moralesaf11df12015-04-30 12:14:34 -0700307 outFilePath := fmt.Sprintf("%s/Androidbp.mk", w.path)
Andres Moralesda8706f2015-04-29 12:46:49 -0700308 fmt.Printf("Writing %s\n", outFilePath)
309
310 f, err := os.Create(outFilePath)
311 if err != nil {
312 panic(err)
313 }
314
Andres Moralesaf11df12015-04-30 12:14:34 -0700315 defer f.Close()
Andres Moralesda8706f2015-04-29 12:46:49 -0700316
317 w.Writer = bufio.NewWriter(f)
318
Andres Morales8ae47de2015-05-11 12:26:07 -0700319 if err := w.handleLocalPath(); err != nil {
320 fmt.Println(err.Error())
321 return
322 }
Andres Moralesaf11df12015-04-30 12:14:34 -0700323
Andres Moralesda8706f2015-04-29 12:46:49 -0700324 for block := range w.iter() {
325 switch block := block.(type) {
326 case *bpparser.Module:
327 w.handleModule(block)
328 case *bpparser.Assignment:
329 w.handleAssignment(block)
330 case bpparser.Comment:
331 w.handleComment(&block)
332 }
333 }
334
335 if err = w.Flush(); err != nil {
336 panic(err)
337 }
338}
339
340func main() {
341 if len(os.Args) < 2 {
342 fmt.Println("No filename supplied")
343 return
344 }
345
346 reader, err := os.Open(os.Args[1])
347 if err != nil {
348 fmt.Println(err.Error())
349 return
350 }
351
352 scope := bpparser.NewScope(nil)
Andres Moralesaf11df12015-04-30 12:14:34 -0700353 blueprint, errs := bpparser.Parse(os.Args[1], reader, scope)
Andres Moralesda8706f2015-04-29 12:46:49 -0700354 if len(errs) > 0 {
355 fmt.Println("%d errors parsing %s", len(errs), os.Args[1])
356 fmt.Println(errs)
357 return
358 }
359
360 writer := &androidMkWriter{
Andres Moralesaf11df12015-04-30 12:14:34 -0700361 blueprint: blueprint,
362 path: path.Dir(os.Args[1]),
363 mapScope: make(map[string][]*bpparser.Property),
Andres Moralesda8706f2015-04-29 12:46:49 -0700364 }
365
366 writer.write()
367}