Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to embed custom libraries into the sketch #2514

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func NewBuilder(
logger := logger.New(stdout, stderr, verbose, warningsLevel)
libsManager, libsResolver, verboseOut, err := detector.LibrariesLoader(
useCachedLibrariesResolution, librariesManager,
builtInLibrariesDirs, libraryDirs, otherLibrariesDirs,
sk, builtInLibrariesDirs, libraryDirs, otherLibrariesDirs,
actualPlatform, targetPlatform,
)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/arduino/builder/internal/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ func (f *sourceFile) DepfilePath() *paths.Path {
func LibrariesLoader(
useCachedLibrariesResolution bool,
librariesManager *librariesmanager.LibrariesManager,
sk *sketch.Sketch,
builtInLibrariesDirs *paths.Path, libraryDirs, otherLibrariesDirs paths.PathList,
actualPlatform, targetPlatform *cores.PlatformRelease,
) (*librariesmanager.LibrariesManager, *librariesresolver.Cpp, []byte, error) {
Expand Down Expand Up @@ -660,7 +661,7 @@ func LibrariesLoader(
}

allLibs := lm.FindAllInstalled()
resolver := librariesresolver.NewCppResolver(allLibs, targetPlatform, actualPlatform)
resolver := librariesresolver.NewCppResolver(allLibs, sk, targetPlatform, actualPlatform)
return lm, resolver, verboseOut.Bytes(), nil
}

Expand Down
12 changes: 10 additions & 2 deletions internal/arduino/libraries/librariesresolver/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/arduino/arduino-cli/internal/arduino/cores"
"github.com/arduino/arduino-cli/internal/arduino/libraries"
"github.com/arduino/arduino-cli/internal/arduino/sketch"
"github.com/arduino/arduino-cli/internal/arduino/utils"
"github.com/arduino/arduino-cli/internal/i18n"
"github.com/schollz/closestmatch"
Expand All @@ -36,7 +37,7 @@ type Cpp struct {
var tr = i18n.Tr

// NewCppResolver creates a new Cpp resolver
func NewCppResolver(allLibs []*libraries.Library, targetPlatform, actualPlatform *cores.PlatformRelease) *Cpp {
func NewCppResolver(allLibs []*libraries.Library, sk *sketch.Sketch, targetPlatform, actualPlatform *cores.PlatformRelease) *Cpp {
resolver := &Cpp{
headers: map[string]libraries.List{},
}
Expand All @@ -46,10 +47,17 @@ func NewCppResolver(allLibs []*libraries.Library, targetPlatform, actualPlatform
if actualPlatform != targetPlatform {
resolver.ScanPlatformLibraries(allLibs, actualPlatform)
}

resolver.ScanSketchLibraries(sk)
return resolver
}

// ScanSketchLibraries loads libraries bundled with the sketch
func (resolver *Cpp) ScanSketchLibraries(sk *sketch.Sketch) {
for _, lib := range sk.VendoredLibraries() {
_ = resolver.ScanLibrary(lib)
}
}

// ScanIDEBuiltinLibraries reads ide-builtin librariers loaded in the LibrariesManager to find
// and cache all C++ headers for later retrieval.
func (resolver *Cpp) ScanIDEBuiltinLibraries(allLibs []*libraries.Library) {
Expand Down
48 changes: 41 additions & 7 deletions internal/arduino/sketch/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ import (
"github.com/arduino/arduino-cli/commands/cmderrors"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/internal/arduino/globals"
"github.com/arduino/arduino-cli/internal/arduino/libraries"
"github.com/arduino/arduino-cli/internal/i18n"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
)

// Sketch holds all the files composing a sketch
type Sketch struct {
Name string
MainFile *paths.Path
FullPath *paths.Path // FullPath is the path to the Sketch folder
OtherSketchFiles paths.PathList // Sketch files that end in .ino other than main file
AdditionalFiles paths.PathList
RootFolderFiles paths.PathList // All files that are in the Sketch root
Project *Project
Name string
MainFile *paths.Path
FullPath *paths.Path // FullPath is the path to the Sketch folder
OtherSketchFiles paths.PathList // Sketch files that end in .ino other than main file
AdditionalFiles paths.PathList
RootFolderFiles paths.PathList // All files that are in the Sketch root
vendoredLibraries []*libraries.Library // All libraries in the 'libraries' directory in the sketch
Project *Project
}

var tr = i18n.Tr
Expand Down Expand Up @@ -148,9 +150,41 @@ func New(path *paths.Path) (*Sketch, error) {
sort.Sort(&sketch.OtherSketchFiles)
sort.Sort(&sketch.RootFolderFiles)

// Collect vedndored libraries
if librariesPath, ok := sketch.GetVendoredLibrariesDir(); ok {
libDirs, err := librariesPath.ReadDir()
if err != nil {
return nil, fmt.Errorf("%s: %w", tr("reading sketch libraries"), err)
}
libDirs.FilterDirs()
for _, libDir := range libDirs {
lib, err := libraries.Load(libDir, libraries.Unmanaged)
if err != nil {
return nil, fmt.Errorf("%s: %w", tr("reading sketch libraries"), err)
}
sketch.vendoredLibraries = append(sketch.vendoredLibraries, lib)
}
}

return sketch, nil
}

// GetVendoredLibrariesDir returns the 'libraries' directory path.
// The result is in the res,ok format ok is true if the 'libraries' directory
// is present in the sketch, false otherwise.
func (s *Sketch) GetVendoredLibrariesDir() (res *paths.Path, ok bool) {
libsDir := s.FullPath.Join("libraries")
if libsDir.IsDir() {
return libsDir, true
}
return nil, false
}

// VendoredLibraries returns the libraries bundled in the sketch' 'libraries' directory.
func (s *Sketch) VendoredLibraries() []*libraries.Library {
return s.vendoredLibraries
}

// supportedFiles reads all files recursively contained in Sketch and
// filter out unneded or unsupported ones and returns them
func (s *Sketch) supportedFiles() (paths.PathList, error) {
Expand Down
8 changes: 8 additions & 0 deletions internal/arduino/sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,11 @@ func TestSketchWithMultipleSymlinkLoops(t *testing.T) {
require.Error(t, err)
require.Nil(t, sketch)
}

func TestSketchWithVendoredLibraries(t *testing.T) {
sketchPath := paths.New("testdata", "SketchWithLibraries")
sk, err := New(sketchPath)
require.NoError(t, err)
require.Len(t, sk.vendoredLibraries, 1)
require.Equal(t, "MyLib", sk.vendoredLibraries[0].Name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <MyLib.h>

void setup() {}
void loop() {
myFunction();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

void myFunction() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name=MyLib
6 changes: 6 additions & 0 deletions internal/integrationtest/arduino-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ func (cli *ArduinoCLI) SketchbookDir() *paths.Path {
return cli.sketchbookDir
}

// SetSketchbookDir sets the sketchbook directory
func (cli *ArduinoCLI) SetSketchbookDir(d *paths.Path) {
cli.sketchbookDir = d
cli.cliEnvVars["ARDUINO_SKETCHBOOK_DIR"] = d.String()
}

// WorkingDir returns the working directory
func (cli *ArduinoCLI) WorkingDir() *paths.Path {
return cli.workingDir
Expand Down
39 changes: 39 additions & 0 deletions internal/integrationtest/compile_2/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/stretchr/testify/require"
"go.bug.st/testifyjson/requirejson"
)

func TestCompilePart4(t *testing.T) {
Expand Down Expand Up @@ -449,3 +450,41 @@ func TestCompileWithKnownPlatformNotInstalled(t *testing.T) {
// Verifies command to fix error is shown to user
require.Contains(t, string(stderr), "Try running `arduino-cli core install arduino:avr`")
}

func TestSketchWithVendoredLibraries(t *testing.T) {
sketchBook, err := paths.New("testdata", "sketchbook_1").Abs()
require.NoError(t, err)

env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

cli.SetSketchbookDir(sketchBook)

_, _, err = cli.Run("core", "install", "arduino:avr")
require.NoError(t, err)

{
sketchWithLibsPath := sketchBook.Join("SketchWithLibraries")
// Sketch should use sketch bundled "MyLib" with and without profiles
out, _, err := cli.Run("compile", "-b", "arduino:avr:uno", sketchWithLibsPath.String(), "--format", "json")
require.NoError(t, err)
requirejson.Query(t, out, ".builder_result.used_libraries[0].name", `"MyLib"`)
requirejson.Query(t, out, ".builder_result.used_libraries[0].author", `"user"`)
out, _, err = cli.Run("compile", "--profile", "uno", sketchWithLibsPath.String(), "--format", "json")
require.NoError(t, err)
requirejson.Query(t, out, ".builder_result.used_libraries[0].name", `"MyLib"`)
requirejson.Query(t, out, ".builder_result.used_libraries[0].author", `"user"`)
}

{
sketchWithoutLibsPath := sketchBook.Join("SketchWithoutLibraries")
// This sketch should take the user-installed MyLib
out, _, err := cli.Run("compile", "-b", "arduino:avr:uno", sketchWithoutLibsPath.String(), "--format", "json")
require.NoError(t, err)
requirejson.Query(t, out, ".builder_result.used_libraries[0].name", `"MyLib"`)
requirejson.Query(t, out, ".builder_result.used_libraries[0].author", `"upstream"`)
// This sketch should fail to compile since profiles will not see the user-installed MyLib
_, _, err = cli.Run("compile", "--profile", "uno", sketchWithoutLibsPath.String())
require.Error(t, err)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <MyLib.h>

void setup() {}
void loop() {
myFunction();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

void anotherFunction() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name=AnotherLib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

void myFunction() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name=MyLib
author=user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
profiles:
uno:
fqbn: arduino:avr:uno
platforms:
- platform: arduino:avr (1.8.5)

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <MyLib.h>

void setup() {}
void loop() {
myWrongFunction();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
profiles:
uno:
fqbn: arduino:avr:uno
platforms:
- platform: arduino:avr (1.8.5)

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

// This is the wrong library to include (the sketch-bundled one should take priority)

void myWrongFunction() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name=MyLib
author=upstream
Loading