Skip to content

Commit 05ff01a

Browse files
committed
Replaced SourceFile.Origin field with SourceFile.Library
Previously the Origin interface{} field was used to store the "origin" of the source file (being sketch or library). Actually Origin is really needed only to reconstruct BuildPath and RootPath when the origin is a Library, in all the other cases we can reconstruct BuildPath and RootPath looking inside Context. With the above in mind, this commit removes the non-idiomatic interface{} by replacing it with a Library, so now the meaning of the SourceFile.Library field is "the library where the source file is contained or nil if not part of a library".
1 parent de128a9 commit 05ff01a

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

legacy/builder/container_find_includes.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,16 @@ func (f *CppIncludesFinder) DetectLibraries() error {
144144
f.appendIncludeFolder(nil, "", f.ctx.BuildProperties.GetPath("build.variant.path"))
145145
}
146146

147-
mergedfile, err := MakeSourceFile(f.ctx, f.sketch, paths.New(f.sketch.MainFile.Name.Base()+".cpp"))
147+
mergedfile, err := MakeSourceFile(f.ctx, nil, paths.New(f.sketch.MainFile.Name.Base()+".cpp"))
148148
if err != nil {
149149
return errors.WithStack(err)
150150
}
151151
f.queue.Push(mergedfile)
152152

153-
f.queueSourceFilesFromFolder(f.sketch, f.ctx.SketchBuildPath, false /* recurse */)
153+
f.queueSourceFilesFromFolder(nil, f.ctx.SketchBuildPath, false /* recurse */)
154154
srcSubfolderPath := f.ctx.SketchBuildPath.Join("src")
155155
if srcSubfolderPath.IsDir() {
156-
f.queueSourceFilesFromFolder(f.sketch, srcSubfolderPath, true /* recurse */)
156+
f.queueSourceFilesFromFolder(nil, srcSubfolderPath, true /* recurse */)
157157
}
158158

159159
for !f.queue.Empty() {
@@ -332,8 +332,8 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile SourceFile) error {
332332
f.cache.ExpectFile(sourcePath)
333333

334334
includes := f.ctx.IncludeFolders
335-
if library, ok := sourceFile.Origin.(*libraries.Library); ok && library.UtilityDir != nil {
336-
includes = append(includes, library.UtilityDir)
335+
if sourceFile.Library != nil && sourceFile.Library.UtilityDir != nil {
336+
includes = append(includes, sourceFile.Library.UtilityDir)
337337
}
338338
var preproc_err error
339339
var preproc_stderr []byte
@@ -401,7 +401,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile SourceFile) error {
401401
}
402402
}
403403

404-
func (f *CppIncludesFinder) queueSourceFilesFromFolder(origin interface{}, folder *paths.Path, recurse bool) error {
404+
func (f *CppIncludesFinder) queueSourceFilesFromFolder(lib *libraries.Library, folder *paths.Path, recurse bool) error {
405405
extensions := func(ext string) bool { return ADDITIONAL_FILE_VALID_EXTENSIONS_NO_HEADERS[ext] }
406406

407407
filePaths := []string{}
@@ -411,7 +411,7 @@ func (f *CppIncludesFinder) queueSourceFilesFromFolder(origin interface{}, folde
411411
}
412412

413413
for _, filePath := range filePaths {
414-
sourceFile, err := MakeSourceFile(f.ctx, origin, paths.New(filePath))
414+
sourceFile, err := MakeSourceFile(f.ctx, lib, paths.New(filePath))
415415
if err != nil {
416416
return errors.WithStack(err)
417417
}
@@ -422,64 +422,57 @@ func (f *CppIncludesFinder) queueSourceFilesFromFolder(origin interface{}, folde
422422
}
423423

424424
type SourceFile struct {
425-
// Sketch or Library pointer that this source file lives in
426-
Origin interface{}
425+
// Library pointer that this source file lives in or nil if not part of a library
426+
Library *libraries.Library
427+
427428
// Path to the source file within the sketch/library root folder
428429
RelativePath *paths.Path
429430
}
430431

431432
// Create a SourceFile containing the given source file path within the
432433
// given origin. The given path can be absolute, or relative within the
433434
// origin's root source folder
434-
func MakeSourceFile(ctx *types.Context, origin interface{}, path *paths.Path) (SourceFile, error) {
435+
func MakeSourceFile(ctx *types.Context, lib *libraries.Library, path *paths.Path) (SourceFile, error) {
435436
if path.IsAbs() {
436437
var err error
437-
path, err = sourceRoot(ctx, origin).RelTo(path)
438+
path, err = sourceRoot(ctx, lib).RelTo(path)
438439
if err != nil {
439440
return SourceFile{}, err
440441
}
441442
}
442-
return SourceFile{Origin: origin, RelativePath: path}, nil
443+
return SourceFile{Library: lib, RelativePath: path}, nil
443444
}
444445

445446
// Return the build root for the given origin, where build products will
446447
// be placed. Any directories inside SourceFile.RelativePath will be
447448
// appended here.
448-
func buildRoot(ctx *types.Context, origin interface{}) *paths.Path {
449-
switch o := origin.(type) {
450-
case *types.Sketch:
449+
func buildRoot(ctx *types.Context, lib *libraries.Library) *paths.Path {
450+
if lib == nil {
451451
return ctx.SketchBuildPath
452-
case *libraries.Library:
453-
return ctx.LibrariesBuildPath.Join(o.Name)
454-
default:
455-
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
456452
}
453+
return ctx.LibrariesBuildPath.Join(lib.Name)
457454
}
458455

459456
// Return the source root for the given origin, where its source files
460457
// can be found. Prepending this to SourceFile.RelativePath will give
461458
// the full path to that source file.
462-
func sourceRoot(ctx *types.Context, origin interface{}) *paths.Path {
463-
switch o := origin.(type) {
464-
case *types.Sketch:
459+
func sourceRoot(ctx *types.Context, lib *libraries.Library) *paths.Path {
460+
if lib == nil {
465461
return ctx.SketchBuildPath
466-
case *libraries.Library:
467-
return o.SourceDir
468-
default:
469-
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
470462
}
463+
return lib.SourceDir
471464
}
472465

473466
func (f *SourceFile) SourcePath(ctx *types.Context) *paths.Path {
474-
return sourceRoot(ctx, f.Origin).JoinPath(f.RelativePath)
467+
return sourceRoot(ctx, f.Library).JoinPath(f.RelativePath)
475468
}
476469

477470
func (f *SourceFile) ObjectPath(ctx *types.Context) *paths.Path {
478-
return buildRoot(ctx, f.Origin).Join(f.RelativePath.String() + ".o")
471+
return buildRoot(ctx, f.Library).Join(f.RelativePath.String() + ".o")
479472
}
480473

481474
func (f *SourceFile) DepfilePath(ctx *types.Context) *paths.Path {
482-
return buildRoot(ctx, f.Origin).Join(f.RelativePath.String() + ".d")
475+
return buildRoot(ctx, f.Library).Join(f.RelativePath.String() + ".d")
483476
}
484477

485478
type UniqueSourceFileQueue struct {
@@ -509,7 +502,7 @@ func (q *UniqueSourceFileQueue) Empty() bool {
509502

510503
func (q *UniqueSourceFileQueue) Contains(target SourceFile) bool {
511504
for _, elem := range q.queue {
512-
if elem.Origin == target.Origin && elem.RelativePath.EqualsTo(target.RelativePath) {
505+
if elem.Library == target.Library && elem.RelativePath.EqualsTo(target.RelativePath) {
513506
return true
514507
}
515508
}

0 commit comments

Comments
 (0)