|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2024 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | + |
| 16 | +package detector |
| 17 | + |
| 18 | +import "github.com/arduino/go-paths-helper" |
| 19 | + |
| 20 | +type sourceFile struct { |
| 21 | + // Path to the source file within the sketch/library root folder |
| 22 | + relativePath *paths.Path |
| 23 | + |
| 24 | + // ExtraIncludePath contains an extra include path that must be |
| 25 | + // used to compile this source file. |
| 26 | + // This is mainly used for source files that comes from old-style libraries |
| 27 | + // (Arduino IDE <1.5) requiring an extra include path to the "utility" folder. |
| 28 | + extraIncludePath *paths.Path |
| 29 | + |
| 30 | + // The source root for the given origin, where its source files |
| 31 | + // can be found. Prepending this to SourceFile.RelativePath will give |
| 32 | + // the full path to that source file. |
| 33 | + sourceRoot *paths.Path |
| 34 | + |
| 35 | + // The build root for the given origin, where build products will |
| 36 | + // be placed. Any directories inside SourceFile.RelativePath will be |
| 37 | + // appended here. |
| 38 | + buildRoot *paths.Path |
| 39 | +} |
| 40 | + |
| 41 | +// Equals fixdoc |
| 42 | +func (f *sourceFile) Equals(g *sourceFile) bool { |
| 43 | + return f.relativePath.EqualsTo(g.relativePath) && |
| 44 | + f.buildRoot.EqualsTo(g.buildRoot) && |
| 45 | + f.sourceRoot.EqualsTo(g.sourceRoot) |
| 46 | +} |
| 47 | + |
| 48 | +// makeSourceFile create a sourceFile object for the given source file path. |
| 49 | +// The given sourceFilePath can be absolute, or relative within the sourceRoot root folder. |
| 50 | +func makeSourceFile(sourceRoot, buildRoot, sourceFilePath *paths.Path, extraIncludePath ...*paths.Path) (*sourceFile, error) { |
| 51 | + res := &sourceFile{ |
| 52 | + buildRoot: buildRoot, |
| 53 | + sourceRoot: sourceRoot, |
| 54 | + } |
| 55 | + |
| 56 | + if len(extraIncludePath) > 1 { |
| 57 | + panic("only one extra include path allowed") |
| 58 | + } |
| 59 | + if len(extraIncludePath) > 0 { |
| 60 | + res.extraIncludePath = extraIncludePath[0] |
| 61 | + } |
| 62 | + |
| 63 | + if sourceFilePath.IsAbs() { |
| 64 | + var err error |
| 65 | + sourceFilePath, err = res.sourceRoot.RelTo(sourceFilePath) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + } |
| 70 | + res.relativePath = sourceFilePath |
| 71 | + return res, nil |
| 72 | +} |
| 73 | + |
| 74 | +// ExtraIncludePath returns the extra include path required to build the source file. |
| 75 | +func (f *sourceFile) ExtraIncludePath() *paths.Path { |
| 76 | + return f.extraIncludePath |
| 77 | +} |
| 78 | + |
| 79 | +// SourcePath return the full path to the source file. |
| 80 | +func (f *sourceFile) SourcePath() *paths.Path { |
| 81 | + return f.sourceRoot.JoinPath(f.relativePath) |
| 82 | +} |
| 83 | + |
| 84 | +// ObjectPath return the full path to the object file. |
| 85 | +func (f *sourceFile) ObjectPath() *paths.Path { |
| 86 | + return f.buildRoot.Join(f.relativePath.String() + ".o") |
| 87 | +} |
| 88 | + |
| 89 | +// DepfilePath return the full path to the dependency file. |
| 90 | +func (f *sourceFile) DepfilePath() *paths.Path { |
| 91 | + return f.buildRoot.Join(f.relativePath.String() + ".d") |
| 92 | +} |
0 commit comments