Skip to content

Commit c3a46e0

Browse files
committed
Removed reference to Library in SourceFile struct
1 parent 93740ed commit c3a46e0

File tree

1 file changed

+35
-53
lines changed

1 file changed

+35
-53
lines changed

legacy/builder/container_find_includes.go

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ import (
9999
"os/exec"
100100
"time"
101101

102-
"github.com/arduino/arduino-cli/arduino/libraries"
103102
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
104103
"github.com/arduino/arduino-cli/legacy/builder/constants"
105104
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -147,17 +146,17 @@ func (f *CppIncludesFinder) DetectLibraries() error {
147146
f.appendIncludeFolder(nil, "", f.ctx.BuildProperties.GetPath("build.variant.path"))
148147
}
149148

150-
mergedfile, err := MakeSourceFile(f.ctx, nil, paths.New(f.sketch.MainFile.Name.Base()+".cpp"))
149+
mergedfile, err := MakeSourceFile(f.ctx.SketchBuildPath, f.ctx.SketchBuildPath, paths.New(f.sketch.MainFile.Name.Base()+".cpp"))
151150
if err != nil {
152151
return errors.WithStack(err)
153152
}
154153
f.log.Debugf("Queueing merged sketch: %s", mergedfile)
155154
f.queue.Push(mergedfile)
156155

157-
f.queueSourceFilesFromFolder(nil, f.ctx.SketchBuildPath, false /* recurse */)
156+
f.queueSourceFilesFromFolder(f.ctx.SketchBuildPath, f.ctx.SketchBuildPath, false /* recurse */)
158157
srcSubfolderPath := f.ctx.SketchBuildPath.Join("src")
159158
if srcSubfolderPath.IsDir() {
160-
f.queueSourceFilesFromFolder(nil, srcSubfolderPath, true /* recurse */)
159+
f.queueSourceFilesFromFolder(srcSubfolderPath, f.ctx.SketchBuildPath, true /* recurse */)
161160
}
162161

163162
for !f.queue.Empty() {
@@ -309,10 +308,10 @@ func (cache *includeCache) WriteToFile() error {
309308
}
310309

311310
func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile SourceFile) error {
312-
sourcePath := sourceFile.SourcePath(f.ctx)
311+
sourcePath := sourceFile.SourcePath()
313312
targetFilePath := paths.NullPath()
314-
depPath := sourceFile.DepfilePath(f.ctx)
315-
objPath := sourceFile.ObjectPath(f.ctx)
313+
depPath := sourceFile.DepfilePath()
314+
objPath := sourceFile.ObjectPath()
316315

317316
// TODO: This should perhaps also compare against the
318317
// include.cache file timestamp. Now, it only checks if the file
@@ -333,11 +332,11 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile SourceFile) error {
333332

334333
first := true
335334
for {
336-
var include string
337335
f.cache.ExpectFile(sourcePath)
338336

339337
var preprocErr error
340338
var preprocStderr []byte
339+
var include string
341340
if unchanged && f.cache.valid {
342341
include = f.cache.Next().Include
343342
if first && f.ctx.Verbose {
@@ -399,24 +398,25 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile SourceFile) error {
399398
f.ctx.IncludeFolders = append(f.ctx.IncludeFolders, library.UtilityDir)
400399
}
401400
sourceDirs := library.SourceDirs()
401+
buildDir := f.ctx.LibrariesBuildPath.Join(library.Name)
402402
for _, sourceDir := range sourceDirs {
403-
f.queueSourceFilesFromFolder(library, sourceDir.Dir, sourceDir.Recurse)
403+
f.queueSourceFilesFromFolder(buildDir, sourceDir.Dir, sourceDir.Recurse)
404404
}
405405
first = false
406406
}
407407
}
408408

409-
func (f *CppIncludesFinder) queueSourceFilesFromFolder(lib *libraries.Library, folder *paths.Path, recurse bool) error {
409+
func (f *CppIncludesFinder) queueSourceFilesFromFolder(srcDir, buildDir *paths.Path, recurse bool) error {
410410
extensions := func(ext string) bool { return ADDITIONAL_FILE_VALID_EXTENSIONS_NO_HEADERS[ext] }
411-
f.log.Debugf(" Queueing source files from %s (recurse %v)", folder, recurse)
411+
f.log.Debugf(" Queueing source files from %s (recurse %v)", srcDir, recurse)
412412
filePaths := []string{}
413-
err := utils.FindFilesInFolder(&filePaths, folder.String(), extensions, recurse)
413+
err := utils.FindFilesInFolder(&filePaths, srcDir.String(), extensions, recurse)
414414
if err != nil {
415415
return errors.WithStack(err)
416416
}
417417

418418
for _, filePath := range filePaths {
419-
sourceFile, err := MakeSourceFile(f.ctx, lib, paths.New(filePath))
419+
sourceFile, err := MakeSourceFile(srcDir, buildDir, paths.New(filePath))
420420
if err != nil {
421421
return errors.WithStack(err)
422422
}
@@ -428,64 +428,44 @@ func (f *CppIncludesFinder) queueSourceFilesFromFolder(lib *libraries.Library, f
428428
}
429429

430430
type SourceFile struct {
431-
// Library pointer that this source file lives in or nil if not part of a library
432-
Library *libraries.Library
431+
// SourceRoot is the path to the source code root directory
432+
SourceRoot *paths.Path
433+
434+
// BuildRoot is the path to the build root directory
435+
BuildRoot *paths.Path
433436

434437
// Path to the source file within the sketch/library root folder
435438
RelativePath *paths.Path
436-
437-
ctx *types.Context
438439
}
439440

440441
func (f SourceFile) String() string {
441442
return fmt.Sprintf("Root: %s - Path: %s - BuildPath: %s",
442-
sourceRoot(f.ctx, f.Library), f.RelativePath, buildRoot(f.ctx, f.Library))
443+
f.SourceRoot, f.RelativePath, f.BuildRoot)
443444
}
444445

445-
// Create a SourceFile containing the given source file path within the
446-
// given origin. The given path can be absolute, or relative within the
447-
// origin's root source folder
448-
func MakeSourceFile(ctx *types.Context, lib *libraries.Library, path *paths.Path) (SourceFile, error) {
446+
// MakeSourceFile creates a SourceFile containing the given source file path
447+
// within the given sourceRoot.
448+
func MakeSourceFile(sourceRoot, buildRoot, path *paths.Path) (SourceFile, error) {
449449
if path.IsAbs() {
450-
var err error
451-
path, err = sourceRoot(ctx, lib).RelTo(path)
452-
if err != nil {
450+
if relPath, err := sourceRoot.RelTo(path); err == nil {
451+
path = relPath
452+
} else {
453453
return SourceFile{}, err
454454
}
455455
}
456-
return SourceFile{Library: lib, RelativePath: path, ctx: ctx}, nil
457-
}
458-
459-
// Return the build root for the given origin, where build products will
460-
// be placed. Any directories inside SourceFile.RelativePath will be
461-
// appended here.
462-
func buildRoot(ctx *types.Context, lib *libraries.Library) *paths.Path {
463-
if lib == nil {
464-
return ctx.SketchBuildPath
465-
}
466-
return ctx.LibrariesBuildPath.Join(lib.Name)
467-
}
468-
469-
// Return the source root for the given origin, where its source files
470-
// can be found. Prepending this to SourceFile.RelativePath will give
471-
// the full path to that source file.
472-
func sourceRoot(ctx *types.Context, lib *libraries.Library) *paths.Path {
473-
if lib == nil {
474-
return ctx.SketchBuildPath
475-
}
476-
return lib.SourceDir
456+
return SourceFile{SourceRoot: sourceRoot, BuildRoot: buildRoot, RelativePath: path}, nil
477457
}
478458

479-
func (f *SourceFile) SourcePath(ctx *types.Context) *paths.Path {
480-
return sourceRoot(ctx, f.Library).JoinPath(f.RelativePath)
459+
func (f *SourceFile) SourcePath() *paths.Path {
460+
return f.SourceRoot.JoinPath(f.RelativePath)
481461
}
482462

483-
func (f *SourceFile) ObjectPath(ctx *types.Context) *paths.Path {
484-
return buildRoot(ctx, f.Library).Join(f.RelativePath.String() + ".o")
463+
func (f *SourceFile) ObjectPath() *paths.Path {
464+
return f.BuildRoot.Join(f.RelativePath.String() + ".o")
485465
}
486466

487-
func (f *SourceFile) DepfilePath(ctx *types.Context) *paths.Path {
488-
return buildRoot(ctx, f.Library).Join(f.RelativePath.String() + ".d")
467+
func (f *SourceFile) DepfilePath() *paths.Path {
468+
return f.BuildRoot.Join(f.RelativePath.String() + ".d")
489469
}
490470

491471
type UniqueSourceFileQueue struct {
@@ -515,7 +495,9 @@ func (q *UniqueSourceFileQueue) Empty() bool {
515495

516496
func (q *UniqueSourceFileQueue) Contains(target SourceFile) bool {
517497
for _, elem := range q.queue {
518-
if elem.Library == target.Library && elem.RelativePath.EqualsTo(target.RelativePath) {
498+
if elem.BuildRoot.EqualsTo(target.BuildRoot) &&
499+
elem.SourceRoot.EqualsTo(target.SourceRoot) &&
500+
elem.RelativePath.EqualsTo(target.RelativePath) {
519501
return true
520502
}
521503
}

0 commit comments

Comments
 (0)