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