Skip to content

Commit 6b29889

Browse files
Do not rebuild the include folder list repeatedely
Previously, during include detection, every pass through IncludesToIncludeFolders would iterate over all included filenames found so far, and rebuild the list of include folders from the list of included libraries again. This commit consistes of these changes: - IncludesToIncludeFolders now looks at ctx.IncludeJustFound to find new libraries (instead of ctx.Includes which contains all included filenames found so far). To minimize changes in this commit, it still creates a slice with just the single include found and processes that. A future commit will simplify this. - With that change, ctx.Includes is no longer used so it is removed. - The resolveIncludeFolders function is removed. It used to build the list of include folders, based on the list of imported libaries. This list was built from scratch again every time, including the core and variant folders. These two folders are now added once, by ContainerFindIncludes, and adding the library's source folders to the include folder list is done by IncludesToIncludeFolders. - ContainerFindIncludes would start by calling IncludesToIncludeFolders (even before any includes were detected) to ensure that the include path contained the core and variant folders on the first preprocessor run already. Now that these paths are added by ContainerFindIncludes, there is no need for this extra call to IncludesToIncludeFolders, so it is removed. - resolveLibraries is modified to actually return only newly imported libraries, not all imported libraries. - resolveLibrary now returns the library it resolved to, instead of just adding it to the markImportedLibrary map. Signed-off-by: Matthijs Kooijman <matthijs@stdin.nl>
1 parent e6bb6fd commit 6b29889

File tree

5 files changed

+23
-85
lines changed

5 files changed

+23
-85
lines changed

