Skip to content

Commit 2f5b767

Browse files
authored
Refactored build-properties creation (and removed some legacy code) (arduino#2082)
* Moved 'build.fqbn' and 'build.arch' properties generation * Moved some runtime build path properties to proper function * Factored tools runtime properties generation * Factored subroutine for time-related properties generation * Made the referenced-core determination a bit more readable Equivalent change, should not change behaviour * Factored 'build.variant' and related properties calculation * Removed useless state variable 'BuildCore' * Refactoring of some legacy properties generation subroutines * Refactored generation of 'software' build property * Refactored build properties overlaying logic * Refactored custom global properties handling * Moved corePlatform and variantPlatform determination inside a specific method
1 parent 78bfa74 commit 2f5b767

23 files changed

+167
-210
lines changed

arduino/cores/board.go

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ func (b *Board) GetBuildProperties(userConfigs *properties.Map) (*properties.Map
125125

126126
// Start with board's base properties
127127
buildProperties := b.Properties.Clone()
128+
buildProperties.Set("build.fqbn", b.FQBN())
129+
buildProperties.Set("build.arch", strings.ToUpper(b.PlatformRelease.Platform.Architecture))
128130

129131
// Add all sub-configurations one by one (a config is: option=value)
130132
// Check for residual invalid options...

arduino/cores/board_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,11 @@ func TestBoardOptions(t *testing.T) {
216216
expConf2560.Set("bootloader.low_fuses", "0xFF")
217217
expConf2560.Set("bootloader.tool", "avrdude")
218218
expConf2560.Set("bootloader.unlock_bits", "0x3F")
219+
expConf2560.Set("build.arch", "AVR")
219220
expConf2560.Set("build.board", "AVR_MEGA2560")
220221
expConf2560.Set("build.core", "arduino")
221222
expConf2560.Set("build.f_cpu", "16000000L")
223+
expConf2560.Set("build.fqbn", "arduino:avr:mega")
222224
expConf2560.Set("build.mcu", "atmega2560")
223225
expConf2560.Set("build.variant", "mega")
224226
expConf2560.Set("menu.cpu.atmega1280", "ATmega1280")
@@ -275,9 +277,11 @@ func TestBoardOptions(t *testing.T) {
275277
expConf1280.Set("bootloader.low_fuses", "0xFF")
276278
expConf1280.Set("bootloader.tool", "avrdude")
277279
expConf1280.Set("bootloader.unlock_bits", "0x3F")
280+
expConf1280.Set("build.arch", "AVR")
278281
expConf1280.Set("build.board", "AVR_MEGA")
279282
expConf1280.Set("build.core", "arduino")
280283
expConf1280.Set("build.f_cpu", "16000000L")
284+
expConf1280.Set("build.fqbn", "arduino:avr:mega")
281285
expConf1280.Set("build.mcu", "atmega1280")
282286
expConf1280.Set("build.variant", "mega")
283287
expConf1280.Set("menu.cpu.atmega1280", "ATmega1280")
@@ -334,9 +338,11 @@ func TestBoardOptions(t *testing.T) {
334338
expWatterott.Set("bootloader.low_fuses", "0xE2")
335339
expWatterott.Set("bootloader.tool", "avrdude")
336340
expWatterott.Set("bootloader.unlock_bits", "0xFF")
341+
expWatterott.Set("build.arch", "AVR")
337342
expWatterott.Set("build.board", "AVR_ATTINY841")
338343
expWatterott.Set("build.core", "tiny841")
339344
expWatterott.Set("build.f_cpu", "8000000L")
345+
expWatterott.Set("build.fqbn", "watterott:avr:attiny841")
340346
expWatterott.Set("build.mcu", "attiny841")
341347
expWatterott.Set("build.variant", "tiny14")
342348
expWatterott.Set("menu.core.arduino", "Standard Arduino")

arduino/cores/cores.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ func (release *PlatformRelease) RequiresToolRelease(toolRelease *ToolRelease) bo
336336
func (release *PlatformRelease) RuntimeProperties() *properties.Map {
337337
res := properties.NewMap()
338338
if release.InstallDir != nil {
339-
res.Set("runtime.platform.path", release.InstallDir.String())
340-
res.Set("runtime.hardware.path", release.InstallDir.Join("..").String())
339+
res.SetPath("runtime.platform.path", release.InstallDir)
340+
res.SetPath("runtime.hardware.path", release.InstallDir.Join(".."))
341341
}
342342

343343
return res

arduino/cores/packagemanager/package_manager.go

+121-25
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ package packagemanager
1818
import (
1919
"fmt"
2020
"net/url"
21+
"os"
2122
"path"
23+
"path/filepath"
24+
"strconv"
2225
"strings"
2326
"sync"
27+
"time"
2428

2529
"github.com/arduino/arduino-cli/arduino/cores"
2630
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
@@ -29,6 +33,7 @@ import (
2933
"github.com/arduino/arduino-cli/i18n"
3034
paths "github.com/arduino/go-paths-helper"
3135
properties "github.com/arduino/go-properties-orderedmap"
36+
"github.com/arduino/go-timeutils"
3237
"github.com/sirupsen/logrus"
3338
semver "go.bug.st/relaxed-semver"
3439
)
@@ -275,50 +280,141 @@ func (pme *Explorer) ResolveFQBN(fqbn *cores.FQBN) (
275280
return targetPackage, nil, nil, nil, nil,
276281
fmt.Errorf(tr("unknown platform %s:%s"), targetPackage, fqbn.PlatformArch)
277282
}
278-
platformRelease := pme.GetInstalledPlatformRelease(platform)
279-
if platformRelease == nil {
283+
boardPlatformRelease := pme.GetInstalledPlatformRelease(platform)
284+
if boardPlatformRelease == nil {
280285
return targetPackage, nil, nil, nil, nil,
281286
fmt.Errorf(tr("platform %s is not installed"), platform)
282287
}
283288

284289
// Find board
285-
board := platformRelease.Boards[fqbn.BoardID]
290+
board := boardPlatformRelease.Boards[fqbn.BoardID]
286291
if board == nil {
287-
return targetPackage, platformRelease, nil, nil, nil,
292+
return targetPackage, boardPlatformRelease, nil, nil, nil,
288293
fmt.Errorf(tr("board %s not found"), fqbn.StringWithoutConfig())
289294
}
290295

291-
buildProperties, err := board.GetBuildProperties(fqbn.Configs)
296+
boardBuildProperties, err := board.GetBuildProperties(fqbn.Configs)
292297
if err != nil {
293-
return targetPackage, platformRelease, board, nil, nil,
298+
return targetPackage, boardPlatformRelease, board, nil, nil,
294299
fmt.Errorf(tr("getting build properties for board %[1]s: %[2]s"), board, err)
295300
}
296301

297-
// Determine the platform used for the build (in case the board refers
302+
// Determine the platform used for the build and the variant (in case the board refers
298303
// to a core contained in another platform)
299-
buildPlatformRelease := platformRelease
300-
coreParts := strings.Split(buildProperties.Get("build.core"), ":")
301-
if len(coreParts) > 1 {
302-
referredPackage := coreParts[0]
303-
buildPackage := pme.packages[referredPackage]
304-
if buildPackage == nil {
305-
return targetPackage, platformRelease, board, buildProperties, nil,
306-
fmt.Errorf(tr("missing package %[1]s referenced by board %[2]s"), referredPackage, fqbn)
304+
core, corePlatformRelease, variant, variantPlatformRelease, err := pme.determineReferencedPlatformRelease(boardBuildProperties, boardPlatformRelease, fqbn)
305+
if err != nil {
306+
return targetPackage, boardPlatformRelease, board, nil, nil, err
307+
}
308+
309+
// Create the build properties map by overlaying the properties of the
310+
// referenced platform propeties, the board platform properties and the
311+
// board specific properties.
312+
buildProperties := properties.NewMap()
313+
buildProperties.Merge(variantPlatformRelease.Properties)
314+
buildProperties.Merge(corePlatformRelease.Properties)
315+
buildProperties.Merge(boardPlatformRelease.Properties)
316+
buildProperties.Merge(boardBuildProperties)
317+
318+
// Add runtime build properties
319+
buildProperties.Merge(boardPlatformRelease.RuntimeProperties())
320+
buildProperties.SetPath("build.core.path", corePlatformRelease.InstallDir.Join("cores", core))
321+
buildProperties.SetPath("build.system.path", corePlatformRelease.InstallDir.Join("system"))
322+
buildProperties.Set("build.variant.path", "")
323+
if variant != "" {
324+
buildProperties.SetPath("build.variant.path", variantPlatformRelease.InstallDir.Join("variants", variant))
325+
}
326+
327+
for _, tool := range pme.GetAllInstalledToolsReleases() {
328+
buildProperties.Merge(tool.RuntimeProperties())
329+
}
330+
requiredTools, err := pme.FindToolsRequiredForBuild(boardPlatformRelease, corePlatformRelease)
331+
if err != nil {
332+
return targetPackage, boardPlatformRelease, board, buildProperties, corePlatformRelease, err
333+
}
334+
for _, tool := range requiredTools {
335+
buildProperties.Merge(tool.RuntimeProperties())
336+
}
337+
now := time.Now()
338+
buildProperties.Set("extra.time.utc", strconv.FormatInt(now.Unix(), 10))
339+
buildProperties.Set("extra.time.local", strconv.FormatInt(timeutils.LocalUnix(now), 10))
340+
buildProperties.Set("extra.time.zone", strconv.Itoa(timeutils.TimezoneOffsetNoDST(now)))
341+
buildProperties.Set("extra.time.dst", strconv.Itoa(timeutils.DaylightSavingsOffset(now)))
342+
343+
if !buildProperties.ContainsKey("runtime.ide.path") {
344+
if ex, err := os.Executable(); err == nil {
345+
buildProperties.Set("runtime.ide.path", filepath.Dir(ex))
346+
} else {
347+
buildProperties.Set("runtime.ide.path", "")
307348
}
308-
buildPlatform := buildPackage.Platforms[fqbn.PlatformArch]
309-
if buildPlatform == nil {
310-
return targetPackage, platformRelease, board, buildProperties, nil,
311-
fmt.Errorf(tr("missing platform %[1]s:%[2]s referenced by board %[3]s"), referredPackage, fqbn.PlatformArch, fqbn)
349+
}
350+
buildProperties.Set("runtime.os", properties.GetOSSuffix())
351+
buildProperties.Set("build.library_discovery_phase", "0")
352+
// Deprecated properties
353+
buildProperties.Set("ide_version", "10607")
354+
buildProperties.Set("runtime.ide.version", "10607")
355+
if !buildProperties.ContainsKey("software") {
356+
buildProperties.Set("software", "ARDUINO")
357+
}
358+
359+
buildProperties.Merge(pme.GetCustomGlobalProperties())
360+
361+
return targetPackage, boardPlatformRelease, board, buildProperties, corePlatformRelease, nil
362+
}
363+
364+
func (pme *Explorer) determineReferencedPlatformRelease(boardBuildProperties *properties.Map, boardPlatformRelease *cores.PlatformRelease, fqbn *cores.FQBN) (string, *cores.PlatformRelease, string, *cores.PlatformRelease, error) {
365+
core := boardBuildProperties.Get("build.core")
366+
referredCore := ""
367+
if split := strings.Split(core, ":"); len(split) > 1 {
368+
referredCore, core = split[0], split[1]
369+
}
370+
371+
variant := boardBuildProperties.Get("build.variant")
372+
referredVariant := ""
373+
if split := strings.Split(variant, ":"); len(split) > 1 {
374+
referredVariant, variant = split[0], split[1]
375+
}
376+
377+
// core and variant cannot refer to two different platforms
378+
if referredCore != "" && referredVariant != "" && referredCore != referredVariant {
379+
return "", nil, "", nil,
380+
fmt.Errorf(tr("'build.core' and 'build.variant' refer to different platforms: %[1]s and %[2]s"), referredCore+":"+core, referredVariant+":"+variant)
381+
}
382+
383+
// extract the referred platform
384+
var referredPlatformRelease *cores.PlatformRelease
385+
referredPackageName := referredCore
386+
if referredPackageName == "" {
387+
referredPackageName = referredVariant
388+
}
389+
if referredPackageName != "" {
390+
referredPackage := pme.packages[referredPackageName]
391+
if referredPackage == nil {
392+
return "", nil, "", nil,
393+
fmt.Errorf(tr("missing package %[1]s referenced by board %[2]s"), referredPackageName, fqbn)
394+
}
395+
referredPlatform := referredPackage.Platforms[fqbn.PlatformArch]
396+
if referredPlatform == nil {
397+
return "", nil, "", nil,
398+
fmt.Errorf(tr("missing platform %[1]s:%[2]s referenced by board %[3]s"), referredPackageName, fqbn.PlatformArch, fqbn)
312399
}
313-
buildPlatformRelease = pme.GetInstalledPlatformRelease(buildPlatform)
314-
if buildPlatformRelease == nil {
315-
return targetPackage, platformRelease, board, buildProperties, nil,
316-
fmt.Errorf(tr("missing platform release %[1]s:%[2]s referenced by board %[3]s"), referredPackage, fqbn.PlatformArch, fqbn)
400+
referredPlatformRelease = pme.GetInstalledPlatformRelease(referredPlatform)
401+
if referredPlatformRelease == nil {
402+
return "", nil, "", nil,
403+
fmt.Errorf(tr("missing platform release %[1]s:%[2]s referenced by board %[3]s"), referredPackageName, fqbn.PlatformArch, fqbn)
317404
}
318405
}
319406

320-
// No errors... phew!
321-
return targetPackage, platformRelease, board, buildProperties, buildPlatformRelease, nil
407+
corePlatformRelease := boardPlatformRelease
408+
if referredCore != "" {
409+
corePlatformRelease = referredPlatformRelease
410+
}
411+
412+
variantPlatformRelease := boardPlatformRelease
413+
if referredVariant != "" {
414+
variantPlatformRelease = referredPlatformRelease
415+
}
416+
417+
return core, corePlatformRelease, variant, variantPlatformRelease, nil
322418
}
323419

324420
// LoadPackageIndex loads a package index by looking up the local cached file from the specified URL

arduino/cores/packagemanager/package_manager_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestResolveFQBN(t *testing.T) {
141141
require.Equal(t, platformRelease.Platform.String(), "referenced:avr")
142142
require.NotNil(t, board)
143143
require.Equal(t, board.Name(), "Referenced dummy with invalid package")
144-
require.NotNil(t, props)
144+
require.Nil(t, props)
145145
require.Nil(t, buildPlatformRelease)
146146

147147
// Test a board referenced from a non-existent platform/architecture
@@ -156,7 +156,7 @@ func TestResolveFQBN(t *testing.T) {
156156
require.Equal(t, platformRelease.Platform.String(), "referenced:avr")
157157
require.NotNil(t, board)
158158
require.Equal(t, board.Name(), "Referenced dummy with invalid platform")
159-
require.NotNil(t, props)
159+
require.Nil(t, props)
160160
require.Nil(t, buildPlatformRelease)
161161

162162
// Test a board referenced from a non-existent core

commands/compile/compile.go

-3
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
181181

182182
builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings)
183183

184-
// Will be deprecated.
185-
builderCtx.ArduinoAPIVersion = "10607"
186-
187184
builderCtx.Stdout = outStream
188185
builderCtx.Stderr = errStream
189186
builderCtx.Clean = req.GetClean()

commands/upload/upload.go

-14
Original file line numberDiff line numberDiff line change
@@ -292,20 +292,6 @@ func runProgramAction(pme *packagemanager.Explorer,
292292
uploadProperties.Merge(programmer.Properties)
293293
}
294294

295-
for _, tool := range pme.GetAllInstalledToolsReleases() {
296-
uploadProperties.Merge(tool.RuntimeProperties())
297-
}
298-
if requiredTools, err := pme.FindToolsRequiredForBuild(boardPlatform, buildPlatform); err == nil {
299-
for _, requiredTool := range requiredTools {
300-
logrus.WithField("tool", requiredTool).Info("Tool required for upload")
301-
if requiredTool.IsInstalled() {
302-
uploadProperties.Merge(requiredTool.RuntimeProperties())
303-
} else {
304-
errStream.Write([]byte(tr("Warning: tool '%s' is not installed. It might not be available for your OS.", requiredTool)))
305-
}
306-
}
307-
}
308-
309295
// Certain tools require the user to provide custom fields at run time,
310296
// if they've been provided set them
311297
// For more info:

legacy/builder/builder.go

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ var tr = i18n.Tr
3131

3232
const DEFAULT_DEBUG_LEVEL = 5
3333
const DEFAULT_WARNINGS_LEVEL = "none"
34-
const DEFAULT_SOFTWARE = "ARDUINO"
3534

3635
type Builder struct{}
3736

0 commit comments

Comments
 (0)