Skip to content

Commit 1da2a07

Browse files
committed
Removed reference to Library in SourceFile struct
1 parent 5acd942 commit 1da2a07

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
@@ -98,7 +98,6 @@ import (
9898
"os/exec"
9999
"time"
100100

101-
"github.com/arduino/arduino-cli/arduino/libraries"
102101
"github.com/arduino/arduino-cli/arduino/sketch"
103102
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
104103
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -158,17 +157,17 @@ func (f *CppIncludesFinder) DetectLibraries() error {
158157
f.appendIncludeFolder(nil, "", f.ctx.BuildProperties.GetPath("build.variant.path"))
159158
}
160159

161-
mergedfile, err := MakeSourceFile(f.ctx, nil, paths.New(f.sketch.MainFile.Base()+".cpp"))
160+
mergedfile, err := MakeSourceFile(f.ctx.SketchBuildPath, f.ctx.SketchBuildPath, paths.New(f.sketch.MainFile.Base()+".cpp"))
162161
if err != nil {
163162
return errors.WithStack(err)
164163
}
165164
f.log.Debugf("Queueing merged sketch: %s", mergedfile)
166165
f.queue.Push(mergedfile)
167166

168-
f.queueSourceFilesFromFolder(nil, f.ctx.SketchBuildPath, false /* recurse */)
167+
f.queueSourceFilesFromFolder(f.ctx.SketchBuildPath, f.ctx.SketchBuildPath, false /* recurse */)
169168
srcSubfolderPath := f.ctx.SketchBuildPath.Join("src")
170169
if srcSubfolderPath.IsDir() {
171-
f.queueSourceFilesFromFolder(nil, srcSubfolderPath, true /* recurse */)
170+
f.queueSourceFilesFromFolder(srcSubfolderPath, f.ctx.SketchBuildPath, true /* recurse */)
172171
}
173172

174173
for !f.queue.Empty() {
@@ -320,10 +319,10 @@ func (cache *includeCache) WriteToFile() error {
320319
}
321320

322321
func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile SourceFile) error {
323-
sourcePath := sourceFile.SourcePath(f.ctx)
322+
sourcePath := sourceFile.SourcePath()
324323
targetFilePath := paths.NullPath()
325-
depPath := sourceFile.DepfilePath(f.ctx)
326-
objPath := sourceFile.ObjectPath(f.ctx)
324+
depPath := sourceFile.DepfilePath()
325+
objPath := sourceFile.ObjectPath()
327326

328327
// TODO: This should perhaps also compare against the
329328
// include.cache file timestamp. Now, it only checks if the file
@@ -344,11 +343,11 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile SourceFile) error {
344343

345344
first := true
346345
for {
347-
var include string
348346
f.cache.ExpectFile(sourcePath)
349347

350348
var preprocErr error
351349
var preprocStderr []byte
350+
var include string
352351
if unchanged && f.cache.valid {
353352
include = f.cache.Next().Include
354353
if first && f.ctx.Verbose {
@@ -410,6 +409,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile SourceFile) error {
410409
f.ctx.IncludeFolders = append(f.ctx.IncludeFolders, library.UtilityDir)
411410
}
412411
sourceDirs := library.SourceDirs()
412+
buildDir := f.ctx.LibrariesBuildPath.Join(library.Name)
413413
for _, sourceDir := range sourceDirs {
414414
if library.Precompiled && library.PrecompiledWithSources {
415415
// Fully precompiled libraries should have no dependencies
@@ -418,24 +418,24 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile SourceFile) error {
418418
f.ctx.Info(tr("Skipping dependencies detection for precompiled library %[1]s", library.Name))
419419
}
420420
} else {
421-
f.queueSourceFilesFromFolder(library, sourceDir.Dir, sourceDir.Recurse)
421+
f.queueSourceFilesFromFolder(buildDir, sourceDir.Dir, sourceDir.Recurse)
422422
}
423423
}
424424
first = false
425425
}
426426
}
427427

428-
func (f *CppIncludesFinder) queueSourceFilesFromFolder(lib *libraries.Library, folder *paths.Path, recurse bool) error {
428+
func (f *CppIncludesFinder) queueSourceFilesFromFolder(srcDir, buildDir *paths.Path, recurse bool) error {
429429
extensions := func(ext string) bool { return ADDITIONAL_FILE_VALID_EXTENSIONS_NO_HEADERS[ext] }
430-
f.log.Debugf(" Queueing source files from %s (recurse %v)", folder, recurse)
430+
f.log.Debugf(" Queueing source files from %s (recurse %v)", srcDir, recurse)
431431
filePaths := []string{}
432-
err := utils.FindFilesInFolder(&filePaths, folder.String(), extensions, recurse)
432+
err := utils.FindFilesInFolder(&filePaths, srcDir.String(), extensions, recurse)
433433
if err != nil {
434434
return errors.WithStack(err)
435435
}
436436

437437
for _, filePath := range filePaths {
438-
sourceFile, err := MakeSourceFile(f.ctx, lib, paths.New(filePath))
438+
sourceFile, err := MakeSourceFile(srcDir, buildDir, paths.New(filePath))
439439
if err != nil {
440440
return errors.WithStack(err)
441441
}
@@ -447,64 +447,44 @@ func (f *CppIncludesFinder) queueSourceFilesFromFolder(lib *libraries.Library, f
447447
}
448448

449449
type SourceFile struct {
450-
// Library pointer that this source file lives in or nil if not part of a library
451-
Library *libraries.Library
450+
// SourceRoot is the path to the source code root directory
451+
SourceRoot *paths.Path
452+
453+
// BuildRoot is the path to the build root directory
454+
BuildRoot *paths.Path
452455

453456
// Path to the source file within the sketch/library root folder
454457
RelativePath *paths.Path
455-
456-
ctx *types.Context
457458
}
458459

459460
func (f SourceFile) String() string {
460461
return fmt.Sprintf("Root: %s - Path: %s - BuildPath: %s",
461-
sourceRoot(f.ctx, f.Library), f.RelativePath, buildRoot(f.ctx, f.Library))
462+
f.SourceRoot, f.RelativePath, f.BuildRoot)
462463
}
463464

464-
// Create a SourceFile containing the given source file path within the
465-
// given origin. The given path can be absolute, or relative within the
466-
// origin's root source folder
467-
func MakeSourceFile(ctx *types.Context, lib *libraries.Library, path *paths.Path) (SourceFile, error) {
465+
// MakeSourceFile creates a SourceFile containing the given source file path
466+
// within the given sourceRoot.
467+
func MakeSourceFile(sourceRoot, buildRoot, path *paths.Path) (SourceFile, error) {
468468
if path.IsAbs() {
469-
var err error
470-
path, err = sourceRoot(ctx, lib).RelTo(path)
471-
if err != nil {
469+
if relPath, err := sourceRoot.RelTo(path); err == nil {
470+
path = relPath
471+
} else {
472472
return SourceFile{}, err
473473
}
474474
}
475-
return SourceFile{Library: lib, RelativePath: path, ctx: ctx}, nil
476-
}
477-
478-
// Return the build root for the given origin, where build products will
479-
// be placed. Any directories inside SourceFile.RelativePath will be
480-
// appended here.
481-
func buildRoot(ctx *types.Context, lib *libraries.Library) *paths.Path {
482-
if lib == nil {
483-
return ctx.SketchBuildPath
484-
}
485-
return ctx.LibrariesBuildPath.Join(lib.Name)
486-
}
487-
488-
// Return the source root for the given origin, where its source files
489-
// can be found. Prepending this to SourceFile.RelativePath will give
490-
// the full path to that source file.
491-
func sourceRoot(ctx *types.Context, lib *libraries.Library) *paths.Path {
492-
if lib == nil {
493-
return ctx.SketchBuildPath
494-
}
495-
return lib.SourceDir
475+
return SourceFile{SourceRoot: sourceRoot, BuildRoot: buildRoot, RelativePath: path}, nil
496476
}
497477

498-
func (f *SourceFile) SourcePath(ctx *types.Context) *paths.Path {
499-
return sourceRoot(ctx, f.Library).JoinPath(f.RelativePath)
478+
func (f *SourceFile) SourcePath() *paths.Path {
479+
return f.SourceRoot.JoinPath(f.RelativePath)
500480
}
501481

502-
func (f *SourceFile) ObjectPath(ctx *types.Context) *paths.Path {
503-
return buildRoot(ctx, f.Library).Join(f.RelativePath.String() + ".o")
482+
func (f *SourceFile) ObjectPath() *paths.Path {
483+
return f.BuildRoot.Join(f.RelativePath.String() + ".o")
504484
}
505485

506-
func (f *SourceFile) DepfilePath(ctx *types.Context) *paths.Path {
507-
return buildRoot(ctx, f.Library).Join(f.RelativePath.String() + ".d")
486+
func (f *SourceFile) DepfilePath() *paths.Path {
487+
return f.BuildRoot.Join(f.RelativePath.String() + ".d")
508488
}
509489

510490
type UniqueSourceFileQueue struct {
@@ -534,7 +514,9 @@ func (q *UniqueSourceFileQueue) Empty() bool {
534514

535515
func (q *UniqueSourceFileQueue) Contains(target SourceFile) bool {
536516
for _, elem := range q.queue {
537-
if elem.Library == target.Library && elem.RelativePath.EqualsTo(target.RelativePath) {
517+
if elem.BuildRoot.EqualsTo(target.BuildRoot) &&
518+
elem.SourceRoot.EqualsTo(target.SourceRoot) &&
519+
elem.RelativePath.EqualsTo(target.RelativePath) {
538520
return true
539521
}
540522
}

0 commit comments

Comments
 (0)