Skip to content

Commit c062430

Browse files
Add SourceFile struct
This struct contains the path of a source file, together with the library or sketch it is contained in. This allows the SourceFile methods to generate the full paths to the source file, object file and dependency file, making it easier to pass around source files. This commit only adds the struct. Next commits will start to use this struct in the include detection, where it fills an immediate need, later it will be used in more places as well. Signed-off-by: Matthijs Kooijman <matthijs@stdin.nl>
1 parent c4cd959 commit c062430

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/arduino.cc/builder/types/types.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,75 @@ package types
3131

3232
import (
3333
"os"
34+
"fmt"
3435
"path/filepath"
3536
"strconv"
3637

3738
"arduino.cc/builder/constants"
3839
"arduino.cc/properties"
3940
)
4041

42+
type SourceFile struct {
43+
// Sketch or Library pointer that this source file lives in
44+
Origin interface{}
45+
// Path to the source file within the sketch/library root folder
46+
RelativePath string
47+
}
48+
49+
// Create a SourceFile containing the given source file path within the
50+
// given origin. The given path can be absolute, or relative within the
51+
// origin's root source folder
52+
func MakeSourceFile(ctx *Context, origin interface{}, path string) (SourceFile, error) {
53+
if filepath.IsAbs(path) {
54+
var err error
55+
path, err = filepath.Rel(sourceRoot(ctx, origin), path)
56+
if err != nil {
57+
return SourceFile{}, err
58+
}
59+
}
60+
return SourceFile{Origin: origin, RelativePath: path}, nil
61+
}
62+
63+
// Return the build root for the given origin, where build products will
64+
// be placed. Any directories inside SourceFile.RelativePath will be
65+
// appended here.
66+
func buildRoot(ctx *Context, origin interface{}) string {
67+
switch o := origin.(type) {
68+
case *Sketch:
69+
return ctx.SketchBuildPath
70+
case *Library:
71+
return filepath.Join(ctx.LibrariesBuildPath, o.Name)
72+
default:
73+
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
74+
}
75+
}
76+
77+
// Return the source root for the given origin, where its source files
78+
// can be found. Prepending this to SourceFile.RelativePath will give
79+
// the full path to that source file.
80+
func sourceRoot(ctx *Context, origin interface{}) string {
81+
switch o := origin.(type) {
82+
case *Sketch:
83+
return ctx.SketchBuildPath
84+
case *Library:
85+
return o.SrcFolder
86+
default:
87+
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
88+
}
89+
}
90+
91+
func (f *SourceFile) SourcePath(ctx *Context) string {
92+
return filepath.Join(sourceRoot(ctx, f.Origin), f.RelativePath)
93+
}
94+
95+
func (f *SourceFile) ObjectPath(ctx *Context) string {
96+
return filepath.Join(buildRoot(ctx, f.Origin), f.RelativePath + ".o")
97+
}
98+
99+
func (f *SourceFile) DepfilePath(ctx *Context) string {
100+
return filepath.Join(buildRoot(ctx, f.Origin), f.RelativePath + ".d")
101+
}
102+
41103
type SketchFile struct {
42104
Name string
43105
Source string

0 commit comments

Comments
 (0)