10000 Some refactorings on library resolution code by cmaglie · Pull Request #1766 · arduino/arduino-cli · GitHub
[go: up one dir, main page]

Skip to content

Some refactorings on library resolution code #1766

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 8 commits into from
Aug 25, 2023
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
Dramatically simplified SourceFile object
Removed dependency from types.Context
  • Loading branch information
cmaglie committed Jun 8, 2023
commit 7de56418ad9468610bcc65e5e150e185441c3b5b
10 changes: 5 additions & 5 deletions legacy/builder/container_find_includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,10 @@ func writeCache(cache *includeCache, path *paths.Path) error {

func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFileQueue *types.UniqueSourceFileQueue) error {
sourceFile := sourceFileQueue.Pop()
sourcePath := sourceFile.SourcePath(ctx)
sourcePath := sourceFile.SourcePath()
targetFilePath := paths.NullPath()
depPath := sourceFile.DepfilePath(ctx)
objPath := sourceFile.ObjectPath(ctx)
depPath := sourceFile.DepfilePath()
objPath := sourceFile.ObjectPath()

// TODO: This should perhaps also compare against the
// include.cache file timestamp. Now, it only checks if the file
Expand All @@ -336,11 +336,11 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFileQu
cache.ExpectFile(sourcePath)

includeFolders := ctx.IncludeFolders
if library, ok := sourceFile.Origin.(*libraries.Library); ok && library.UtilityDir != nil {
if library := sourceFile.Library; library != nil && library.UtilityDir != nil {
includeFolders = append(includeFolders, library.UtilityDir)
}

if library, ok := sourceFile.Origin.(*libraries.Library); ok {
if library := sourceFile.Library; library != nil {
if library.Precompiled && library.PrecompiledWithSources {
// Fully precompiled libraries should have no dependencies
// to avoid ABI breakage
Expand Down
75 changes: 38 additions & 37 deletions legacy/builder/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,69 +24,70 @@ import (
)

type SourceFile struct {
// Sketch or Library pointer that this source file lives in
Origin interface{}
// Path to the source file within the sketch/library root folder
RelativePath *paths.Path

// Set to the Library object of origin if this source file comes
// from a library
Library *libraries.Library

// 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
}

func (f *SourceFile) Equals(g *SourceFile) bool {
return f.Origin == g.Origin &&
f.RelativePath.EqualsTo(g.RelativePath)
return f.Library == g.Library &&
f.RelativePath.EqualsTo(g.RelativePath) &&
f.buildRoot.EqualsTo(g.buildRoot) &&
f.sourceRoot.EqualsTo(g.sourceRoot)
}

// Create a SourceFile 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(ctx *Context, origin interface{}, path *paths.Path) (*SourceFile, error) {
if path.IsAbs() {
var err error
path, err = sourceRoot(ctx, origin).RelTo(path)
if err != nil {
return nil, err
}
}
return &SourceFile{Origin: origin, RelativePath: path}, nil
}
res := &SourceFile{}

// Return the build root for the given origin, where build products will
// be placed. Any directories inside SourceFile.RelativePath will be
// appended here.
func buildRoot(ctx *Context, origin interface{}) *paths.Path {
switch o := origin.(type) {
case *sketch.Sketch:
return ctx.SketchBuildPath
res.buildRoot = ctx.SketchBuildPath
res.sourceRoot = ctx.SketchBuildPath
case *libraries.Library:
return ctx.LibrariesBuildPath.Join(o.DirName)
res.buildRoot = ctx.LibrariesBuildPath.Join(o.DirName)
res.sourceRoot = o.SourceDir
res.Library = o
default:
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
}
}

// Return 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.
func sourceRoot(ctx *Context, origin interface{}) *paths.Path {
switch o := origin.(type) {
case *sketch.Sketch:
return ctx.SketchBuildPath
case *libraries.Library:
return o.SourceDir
default:
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
if path.IsAbs() {
var err error
path, err = res.sourceRoot.RelTo(path)
if err != nil {
return nil, err
}
}
res.RelativePath = path
return res, nil
}

func (f *SourceFile) SourcePath(ctx *Context) *paths.Path {
return sourceRoot(ctx, f.Origin).JoinPath(f.RelativePath)
func (f *SourceFile) SourcePath() *paths.Path {
return f.sourceRoot.JoinPath(f.RelativePath)
}

func (f *SourceFile) ObjectPath(ctx *Context) *paths.Path {
return buildRoot(ctx, f.Origin).Join(f.RelativePath.String() + ".o")
func (f *SourceFile) ObjectPath() *paths.Path {
return f.buildRoot.Join(f.RelativePath.String() + ".o")
}

func (f *SourceFile) DepfilePath(ctx *Context) *paths.Path {
return buildRoot(ctx, f.Origin).Join(f.RelativePath.String() + ".d")
func (f *SourceFile) DepfilePath() *paths.Path {
return f.buildRoot.Join(f.RelativePath.String() + ".d")
}

type LibraryResolutionResult struct {
Expand Down
0