Skip to content

Commit 08890f7

Browse files
author
Federico Fissore
committed
Added FailIfBuildpathEqualsSketchPath: stops compilation when sketch (.ino) is
located in the same folder specified as build path Signed-off-by: Federico Fissore <[email protected]>
1 parent 8c877a1 commit 08890f7

5 files changed

+212
-4
lines changed

Diff for: src/arduino.cc/builder/add_additional_entries_to_context.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,22 @@ type AddAdditionalEntriesToContext struct{}
4040
func (s *AddAdditionalEntriesToContext) Run(context map[string]interface{}) error {
4141
if utils.MapHas(context, constants.CTX_BUILD_PATH) {
4242
buildPath := context[constants.CTX_BUILD_PATH].(string)
43-
preprocPath := filepath.Join(buildPath, constants.FOLDER_PREPROC)
44-
sketchBuildPath := filepath.Join(buildPath, constants.FOLDER_SKETCH)
45-
librariesBuildPath := filepath.Join(buildPath, constants.FOLDER_LIBRARIES)
46-
coreBuildPath := filepath.Join(buildPath, constants.FOLDER_CORE)
43+
preprocPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_PREPROC))
44+
if err != nil {
45+
return utils.WrapError(err)
46+
}
47+
sketchBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_SKETCH))
48+
if err != nil {
49+
return utils.WrapError(err)
50+
}
51+
librariesBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_LIBRARIES))
52+
if err != nil {
53+
return utils.WrapError(err)
54+
}
55+
coreBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_CORE))
56+
if err != nil {
57+
return utils.WrapError(err)
58+
}
4759

4860
context[constants.CTX_PREPROC_PATH] = preprocPath
4961
context[constants.CTX_SKETCH_BUILD_PATH] = sketchBuildPath

Diff for: src/arduino.cc/builder/container_setup.go

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type ContainerSetupHardwareToolsLibsSketchAndProps struct{}
4040
func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(context map[string]interface{}) error {
4141
commands := []types.Command{
4242
&AddAdditionalEntriesToContext{},
43+
&FailIfBuildpathEqualsSketchPath{},
4344
&RecipeByPrefixSuffixRunner{Prefix: constants.HOOKS_PREBUILD, Suffix: constants.HOOKS_PATTERN_SUFFIX},
4445
&HardwareLoader{},
4546
&PlatformKeysRewriteLoader{},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* This file is part of Arduino Builder.
3+
*
4+
* Arduino Builder is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
28+
*/
29+
30+
package builder
31+
32+
import (
33+
"arduino.cc/builder/constants"
34+
"arduino.cc/builder/utils"
35+
"path/filepath"
36+
)
37+
38+
type FailIfBuildpathEqualsSketchPath struct{}
39+
40+
func (s *FailIfBuildpathEqualsSketchPath) Run(context map[string]interface{}) error {
41+
if !utils.MapHas(context, constants.CTX_BUILD_PATH) || !utils.MapHas(context, constants.CTX_SKETCH_LOCATION) {
42+
return nil
43+
}
44+
45+
buildPath, err := filepath.Abs(context[constants.CTX_BUILD_PATH].(string))
46+
if err != nil {
47+
return utils.WrapError(err)
48+
}
49+
50+
sketchPath, err := filepath.Abs(context[constants.CTX_SKETCH_LOCATION].(string))
51+
if err != nil {
52+
return utils.WrapError(err)
53+
}
54+
sketchPath = filepath.Dir(sketchPath)
55+
56+
rel, err := filepath.Rel(buildPath, sketchPath)
57+
if err != nil {
58+
return utils.WrapError(err)
59+
}
60+
61+
if rel == "." {
62+
return utils.Errorf(context, "Sketch cannot be located in build path. Please specify a different build path")
63+
}
64+
65+
return nil
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* This file is part of Arduino Builder.
3+
*
4+
* Arduino Builder is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
28+
*/
29+
30+
package test
31+
32+
import (
33+
"arduino.cc/builder"
34+
"arduino.cc/builder/constants"
35+
"github.com/stretchr/testify/require"
36+
"path/filepath"
37+
"testing"
38+
)
39+
40+
func TestAddAdditionalEntriesToContextNoBuildPath(t *testing.T) {
41+
context := make(map[string]interface{})
42+
43+
command := builder.AddAdditionalEntriesToContext{}
44+
NoError(t, command.Run(context))
45+
46+
require.Nil(t, context[constants.CTX_PREPROC_PATH])
47+
require.Nil(t, context[constants.CTX_SKETCH_BUILD_PATH])
48+
require.Nil(t, context[constants.CTX_LIBRARIES_BUILD_PATH])
49+
require.Nil(t, context[constants.CTX_CORE_BUILD_PATH])
50+
51+
require.NotNil(t, context[constants.CTX_WARNINGS_LEVEL])
52+
require.NotNil(t, context[constants.CTX_VERBOSE])
53+
require.NotNil(t, context[constants.CTX_DEBUG_LEVEL])
54+
}
55+
56+
func TestAddAdditionalEntriesToContextWithBuildPath(t *testing.T) {
57+
context := make(map[string]interface{})
58+
59+
context[constants.CTX_BUILD_PATH] = "folder"
60+
61+
command := builder.AddAdditionalEntriesToContext{}
62+
NoError(t, command.Run(context))
63+
64+
require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_PREPROC)), context[constants.CTX_PREPROC_PATH])
65+
require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_SKETCH)), context[constants.CTX_SKETCH_BUILD_PATH])
66+
require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_LIBRARIES)), context[constants.CTX_LIBRARIES_BUILD_PATH])
67+
require.Equal(t, Abs(t, filepath.Join("folder", constants.FOLDER_CORE)), context[constants.CTX_CORE_BUILD_PATH])
68+
69+
require.NotNil(t, context[constants.CTX_WARNINGS_LEVEL])
70+
require.NotNil(t, context[constants.CTX_VERBOSE])
71+
require.NotNil(t, context[constants.CTX_DEBUG_LEVEL])
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of Arduino Builder.
3+
*
4+
* Arduino Builder is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
28+
*/
29+
30+
package test
31+
32+
import (
33+
"arduino.cc/builder"
34+
"arduino.cc/builder/constants"
35+
"github.com/stretchr/testify/require"
36+
"testing"
37+
)
38+
39+
func TestFailIfBuildpathEqualsSketchPath(t *testing.T) {
40+
context := make(map[string]interface{})
41+
42+
context[constants.CTX_BUILD_PATH] = "buildPath"
43+
context[constants.CTX_SKETCH_LOCATION] = "buildPath/sketch.ino"
44+
45+
command := builder.FailIfBuildpathEqualsSketchPath{}
46+
require.Error(t, command.Run(context))
47+
}
48+
49+
func TestFailIfBuildpathEqualsSketchPathSketchPathDiffers(t *testing.T) {
50+
context := make(map[string]interface{})
51+
52+
context[constants.CTX_BUILD_PATH] = "buildPath"
53+
context[constants.CTX_SKETCH_LOCATION] = "sketchPath/sketch.ino"
54+
55+
command := builder.FailIfBuildpathEqualsSketchPath{}
56+
NoError(t, command.Run(context))
57+
}

0 commit comments

Comments
 (0)