Skip to content

Commit 8b6dba4

Browse files
committed
Dramatically simplified SourceFile object
Removed dependency from types.Context
1 parent 6b75221 commit 8b6dba4

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

legacy/builder/container_find_includes.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,10 @@ func writeCache(cache *includeCache, path *paths.Path) error {
286286
}
287287

288288
func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile *types.SourceFile) error {
289-
sourcePath := sourceFile.SourcePath(ctx)
289+
sourcePath := sourceFile.SourcePath()
290290
targetFilePath := paths.NullPath()
291-
depPath := sourceFile.DepfilePath(ctx)
292-
objPath := sourceFile.ObjectPath(ctx)
291+
depPath := sourceFile.DepfilePath()
292+
objPath := sourceFile.ObjectPath()
293293

294294
// TODO: This should perhaps also compare against the
295295
// include.cache file timestamp. Now, it only checks if the file
@@ -313,11 +313,11 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile *
313313
cache.ExpectFile(sourcePath)
314314

315315
includes := ctx.IncludeFolders
316-
if library, ok := sourceFile.Origin.(*libraries.Library); ok && library.UtilityDir != nil {
316+
if library := sourceFile.Library; library != nil && library.UtilityDir != nil {
317317
includes = append(includes, library.UtilityDir)
318318
}
319319

320-
if library, ok := sourceFile.Origin.(*libraries.Library); ok {
320+
if library := sourceFile.Library; library != nil {
321321
if library.Precompiled && library.PrecompiledWithSources {
322322
// Fully precompiled libraries should have no dependencies
323323
// to avoid ABI breakage

legacy/builder/types/types.go

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,69 +25,70 @@ import (
2525
)
2626

2727
type SourceFile struct {
28-
// Sketch or Library pointer that this source file lives in
29-
Origin interface{}
3028
// Path to the source file within the sketch/library root folder
3129
RelativePath *paths.Path
30+
31+
// Set to the Library object of origin if this source file comes
32+
// from a library
33+
Library *libraries.Library
34+
35+
// The source root for the given origin, where its source files
36+
// can be found. Prepending this to SourceFile.RelativePath will give
37+
// the full path to that source file.
38+
sourceRoot *paths.Path
39+
40+
// The build root for the given origin, where build products will
41+
// be placed. Any directories inside SourceFile.RelativePath will be
42+
// appended here.
43+
buildRoot *paths.Path
3244
}
3345

3446
func (f *SourceFile) Equals(g *SourceFile) bool {
35-
return f.Origin == g.Origin &&
36-
f.RelativePath.EqualsTo(g.RelativePath)
47+
return f.Library == g.Library &&
48+
f.RelativePath.EqualsTo(g.RelativePath) &&
49+
f.buildRoot.EqualsTo(g.buildRoot) &&
50+
f.sourceRoot.EqualsTo(g.sourceRoot)
3751
}
3852

3953
// Create a SourceFile containing the given source file path within the
4054
// given origin. The given path can be absolute, or relative within the
4155
// origin's root source folder
4256
func MakeSourceFile(ctx *Context, origin interface{}, path *paths.Path) (*SourceFile, error) {
43-
if path.IsAbs() {
44-
var err error
45-
path, err = sourceRoot(ctx, origin).RelTo(path)
46-
if err != nil {
47-
return nil, err
48-
}
49-
}
50-
return &SourceFile{Origin: origin, RelativePath: path}, nil
51-
}
57+
res := &SourceFile{}
5258

53-
// Return the build root for the given origin, where build products will
54-
// be placed. Any directories inside SourceFile.RelativePath will be
55-
// appended here.
56-
func buildRoot(ctx *Context, origin interface{}) *paths.Path {
5759
switch o := origin.(type) {
5860
case *sketch.Sketch:
59-
return ctx.SketchBuildPath
61+
res.buildRoot = ctx.SketchBuildPath
62+
res.sourceRoot = ctx.SketchBuildPath
6063
case *libraries.Library:
61-
return ctx.LibrariesBuildPath.Join(o.Name)
64+
res.buildRoot = ctx.LibrariesBuildPath.Join(o.Name)
65+
res.sourceRoot = o.SourceDir
66+
res.Library = o
6267
default:
6368
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
6469
}
65-
}
6670

67-
// Return the source root for the given origin, where its source files
68-
// can be found. Prepending this to SourceFile.RelativePath will give
69-
// the full path to that source file.
70-
func sourceRoot(ctx *Context, origin interface{}) *paths.Path {
71-
switch o := origin.(type) {
72-
case *sketch.Sketch:
73-
return ctx.SketchBuildPath
74-
case *libraries.Library:
75-
return o.SourceDir
76-
default:
77-
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
71+
if path.IsAbs() {
72+
var err error
73+
path, err = res.sourceRoot.RelTo(path)
74+
if err != nil {
75+
return nil, err
76+
}
7877
}
78+
res.RelativePath = path
79+
return res, nil
7980
}
8081

81-
func (f *SourceFile) SourcePath(ctx *Context) *paths.Path {
82-
return sourceRoot(ctx, f.Origin).JoinPath(f.RelativePath)
82+
func (f *SourceFile) SourcePath() *paths.Path {
83+
return f.sourceRoot.JoinPath(f.RelativePath)
8384
}
8485

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

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

9394
type SketchFile struct {

0 commit comments

Comments
 (0)