Skip to content

Commit db2a3d1

Browse files
committed
Board's build options properties are now calculated only once and cached
1 parent c48573a commit db2a3d1

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

arduino/cores/board.go

+33-35
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ import (
2424

2525
// Board represents a board loaded from an installed platform
2626
type Board struct {
27-
BoardID string
28-
Properties *properties.Map `json:"-"`
29-
PlatformRelease *PlatformRelease `json:"-"`
30-
configOptions *properties.Map
31-
configOptionValues map[string]*properties.Map
32-
identificationProperties []*properties.Map
27+
BoardID string
28+
Properties *properties.Map `json:"-"`
29+
PlatformRelease *PlatformRelease `json:"-"`
30+
configOptions *properties.Map
31+
configOptionValues map[string]*properties.Map
32+
configOptionProperties map[string]*properties.Map
33+
defaultConfig *properties.Map
34+
identificationProperties []*properties.Map
3335
}
3436

3537
// HasUsbID returns true if the board match the usb vid and pid parameters
@@ -78,11 +80,16 @@ func (b *Board) buildConfigOptionsStructures() {
7880
}
7981

8082
b.configOptionValues = map[string]*properties.Map{}
81-
for configName, options := range allConfigs.FirstLevelOf() {
82-
b.configOptionValues[configName] = properties.NewMap()
83-
for _, value := range options.FirstLevelKeys() {
84-
if label, ok := options.GetOk(value); ok {
85-
b.configOptionValues[configName].Set(value, label)
83+
b.configOptionProperties = map[string]*properties.Map{}
84+
b.defaultConfig = properties.NewMap()
85+
for option, optionProps := range allConfigs.FirstLevelOf() {
86+
b.configOptionValues[option] = properties.NewMap()
87+
values := optionProps.FirstLevelKeys()
88+
b.defaultConfig.Set(option, values[0])
89+
for _, value := range values {
90+
if label, ok := optionProps.GetOk(value); ok {
91+
b.configOptionValues[option].Set(value, label)
92+
b.configOptionProperties[option+"="+value] = optionProps.SubTree(value)
8693
}
8794
}
8895
}
@@ -106,38 +113,29 @@ func (b *Board) GetConfigOptionValues(option string) *properties.Map {
106113
// GetBuildProperties returns the build properties and the build
107114
// platform for the Board with the configuration passed as parameter.
108115
func (b *Board) GetBuildProperties(userConfigs *properties.Map) (*properties.Map, error) {
109-
// Clone user configs because they are destroyed during iteration
110-
userConfigs = userConfigs.Clone()
116+
b.buildConfigOptionsStructures()
117+
118+
// Override default configs with user configs
119+
config := b.defaultConfig.Clone()
120+
config.Merge(userConfigs)
111121

112122
// Start with board's base properties
113123
buildProperties := b.Properties.Clone()
114124

115125
// Add all sub-configurations one by one (a config is: option=value)
116-
menu := b.Properties.SubTree("menu")
117-
for _, option := range menu.FirstLevelKeys() {
118-
optionMenu := menu.SubTree(option)
119-
userValue, haveUserValue := userConfigs.GetOk(option)
120-
if haveUserValue {
121-
userConfigs.Remove(option)
122-
if !optionMenu.ContainsKey(userValue) {
123-
return nil, fmt.Errorf(tr("invalid value '%[1]s' for option '%[2]s'"), userValue, option)
124-
}
125-
} else {
126-
// apply default
127-
userValue = optionMenu.FirstLevelKeys()[0]
128-
}
129-
130-
optionsConf := optionMenu.SubTree(userValue)
131-
buildProperties.Merge(optionsConf)
132-
}
133-
134126
// Check for residual invalid options...
135-
if invalidKeys := userConfigs.Keys(); len(invalidKeys) > 0 {
136-
invalidOption := invalidKeys[0]
137-
if invalidOption == "" {
127+
for option, value := range config.AsMap() {
128+
if option == "" {
138129
return nil, fmt.Errorf(tr("invalid empty option found"))
139130
}
140-
return nil, fmt.Errorf(tr("invalid option '%s'"), invalidOption)
131+
if _, ok := b.configOptions.GetOk(option); !ok {
132+
return nil, fmt.Errorf(tr("invalid option '%s'"), option)
133+
}
134+
optionsConf, ok := b.configOptionProperties[option+"="+value]
135+
if !ok {
136+
return nil, fmt.Errorf(tr("invalid value '%[1]s' for option '%[2]s'"), value, option)
137+
}
138+
buildProperties.Merge(optionsConf)
141139
}
142140

143141
return buildProperties, nil

arduino/cores/board_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var boardUno = &Board{
5959
Name: "arduino",
6060
},
6161
},
62+
Menus: properties.NewMap(),
6263
},
6364
}
6465

@@ -114,6 +115,9 @@ var boardMega = &Board{
114115
Name: "arduino",
115116
},
116117
},
118+
Menus: properties.NewFromHashmap(map[string]string{
119+
"cpu": "Processor",
120+
}),
117121
},
118122
}
119123

@@ -154,6 +158,10 @@ var boardWatterottTiny841 = &Board{
154158
Name: "watterott",
155159
},
156160
},
161+
Menus: properties.NewFromHashmap(map[string]string{
162+
"core": "Core",
163+
"info": "Info",
164+
}),
157165
},
158166
}
159167

0 commit comments

Comments
 (0)