forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsketch.go
198 lines (168 loc) · 6.1 KB
/
sketch.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
// 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 builder
import (
"bytes"
"fmt"
"regexp"
"strings"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
)
var (
includesArduinoH = regexp.MustCompile(`(?m)^\s*#\s*include\s*[<\"]Arduino\.h[>\"]`)
tr = i18n.Tr
)
// QuoteCppString returns the given string as a quoted string for use with the C
// preprocessor. This adds double quotes around it and escapes any
// double quotes and backslashes in the string.
func QuoteCppString(str string) string {
str = strings.Replace(str, "\\", "\\\\", -1)
str = strings.Replace(str, "\"", "\\\"", -1)
return "\"" + str + "\""
}
// SketchSaveItemCpp saves a preprocessed .cpp sketch file on disk
func SketchSaveItemCpp(path *paths.Path, contents []byte, destPath *paths.Path) error {
sketchName := path.Base()
if err := destPath.MkdirAll(); err != nil {
return errors.Wrap(err, tr("unable to create a folder to save the sketch"))
}
destFile := destPath.Join(fmt.Sprintf("%s.cpp", sketchName))
if err := destFile.WriteFile(contents); err != nil {
return errors.Wrap(err, tr("unable to save the sketch on disk"))
}
return nil
}
// SketchMergeSources merges all the source files included in a sketch
func SketchMergeSources(sk *sketch.Sketch, overrides map[string]string) (int, string, error) {
lineOffset := 0
mergedSource := ""
getSource := func(f *paths.Path) (string, error) {
path, err := sk.FullPath.RelTo(f)
if err != nil {
return "", errors.Wrap(err, tr("unable to compute relative path to the sketch for the item"))
}
if override, ok := overrides[path.String()]; ok {
return override, nil
}
data, err := f.ReadFile()
if err != nil {
return "", fmt.Errorf(tr("reading file %[1]s: %[2]s"), f, err)
}
return string(data), nil
}
// add Arduino.h inclusion directive if missing
mainSrc, err := getSource(sk.MainFile)
if err != nil {
return 0, "", err
}
if !includesArduinoH.MatchString(mainSrc) {
mergedSource += "#include <Arduino.h>\n"
lineOffset++
}
mergedSource += "#line 1 " + QuoteCppString(sk.MainFile.String()) + "\n"
mergedSource += mainSrc + "\n"
lineOffset++
for _, file := range sk.OtherSketchFiles {
src, err := getSource(file)
if err != nil {
return 0, "", err
}
mergedSource += "#line 1 " + QuoteCppString(file.String()) + "\n"
mergedSource += src + "\n"
}
return lineOffset, mergedSource, nil
}
// SketchCopyAdditionalFiles copies the additional files for a sketch to the
// specified destination directory.
func SketchCopyAdditionalFiles(sketch *sketch.Sketch, destPath *paths.Path, overrides map[string]string) error {
if err := destPath.MkdirAll(); err != nil {
return errors.Wrap(err, tr("unable to create a folder to save the sketch files"))
}
for _, file := range sketch.AdditionalFiles {
relpath, err := sketch.FullPath.RelTo(file)
if err != nil {
return errors.Wrap(err, tr("unable to compute relative path to the sketch for the item"))
}
targetPath := destPath.JoinPath(relpath)
// create the directory containing the target
if err = targetPath.Parent().MkdirAll(); err != nil {
return errors.Wrap(err, tr("unable to create the folder containing the item"))
}
var sourceBytes []byte
if override, ok := overrides[relpath.String()]; ok {
// use override source
sourceBytes = []byte(override)
} else {
// read the source file
s, err := file.ReadFile()
if err != nil {
return errors.Wrap(err, tr("unable to read contents of the source item"))
}
sourceBytes = s
}
// tag each addtional file with the filename of the source it was copied from
sourceBytes = append([]byte("#line 1 "+QuoteCppString(file.String())+"\n"), sourceBytes...)
err = writeIfDifferent(sourceBytes, targetPath)
if err != nil {
return errors.Wrap(err, tr("unable to write to destination file"))
}
}
return nil
}
func writeIfDifferent(source []byte, destPath *paths.Path) error {
// Check whether the destination file exists
if destPath.NotExist() {
// Write directly
return destPath.WriteFile(source)
}
// Read the destination file if it exists
existingBytes, err := destPath.ReadFile()
if err != nil {
return errors.Wrap(err, tr("unable to read contents of the destination item"))
}
// Overwrite if contents are different
if !bytes.Equal(existingBytes, source) {
return destPath.WriteFile(source)
}
// Source and destination are the same, don't write anything
return nil
}
// SetupBuildProperties adds the build properties related to the sketch to the
// default board build properties map.
func SetupBuildProperties(boardBuildProperties *properties.Map, buildPath *paths.Path, sketch *sketch.Sketch, optimizeForDebug bool) *properties.Map {
buildProperties := properties.NewMap()
buildProperties.Merge(boardBuildProperties)
if buildPath != nil {
buildProperties.SetPath("build.path", buildPath)
}
if sketch != nil {
buildProperties.Set("build.project_name", sketch.MainFile.Base())
buildProperties.SetPath("build.source.path", sketch.FullPath)
}
if optimizeForDebug {
if debugFlags, ok := buildProperties.GetOk("compiler.optimization_flags.debug"); ok {
buildProperties.Set("compiler.optimization_flags", debugFlags)
}
} else {
if releaseFlags, ok := buildProperties.GetOk("compiler.optimization_flags.release"); ok {
buildProperties.Set("compiler.optimization_flags", releaseFlags)
}
}
return buildProperties
}