|
16 | 16 | package detector
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "fmt" |
19 | 20 | "slices"
|
20 | 21 |
|
21 | 22 | "github.com/arduino/go-paths-helper"
|
22 | 23 | )
|
23 | 24 |
|
24 | 25 | type sourceFile struct {
|
25 |
| - // Path to the source file within the sketch/library root folder |
26 |
| - relativePath *paths.Path |
| 26 | + // SourcePath is the path to the source file |
| 27 | + SourcePath *paths.Path `json:"source_path"` |
| 28 | + |
| 29 | + // ObjectPath is the path to the object file that will be generated |
| 30 | + ObjectPath *paths.Path `json:"object_path"` |
| 31 | + |
| 32 | + // DepfilePath is the path to the dependency file that will be generated |
| 33 | + DepfilePath *paths.Path `json:"depfile_path"` |
27 | 34 |
|
28 | 35 | // ExtraIncludePath contains an extra include path that must be
|
29 | 36 | // used to compile this source file.
|
30 | 37 | // This is mainly used for source files that comes from old-style libraries
|
31 | 38 | // (Arduino IDE <1.5) requiring an extra include path to the "utility" folder.
|
32 |
| - extraIncludePath *paths.Path |
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 |
| 39 | + ExtraIncludePath *paths.Path `json:"extra_include_path,omitempty"` |
| 40 | +} |
38 | 41 |
|
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 |
| 42 | +func (f *sourceFile) String() string { |
| 43 | + return fmt.Sprintf("SourcePath:%s SourceRoot:%s BuildRoot:%s ExtraInclude:%s", |
| 44 | + f.SourcePath, f.ObjectPath, f.DepfilePath, f.ExtraIncludePath) |
43 | 45 | }
|
44 | 46 |
|
45 |
| -// Equals fixdoc |
| 47 | +// Equals checks if a sourceFile is equal to another. |
46 | 48 | func (f *sourceFile) Equals(g *sourceFile) bool {
|
47 |
| - return f.relativePath.EqualsTo(g.relativePath) && |
48 |
| - f.buildRoot.EqualsTo(g.buildRoot) && |
49 |
| - f.sourceRoot.EqualsTo(g.sourceRoot) |
| 49 | + return f.SourcePath.EqualsTo(g.SourcePath) && |
| 50 | + f.ObjectPath.EqualsTo(g.ObjectPath) && |
| 51 | + f.DepfilePath.EqualsTo(g.DepfilePath) && |
| 52 | + ((f.ExtraIncludePath == nil && g.ExtraIncludePath == nil) || |
| 53 | + (f.ExtraIncludePath != nil && g.ExtraIncludePath != nil && f.ExtraIncludePath.EqualsTo(g.ExtraIncludePath))) |
50 | 54 | }
|
51 | 55 |
|
52 | 56 | // makeSourceFile create a sourceFile object for the given source file path.
|
53 | 57 | // The given sourceFilePath can be absolute, or relative within the sourceRoot root folder.
|
54 |
| -func makeSourceFile(sourceRoot, buildRoot, sourceFilePath *paths.Path, extraIncludePath ...*paths.Path) (*sourceFile, error) { |
55 |
| - res := &sourceFile{ |
56 |
| - buildRoot: buildRoot, |
57 |
| - sourceRoot: sourceRoot, |
58 |
| - } |
59 |
| - |
60 |
| - if len(extraIncludePath) > 1 { |
| 58 | +func makeSourceFile(sourceRoot, buildRoot, sourceFilePath *paths.Path, extraIncludePaths ...*paths.Path) (*sourceFile, error) { |
| 59 | + if len(extraIncludePaths) > 1 { |
61 | 60 | panic("only one extra include path allowed")
|
62 | 61 | }
|
63 |
| - if len(extraIncludePath) > 0 { |
64 |
| - res.extraIncludePath = extraIncludePath[0] |
| 62 | + var extraIncludePath *paths.Path |
| 63 | + if len(extraIncludePaths) > 0 { |
| 64 | + extraIncludePath = extraIncludePaths[0] |
65 | 65 | }
|
66 | 66 |
|
67 | 67 | if sourceFilePath.IsAbs() {
|
68 | 68 | var err error
|
69 |
| - sourceFilePath, err = res.sourceRoot.RelTo(sourceFilePath) |
| 69 | + sourceFilePath, err = sourceRoot.RelTo(sourceFilePath) |
70 | 70 | if err != nil {
|
71 | 71 | return nil, err
|
72 | 72 | }
|
73 | 73 | }
|
74 |
| - res.relativePath = sourceFilePath |
| 74 | + res := &sourceFile{ |
| 75 | + SourcePath: sourceRoot.JoinPath(sourceFilePath), |
| 76 | + ObjectPath: buildRoot.Join(sourceFilePath.String() + ".o"), |
| 77 | + DepfilePath: buildRoot.Join(sourceFilePath.String() + ".d"), |
| 78 | + ExtraIncludePath: extraIncludePath, |
| 79 | + } |
75 | 80 | return res, nil
|
76 | 81 | }
|
77 | 82 |
|
78 |
| -// ExtraIncludePath returns the extra include path required to build the source file. |
79 |
| -func (f *sourceFile) ExtraIncludePath() *paths.Path { |
80 |
| - return f.extraIncludePath |
81 |
| -} |
82 |
| - |
83 |
| -// SourcePath return the full path to the source file. |
84 |
| -func (f *sourceFile) SourcePath() *paths.Path { |
85 |
| - return f.sourceRoot.JoinPath(f.relativePath) |
86 |
| -} |
87 |
| - |
88 |
| -// ObjectPath return the full path to the object file. |
89 |
| -func (f *sourceFile) ObjectPath() *paths.Path { |
90 |
| - return f.buildRoot.Join(f.relativePath.String() + ".o") |
91 |
| -} |
92 |
| - |
93 |
| -// DepfilePath return the full path to the dependency file. |
94 |
| -func (f *sourceFile) DepfilePath() *paths.Path { |
95 |
| - return f.buildRoot.Join(f.relativePath.String() + ".d") |
96 |
| -} |
97 |
| - |
98 | 83 | // uniqueSourceFileQueue is a queue of source files that does not allow duplicates.
|
99 | 84 | type uniqueSourceFileQueue []*sourceFile
|
100 | 85 |
|
|
0 commit comments