src/arduino.cc/builder/container_find_includes.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ import (
4242
type ContainerFindIncludes struct{}
4343

4444
func (s *ContainerFindIncludes) Run(ctx *types.Context) error {
45-
err := runCommand(ctx, &IncludesToIncludeFolders{})
46-
if err != nil {
47-
return i18n.WrapError(err)
45+
ctx.IncludeFolders = append(ctx.IncludeFolders, ctx.BuildProperties[constants.BUILD_PROPERTIES_BUILD_CORE_PATH])
46+
if ctx.BuildProperties[constants.BUILD_PROPERTIES_BUILD_VARIANT_PATH] != constants.EMPTY_STRING {
47+
ctx.IncludeFolders = append(ctx.IncludeFolders, ctx.BuildProperties[constants.BUILD_PROPERTIES_BUILD_VARIANT_PATH])
4848
}
4949

5050
sketchBuildPath := ctx.SketchBuildPath
5151
sketch := ctx.Sketch
52-
err = findIncludesUntilDone(ctx, filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp"))
52+
err := findIncludesUntilDone(ctx, filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp"))
5353
if err != nil {
5454
return i18n.WrapError(err)
5555
}

src/arduino.cc/builder/includes_finder_with_regexp.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ package builder
3131

3232
import (
3333
"arduino.cc/builder/types"
34-
"arduino.cc/builder/utils"
3534
"regexp"
3635
"strings"
3736
)
@@ -52,10 +51,6 @@ func (s *IncludesFinderWithRegExp) Run(ctx *types.Context) error {
5251
ctx.IncludeJustFound = findIncludeForOldCompilers(source)
5352
}
5453

55-
if ctx.IncludeJustFound != "" {
56-
ctx.Includes = utils.AppendIfNotPresent(ctx.Includes, ctx.IncludeJustFound)
57-
}
58-
5954
return nil
6055
}
6156

src/arduino.cc/builder/includes_to_include_folders.go

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,26 @@ import (
3636
"arduino.cc/builder/constants"
3737
"arduino.cc/builder/types"
3838
"arduino.cc/builder/utils"
39-
"arduino.cc/properties"
4039
)
4140

4241
type IncludesToIncludeFolders struct{}
4342

4443
func (s *IncludesToIncludeFolders) Run(ctx *types.Context) error {
45-
includes := ctx.Includes
44+
include := ctx.IncludeJustFound
45+
includes := []string{include}
46+
includeFolders := ctx.IncludeFolders
4647
headerToLibraries := ctx.HeaderToLibraries
4748

4849
platform := ctx.TargetPlatform
4950
actualPlatform := ctx.ActualPlatform
5051
libraryResolutionResults := ctx.LibrariesResolutionResults
5152
importedLibraries := ctx.ImportedLibraries
5253

53-
newlyImportedLibraries := resolveLibraries(includes, headerToLibraries, importedLibraries, []*types.Platform{actualPlatform, platform}, libraryResolutionResults)
54+
if include == "" {
55+
return nil;
56+
}
5457

58+
newlyImportedLibraries := resolveLibraries(includes, headerToLibraries, importedLibraries, []*types.Platform{actualPlatform, platform}, libraryResolutionResults)
5559
foldersWithSources := ctx.FoldersWithSourceFiles
5660

5761
for _, newlyImportedLibrary := range newlyImportedLibraries {
@@ -61,61 +65,46 @@ func (s *IncludesToIncludeFolders) Run(ctx *types.Context) error {
6165
for _, sourceFolder := range sourceFolders {
6266
foldersWithSources.Push(sourceFolder)
6367
}
68+
includeFolders = append(includeFolders, newlyImportedLibrary.SrcFolder)
6469
}
6570
}
6671

6772
ctx.ImportedLibraries = importedLibraries
68-
ctx.IncludeFolders = resolveIncludeFolders(newlyImportedLibraries, ctx.BuildProperties, ctx.Verbose)
73+
ctx.IncludeFolders = includeFolders
6974

7075
return nil
7176
}
7277

73-
func resolveIncludeFolders(importedLibraries []*types.Library, buildProperties properties.Map, verbose bool) []string {
74-
var includeFolders []string
75-
includeFolders = append(includeFolders, buildProperties[constants.BUILD_PROPERTIES_BUILD_CORE_PATH])
76-
if buildProperties[constants.BUILD_PROPERTIES_BUILD_VARIANT_PATH] != constants.EMPTY_STRING {
77-
includeFolders = append(includeFolders, buildProperties[constants.BUILD_PROPERTIES_BUILD_VARIANT_PATH])
78-
}
79-
80-
for _, library := range importedLibraries {
81-
includeFolders = append(includeFolders, library.SrcFolder)
82-
}
83-
84-
return includeFolders
85-
}
86-
87-
//FIXME it's also resolving previously resolved libraries
8878
func resolveLibraries(includes []string, headerToLibraries map[string][]*types.Library, importedLibraries []*types.Library, platforms []*types.Platform, libraryResolutionResults map[string]types.LibraryResolutionResult) []*types.Library {
8979
markImportedLibrary := make(map[*types.Library]bool)
9080
for _, library := range importedLibraries {
9181
markImportedLibrary[library] = true
9282
}
93-
for _, header := range includes {
94-
resolveLibrary(header, headerToLibraries, markImportedLibrary, platforms, libraryResolutionResults)
95-
}
96-
9783
var newlyImportedLibraries []*types.Library
98-
for library, _ := range markImportedLibrary {
99-
newlyImportedLibraries = append(newlyImportedLibraries, library)
84+
for _, header := range includes {
85+
library := resolveLibrary(header, headerToLibraries, markImportedLibrary, platforms, libraryResolutionResults)
86+
if library != nil {
87+
newlyImportedLibraries = append(newlyImportedLibraries, library)
88+
}
10089
}
10190

10291
return newlyImportedLibraries
10392
}
10493

105-
func resolveLibrary(header string, headerToLibraries map[string][]*types.Library, markImportedLibrary map[*types.Library]bool, platforms []*types.Platform, libraryResolutionResults map[string]types.LibraryResolutionResult) {
94+
func resolveLibrary(header string, headerToLibraries map[string][]*types.Library, markImportedLibrary map[*types.Library]bool, platforms []*types.Platform, libraryResolutionResults map[string]types.LibraryResolutionResult) *types.Library {
10695
libraries := append([]*types.Library{}, headerToLibraries[header]...)
10796

10897
if libraries == nil || len(libraries) == 0 {
109-
return
98+
return nil
11099
}
111100

112101
if len(libraries) == 1 {
113102
markImportedLibrary[libraries[0]] = true
114-
return
103+
return libraries[0]
115104
}
116105

117106
if markImportedLibraryContainsOneOfCandidates(markImportedLibrary, libraries) {
118-
return
107+
return nil
119108
}
120109

121110
reverse(libraries)
@@ -147,6 +136,7 @@ func resolveLibrary(header string, headerToLibraries map[string][]*types.Library
147136
libraryResolutionResults[header] = types.LibraryResolutionResult{Library: library, NotUsedLibraries: filterOutLibraryFrom(libraries, library)}
148137

149138
markImportedLibrary[library] = true
139+
return library
150140
}
151141

152142
//facepalm. sort.Reverse needs an Interface that implements Len/Less/Swap. It's a slice! What else for reversing it?!?

src/arduino.cc/builder/test/includes_finder_with_regexp_test.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"arduino.cc/builder"
3434
"arduino.cc/builder/types"
3535
"github.com/stretchr/testify/require"
36-
"sort"
3736
"testing"
3837
)
3938

@@ -50,9 +49,6 @@ func TestIncludesFinderWithRegExp(t *testing.T) {
5049
err := parser.Run(ctx)
5150
NoError(t, err)
5251

53-
includes := ctx.Includes
54-
require.Equal(t, 1, len(includes))
55-
require.Equal(t, "SPI.h", includes[0])
5652
require.Equal(t, "SPI.h", ctx.IncludeJustFound)
5753
}
5854

@@ -67,35 +63,9 @@ func TestIncludesFinderWithRegExpEmptyOutput(t *testing.T) {
6763
err := parser.Run(ctx)
6864
NoError(t, err)
6965

70-
includes := ctx.Includes
71-
require.Equal(t, 0, len(includes))
7266
require.Equal(t, "", ctx.IncludeJustFound)
7367
}
7468

75-
func TestIncludesFinderWithRegExpPreviousIncludes(t *testing.T) {
76-
ctx := &types.Context{
77-
Includes: []string{"test.h"},
78-
}
79-
80-
output := "/some/path/sketch.ino:1:17: fatal error: SPI.h: No such file or directory\n" +
81-
"#include <SPI.h>\n" +
82-
"^\n" +
83-
"compilation terminated."
84-
85-
ctx.Source = output
86-
87-
parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source}
88-
err := parser.Run(ctx)
89-
NoError(t, err)
90-
91-
includes := ctx.Includes
92-
require.Equal(t, 2, len(includes))
93-
sort.Strings(includes)
94-
require.Equal(t, "SPI.h", includes[0])
95-
require.Equal(t, "test.h", includes[1])
96-
require.Equal(t, "SPI.h", ctx.IncludeJustFound)
97-
}
98-
9969
func TestIncludesFinderWithRegExpPaddedIncludes(t *testing.T) {
10070
ctx := &types.Context{}
10171

@@ -109,10 +79,6 @@ func TestIncludesFinderWithRegExpPaddedIncludes(t *testing.T) {
10979
err := parser.Run(ctx)
11080
NoError(t, err)
11181

112-
includes := ctx.Includes
113-
require.Equal(t, 1, len(includes))
114-
sort.Strings(includes)
115-
require.Equal(t, "Wire.h", includes[0])
11682
require.Equal(t, "Wire.h", ctx.IncludeJustFound)
11783
}
11884

@@ -129,10 +95,6 @@ func TestIncludesFinderWithRegExpPaddedIncludes2(t *testing.T) {
12995
err := parser.Run(ctx)
13096
NoError(t, err)
13197

132-
includes := ctx.Includes
133-
require.Equal(t, 1, len(includes))
134-
sort.Strings(includes)
135-
require.Equal(t, "Wire.h", includes[0])
13698
require.Equal(t, "Wire.h", ctx.IncludeJustFound)
13799
}
138100

@@ -148,10 +110,6 @@ func TestIncludesFinderWithRegExpPaddedIncludes3(t *testing.T) {
148110
err := parser.Run(ctx)
149111
NoError(t, err)
150112

151-
includes := ctx.Includes
152-
require.Equal(t, 1, len(includes))
153-
sort.Strings(includes)
154-
require.Equal(t, "SPI.h", includes[0])
155113
require.Equal(t, "SPI.h", ctx.IncludeJustFound)
156114
}
157115

@@ -167,9 +125,5 @@ func TestIncludesFinderWithRegExpPaddedIncludes4(t *testing.T) {
167125
err := parser.Run(ctx)
168126
NoError(t, err)
169127

170-
includes := ctx.Includes
171-
require.Equal(t, 1, len(includes))
172-
sort.Strings(includes)
173-
require.Equal(t, "register.h", includes[0])
174128
require.Equal(t, "register.h", ctx.IncludeJustFound)
175129
}

src/arduino.cc/builder/types/context.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ type Context struct {
5656
WarningsLevel string
5757

5858
// Libraries handling
59-
Includes []string
6059
Libraries []*Library
6160
HeaderToLibraries map[string][]*Library
6261
ImportedLibraries []*Library

0 commit comments

Comments
 (0)