Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 5 | "errors" |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 6 | "fmt" |
| 7 | "os" |
| 8 | "path" |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 9 | "path/filepath" |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 10 | "regexp" |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 11 | "strings" |
| 12 | |
| 13 | bpparser "github.com/google/blueprint/parser" |
| 14 | ) |
| 15 | |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 16 | var recursiveSubdirRegex *regexp.Regexp = regexp.MustCompile("(.+)/\\*\\*/(.+)") |
| 17 | |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 18 | type androidMkWriter struct { |
| 19 | *bufio.Writer |
| 20 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 21 | blueprint *bpparser.File |
| 22 | path string |
| 23 | |
| 24 | mapScope map[string][]*bpparser.Property |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 27 | func valueToString(value bpparser.Value) string { |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 28 | 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 Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 35 | return fmt.Sprintf(`"%s"`, processWildcards(value.StringValue)) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 36 | case bpparser.List: |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 37 | return fmt.Sprintf("\\\n%s\n", listToMkString(value.ListValue)) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 38 | case bpparser.Map: |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 39 | return fmt.Sprintf("ERROR can't convert map to string") |
| 40 | default: |
| 41 | return fmt.Sprintf("ERROR: unsupported type %d", value.Type) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 46 | func 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 Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 67 | // TODO: handle non-recursive wildcards? |
| 68 | func processWildcards(s string) string { |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 69 | submatches := recursiveSubdirRegex.FindStringSubmatch(s) |
| 70 | if len(submatches) > 2 { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 71 | // Found a wildcard rule |
| 72 | return fmt.Sprintf("$(call find-files-in-subdirs, $(LOCAL_PATH), %s, %s)", |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 73 | submatches[2], submatches[1]) |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return s |
| 77 | } |
| 78 | |
| 79 | func listToMkString(list []bpparser.Value) string { |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 80 | lines := make([]string, 0, len(list)) |
| 81 | for _, tok := range list { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 82 | 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 Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return strings.Join(lines, " \\\n") |
| 91 | } |
| 92 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 93 | func 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 Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 128 | } |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 129 | |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | func 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 | |
| 149 | func (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 Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | func (w *androidMkWriter) handleComment(comment *bpparser.Comment) { |
| 159 | for _, c := range comment.Comment { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 160 | fmt.Fprintf(w, "#%s\n", c) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 164 | func (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 Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 174 | } |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 175 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 176 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 177 | 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 Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 181 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 182 | func (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 | |
| 220 | func (w *androidMkWriter) handleSubdirs(value bpparser.Value) { |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 221 | subdirs := make([]string, 0, len(value.ListValue)) |
| 222 | for _, tok := range value.ListValue { |
| 223 | subdirs = append(subdirs, tok.StringValue) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 224 | } |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 225 | fmt.Fprintf(w, "include $(wildcard $(addsuffix %s, Android.mk))\n", strings.Join(subdirs, " ")) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | func (w *androidMkWriter) handleAssignment(assignment *bpparser.Assignment) { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 229 | 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 Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 242 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | func (w *androidMkWriter) iter() <-chan interface{} { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 246 | ch := make(chan interface{}, len(w.blueprint.Comments)+len(w.blueprint.Defs)) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 247 | go func() { |
| 248 | commIdx := 0 |
| 249 | defsIdx := 0 |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 250 | 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 Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 253 | commIdx++ |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 254 | } else if commIdx == len(w.blueprint.Comments) { |
| 255 | ch <- w.blueprint.Defs[defsIdx] |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 256 | defsIdx++ |
| 257 | } else { |
| 258 | commentsPos := 0 |
| 259 | defsPos := 0 |
| 260 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 261 | def := w.blueprint.Defs[defsIdx] |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 262 | 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 Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 269 | comment := w.blueprint.Comments[commIdx] |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 270 | 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 Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 286 | func (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 Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 306 | func (w *androidMkWriter) write() { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 307 | outFilePath := fmt.Sprintf("%s/Androidbp.mk", w.path) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 308 | fmt.Printf("Writing %s\n", outFilePath) |
| 309 | |
| 310 | f, err := os.Create(outFilePath) |
| 311 | if err != nil { |
| 312 | panic(err) |
| 313 | } |
| 314 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 315 | defer f.Close() |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 316 | |
| 317 | w.Writer = bufio.NewWriter(f) |
| 318 | |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 319 | if err := w.handleLocalPath(); err != nil { |
| 320 | fmt.Println(err.Error()) |
| 321 | return |
| 322 | } |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 323 | |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 324 | 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 | |
| 340 | func 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 Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 353 | blueprint, errs := bpparser.Parse(os.Args[1], reader, scope) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 354 | 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 Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 361 | blueprint: blueprint, |
| 362 | path: path.Dir(os.Args[1]), |
| 363 | mapScope: make(map[string][]*bpparser.Property), |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | writer.write() |
| 367 | } |