forked from arduino/arduino-builder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
346 lines (296 loc) · 11.7 KB
/
utils.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* This file is part of Arduino Builder.
*
* Arduino Builder is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
*/
package builder_utils
import (
"arduino.cc/builder/constants"
"arduino.cc/builder/i18n"
"arduino.cc/builder/props"
"arduino.cc/builder/utils"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func CompileFilesRecursive(objectFiles []string, sourcePath string, buildPath string, buildProperties props.PropertiesMap, includes []string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) {
objectFiles, err := CompileFiles(objectFiles, sourcePath, false, buildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
folders, err := utils.ReadDirFiltered(sourcePath, utils.FilterDirs)
if err != nil {
return nil, i18n.WrapError(err)
}
for _, folder := range folders {
objectFiles, err = CompileFilesRecursive(objectFiles, filepath.Join(sourcePath, folder.Name()), filepath.Join(buildPath, folder.Name()), buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
}
return objectFiles, nil
}
func CompileFiles(objectFiles []string, sourcePath string, recurse bool, buildPath string, buildProperties props.PropertiesMap, includes []string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) {
objectFiles, err := compileFilesWithExtensionWithRecipe(objectFiles, sourcePath, recurse, buildPath, buildProperties, includes, ".S", constants.RECIPE_S_PATTERN, verbose, warningsLevel, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
objectFiles, err = compileFilesWithExtensionWithRecipe(objectFiles, sourcePath, recurse, buildPath, buildProperties, includes, ".c", constants.RECIPE_C_PATTERN, verbose, warningsLevel, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
objectFiles, err = compileFilesWithExtensionWithRecipe(objectFiles, sourcePath, recurse, buildPath, buildProperties, includes, ".cpp", constants.RECIPE_CPP_PATTERN, verbose, warningsLevel, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
return objectFiles, nil
}
func compileFilesWithExtensionWithRecipe(objectFiles []string, sourcePath string, recurse bool, buildPath string, buildProperties props.PropertiesMap, includes []string, extension string, recipe string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) {
sources, err := findFilesInFolder(sourcePath, extension, recurse)
if err != nil {
return nil, i18n.WrapError(err)
}
return compileFilesWithRecipe(objectFiles, sourcePath, sources, buildPath, buildProperties, includes, recipe, verbose, warningsLevel, logger)
}
func findFilesInFolder(sourcePath string, extension string, recurse bool) ([]string, error) {
files, err := utils.ReadDirFiltered(sourcePath, utils.FilterFilesWithExtension(extension))
if err != nil {
return nil, i18n.WrapError(err)
}
var sources []string
for _, file := range files {
sources = append(sources, filepath.Join(sourcePath, file.Name()))
}
if recurse {
folders, err := utils.ReadDirFiltered(sourcePath, utils.FilterDirs)
if err != nil {
return nil, i18n.WrapError(err)
}
for _, folder := range folders {
otherSources, err := findFilesInFolder(filepath.Join(sourcePath, folder.Name()), extension, recurse)
if err != nil {
return nil, i18n.WrapError(err)
}
sources = append(sources, otherSources...)
}
}
return sources, nil
}
func compileFilesWithRecipe(objectFiles []string, sourcePath string, sources []string, buildPath string, buildProperties props.PropertiesMap, includes []string, recipe string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) {
for _, source := range sources {
objectFile, err := compileFileWithRecipe(sourcePath, source, buildPath, buildProperties, includes, recipe, verbose, warningsLevel, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
objectFiles = append(objectFiles, objectFile)
}
return objectFiles, nil
}
func compileFileWithRecipe(sourcePath string, source string, buildPath string, buildProperties props.PropertiesMap, includes []string, recipe string, verbose bool, warningsLevel string, logger i18n.Logger) (string, error) {
properties := buildProperties.Clone()
properties[constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS] = properties[constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS+"."+warningsLevel]
properties[constants.BUILD_PROPERTIES_INCLUDES] = strings.Join(includes, constants.SPACE)
properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = source
relativeSource, err := filepath.Rel(sourcePath, source)
if err != nil {
return "", i18n.WrapError(err)
}
properties[constants.BUILD_PROPERTIES_OBJECT_FILE] = filepath.Join(buildPath, relativeSource+".o")
err = utils.EnsureFolderExists(filepath.Dir(properties[constants.BUILD_PROPERTIES_OBJECT_FILE]))
if err != nil {
return "", i18n.WrapError(err)
}
objIsUpToDate, err := ObjFileIsUpToDate(properties[constants.BUILD_PROPERTIES_SOURCE_FILE], properties[constants.BUILD_PROPERTIES_OBJECT_FILE], filepath.Join(buildPath, relativeSource+".d"))
if err != nil {
return "", i18n.WrapError(err)
}
if !objIsUpToDate {
_, err = ExecRecipe(properties, recipe, false, verbose, verbose, logger)
if err != nil {
return "", i18n.WrapError(err)
}
} else if verbose {
logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_USING_PREVIOUS_COMPILED_FILE, properties[constants.BUILD_PROPERTIES_OBJECT_FILE])
}
return properties[constants.BUILD_PROPERTIES_OBJECT_FILE], nil
}
func ObjFileIsUpToDate(sourceFile, objectFile, dependencyFile string) (bool, error) {
sourceFile = filepath.Clean(sourceFile)
objectFile = filepath.Clean(objectFile)
dependencyFile = filepath.Clean(dependencyFile)
sourceFileStat, err := os.Stat(sourceFile)
if err != nil {
return false, i18n.WrapError(err)
}
objectFileStat, err := os.Stat(objectFile)
if err != nil {
if os.IsNotExist(err) {
return false, nil
} else {
return false, i18n.WrapError(err)
}
}
dependencyFileStat, err := os.Stat(dependencyFile)
if err != nil {
if os.IsNotExist(err) {
return false, nil
} else {
return false, i18n.WrapError(err)
}
}
if sourceFileStat.ModTime().After(objectFileStat.ModTime()) {
return false, nil
}
if sourceFileStat.ModTime().After(dependencyFileStat.ModTime()) {
return false, nil
}
rows, err := utils.ReadFileToRows(dependencyFile)
if err != nil {
return false, i18n.WrapError(err)
}
rows = utils.Map(rows, removeEndingBackSlash)
rows = utils.Map(rows, strings.TrimSpace)
rows = utils.Map(rows, unescapeDep)
rows = utils.Filter(rows, nonEmptyString)
if len(rows) == 0 {
return true, nil
}
firstRow := rows[0]
if !strings.HasSuffix(firstRow, ":") {
return false, nil
}
objFileInDepFile := firstRow[:len(firstRow)-1]
if objFileInDepFile != objectFile {
return false, nil
}
rows = rows[1:]
for _, row := range rows {
depStat, err := os.Stat(row)
if err != nil && !os.IsNotExist(err) {
// There is probably a parsing error of the dep file
// Ignore the error and trigger a full rebuild anyway
return false, nil
}
if os.IsNotExist(err) {
return false, nil
}
if depStat.ModTime().After(objectFileStat.ModTime()) {
return false, nil
}
}
return true, nil
}
func unescapeDep(s string) string {
s = strings.Replace(s, "\\ ", " ", -1)
s = strings.Replace(s, "\\\t", "\t", -1)
s = strings.Replace(s, "\\#", "#", -1)
s = strings.Replace(s, "$$", "$", -1)
s = strings.Replace(s, "\\\\", "\\", -1)
return s
}
func removeEndingBackSlash(s string) string {
if strings.HasSuffix(s, "\\") {
s = s[:len(s)-1]
}
return s
}
func nonEmptyString(s string) bool {
return s != constants.EMPTY_STRING
}
func ArchiveCompiledFiles(buildPath string, archiveFile string, objectFiles []string, buildProperties props.PropertiesMap, verbose bool, logger i18n.Logger) (string, error) {
archiveFilePath := filepath.Join(buildPath, archiveFile)
if _, err := os.Stat(archiveFilePath); err == nil {
err = os.Remove(archiveFilePath)
if err != nil {
return "", i18n.WrapError(err)
}
}
for _, objectFile := range objectFiles {
properties := buildProperties.Clone()
properties[constants.BUILD_PROPERTIES_ARCHIVE_FILE] = filepath.Base(archiveFilePath)
properties[constants.BUILD_PROPERTIES_ARCHIVE_FILE_PATH] = archiveFilePath
properties[constants.BUILD_PROPERTIES_OBJECT_FILE] = objectFile
_, err := ExecRecipe(properties, constants.RECIPE_AR_PATTERN, false, verbose, verbose, logger)
if err != nil {
return "", i18n.WrapError(err)
}
}
return archiveFilePath, nil
}
func ExecRecipe(properties props.PropertiesMap, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) ([]byte, error) {
command, err := PrepareCommandForRecipe(properties, recipe, removeUnsetProperties, echoCommandLine, echoOutput, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
if echoOutput {
command.Stdout = os.Stdout
}
command.Stderr = os.Stderr
if echoOutput {
err := command.Run()
return nil, i18n.WrapError(err)
}
bytes, err := command.Output()
return bytes, i18n.WrapError(err)
}
func PrepareCommandForRecipe(properties props.PropertiesMap, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) (*exec.Cmd, error) {
pattern := properties[recipe]
if pattern == constants.EMPTY_STRING {
return nil, i18n.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, recipe)
}
var err error
commandLine := properties.ExpandPropsInString(pattern)
if removeUnsetProperties {
commandLine, err = props.DeleteUnexpandedPropsFromString(commandLine)
if err != nil {
return nil, i18n.WrapError(err)
}
}
command, err := utils.PrepareCommand(commandLine, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
if echoCommandLine {
fmt.Println(commandLine)
}
return command, nil
}
func ExecRecipeCollectStdErr(properties props.PropertiesMap, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) (string, error) {
command, err := PrepareCommandForRecipe(properties, recipe, removeUnsetProperties, echoCommandLine, echoOutput, logger)
if err != nil {
return "", i18n.WrapError(err)
}
buffer := &bytes.Buffer{}
command.Stderr = buffer
command.Run()
return string(buffer.Bytes()), nil
}
func RemoveHyphenMDDFlagFromGCCCommandLine(properties props.PropertiesMap) {
properties[constants.BUILD_PROPERTIES_COMPILER_CPP_FLAGS] = strings.Replace(properties[constants.BUILD_PROPERTIES_COMPILER_CPP_FLAGS], "-MMD", "", -1)
}