Skip to content

Commit 81d2e00

Browse files
committed
Using FQBN facilities from arduino-cli
1 parent fc41149 commit 81d2e00

21 files changed

+126
-152
lines changed

arduino-builder/main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
"github.com/arduino/arduino-builder/types"
5252
"github.com/arduino/go-paths-helper"
5353
"github.com/arduino/go-properties-map"
54+
"github.com/bcmi-labs/arduino-cli/arduino/cores"
5455
"github.com/go-errors/errors"
5556
)
5657

@@ -296,12 +297,16 @@ func main() {
296297
}
297298

298299
// FLAG_FQBN
299-
if fqbn, err := gohasissues.Unquote(*fqbnFlag); err != nil {
300+
if fqbnIn, err := gohasissues.Unquote(*fqbnFlag); err != nil {
300301
printCompleteError(err)
301-
} else if fqbn != "" {
302-
ctx.FQBN = fqbn
302+
} else if fqbnIn != "" {
303+
if fqbn, err := cores.ParseFQBN(fqbnIn); err != nil {
304+
printCompleteError(err)
305+
} else {
306+
ctx.FQBN = fqbn
307+
}
303308
}
304-
if ctx.FQBN == "" {
309+
if ctx.FQBN == nil {
305310
printErrorMessageAndFlagUsage(errors.New("Parameter '" + FLAG_FQBN + "' is mandatory"))
306311
}
307312

create_build_options_map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
package builder
3131

3232
import (
33+
"encoding/json"
3334
"github.com/arduino/arduino-builder/i18n"
3435
"github.com/arduino/arduino-builder/types"
35-
"encoding/json"
3636
)
3737

3838
type CreateBuildOptionsMap struct{}

grpc/rpc.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/arduino/go-paths-helper"
12+
"github.com/bcmi-labs/arduino-cli/arduino/cores"
1213

1314
builder "github.com/arduino/arduino-builder"
1415
"github.com/arduino/arduino-builder/i18n"
@@ -89,7 +90,11 @@ func (s *builderServer) Autocomplete(ctx context.Context, args *pb.BuildParams)
8990
s.ctx.SketchLocation = paths.New(args.SketchLocation)
9091
s.ctx.CustomBuildProperties = strings.Split(args.CustomBuildProperties, ",")
9192
s.ctx.ArduinoAPIVersion = args.ArduinoAPIVersion
92-
s.ctx.FQBN = args.FQBN
93+
if fqbn, err := cores.ParseFQBN(args.FQBN); err != nil {
94+
return nil, fmt.Errorf("parsing fqbn: %s", err)
95+
} else {
96+
s.ctx.FQBN = fqbn
97+
}
9398
s.ctx.Verbose = false //p.Verbose
9499
s.ctx.BuildCachePath = paths.New(args.BuildCachePath)
95100
s.ctx.BuildPath = paths.New(args.BuildPath)
@@ -129,7 +134,11 @@ func (s *builderServer) Build(args *pb.BuildParams, stream pb.Builder_BuildServe
129134
s.ctx.SketchLocation = paths.New(args.SketchLocation)
130135
s.ctx.CustomBuildProperties = strings.Split(args.CustomBuildProperties, ",")
131136
s.ctx.ArduinoAPIVersion = args.ArduinoAPIVersion
132-
s.ctx.FQBN = args.FQBN
137+
if fqbn, err := cores.ParseFQBN(args.FQBN); err != nil {
138+
return fmt.Errorf("parsing fqbn: %s", err)
139+
} else {
140+
s.ctx.FQBN = fqbn
141+
}
133142
s.ctx.Verbose = args.Verbose
134143
s.ctx.BuildCachePath = paths.New(args.BuildCachePath)
135144
s.ctx.BuildPath = paths.New(args.BuildPath)

prototypes_adder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
package builder
3131

3232
import (
33+
"fmt"
3334
"github.com/arduino/arduino-builder/constants"
3435
"github.com/arduino/arduino-builder/types"
3536
"github.com/arduino/arduino-builder/utils"
36-
"fmt"
3737
"strconv"
3838
"strings"
3939
)

setup_build_properties.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (s *SetupBuildProperties) Run(ctx *types.Context) error {
7979
buildProperties["runtime.hardware.path"] = filepath.Join(targetPlatform.Folder, "..")
8080
buildProperties["runtime.ide.version"] = ctx.ArduinoAPIVersion
8181
buildProperties["runtime.ide.path"] = exPath
82-
buildProperties["build.fqbn"] = ctx.FQBN
82+
buildProperties["build.fqbn"] = ctx.FQBN.String()
8383
buildProperties["ide_version"] = ctx.ArduinoAPIVersion
8484
buildProperties["runtime.os"] = utils.PrettyOSName()
8585

target_board_resolver.go

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,87 +35,36 @@ import (
3535
"github.com/arduino/arduino-builder/constants"
3636
"github.com/arduino/arduino-builder/i18n"
3737
"github.com/arduino/arduino-builder/types"
38-
"github.com/bcmi-labs/arduino-cli/arduino/cores"
3938
)
4039

4140
type TargetBoardResolver struct{}
4241

4342
func (s *TargetBoardResolver) Run(ctx *types.Context) error {
4443
logger := ctx.GetLogger()
4544

46-
fqbn := ctx.FQBN
47-
48-
fqbnParts := strings.Split(fqbn, ":")
49-
if len(fqbnParts) < 3 {
50-
return i18n.ErrorfWithLogger(logger, constants.MSG_FQBN_INVALID, fqbn)
51-
}
52-
targetPackageName := fqbnParts[0]
53-
targetPlatformName := fqbnParts[1]
54-
targetBoardName := fqbnParts[2]
55-
56-
packages := ctx.Hardware
57-
58-
targetPackage := packages.Packages[targetPackageName]
59-
if targetPackage == nil {
60-
return i18n.ErrorfWithLogger(logger, constants.MSG_PACKAGE_UNKNOWN, targetPackageName)
61-
}
62-
63-
targetPlatforms := targetPackage.Platforms[targetPlatformName]
64-
if targetPlatforms == nil {
65-
return i18n.ErrorfWithLogger(logger, constants.MSG_PLATFORM_UNKNOWN, targetPlatformName, targetPackageName)
66-
}
67-
targetPlatform := targetPlatforms.GetInstalled()
68-
if targetPlatform == nil {
69-
return i18n.ErrorfWithLogger(logger, constants.MSG_PLATFORM_UNKNOWN, targetPlatformName, targetPackageName)
70-
}
71-
72-
targetBoard := targetPlatform.Boards[targetBoardName]
73-
if targetBoard == nil {
74-
return i18n.ErrorfWithLogger(logger, constants.MSG_BOARD_UNKNOWN, targetBoardName, targetPlatformName, targetPackageName)
45+
targetPackage, targetPlatform, targetBoard, buildProperties, actualPlatform, err := ctx.PackageManager.ResolveFQBN(ctx.FQBN)
46+
if err != nil {
47+
return i18n.ErrorfWithLogger(logger, "Error resolving FQBN: {0}", err)
7548
}
7649

77-
ctx.TargetPlatform = targetPlatform
78-
ctx.TargetPackage = targetPackage
79-
ctx.TargetBoard = targetBoard
80-
81-
if len(fqbnParts) > 3 {
82-
if props, err := targetBoard.GeneratePropertiesForConfiguration(fqbnParts[3]); err != nil {
83-
i18n.ErrorfWithLogger(logger, "Error in FQBN: %s", err)
84-
} else {
85-
targetBoard.Properties = props
86-
}
87-
}
50+
targetBoard.Properties = buildProperties // FIXME....
8851

8952
core := targetBoard.Properties["build.core"]
9053
if core == "" {
9154
core = "arduino"
9255
}
93-
94-
var corePlatform *cores.PlatformRelease
95-
coreParts := strings.Split(core, ":")
96-
if len(coreParts) > 1 {
97-
core = coreParts[1]
98-
if packages.Packages[coreParts[0]] == nil {
99-
return i18n.ErrorfWithLogger(logger, constants.MSG_MISSING_CORE_FOR_BOARD, coreParts[0])
100-
101-
}
102-
corePlatform = packages.Packages[coreParts[0]].Platforms[targetPlatforms.Architecture].GetInstalled()
103-
}
104-
105-
var actualPlatform *cores.PlatformRelease
106-
if corePlatform != nil {
107-
actualPlatform = corePlatform
108-
} else {
109-
actualPlatform = targetPlatform
110-
}
56+
// select the core name in case of "package:core" format
57+
core = core[strings.Index(core, ":")+1:]
11158

11259
if ctx.Verbose {
11360
logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_USING_BOARD, targetBoard.BoardId, targetPlatform.Folder)
11461
logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_USING_CORE, core, actualPlatform.Folder)
11562
}
11663

11764
ctx.BuildCore = core
65+
ctx.TargetBoard = targetBoard
66+
ctx.TargetPlatform = targetPlatform
67+
ctx.TargetPackage = targetPackage
11868
ctx.ActualPlatform = actualPlatform
119-
12069
return nil
12170
}

test/add_build_board_property_if_missing_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,26 @@ import (
3434
"testing"
3535

3636
"github.com/arduino/go-paths-helper"
37+
"github.com/bcmi-labs/arduino-cli/arduino/cores"
3738

3839
"github.com/arduino/arduino-builder"
3940
"github.com/arduino/arduino-builder/constants"
4041
"github.com/arduino/arduino-builder/types"
4142
"github.com/stretchr/testify/require"
4243
)
4344

45+
func parseFQBN(t *testing.T, fqbnIn string) *cores.FQBN {
46+
fqbn, err := cores.ParseFQBN(fqbnIn)
47+
require.NoError(t, err)
48+
return fqbn
49+
}
50+
4451
func TestAddBuildBoardPropertyIfMissing(t *testing.T) {
4552
DownloadCoresAndToolsAndLibraries(t)
4653

4754
ctx := &types.Context{
4855
HardwareFolders: paths.NewPathList(filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"),
49-
FQBN: "my_avr_platform:avr:mymega",
56+
FQBN: parseFQBN(t, "my_avr_platform:avr:mymega"),
5057
}
5158

5259
commands := []types.Command{
@@ -77,7 +84,7 @@ func TestAddBuildBoardPropertyIfMissingNotMissing(t *testing.T) {
7784

7885
ctx := &types.Context{
7986
HardwareFolders: paths.NewPathList(filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "user_hardware"),
80-
FQBN: "my_avr_platform:avr:mymega:cpu=atmega2560",
87+
FQBN: parseFQBN(t, "my_avr_platform:avr:mymega:cpu=atmega2560"),
8188
}
8289

8390
commands := []types.Command{

test/builder_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ import (
4444
"github.com/stretchr/testify/require"
4545
)
4646

47-
func prepareBuilderTestContext(sketchPath *paths.Path, fqbn string) *types.Context {
47+
func prepareBuilderTestContext(t *testing.T, sketchPath *paths.Path, fqbn string) *types.Context {
4848
return &types.Context{
4949
SketchLocation: sketchPath,
50-
FQBN: fqbn,
50+
FQBN: parseFQBN(t, fqbn),
5151
HardwareFolders: paths.NewPathList(filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"),
5252
BuiltInToolsFolders: paths.NewPathList("downloaded_tools"),
5353
BuiltInLibrariesFolders: paths.NewPathList("downloaded_libraries"),
@@ -60,7 +60,7 @@ func prepareBuilderTestContext(sketchPath *paths.Path, fqbn string) *types.Conte
6060
func TestBuilderEmptySketch(t *testing.T) {
6161
DownloadCoresAndToolsAndLibraries(t)
6262

63-
ctx := prepareBuilderTestContext(paths.New("sketch1", "sketch.ino"), "arduino:avr:uno")
63+
ctx := prepareBuilderTestContext(t, paths.New("sketch1", "sketch.ino"), "arduino:avr:uno")
6464
ctx.DebugLevel = 10
6565

6666
buildPath := SetupBuildPath(t, ctx)
@@ -91,7 +91,7 @@ func TestBuilderEmptySketch(t *testing.T) {
9191
func TestBuilderBridge(t *testing.T) {
9292
DownloadCoresAndToolsAndLibraries(t)
9393

94-
ctx := prepareBuilderTestContext(paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "arduino:avr:leonardo")
94+
ctx := prepareBuilderTestContext(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "arduino:avr:leonardo")
9595

9696
buildPath := SetupBuildPath(t, ctx)
9797
defer buildPath.RemoveAll()
@@ -124,7 +124,7 @@ func TestBuilderBridge(t *testing.T) {
124124
func TestBuilderSketchWithConfig(t *testing.T) {
125125
DownloadCoresAndToolsAndLibraries(t)
126126

127-
ctx := prepareBuilderTestContext(paths.New("sketch_with_config", "sketch_with_config.ino"), "arduino:avr:leonardo")
127+
ctx := prepareBuilderTestContext(t, paths.New("sketch_with_config", "sketch_with_config.ino"), "arduino:avr:leonardo")
128128

129129
buildPath := SetupBuildPath(t, ctx)
130130
defer buildPath.RemoveAll()
@@ -157,7 +157,7 @@ func TestBuilderSketchWithConfig(t *testing.T) {
157157
func TestBuilderBridgeTwice(t *testing.T) {
158158
DownloadCoresAndToolsAndLibraries(t)
159159

160-
ctx := prepareBuilderTestContext(paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "arduino:avr:leonardo")
160+
ctx := prepareBuilderTestContext(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "arduino:avr:leonardo")
161161

162162
buildPath := SetupBuildPath(t, ctx)
163163
defer buildPath.RemoveAll()
@@ -195,7 +195,7 @@ func TestBuilderBridgeTwice(t *testing.T) {
195195
func TestBuilderBridgeSAM(t *testing.T) {
196196
DownloadCoresAndToolsAndLibraries(t)
197197

198-
ctx := prepareBuilderTestContext(paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "arduino:sam:arduino_due_x_dbg")
198+
ctx := prepareBuilderTestContext(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "arduino:sam:arduino_due_x_dbg")
199199
ctx.WarningsLevel = "all"
200200

201201
buildPath := SetupBuildPath(t, ctx)
@@ -240,7 +240,7 @@ func TestBuilderBridgeSAM(t *testing.T) {
240240
func TestBuilderBridgeRedBearLab(t *testing.T) {
241241
DownloadCoresAndToolsAndLibraries(t)
242242

243-
ctx := prepareBuilderTestContext(paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "RedBearLab:avr:blend")
243+
ctx := prepareBuilderTestContext(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "RedBearLab:avr:blend")
244244
ctx.HardwareFolders = append(ctx.HardwareFolders, paths.New("downloaded_board_manager_stuff"))
245245
ctx.ToolsFolders = append(ctx.ToolsFolders, paths.New("downloaded_board_manager_stuff"))
246246

@@ -275,7 +275,7 @@ func TestBuilderBridgeRedBearLab(t *testing.T) {
275275
func TestBuilderSketchNoFunctions(t *testing.T) {
276276
DownloadCoresAndToolsAndLibraries(t)
277277

278-
ctx := prepareBuilderTestContext(paths.New("sketch_no_functions", "main.ino"), "RedBearLab:avr:blend")
278+
ctx := prepareBuilderTestContext(t, paths.New("sketch_no_functions", "main.ino"), "RedBearLab:avr:blend")
279279
ctx.HardwareFolders = append(ctx.HardwareFolders, paths.New("downloaded_board_manager_stuff"))
280280
ctx.ToolsFolders = append(ctx.ToolsFolders, paths.New("downloaded_board_manager_stuff"))
281281

@@ -291,7 +291,7 @@ func TestBuilderSketchNoFunctions(t *testing.T) {
291291
func TestBuilderSketchWithBackup(t *testing.T) {
292292
DownloadCoresAndToolsAndLibraries(t)
293293

294-
ctx := prepareBuilderTestContext(paths.New("sketch_with_backup_files", "sketch.ino"), "arduino:avr:uno")
294+
ctx := prepareBuilderTestContext(t, paths.New("sketch_with_backup_files", "sketch.ino"), "arduino:avr:uno")
295295
ctx.HardwareFolders = append(ctx.HardwareFolders, paths.New("downloaded_board_manager_stuff"))
296296
ctx.ToolsFolders = append(ctx.ToolsFolders, paths.New("downloaded_board_manager_stuff"))
297297

@@ -307,7 +307,7 @@ func TestBuilderSketchWithBackup(t *testing.T) {
307307
func TestBuilderSketchWithOldLib(t *testing.T) {
308308
DownloadCoresAndToolsAndLibraries(t)
309309

310-
ctx := prepareBuilderTestContext(paths.New("sketch_with_old_lib", "sketch.ino"), "arduino:avr:uno")
310+
ctx := prepareBuilderTestContext(t, paths.New("sketch_with_old_lib", "sketch.ino"), "arduino:avr:uno")
311311

312312
buildPath := SetupBuildPath(t, ctx)
313313
defer buildPath.RemoveAll()
@@ -321,7 +321,7 @@ func TestBuilderSketchWithOldLib(t *testing.T) {
321321
func TestBuilderSketchWithSubfolders(t *testing.T) {
322322
DownloadCoresAndToolsAndLibraries(t)
323323

324-
ctx := prepareBuilderTestContext(paths.New("sketch_with_subfolders", "sketch_with_subfolders.ino"), "arduino:avr:uno")
324+
ctx := prepareBuilderTestContext(t, paths.New("sketch_with_subfolders", "sketch_with_subfolders.ino"), "arduino:avr:uno")
325325

326326
buildPath := SetupBuildPath(t, ctx)
327327
defer buildPath.RemoveAll()
@@ -335,7 +335,7 @@ func TestBuilderSketchWithSubfolders(t *testing.T) {
335335
func TestBuilderSketchBuildPathContainsUnusedPreviouslyCompiledLibrary(t *testing.T) {
336336
DownloadCoresAndToolsAndLibraries(t)
337337

338-
ctx := prepareBuilderTestContext(paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "arduino:avr:leonardo")
338+
ctx := prepareBuilderTestContext(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "arduino:avr:leonardo")
339339

340340
buildPath := SetupBuildPath(t, ctx)
341341
defer buildPath.RemoveAll()
@@ -358,7 +358,7 @@ func TestBuilderSketchBuildPathContainsUnusedPreviouslyCompiledLibrary(t *testin
358358
func TestBuilderWithBuildPathInSketchDir(t *testing.T) {
359359
DownloadCoresAndToolsAndLibraries(t)
360360

361-
ctx := prepareBuilderTestContext(paths.New("sketch1", "sketch.ino"), "arduino:avr:uno")
361+
ctx := prepareBuilderTestContext(t, paths.New("sketch1", "sketch.ino"), "arduino:avr:uno")
362362

363363
var err error
364364
ctx.BuildPath, err = paths.New("sketch1", "build").Abs()
@@ -379,7 +379,7 @@ func TestBuilderWithBuildPathInSketchDir(t *testing.T) {
379379
func TestBuilderCacheCoreAFile(t *testing.T) {
380380
DownloadCoresAndToolsAndLibraries(t)
381381

382-
ctx := prepareBuilderTestContext(paths.New("sketch1", "sketch.ino"), "arduino:avr:uno")
382+
ctx := prepareBuilderTestContext(t, paths.New("sketch1", "sketch.ino"), "arduino:avr:uno")
383383

384384
SetupBuildPath(t, ctx)
385385
defer ctx.BuildPath.RemoveAll()
@@ -393,7 +393,7 @@ func TestBuilderCacheCoreAFile(t *testing.T) {
393393

394394
// Pick timestamp of cached core
395395
coreFolder := paths.New("downloaded_hardware", "arduino", "avr")
396-
coreFileName := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN, coreFolder)
396+
coreFileName := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN.String(), coreFolder)
397397
cachedCoreFile := ctx.CoreBuildCachePath.Join(coreFileName)
398398
coreStatBefore, err := cachedCoreFile.Stat()
399399
require.NoError(t, err)

test/create_build_options_map_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestCreateBuildOptionsMap(t *testing.T) {
4444
ToolsFolders: paths.NewPathList("tools"),
4545
OtherLibrariesFolders: paths.NewPathList("libraries"),
4646
SketchLocation: paths.New("sketchLocation"),
47-
FQBN: "fqbn",
47+
FQBN: parseFQBN(t, "my:nice:fqbn"),
4848
ArduinoAPIVersion: "ideVersion",
4949
Verbose: true,
5050
BuildPath: paths.New("buildPath"),
@@ -59,7 +59,7 @@ func TestCreateBuildOptionsMap(t *testing.T) {
5959
" \"additionalFiles\": \"\",\n"+
6060
" \"builtInLibrariesFolders\": \"\",\n"+
6161
" \"customBuildProperties\": \"\",\n"+
62-
" \"fqbn\": \"fqbn\",\n"+
62+
" \"fqbn\": \"my:nice:fqbn\",\n"+
6363
" \"hardwareFolders\": \"hardware,hardware2\",\n"+
6464
" \"otherLibrariesFolders\": \"libraries\",\n"+
6565
" \"runtime.ide.version\": \"ideVersion\",\n"+

0 commit comments

Comments
 (0)