Skip to content

Commit 56a2ccd

Browse files
[skip-changelog] Legacy removing (take 2) (#1664)
* Fixed all lint warnings * Replaced findFilesInFolder with go-paths equivalent * Removed some constants * Removed unused parameter * Removed redundant builder_utils.CompileFilesRecursive function * Simplified structure of CompileFiles * Inlined call to compileFilesWithRecipe * Removed unused function FindAllSubdirectories * Rewrite of FindFilesInFolder using go-paths-helper * Updated dependency go.sum and licensed cache * Removed duplication of extensions table * Applied suggestion from codereview * Update legacy/builder/builder_utils/utils.go Co-authored-by: Silvano Cerza <3314350+silvanocerza@users.noreply.github.com> * Applied code review suggestions * Applied code review suggestions Co-authored-by: Silvano Cerza <3314350+silvanocerza@users.noreply.github.com>
1 parent 10107d2 commit 56a2ccd

26 files changed

+212
-326
lines changed

.licenses/go/github.com/arduino/go-paths-helper.dep.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
name: github.com/arduino/go-paths-helper
3-
version: v1.6.1
3+
version: v1.7.0
44
type: go
5-
summary:
5+
summary:
66
homepage: https://pkg.go.dev/github.com/arduino/go-paths-helper
77
license: gpl-2.0-or-later
88
licenses:

arduino/discovery/discovery_client/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd
4545
github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c/go.mod h1:HK7SpkEax/3P+0w78iRQx1sz1vCDYYw9RXwHjQTB5i8=
4646
github.com/arduino/go-paths-helper v1.0.1/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
4747
github.com/arduino/go-paths-helper v1.2.0/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
48-
github.com/arduino/go-paths-helper v1.6.1 h1:lha+/BuuBsx0qTZ3gy6IO1kU23lObWdQ/UItkzVWQ+0=
49-
github.com/arduino/go-paths-helper v1.6.1/go.mod h1:V82BWgAAp4IbmlybxQdk9Bpkz8M4Qyx+RAFKaG9NuvU=
48+
github.com/arduino/go-paths-helper v1.7.0 h1:S9l5BP2aogz1CgyqqnncXt0PLpK4yvwOW/wu/LaR3tc=
49+
github.com/arduino/go-paths-helper v1.7.0/go.mod h1:V82BWgAAp4IbmlybxQdk9Bpkz8M4Qyx+RAFKaG9NuvU=
5050
github.com/arduino/go-properties-orderedmap v1.6.0 h1:gp2JoWRETtqwsZ+UHu/PBuYWYH2x2+d+uipDxS4WmvM=
5151
github.com/arduino/go-properties-orderedmap v1.6.0/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
5252
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b/go.mod h1:uwGy5PpN4lqW97FiLnbcx+xx8jly5YuPMJWfVwwjJiQ=

client_example/go.sum

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd
4545
github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c/go.mod h1:HK7SpkEax/3P+0w78iRQx1sz1vCDYYw9RXwHjQTB5i8=
4646
github.com/arduino/go-paths-helper v1.0.1/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
4747
github.com/arduino/go-paths-helper v1.2.0/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
48-
github.com/arduino/go-paths-helper v1.6.1/go.mod h1:V82BWgAAp4IbmlybxQdk9Bpkz8M4Qyx+RAFKaG9NuvU=
48+
github.com/arduino/go-paths-helper v1.7.0/go.mod h1:V82BWgAAp4IbmlybxQdk9Bpkz8M4Qyx+RAFKaG9NuvU=
4949
github.com/arduino/go-properties-orderedmap v1.6.0/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
5050
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b/go.mod h1:uwGy5PpN4lqW97FiLnbcx+xx8jly5YuPMJWfVwwjJiQ=
5151
github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b/go.mod h1:iIPnclBMYm1g32Q5kXoqng4jLhMStReIP7ZxaoUC2y8=

docsgen/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c h1:agh2JT9
4848
github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c/go.mod h1:HK7SpkEax/3P+0w78iRQx1sz1vCDYYw9RXwHjQTB5i8=
4949
github.com/arduino/go-paths-helper v1.0.1/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
5050
github.com/arduino/go-paths-helper v1.2.0/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
51-
github.com/arduino/go-paths-helper v1.6.1 h1:lha+/BuuBsx0qTZ3gy6IO1kU23lObWdQ/UItkzVWQ+0=
52-
github.com/arduino/go-paths-helper v1.6.1/go.mod h1:V82BWgAAp4IbmlybxQdk9Bpkz8M4Qyx+RAFKaG9NuvU=
51+
github.com/arduino/go-paths-helper v1.7.0 h1:S9l5BP2aogz1CgyqqnncXt0PLpK4yvwOW/wu/LaR3tc=
52+
github.com/arduino/go-paths-helper v1.7.0/go.mod h1:V82BWgAAp4IbmlybxQdk9Bpkz8M4Qyx+RAFKaG9NuvU=
5353
github.com/arduino/go-properties-orderedmap v1.6.0 h1:gp2JoWRETtqwsZ+UHu/PBuYWYH2x2+d+uipDxS4WmvM=
5454
github.com/arduino/go-properties-orderedmap v1.6.0/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
5555
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b h1:9hDi4F2st6dbLC3y4i02zFT5quS4X6iioWifGlVwfy4=

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.16
44

55
require (
66
github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c
7-
github.com/arduino/go-paths-helper v1.6.1
7+
github.com/arduino/go-paths-helper v1.7.0
88
github.com/arduino/go-properties-orderedmap v1.6.0
99
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b
1010
github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c h1:agh2JT9
4848
github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c/go.mod h1:HK7SpkEax/3P+0w78iRQx1sz1vCDYYw9RXwHjQTB5i8=
4949
github.com/arduino/go-paths-helper v1.0.1/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
5050
github.com/arduino/go-paths-helper v1.2.0/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
51-
github.com/arduino/go-paths-helper v1.6.1 h1:lha+/BuuBsx0qTZ3gy6IO1kU23lObWdQ/UItkzVWQ+0=
52-
github.com/arduino/go-paths-helper v1.6.1/go.mod h1:V82BWgAAp4IbmlybxQdk9Bpkz8M4Qyx+RAFKaG9NuvU=
51+
github.com/arduino/go-paths-helper v1.7.0 h1:S9l5BP2aogz1CgyqqnncXt0PLpK4yvwOW/wu/LaR3tc=
52+
github.com/arduino/go-paths-helper v1.7.0/go.mod h1:V82BWgAAp4IbmlybxQdk9Bpkz8M4Qyx+RAFKaG9NuvU=
5353
github.com/arduino/go-properties-orderedmap v1.6.0 h1:gp2JoWRETtqwsZ+UHu/PBuYWYH2x2+d+uipDxS4WmvM=
5454
github.com/arduino/go-properties-orderedmap v1.6.0/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
5555
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b h1:9hDi4F2st6dbLC3y4i02zFT5quS4X6iioWifGlVwfy4=

legacy/builder/builder.go

-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ import (
3030

3131
var tr = i18n.Tr
3232

33-
var MAIN_FILE_VALID_EXTENSIONS = map[string]bool{".ino": true, ".pde": true}
34-
var ADDITIONAL_FILE_VALID_EXTENSIONS = map[string]bool{".h": true, ".c": true, ".hpp": true, ".hh": true, ".cpp": true, ".S": true}
35-
var ADDITIONAL_FILE_VALID_EXTENSIONS_NO_HEADERS = map[string]bool{".c": true, ".cpp": true, ".S": true}
36-
3733
const DEFAULT_DEBUG_LEVEL = 5
3834
const DEFAULT_WARNINGS_LEVEL = "none"
3935
const DEFAULT_SOFTWARE = "ARDUINO"

legacy/builder/builder_utils/utils.go

+36-95
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
package builder_utils
1717

1818
import (
19+
"fmt"
1920
"os"
2021
"os/exec"
2122
"path/filepath"
2223
"runtime"
2324
"strings"
2425
"sync"
2526

27+
"github.com/arduino/arduino-cli/arduino/globals"
2628
"github.com/arduino/arduino-cli/i18n"
2729
"github.com/arduino/arduino-cli/legacy/builder/constants"
2830
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -35,93 +37,6 @@ import (
3537

3638
var tr = i18n.Tr
3739

38-
func CompileFilesRecursive(ctx *types.Context, sourcePath *paths.Path, buildPath *paths.Path, buildProperties *properties.Map, includes []string) (paths.PathList, error) {
39-
objectFiles, err := CompileFiles(ctx, sourcePath, false, buildPath, buildProperties, includes)
40-
if err != nil {
41-
return nil, errors.WithStack(err)
42-
}
43-
44-
folders, err := utils.ReadDirFiltered(sourcePath.String(), utils.FilterDirs)
45-
if err != nil {
46-
return nil, errors.WithStack(err)
47-
}
48-
49-
for _, folder := range folders {
50-
subFolderObjectFiles, err := CompileFilesRecursive(ctx, sourcePath.Join(folder.Name()), buildPath.Join(folder.Name()), buildProperties, includes)
51-
if err != nil {
52-
return nil, errors.WithStack(err)
53-
}
54-
objectFiles.AddAll(subFolderObjectFiles)
55-
}
56-
57-
return objectFiles, nil
58-
}
59-
60-
func CompileFiles(ctx *types.Context, sourcePath *paths.Path, recurse bool, buildPath *paths.Path, buildProperties *properties.Map, includes []string) (paths.PathList, error) {
61-
sSources, err := findFilesInFolder(sourcePath, ".S", recurse)
62-
if err != nil {
63-
return nil, errors.WithStack(err)
64-
}
65-
cSources, err := findFilesInFolder(sourcePath, ".c", recurse)
66-
if err != nil {
67-
return nil, errors.WithStack(err)
68-
}
69-
cppSources, err := findFilesInFolder(sourcePath, ".cpp", recurse)
70-
if err != nil {
71-
return nil, errors.WithStack(err)
72-
}
73-
74-
ctx.Progress.AddSubSteps(len(sSources) + len(cSources) + len(cppSources))
75-
defer ctx.Progress.RemoveSubSteps()
76-
77-
sObjectFiles, err := compileFilesWithRecipe(ctx, sourcePath, sSources, buildPath, buildProperties, includes, constants.RECIPE_S_PATTERN)
78-
if err != nil {
79-
return nil, errors.WithStack(err)
80-
}
81-
cObjectFiles, err := compileFilesWithRecipe(ctx, sourcePath, cSources, buildPath, buildProperties, includes, constants.RECIPE_C_PATTERN)
82-
if err != nil {
83-
return nil, errors.WithStack(err)
84-
}
85-
cppObjectFiles, err := compileFilesWithRecipe(ctx, sourcePath, cppSources, buildPath, buildProperties, includes, constants.RECIPE_CPP_PATTERN)
86-
if err != nil {
87-
return nil, errors.WithStack(err)
88-
}
89-
90-
objectFiles := paths.NewPathList()
91-
objectFiles.AddAll(sObjectFiles)
92-
objectFiles.AddAll(cObjectFiles)
93-
objectFiles.AddAll(cppObjectFiles)
94-
return objectFiles, nil
95-
}
96-
97-
func findFilesInFolder(sourcePath *paths.Path, extension string, recurse bool) (paths.PathList, error) {
98-
files, err := utils.ReadDirFiltered(sourcePath.String(), utils.FilterFilesWithExtensions(extension))
99-
if err != nil {
100-
return nil, errors.WithStack(err)
101-
}
102-
var sources paths.PathList
103-
for _, file := range files {
104-
sources = append(sources, sourcePath.Join(file.Name()))
105-
}
106-
107-
if recurse {
108-
folders, err := utils.ReadDirFiltered(sourcePath.String(), utils.FilterDirs)
109-
if err != nil {
110-
return nil, errors.WithStack(err)
111-
}
112-
113-
for _, folder := range folders {
114-
otherSources, err := findFilesInFolder(sourcePath.Join(folder.Name()), extension, recurse)
115-
if err != nil {
116-
return nil, errors.WithStack(err)
117-
}
118-
sources = append(sources, otherSources...)
119-
}
120-
}
121-
122-
return sources, nil
123-
}
124-
12540
func findAllFilesInFolder(sourcePath string, recurse bool) ([]string, error) {
12641
files, err := utils.ReadDirFiltered(sourcePath, utils.FilterFiles())
12742
if err != nil {
@@ -153,17 +68,46 @@ func findAllFilesInFolder(sourcePath string, recurse bool) ([]string, error) {
15368
return sources, nil
15469
}
15570

156-
func compileFilesWithRecipe(ctx *types.Context, sourcePath *paths.Path, sources paths.PathList, buildPath *paths.Path, buildProperties *properties.Map, includes []string, recipe string) (paths.PathList, error) {
71+
func CompileFiles(ctx *types.Context, sourcePath *paths.Path, buildPath *paths.Path, buildProperties *properties.Map, includes []string) (paths.PathList, error) {
72+
return compileFiles(ctx, sourcePath, false, buildPath, buildProperties, includes)
73+
}
74+
75+
func CompileFilesRecursive(ctx *types.Context, sourcePath *paths.Path, buildPath *paths.Path, buildProperties *properties.Map, includes []string) (paths.PathList, error) {
76+
return compileFiles(ctx, sourcePath, true, buildPath, buildProperties, includes)
77+
}
78+
79+
func compileFiles(ctx *types.Context, sourcePath *paths.Path, recurse bool, buildPath *paths.Path, buildProperties *properties.Map, includes []string) (paths.PathList, error) {
80+
var sources paths.PathList
81+
var err error
82+
if recurse {
83+
sources, err = sourcePath.ReadDirRecursive()
84+
} else {
85+
sources, err = sourcePath.ReadDir()
86+
}
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
validExtensions := []string{}
92+
for ext := range globals.SourceFilesValidExtensions {
93+
validExtensions = append(validExtensions, ext)
94+
}
95+
96+
sources.FilterSuffix(validExtensions...)
97+
ctx.Progress.AddSubSteps(len(sources))
98+
defer ctx.Progress.RemoveSubSteps()
99+
157100
objectFiles := paths.NewPathList()
101+
var objectFilesMux sync.Mutex
158102
if len(sources) == 0 {
159103
return objectFiles, nil
160104
}
161-
var objectFilesMux sync.Mutex
162105
var errorsList []error
163106
var errorsMux sync.Mutex
164107

165108
queue := make(chan *paths.Path)
166109
job := func(source *paths.Path) {
110+
recipe := fmt.Sprintf("recipe%s.o.pattern", source.Ext())
167111
objectFile, err := compileFileWithRecipe(ctx, sourcePath, source, buildPath, buildProperties, includes, recipe)
168112
if err != nil {
169113
errorsMux.Lock()
@@ -233,7 +177,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
233177
return nil, errors.WithStack(err)
234178
}
235179

236-
objIsUpToDate, err := ObjFileIsUpToDate(ctx, source, objectFile, depsFile)
180+
objIsUpToDate, err := ObjFileIsUpToDate(source, objectFile, depsFile)
237181
if err != nil {
238182
return nil, errors.WithStack(err)
239183
}
@@ -260,7 +204,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
260204
return objectFile, nil
261205
}
262206

263-
func ObjFileIsUpToDate(ctx *types.Context, sourceFile, objectFile, dependencyFile *paths.Path) (bool, error) {
207+
func ObjFileIsUpToDate(sourceFile, objectFile, dependencyFile *paths.Path) (bool, error) {
264208
logrus.Debugf("Checking previous results for %v (result = %v, dep = %v)", sourceFile, objectFile, dependencyFile)
265209
if objectFile == nil || dependencyFile == nil {
266210
logrus.Debugf("Not found: nil")
@@ -371,10 +315,7 @@ func unescapeDep(s string) string {
371315
}
372316

373317
func removeEndingBackSlash(s string) string {
374-
if strings.HasSuffix(s, "\\") {
375-
s = s[:len(s)-1]
376-
}
377-
return s
318+
return strings.TrimSuffix(s, "\\")
378319
}
379320

380321
func nonEmptyString(s string) bool {

legacy/builder/constants/constants.go

-3
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ const PLATFORM_URL = "url"
6666
const PLATFORM_VERSION = "version"
6767
const RECIPE_AR_PATTERN = "recipe.ar.pattern"
6868
const RECIPE_C_COMBINE_PATTERN = "recipe.c.combine.pattern"
69-
const RECIPE_C_PATTERN = "recipe.c.o.pattern"
70-
const RECIPE_CPP_PATTERN = "recipe.cpp.o.pattern"
7169
const RECIPE_PREPROC_MACROS = "recipe.preproc.macros"
72-
const RECIPE_S_PATTERN = "recipe.S.o.pattern"
7370
const REWRITING_DISABLED = "disabled"
7471
const REWRITING = "rewriting"
7572
const SPACE = " "

legacy/builder/container_find_includes.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import (
9898
"os/exec"
9999
"time"
100100

101+
"github.com/arduino/arduino-cli/arduino/globals"
101102
"github.com/arduino/arduino-cli/arduino/libraries"
102103
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
103104
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -312,7 +313,7 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile t
312313
// TODO: This reads the dependency file, but the actual building
313314
// does it again. Should the result be somehow cached? Perhaps
314315
// remove the object file if it is found to be stale?
315-
unchanged, err := builder_utils.ObjFileIsUpToDate(ctx, sourcePath, objPath, depPath)
316+
unchanged, err := builder_utils.ObjFileIsUpToDate(sourcePath, objPath, depPath)
316317
if err != nil {
317318
return errors.WithStack(err)
318319
}
@@ -406,16 +407,17 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile t
406407
}
407408

408409
func queueSourceFilesFromFolder(ctx *types.Context, queue *types.UniqueSourceFileQueue, origin interface{}, folder *paths.Path, recurse bool) error {
409-
extensions := func(ext string) bool { return ADDITIONAL_FILE_VALID_EXTENSIONS_NO_HEADERS[ext] }
410-
411-
filePaths := []string{}
412-
err := utils.FindFilesInFolder(&filePaths, folder.String(), extensions, recurse)
410+
sourceFileExtensions := []string{}
411+
for k := range globals.SourceFilesValidExtensions {
412+
sourceFileExtensions = append(sourceFileExtensions, k)
413+
}
414+
filePaths, err := utils.FindFilesInFolder(folder, recurse, sourceFileExtensions)
413415
if err != nil {
414416
return errors.WithStack(err)
415417
}
416418

417419
for _, filePath := range filePaths {
418-
sourceFile, err := types.MakeSourceFile(ctx, origin, paths.New(filePath))
420+
sourceFile, err := types.MakeSourceFile(ctx, origin, filePath)
419421
if err != nil {
420422
return errors.WithStack(err)
421423
}

0 commit comments

Comments
 (0)