Skip to content

Auto-import libraries based on sketch profile. #2951

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

Merged
merged 13 commits into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Moving sourceFile object in his own file
  • Loading branch information
cmaglie committed Jul 7, 2025
commit c72f35bb3aad7bf2f77ffeca4d6a2b74fe0902a5
91 changes: 0 additions & 91 deletions internal/arduino/builder/internal/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,97 +502,6 @@ func findIncludeForOldCompilers(source string) string {
return ""
}

type sourceFile struct {
// Path to the source file within the sketch/library root folder
relativePath *paths.Path

// ExtraIncludePath contains an extra include path that must be
// used to compile this source file.
// This is mainly used for source files that comes from old-style libraries
// (Arduino IDE <1.5) requiring an extra include path to the "utility" folder.
extraIncludePath *paths.Path

// The source root for the given origin, where its source files
// can be found. Prepending this to SourceFile.RelativePath will give
// the full path to that source file.
sourceRoot *paths.Path

// The build root for the given origin, where build products will
// be placed. Any directories inside SourceFile.RelativePath will be
// appended here.
buildRoot *paths.Path
}

// Equals fixdoc
func (f *sourceFile) Equals(g *sourceFile) bool {
return f.relativePath.EqualsTo(g.relativePath) &&
f.buildRoot.EqualsTo(g.buildRoot) &&
f.sourceRoot.EqualsTo(g.sourceRoot)
}

// makeSourceFile containing the given source file path within the
// given origin. The given path can be absolute, or relative within the
// origin's root source folder
func makeSourceFile(
sourceDir *paths.Path,
buildDir *paths.Path,
sourceFilePath *paths.Path,
extraIncludePath ...*paths.Path,
) (*sourceFile, error) {
res := &sourceFile{
buildRoot: buildDir,
sourceRoot: sourceDir,
}

if len(extraIncludePath) > 1 {
panic("only one extra include path allowed")
}
if len(extraIncludePath) > 0 {
res.extraIncludePath = extraIncludePath[0]
}
// switch o := origin.(type) {
// case *sketch.Sketch:
// res.buildRoot = sketchBuildPath
// res.sourceRoot = sketchBuildPath
// case *libraries.Library:
// res.buildRoot = librariesBuildPath.Join(o.DirName)
// res.sourceRoot = o.SourceDir
// res.extraIncludePath = o.UtilityDir
// default:
// panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
// }

if sourceFilePath.IsAbs() {
var err error
sourceFilePath, err = res.sourceRoot.RelTo(sourceFilePath)
if err != nil {
return nil, err
}
}
res.relativePath = sourceFilePath
return res, nil
}

// ExtraIncludePath fixdoc
func (f *sourceFile) ExtraIncludePath() *paths.Path {
return f.extraIncludePath
}

// SourcePath fixdoc
func (f *sourceFile) SourcePath() *paths.Path {
return f.sourceRoot.JoinPath(f.relativePath)
}

// ObjectPath fixdoc
func (f *sourceFile) ObjectPath() *paths.Path {
return f.buildRoot.Join(f.relativePath.String() + ".o")
}

// DepfilePath fixdoc
func (f *sourceFile) DepfilePath() *paths.Path {
return f.buildRoot.Join(f.relativePath.String() + ".d")
}

// LibrariesLoader todo
func LibrariesLoader(
useCachedLibrariesResolution bool,
Expand Down
92 changes: 92 additions & 0 deletions internal/arduino/builder/internal/detector/source_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// This file is part of arduino-cli.
//
// Copyright 2024 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package detector

import "github.com/arduino/go-paths-helper"

type sourceFile struct {
// Path to the source file within the sketch/library root folder
relativePath *paths.Path

// ExtraIncludePath contains an extra include path that must be
// used to compile this source file.
// This is mainly used for source files that comes from old-style libraries
// (Arduino IDE <1.5) requiring an extra include path to the "utility" folder.
extraIncludePath *paths.Path

// The source root for the given origin, where its source files
// can be found. Prepending this to SourceFile.RelativePath will give
// the full path to that source file.
sourceRoot *paths.Path

// The build root for the given origin, where build products will
// be placed. Any directories inside SourceFile.RelativePath will be
// appended here.
buildRoot *paths.Path
}

// Equals fixdoc
func (f *sourceFile) Equals(g *sourceFile) bool {
return f.relativePath.EqualsTo(g.relativePath) &&
f.buildRoot.EqualsTo(g.buildRoot) &&
f.sourceRoot.EqualsTo(g.sourceRoot)
}

// makeSourceFile create a sourceFile object for the given source file path.
// The given sourceFilePath can be absolute, or relative within the sourceRoot root folder.
func makeSourceFile(sourceRoot, buildRoot, sourceFilePath *paths.Path, extraIncludePath ...*paths.Path) (*sourceFile, error) {
res := &sourceFile{
buildRoot: buildRoot,
sourceRoot: sourceRoot,
}

if len(extraIncludePath) > 1 {
panic("only one extra include path allowed")
}
if len(extraIncludePath) > 0 {
res.extraIncludePath = extraIncludePath[0]
}

if sourceFilePath.IsAbs() {
var err error
sourceFilePath, err = res.sourceRoot.RelTo(sourceFilePath)
if err != nil {
return nil, err
}
}
res.relativePath = sourceFilePath
return res, nil
}

// ExtraIncludePath returns the extra include path required to build the source file.
func (f *sourceFile) ExtraIncludePath() *paths.Path {
return f.extraIncludePath
}

// SourcePath return the full path to the source file.
func (f *sourceFile) SourcePath() *paths.Path {
return f.sourceRoot.JoinPath(f.relativePath)
}

// ObjectPath return the full path to the object file.
func (f *sourceFile) ObjectPath() *paths.Path {
return f.buildRoot.Join(f.relativePath.String() + ".o")
}

// DepfilePath return the full path to the dependency file.
func (f *sourceFile) DepfilePath() *paths.Path {
return f.buildRoot.Join(f.relativePath.String() + ".d")
}