-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathcontext.go
298 lines (256 loc) · 9.58 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package types
import (
"fmt"
"io"
"os"
"strings"
"sync"
"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/constants"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
paths "github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
)
type ProgressStruct struct {
Progress float32
StepAmount float32
Parent *ProgressStruct
}
func (p *ProgressStruct) AddSubSteps(steps int) {
p.Parent = &ProgressStruct{
Progress: p.Progress,
StepAmount: p.StepAmount,
Parent: p.Parent,
}
if p.StepAmount == 0.0 {
p.StepAmount = 100.0
}
p.StepAmount /= float32(steps)
}
func (p *ProgressStruct) RemoveSubSteps() {
p.Progress = p.Parent.Progress
p.StepAmount = p.Parent.StepAmount
p.Parent = p.Parent.Parent
}
func (p *ProgressStruct) CompleteStep() {
p.Progress += p.StepAmount
}
// Context structure
type Context struct {
// Build options
HardwareDirs paths.PathList
BuiltInToolsDirs paths.PathList
BuiltInLibrariesDirs paths.PathList
OtherLibrariesDirs paths.PathList
LibraryDirs paths.PathList // List of paths pointing to individual library root folders
SketchLocation *paths.Path // SketchLocation points to the main Sketch file
WatchedLocations paths.PathList
ArduinoAPIVersion string
FQBN *cores.FQBN
CodeCompleteAt string
Clean bool
// Build options are serialized here
BuildOptionsJson string
BuildOptionsJsonPrevious string
PackageManager *packagemanager.PackageManager
Hardware cores.Packages
AllTools []*cores.ToolRelease
RequiredTools []*cores.ToolRelease
TargetBoard *cores.Board
TargetPackage *cores.Package
TargetPlatform *cores.PlatformRelease
ActualPlatform *cores.PlatformRelease
USBVidPid string
PlatformKeyRewrites PlatforKeysRewrite
HardwareRewriteResults map[*cores.PlatformRelease][]PlatforKeyRewrite
BuildProperties *properties.Map
BuildCore string
BuildPath *paths.Path
BuildCachePath *paths.Path
SketchBuildPath *paths.Path
CoreBuildPath *paths.Path
CoreBuildCachePath *paths.Path
CoreArchiveFilePath *paths.Path
CoreObjectsFiles paths.PathList
LibrariesBuildPath *paths.Path
LibrariesObjectFiles paths.PathList
PreprocPath *paths.Path
SketchObjectFiles paths.PathList
IgnoreSketchFolderNameErrors bool
CollectedSourceFiles *UniqueSourceFileQueue
Sketch *sketch.Sketch
Source string
SourceGccMinusE string
CodeCompletions string
WarningsLevel string
// Libraries handling
LibrariesManager *librariesmanager.LibrariesManager
LibrariesResolver *librariesresolver.Cpp
ImportedLibraries libraries.List
LibrariesResolutionResults map[string]LibraryResolutionResult
IncludeFolders paths.PathList
//OutputGccMinusM string
// C++ Parsing
CTagsOutput string
CTagsTargetFile *paths.Path
CTagsOfPreprocessedSource []*CTag
LineOffset int
PrototypesSection string
PrototypesLineWhereToInsert int
Prototypes []*Prototype
// Verbosity settings
Verbose bool
DebugPreprocessor bool
// Compile optimization settings
OptimizeForDebug bool
OptimizationFlags string
// Dry run, only create progress map
Progress ProgressStruct
// Send progress events to this callback
ProgressCB commands.TaskProgressCB
// Contents of a custom build properties file (line by line)
CustomBuildProperties []string
// Reuse old tools since the backing storage didn't change
CanUseCachedTools bool
// Experimental: use arduino-preprocessor to create prototypes
UseArduinoPreprocessor bool
// Parallel processes
Jobs int
// Out and Err stream to redirect all output
Stdout io.Writer
Stderr io.Writer
stdLock sync.Mutex
// Sizer results
ExecutableSectionsSize ExecutablesFileSections
// Compilation Database to build/update
CompilationDatabase *builder.CompilationDatabase
// Set to true to skip build and produce only Compilation Database
OnlyUpdateCompilationDatabase bool
// Source code overrides (filename -> content map).
// The provided source data is used instead of reading it from disk.
// The keys of the map are paths relative to sketch folder.
SourceOverride map[string]string
// Compiler directive for transparent inclusion of global definition when present
GlobalIncludeOption string
}
// ExecutableSectionSize represents a section of the executable output file
type ExecutableSectionSize struct {
Name string
Size int
MaxSize int
}
// ExecutablesFileSections is an array of ExecutablesFileSection
type ExecutablesFileSections []ExecutableSectionSize
// ToRPCExecutableSectionSizeArray transforms this array into a []*rpc.ExecutableSectionSize
func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.ExecutableSectionSize {
res := []*rpc.ExecutableSectionSize{}
for _, section := range s {
res = append(res, &rpc.ExecutableSectionSize{
Name: section.Name,
Size: int64(section.Size),
MaxSize: int64(section.MaxSize),
})
}
return res
}
func (ctx *Context) ExtractBuildOptions() *properties.Map {
opts := properties.NewMap()
opts.Set("hardwareFolders", strings.Join(ctx.HardwareDirs.AsStrings(), ","))
opts.Set("builtInToolsFolders", strings.Join(ctx.BuiltInToolsDirs.AsStrings(), ","))
opts.Set("builtInLibrariesFolders", strings.Join(ctx.BuiltInLibrariesDirs.AsStrings(), ","))
opts.Set("otherLibrariesFolders", strings.Join(ctx.OtherLibrariesDirs.AsStrings(), ","))
opts.SetPath("sketchLocation", ctx.SketchLocation)
var additionalFilesRelative []string
if ctx.Sketch != nil {
for _, f := range ctx.Sketch.AdditionalFiles {
absPath := ctx.SketchLocation.Parent()
relPath, err := f.RelTo(absPath)
if err != nil {
continue // ignore
}
additionalFilesRelative = append(additionalFilesRelative, relPath.String())
}
}
opts.Set("fqbn", ctx.FQBN.String())
opts.Set("runtime.ide.version", ctx.ArduinoAPIVersion)
opts.Set("customBuildProperties", strings.Join(ctx.CustomBuildProperties, ","))
opts.Set("additionalFiles", strings.Join(additionalFilesRelative, ","))
opts.Set("compiler.optimization_flags", ctx.OptimizationFlags)
return opts
}
func (ctx *Context) InjectBuildOptions(opts *properties.Map) {
ctx.HardwareDirs = paths.NewPathList(strings.Split(opts.Get("hardwareFolders"), ",")...)
ctx.BuiltInToolsDirs = paths.NewPathList(strings.Split(opts.Get("builtInToolsFolders"), ",")...)
ctx.BuiltInLibrariesDirs = paths.NewPathList(strings.Split(opts.Get("builtInLibrariesFolders"), ",")...)
ctx.OtherLibrariesDirs = paths.NewPathList(strings.Split(opts.Get("otherLibrariesFolders"), ",")...)
ctx.SketchLocation = opts.GetPath("sketchLocation")
fqbn, err := cores.ParseFQBN(opts.Get("fqbn"))
if err != nil {
fmt.Fprintln(ctx.Stderr, &arduino.InvalidFQBNError{Cause: err})
}
ctx.FQBN = fqbn
ctx.ArduinoAPIVersion = opts.Get("runtime.ide.version")
ctx.CustomBuildProperties = strings.Split(opts.Get("customBuildProperties"), ",")
ctx.OptimizationFlags = opts.Get("compiler.optimization_flags")
}
func (ctx *Context) PushProgress() {
if ctx.ProgressCB != nil {
ctx.ProgressCB(&rpc.TaskProgress{Percent: ctx.Progress.Progress})
}
}
func (ctx *Context) Info(msg string) {
ctx.stdLock.Lock()
if ctx.Stdout == nil {
fmt.Fprintln(os.Stdout, msg)
} else {
fmt.Fprintln(ctx.Stdout, msg)
}
ctx.stdLock.Unlock()
}
func (ctx *Context) Warn(msg string) {
ctx.stdLock.Lock()
if ctx.Stderr == nil {
fmt.Fprintln(os.Stderr, msg)
} else {
fmt.Fprintln(ctx.Stderr, msg)
}
ctx.stdLock.Unlock()
}
func (ctx *Context) SetGlobalIncludeOption () {
if len(ctx.GlobalIncludeOption) == 0 {
// testing existence of path/to/sketch/sketch_globals.h
globalsHeaderName := ctx.BuildPath.Join("sketch").Join(ctx.Sketch.Name + "_globals.h").String()
_, err := os.Stat(globalsHeaderName);
if os.IsNotExist(err) {
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_INFO, tr("global definition file is not present") + " '" + globalsHeaderName + "'")
} else {
ctx.GlobalIncludeOption = "-include "
ctx.GlobalIncludeOption += globalsHeaderName
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_INFO, tr("Using global definition file") + " '" + globalsHeaderName + "'")
}
ctx.GlobalIncludeOption += " "
}
}