Dan Willemsen | 2701212 | 2015-06-26 17:40:54 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | bpparser "github.com/google/blueprint/parser" |
| 8 | ) |
| 9 | |
| 10 | type Module struct { |
| 11 | bpmod *bpparser.Module |
| 12 | bpname string |
| 13 | mkname string |
| 14 | isHostRule bool |
| 15 | } |
| 16 | |
| 17 | func newModule(mod *bpparser.Module) *Module { |
| 18 | return &Module{ |
| 19 | bpmod: mod.Copy(), |
| 20 | bpname: mod.Type.Name, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func (m *Module) translateRuleName() error { |
| 25 | var name string |
| 26 | if translation, ok := moduleTypeToRule[m.bpname]; ok { |
| 27 | name = translation |
| 28 | } else { |
| 29 | return fmt.Errorf("Unknown module type %q", m.bpname) |
| 30 | } |
| 31 | |
| 32 | if m.isHostRule { |
| 33 | if trans, ok := targetToHostModuleRule[name]; ok { |
| 34 | name = trans |
| 35 | } else { |
| 36 | return fmt.Errorf("No corresponding host rule for %q", name) |
| 37 | } |
| 38 | } else { |
| 39 | m.isHostRule = strings.Contains(name, "HOST") |
| 40 | } |
| 41 | |
| 42 | m.mkname = name |
| 43 | |
| 44 | return nil |
| 45 | } |
| 46 | |
| 47 | func (m *Module) Properties() Properties { |
| 48 | return Properties{&m.bpmod.Properties} |
| 49 | } |
| 50 | |
| 51 | func (m *Module) PropBool(name string) bool { |
| 52 | if prop, ok := m.Properties().Prop(name); ok { |
| 53 | return prop.Value.BoolValue |
| 54 | } |
| 55 | return false |
| 56 | } |
| 57 | |
| 58 | func (m *Module) IterateArchPropertiesWithName(name string, f func(Properties, *bpparser.Property) error) error { |
| 59 | if p, ok := m.Properties().Prop(name); ok { |
| 60 | err := f(m.Properties(), p) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | for _, prop := range m.bpmod.Properties { |
| 67 | switch prop.Name.Name { |
| 68 | case "arch", "multilib", "target": |
| 69 | for _, sub := range prop.Value.MapValue { |
| 70 | props := Properties{&sub.Value.MapValue} |
| 71 | if p, ok := props.Prop(name); ok { |
| 72 | err := f(props, p) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | type Properties struct { |
| 85 | props *[]*bpparser.Property |
| 86 | } |
| 87 | |
| 88 | func (p Properties) Prop(name string) (*bpparser.Property, bool) { |
| 89 | for _, prop := range *p.props { |
| 90 | if name == prop.Name.Name { |
| 91 | return prop, true |
| 92 | } |
| 93 | } |
| 94 | return nil, false |
| 95 | } |
| 96 | |
| 97 | func (p Properties) AppendToProp(name string, src *bpparser.Property) error { |
| 98 | if d, ok := p.Prop(name); ok { |
| 99 | val, err := appendValueToValue(d.Value, src.Value) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | |
| 104 | d.Value = val |
| 105 | } else { |
| 106 | prop := src.Copy() |
| 107 | prop.Name.Name = name |
| 108 | *p.props = append(*p.props, prop) |
| 109 | } |
| 110 | |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | func (p Properties) DeleteProp(name string) { |
| 115 | for i, prop := range *p.props { |
| 116 | if prop.Name.Name == name { |
| 117 | *p.props = append((*p.props)[0:i], (*p.props)[i+1:]...) |
| 118 | return |
| 119 | } |
| 120 | } |
| 121 | } |