Skip to content

Commit 9d7d1d7

Browse files
cmagliematthijskooijman
authored andcommitted
Do not limit includes to the src sketch subdirectory
Commit 405a24a (Limit recursive sketch compilation to the `src` directory) modified the sketch loading to only recursively load files from the `src` subdirectory of a sketch, in order to only compile source files in that subdirectory. As a side effect, this also prevents source and header files in other subdirectories from being copied into the build directory, and hence from being included. This side effect was not entirely unanticipated, but its implications were underestimated. This commit undoes these side effects: source and header files are again loaded (and thus copied) recursively in all subdirectories of a sketch. Compilation is still limited to the src subdirectory (there already was code to achieve this). This commit also modifies include detection to be limited to the src subdirectory (which was previously implicit, since it can only look at files that were copied into the build directory). With this commit, the the meaning of "loading" a file becomes a bit fuzzy in the code, but this is intended to be fixed in a future refactor. A test will be added in the next commit. This fixes arduino/Arduino#5186. Signed-off-by: Cristian Maglie <c.maglie@arduino.cc> Signed-off-by: Matthijs Kooijman <matthijs@stdin.nl>
1 parent 26eadb9 commit 9d7d1d7

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

src/arduino.cc/builder/container_find_includes.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
package builder
3131

3232
import (
33+
"os"
34+
"path/filepath"
35+
3336
"arduino.cc/builder/constants"
3437
"arduino.cc/builder/i18n"
3538
"arduino.cc/builder/types"
3639
"arduino.cc/builder/utils"
37-
"path/filepath"
3840
)
3941

4042
type ContainerFindIncludes struct{}
@@ -53,7 +55,11 @@ func (s *ContainerFindIncludes) Run(ctx *types.Context) error {
5355
}
5456

5557
foldersWithSources := ctx.FoldersWithSourceFiles
56-
foldersWithSources.Push(types.SourceFolder{Folder: ctx.SketchBuildPath, Recurse: true})
58+
foldersWithSources.Push(types.SourceFolder{Folder: ctx.SketchBuildPath, Recurse: false})
59+
srcSubfolderPath := filepath.Join(ctx.SketchBuildPath, constants.SKETCH_FOLDER_SRC)
60+
if info, err := os.Stat(srcSubfolderPath); err == nil && info.IsDir() {
61+
foldersWithSources.Push(types.SourceFolder{Folder: srcSubfolderPath, Recurse: true})
62+
}
5763
if len(ctx.ImportedLibraries) > 0 {
5864
for _, library := range ctx.ImportedLibraries {
5965
sourceFolders := types.LibraryToSourceFolder(library)

src/arduino.cc/builder/sketch_loader.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,11 @@ func collectAllSketchFiles(from string) ([]string, error) {
9292
// Source files in the root are compiled, non-recursively. This
9393
// is the only place where .ino files can be present.
9494
rootExtensions := func(ext string) bool { return MAIN_FILE_VALID_EXTENSIONS[ext] || ADDITIONAL_FILE_VALID_EXTENSIONS[ext] }
95-
err := utils.FindFilesInFolder(&filePaths, from, rootExtensions, false /* recurse */)
95+
err := utils.FindFilesInFolder(&filePaths, from, rootExtensions, true /* recurse */)
9696
if err != nil {
9797
return nil, i18n.WrapError(err)
9898
}
9999

100-
// The "src/" subdirectory of a sketch is compiled recursively
101-
// (but .ino files are *not* compiled)
102-
srcPath := filepath.Join(from, constants.SKETCH_FOLDER_SRC)
103-
if info, err := os.Stat(srcPath); err == nil && info.IsDir() {
104-
srcExtensions := func(ext string) bool { return ADDITIONAL_FILE_VALID_EXTENSIONS[ext] }
105-
err = utils.FindFilesInFolder(&filePaths, srcPath, srcExtensions, true /* recurse */)
106-
}
107100
return filePaths, i18n.WrapError(err)
108101
}
109102

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
package test
3131

3232
import (
33+
"path/filepath"
34+
"testing"
35+
3336
"arduino.cc/builder"
3437
"arduino.cc/builder/types"
3538
"github.com/stretchr/testify/require"
36-
"path/filepath"
37-
"testing"
3839
)
3940

4041
func TestLoadSketchWithFolder(t *testing.T) {
@@ -122,9 +123,11 @@ func TestLoadSketchFromFolder(t *testing.T) {
122123

123124
require.Equal(t, 0, len(sketch.OtherSketchFiles))
124125

125-
require.Equal(t, 2, len(sketch.AdditionalFiles))
126-
require.Contains(t, sketch.AdditionalFiles[0].Name, "other.cpp")
127-
require.Contains(t, sketch.AdditionalFiles[1].Name, "other.h")
126+
require.Equal(t, 4, len(sketch.AdditionalFiles))
127+
require.Contains(t, sketch.AdditionalFiles[0].Name, "sketch_with_subfolders/src/subfolder/other.cpp")
128+
require.Contains(t, sketch.AdditionalFiles[1].Name, "sketch_with_subfolders/src/subfolder/other.h")
129+
require.Contains(t, sketch.AdditionalFiles[2].Name, "sketch_with_subfolders/subfolder/dont_load_me.cpp")
130+
require.Contains(t, sketch.AdditionalFiles[3].Name, "sketch_with_subfolders/subfolder/other.h")
128131
}
129132

130133
func TestLoadSketchWithBackup(t *testing.T) {

src/arduino.cc/builder/test/sketch_with_subfolders/subfolder/other.h

Whitespace-only changes.

0 commit comments

Comments
 (0)