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 | |
Dan Willemsen | 360a39c | 2015-06-11 14:34:50 -0700 | [diff] [blame] | 24 | printedLocalPath bool |
| 25 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 26 | mapScope map[string][]*bpparser.Property |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 29 | func valueToString(value bpparser.Value) string { |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 30 | if value.Variable != "" { |
| 31 | return fmt.Sprintf("$(%s)", value.Variable) |
Colin Cross | ff3b795 | 2015-06-22 15:39:35 -0700 | [diff] [blame] | 32 | } 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 Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 39 | } else { |
| 40 | switch value.Type { |
| 41 | case bpparser.Bool: |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 42 | return fmt.Sprintf("%t", value.BoolValue) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 43 | case bpparser.String: |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 44 | return fmt.Sprintf("%s", processWildcards(value.StringValue)) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 45 | case bpparser.List: |
Colin Cross | ff3b795 | 2015-06-22 15:39:35 -0700 | [diff] [blame] | 46 | return fmt.Sprintf("\\\n%s", listToMkString(value.ListValue)) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 47 | case bpparser.Map: |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 48 | return fmt.Sprintf("ERROR can't convert map to string") |
| 49 | default: |
| 50 | return fmt.Sprintf("ERROR: unsupported type %d", value.Type) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 55 | func 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 Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 76 | // TODO: handle non-recursive wildcards? |
| 77 | func processWildcards(s string) string { |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 78 | submatches := recursiveSubdirRegex.FindStringSubmatch(s) |
| 79 | if len(submatches) > 2 { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 80 | // Found a wildcard rule |
| 81 | return fmt.Sprintf("$(call find-files-in-subdirs, $(LOCAL_PATH), %s, %s)", |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 82 | submatches[2], submatches[1]) |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return s |
| 86 | } |
| 87 | |
| 88 | func listToMkString(list []bpparser.Value) string { |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 89 | lines := make([]string, 0, len(list)) |
| 90 | for _, tok := range list { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 91 | if tok.Type == bpparser.String { |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 92 | lines = append(lines, fmt.Sprintf(" %s", processWildcards(tok.StringValue))) |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 93 | } else { |
| 94 | lines = append(lines, fmt.Sprintf("# ERROR: unsupported type %s in list", |
| 95 | tok.Type.String())) |
| 96 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return strings.Join(lines, " \\\n") |
| 100 | } |
| 101 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 102 | func translateTargetConditionals(props []*bpparser.Property, |
| 103 | disabledBuilds map[string]bool, isHostRule bool) (computedProps []string) { |
| 104 | for _, target := range props { |
| 105 | conditionals := targetScopedPropertyConditionals |
Dan Willemsen | 68fdfcc | 2015-06-11 14:05:01 -0700 | [diff] [blame] | 106 | altConditionals := hostScopedPropertyConditionals |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 107 | if isHostRule { |
Dan Willemsen | 68fdfcc | 2015-06-11 14:05:01 -0700 | [diff] [blame] | 108 | conditionals, altConditionals = altConditionals, conditionals |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | conditional, ok := conditionals[target.Name.Name] |
| 112 | if !ok { |
Dan Willemsen | 68fdfcc | 2015-06-11 14:05:01 -0700 | [diff] [blame] | 113 | 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 Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 122 | } |
| 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 Willemsen | 57ad08c | 2015-06-10 16:20:14 -0700 | [diff] [blame] | 129 | } else if rwProp, ok := rewriteProperties[targetScopedProp.Name.Name]; ok { |
| 130 | scopedProps = append(scopedProps, rwProp.f(rwProp.string, targetScopedProp, nil)...) |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 131 | } 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 Willemsen | 68fdfcc | 2015-06-11 14:05:01 -0700 | [diff] [blame] | 141 | 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 Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 148 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 149 | } |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 150 | |
| 151 | return |
| 152 | } |
| 153 | |
| 154 | func 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 Willemsen | 57ad08c | 2015-06-10 16:20:14 -0700 | [diff] [blame] | 161 | } else if rwProp, ok := rewriteProperties[stdProp.Name.Name]; ok { |
| 162 | computedProps = append(computedProps, rwProp.f(rwProp.string, stdProp, &suffix)...) |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 163 | } else { |
| 164 | computedProps = append(computedProps, fmt.Sprintf("# ERROR: unsupported property %s", stdProp.Name.Name)) |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | return |
| 170 | } |
| 171 | |
Dan Willemsen | 57ad08c | 2015-06-10 16:20:14 -0700 | [diff] [blame] | 172 | func prependLocalPath(name string, prop *bpparser.Property, suffix *string) (computedProps []string) { |
Dan Willemsen | 57ad08c | 2015-06-10 16:20:14 -0700 | [diff] [blame] | 173 | if suffix != nil { |
| 174 | name += "_" + *suffix |
| 175 | } |
Colin Cross | ff3b795 | 2015-06-22 15:39:35 -0700 | [diff] [blame] | 176 | return []string{ |
| 177 | fmt.Sprintf("%s := $(addprefix $(LOCAL_PATH)/,%s)\n", name, valueToString(prop.Value)), |
| 178 | } |
Dan Willemsen | 57ad08c | 2015-06-10 16:20:14 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Dan Willemsen | 1d9f279 | 2015-06-22 15:40:14 -0700 | [diff] [blame] | 181 | func prependLocalModule(name string, prop *bpparser.Property, suffix *string) (computedProps []string) { |
| 182 | if suffix != nil { |
| 183 | name += "_" + *suffix |
| 184 | } |
| 185 | return []string { |
Dan Willemsen | d7b11dd | 2015-06-22 16:25:39 -0700 | [diff] [blame] | 186 | fmt.Sprintf("%s := $(LOCAL_MODULE)%s\n", name, valueToString(prop.Value)), |
Dan Willemsen | 1d9f279 | 2015-06-22 15:40:14 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 190 | func (w *androidMkWriter) lookupMap(parent bpparser.Value) (mapValue []*bpparser.Property) { |
| 191 | if parent.Variable != "" { |
| 192 | mapValue = w.mapScope[parent.Variable] |
| 193 | } else { |
| 194 | mapValue = parent.MapValue |
| 195 | } |
| 196 | return |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | func (w *androidMkWriter) handleComment(comment *bpparser.Comment) { |
| 200 | for _, c := range comment.Comment { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 201 | fmt.Fprintf(w, "#%s\n", c) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 205 | func (w *androidMkWriter) writeModule(moduleRule string, props []string, |
| 206 | disabledBuilds map[string]bool, isHostRule bool) { |
| 207 | disabledConditionals := disabledTargetConditionals |
| 208 | if isHostRule { |
| 209 | disabledConditionals = disabledHostConditionals |
| 210 | } |
| 211 | for build, _ := range disabledBuilds { |
| 212 | if conditional, ok := disabledConditionals[build]; ok { |
| 213 | fmt.Fprintf(w, "%s\n", conditional) |
| 214 | defer fmt.Fprintf(w, "endif\n") |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 215 | } |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 216 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 217 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 218 | fmt.Fprintf(w, "include $(CLEAR_VARS)\n") |
| 219 | fmt.Fprintf(w, "%s\n", strings.Join(props, "\n")) |
| 220 | fmt.Fprintf(w, "include $(%s)\n\n", moduleRule) |
| 221 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 222 | |
Dan Willemsen | 68fdfcc | 2015-06-11 14:05:01 -0700 | [diff] [blame] | 223 | func (w *androidMkWriter) parsePropsAndWriteModule(moduleRule string, isHostRule bool, module *bpparser.Module) (hostSupported bool) { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 224 | standardProps := make([]string, 0, len(module.Properties)) |
| 225 | disabledBuilds := make(map[string]bool) |
| 226 | for _, prop := range module.Properties { |
| 227 | if mkProp, ok := standardProperties[prop.Name.Name]; ok { |
| 228 | standardProps = append(standardProps, fmt.Sprintf("%s := %s", mkProp.string, valueToString(prop.Value))) |
Dan Willemsen | 57ad08c | 2015-06-10 16:20:14 -0700 | [diff] [blame] | 229 | } else if rwProp, ok := rewriteProperties[prop.Name.Name]; ok { |
| 230 | standardProps = append(standardProps, rwProp.f(rwProp.string, prop, nil)...) |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 231 | } else if suffixMap, ok := suffixProperties[prop.Name.Name]; ok { |
| 232 | suffixProps := w.lookupMap(prop.Value) |
| 233 | standardProps = append(standardProps, translateSuffixProperties(suffixProps, suffixMap)...) |
| 234 | } else if "target" == prop.Name.Name { |
| 235 | props := w.lookupMap(prop.Value) |
| 236 | standardProps = append(standardProps, translateTargetConditionals(props, disabledBuilds, isHostRule)...) |
| 237 | } else if "host_supported" == prop.Name.Name { |
| 238 | hostSupported = prop.Value.BoolValue |
| 239 | } else { |
| 240 | standardProps = append(standardProps, fmt.Sprintf("# ERROR: Unsupported property %s", prop.Name.Name)) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // write out target build |
| 245 | w.writeModule(moduleRule, standardProps, disabledBuilds, isHostRule) |
Dan Willemsen | 68fdfcc | 2015-06-11 14:05:01 -0700 | [diff] [blame] | 246 | return |
| 247 | } |
| 248 | |
| 249 | func (w *androidMkWriter) handleModule(module *bpparser.Module) { |
| 250 | moduleRule := fmt.Sprintf(module.Type.Name) |
| 251 | if translation, ok := moduleTypeToRule[module.Type.Name]; ok { |
| 252 | moduleRule = translation |
| 253 | } |
| 254 | |
| 255 | isHostRule := strings.Contains(moduleRule, "HOST") |
| 256 | hostSupported := w.parsePropsAndWriteModule(moduleRule, isHostRule, module) |
| 257 | |
| 258 | if !isHostRule && hostSupported { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 259 | hostModuleRule := "NO CORRESPONDING HOST RULE" + moduleRule |
| 260 | if trans, ok := targetToHostModuleRule[moduleRule]; ok { |
| 261 | hostModuleRule = trans |
| 262 | } |
Dan Willemsen | 68fdfcc | 2015-06-11 14:05:01 -0700 | [diff] [blame] | 263 | |
| 264 | w.parsePropsAndWriteModule(hostModuleRule, true, module) |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | func (w *androidMkWriter) handleSubdirs(value bpparser.Value) { |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 269 | subdirs := make([]string, 0, len(value.ListValue)) |
| 270 | for _, tok := range value.ListValue { |
| 271 | subdirs = append(subdirs, tok.StringValue) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 272 | } |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 273 | // The current makefile may be generated to outside the source tree (such as the out directory), with a different structure. |
| 274 | fmt.Fprintf(w, "# Uncomment the following line if you really want to include subdir Android.mks.\n") |
| 275 | fmt.Fprintf(w, "# include $(wildcard $(addsuffix $(LOCAL_PATH)/%s/, Android.mk))\n", strings.Join(subdirs, " ")) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | func (w *androidMkWriter) handleAssignment(assignment *bpparser.Assignment) { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 279 | if "subdirs" == assignment.Name.Name { |
| 280 | w.handleSubdirs(assignment.OrigValue) |
| 281 | } else if assignment.OrigValue.Type == bpparser.Map { |
| 282 | // maps may be assigned in Soong, but can only be translated to .mk |
| 283 | // in the context of the module |
| 284 | w.mapScope[assignment.Name.Name] = assignment.OrigValue.MapValue |
| 285 | } else { |
| 286 | assigner := ":=" |
| 287 | if assignment.Assigner != "=" { |
| 288 | assigner = assignment.Assigner |
| 289 | } |
| 290 | fmt.Fprintf(w, "%s %s %s\n", assignment.Name.Name, assigner, |
| 291 | valueToString(assignment.OrigValue)) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 292 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | func (w *androidMkWriter) iter() <-chan interface{} { |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 296 | ch := make(chan interface{}, len(w.blueprint.Comments)+len(w.blueprint.Defs)) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 297 | go func() { |
| 298 | commIdx := 0 |
| 299 | defsIdx := 0 |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 300 | for defsIdx < len(w.blueprint.Defs) || commIdx < len(w.blueprint.Comments) { |
| 301 | if defsIdx == len(w.blueprint.Defs) { |
| 302 | ch <- w.blueprint.Comments[commIdx] |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 303 | commIdx++ |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 304 | } else if commIdx == len(w.blueprint.Comments) { |
| 305 | ch <- w.blueprint.Defs[defsIdx] |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 306 | defsIdx++ |
| 307 | } else { |
| 308 | commentsPos := 0 |
| 309 | defsPos := 0 |
| 310 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 311 | def := w.blueprint.Defs[defsIdx] |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 312 | switch def := def.(type) { |
| 313 | case *bpparser.Module: |
| 314 | defsPos = def.LbracePos.Line |
| 315 | case *bpparser.Assignment: |
| 316 | defsPos = def.Pos.Line |
| 317 | } |
| 318 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 319 | comment := w.blueprint.Comments[commIdx] |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 320 | commentsPos = comment.Pos.Line |
| 321 | |
| 322 | if commentsPos < defsPos { |
| 323 | commIdx++ |
| 324 | ch <- comment |
| 325 | } else { |
| 326 | defsIdx++ |
| 327 | ch <- def |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | close(ch) |
| 332 | }() |
| 333 | return ch |
| 334 | } |
| 335 | |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 336 | func (w *androidMkWriter) handleLocalPath() error { |
Dan Willemsen | 360a39c | 2015-06-11 14:34:50 -0700 | [diff] [blame] | 337 | if w.printedLocalPath { |
| 338 | return nil |
| 339 | } |
| 340 | w.printedLocalPath = true |
| 341 | |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 342 | localPath, err := filepath.Abs(w.path) |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 343 | if err != nil { |
| 344 | return err |
| 345 | } |
| 346 | |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 347 | top, err := getTopOfAndroidTree(localPath) |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 348 | if err != nil { |
| 349 | return err |
| 350 | } |
| 351 | |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 352 | rel, err := filepath.Rel(top, localPath) |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 353 | if err != nil { |
| 354 | return err |
| 355 | } |
| 356 | |
| 357 | w.WriteString("LOCAL_PATH := " + rel + "\n") |
Dan Willemsen | c2666e6 | 2015-06-10 16:18:58 -0700 | [diff] [blame] | 358 | w.WriteString("LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))\n\n") |
Andres Morales | 8ae47de | 2015-05-11 12:26:07 -0700 | [diff] [blame] | 359 | return nil |
| 360 | } |
| 361 | |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 362 | func (w *androidMkWriter) write(androidMk string) error { |
| 363 | fmt.Printf("Writing %s\n", androidMk) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 364 | |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 365 | f, err := os.Create(androidMk) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 366 | if err != nil { |
| 367 | panic(err) |
| 368 | } |
| 369 | |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 370 | defer f.Close() |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 371 | |
| 372 | w.Writer = bufio.NewWriter(f) |
| 373 | |
| 374 | for block := range w.iter() { |
| 375 | switch block := block.(type) { |
| 376 | case *bpparser.Module: |
Dan Willemsen | 360a39c | 2015-06-11 14:34:50 -0700 | [diff] [blame] | 377 | if err := w.handleLocalPath(); err != nil { |
| 378 | return err |
| 379 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 380 | w.handleModule(block) |
| 381 | case *bpparser.Assignment: |
Dan Willemsen | 360a39c | 2015-06-11 14:34:50 -0700 | [diff] [blame] | 382 | if err := w.handleLocalPath(); err != nil { |
| 383 | return err |
| 384 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 385 | w.handleAssignment(block) |
| 386 | case bpparser.Comment: |
| 387 | w.handleComment(&block) |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if err = w.Flush(); err != nil { |
| 392 | panic(err) |
| 393 | } |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 394 | return nil |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | func main() { |
| 398 | if len(os.Args) < 2 { |
| 399 | fmt.Println("No filename supplied") |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 400 | os.Exit(1) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 403 | androidBp := os.Args[1] |
| 404 | var androidMk string |
| 405 | if len(os.Args) >= 3 { |
| 406 | androidMk = os.Args[2] |
| 407 | } else { |
| 408 | androidMk = androidBp + ".mk" |
| 409 | } |
| 410 | |
| 411 | reader, err := os.Open(androidBp) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 412 | if err != nil { |
| 413 | fmt.Println(err.Error()) |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 414 | os.Exit(1) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | scope := bpparser.NewScope(nil) |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 418 | blueprint, errs := bpparser.Parse(androidBp, reader, scope) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 419 | if len(errs) > 0 { |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 420 | fmt.Println("%d errors parsing %s", len(errs), androidBp) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 421 | fmt.Println(errs) |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 422 | os.Exit(1) |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | writer := &androidMkWriter{ |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 426 | blueprint: blueprint, |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 427 | path: path.Dir(androidBp), |
Andres Morales | af11df1 | 2015-04-30 12:14:34 -0700 | [diff] [blame] | 428 | mapScope: make(map[string][]*bpparser.Property), |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Ying Wang | 3828490 | 2015-06-02 18:44:59 -0700 | [diff] [blame] | 431 | err = writer.write(androidMk) |
| 432 | if err != nil { |
| 433 | fmt.Println(err.Error()) |
| 434 | os.Exit(1) |
| 435 | } |
Andres Morales | da8706f | 2015-04-29 12:46:49 -0700 | [diff] [blame] | 436 | } |