Skip to content

Commit 537227e

Browse files
committed
Dramatically simplified SourceFile object
Removed dependency from types.Context
1 parent e3fbe9a commit 537227e

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
@@ -309,10 +309,10 @@ func writeCache(cache *includeCache, path *paths.Path) error {
309309

310310
func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFileQueue *types.UniqueSourceFileQueue) error {
311311
sourceFile := sourceFileQueue.Pop()
312-
sourcePath := sourceFile.SourcePath(ctx)
312+
sourcePath := sourceFile.SourcePath()
313313
targetFilePath := paths.NullPath()
314-
depPath := sourceFile.DepfilePath(ctx)
315-
objPath := sourceFile.ObjectPath(ctx)
314+
depPath := sourceFile.DepfilePath()
315+
objPath := sourceFile.ObjectPath()
316316

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

338338
includes := ctx.IncludeFolders
339-
if library, ok := sourceFile.Origin.(*libraries.Library); ok && library.UtilityDir != nil {
339+
if library := sourceFile.Library; library != nil && library.UtilityDir != nil {
340340
includes = append(includes, library.UtilityDir)
341341
}
342342

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

legacy/builder/types/types.go

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

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

3345
func (f *SourceFile) Equals(g *SourceFile) bool {
34-
return f.Origin == g.Origin &&
35-
f.RelativePath.EqualsTo(g.RelativePath)
46+
return f.Library == g.Library &&
47+
f.RelativePath.EqualsTo(g.RelativePath) &&
48+
f.buildRoot.EqualsTo(g.buildRoot) &&
49+
f.sourceRoot.EqualsTo(g.sourceRoot)
3650
}
3751

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

52-
// Return the build root for the given origin, where build products will
53-
// be placed. Any directories inside SourceFile.RelativePath will be
54-
// appended here.
55-
func buildRoot(ctx *Context, origin interface{}) *paths.Path {
5658
switch o := origin.(type) {
5759
case *sketch.Sketch:
58-
return ctx.SketchBuildPath
60+
res.buildRoot = ctx.SketchBuildPath
61+
res.sourceRoot = ctx.SketchBuildPath
5962
case *libraries.Library:
60-
return ctx.LibrariesBuildPath.Join(o.DirName)
63+
res.buildRoot = ctx.LibrariesBuildPath.Join(o.DirName)
64+
res.sourceRoot = o.SourceDir
65+
res.Library = o
6166
default:
6267
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
6368
}
64-
}
6569

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

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

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

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

9293
type LibraryResolutionResult struct {

0 commit comments

Comments
 (0)