|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2019 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 modify or |
| 12 | +// otherwise use the software for commercial activities involving the Arduino |
| 13 | +// software without disclosing the source code of your own applications. To purchase |
| 14 | +// a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package types |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/arduino/arduino-cli/arduino/libraries" |
| 22 | + paths "github.com/arduino/go-paths-helper" |
| 23 | +) |
| 24 | + |
| 25 | +// SourceFile represents a source file |
| 26 | +type SourceFile struct { |
| 27 | + // Sketch or Library pointer that this source file lives in |
| 28 | + Origin interface{} |
| 29 | + // Path to the source file within the sketch/library root folder |
| 30 | + RelativePath *paths.Path |
| 31 | +} |
| 32 | + |
| 33 | +// NewSourceFile creates a SourceFile containing the given source file path within the |
| 34 | +// given origin. The given path can be absolute, or relative within the |
| 35 | +// origin's root source folder |
| 36 | +func NewSourceFile(ctx *Context, origin interface{}, path *paths.Path) (SourceFile, error) { |
| 37 | + if path.IsAbs() { |
| 38 | + var err error |
| 39 | + path, err = sourceRoot(ctx, origin).RelTo(path) |
| 40 | + if err != nil { |
| 41 | + return SourceFile{}, err |
| 42 | + } |
| 43 | + } |
| 44 | + return SourceFile{Origin: origin, RelativePath: path}, nil |
| 45 | +} |
| 46 | + |
| 47 | +// Return the build root for the given origin, where build products will |
| 48 | +// be placed. Any directories inside SourceFile.RelativePath will be |
| 49 | +// appended here. |
| 50 | +func buildRoot(ctx *Context, origin interface{}) *paths.Path { |
| 51 | + switch o := origin.(type) { |
| 52 | + case *Sketch: |
| 53 | + return ctx.SketchBuildPath |
| 54 | + case *libraries.Library: |
| 55 | + return ctx.LibrariesBuildPath.Join(o.Name) |
| 56 | + default: |
| 57 | + panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Return the source root for the given origin, where its source files |
| 62 | +// can be found. Prepending this to SourceFile.RelativePath will give |
| 63 | +// the full path to that source file. |
| 64 | +func sourceRoot(ctx *Context, origin interface{}) *paths.Path { |
| 65 | + switch o := origin.(type) { |
| 66 | + case *Sketch: |
| 67 | + return ctx.SketchBuildPath |
| 68 | + case *libraries.Library: |
| 69 | + return o.SourceDir |
| 70 | + default: |
| 71 | + panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin)) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// SourcePath returns the full path to the source file |
| 76 | +func (f *SourceFile) SourcePath(ctx *Context) *paths.Path { |
| 77 | + return sourceRoot(ctx, f.Origin).JoinPath(f.RelativePath) |
| 78 | +} |
| 79 | + |
| 80 | +// ObjectPath returns the full path to the .o file for this source |
| 81 | +func (f *SourceFile) ObjectPath(ctx *Context) *paths.Path { |
| 82 | + return buildRoot(ctx, f.Origin).Join(f.RelativePath.String() + ".o") |
| 83 | +} |
| 84 | + |
| 85 | +// DepfilePath returns the full path to the .d file for this source |
| 86 | +func (f *SourceFile) DepfilePath(ctx *Context) *paths.Path { |
| 87 | + return buildRoot(ctx, f.Origin).Join(f.RelativePath.String() + ".d") |
| 88 | +} |
| 89 | + |
| 90 | +// UniqueSourceFileQueue is a slice of SourceFile |
| 91 | +type UniqueSourceFileQueue []SourceFile |
| 92 | + |
| 93 | +func (queue UniqueSourceFileQueue) Len() int { return len(queue) } |
| 94 | +func (queue UniqueSourceFileQueue) Less(i, j int) bool { return false } |
| 95 | +func (queue UniqueSourceFileQueue) Swap(i, j int) { panic("Who called me?!?") } |
| 96 | + |
| 97 | +// Push add an item if not already present |
| 98 | +func (queue *UniqueSourceFileQueue) Push(value SourceFile) { |
| 99 | + found := false |
| 100 | + for _, elem := range *queue { |
| 101 | + if elem.Origin == value.Origin && elem.RelativePath.EqualsTo(value.RelativePath) { |
| 102 | + found = true |
| 103 | + break |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if !found { |
| 108 | + *queue = append(*queue, value) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Pop returns the first item of the slice and removes it |
| 113 | +func (queue *UniqueSourceFileQueue) Pop() SourceFile { |
| 114 | + old := *queue |
| 115 | + x := old[0] |
| 116 | + *queue = old[1:] |
| 117 | + return x |
| 118 | +} |
| 119 | + |
| 120 | +// Empty returns whether the slice is empty |
| 121 | +func (queue *UniqueSourceFileQueue) Empty() bool { |
| 122 | + return queue.Len() == 0 |
| 123 | +} |
0 commit comments