Skip to content

Commit 9d3dc9e

Browse files
committed
Do not automatically include <Arduino.h> if already included from sketch
Signed-off-by: Cristian Maglie <[email protected]>
1 parent 25a89ba commit 9d3dc9e

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/arduino.cc/builder/sketch_source_merger.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ package builder
3333
import (
3434
"arduino.cc/builder/constants"
3535
"arduino.cc/builder/types"
36+
"regexp"
3637
"strings"
3738
)
3839

@@ -42,8 +43,13 @@ func (s *SketchSourceMerger) Run(context map[string]interface{}) error {
4243
sketch := context[constants.CTX_SKETCH].(*types.Sketch)
4344

4445
lineOffset := 0
45-
includeSection := composeIncludeArduinoSection()
46-
lineOffset += 2
46+
includeSection := ""
47+
if !sketchIncludesArduinoH(&sketch.MainFile) {
48+
includeSection += "#include <Arduino.h>\n"
49+
lineOffset++
50+
}
51+
includeSection += "#line 1\n"
52+
lineOffset++
4753
context[constants.CTX_INCLUDE_SECTION] = includeSection
4854

4955
source := includeSection
@@ -59,17 +65,18 @@ func (s *SketchSourceMerger) Run(context map[string]interface{}) error {
5965
return nil
6066
}
6167

68+
func sketchIncludesArduinoH(sketch *types.SketchFile) bool {
69+
if matched, err := regexp.MatchString("(?m)^\\s*#\\s*include\\s*[<\"]Arduino\\.h[>\"]", sketch.Source); err != nil {
70+
panic(err)
71+
} else {
72+
return matched
73+
}
74+
}
75+
6276
func addSourceWrappedWithLineDirective(sketch *types.SketchFile) string {
6377
source := "#line 1 \"" + strings.Replace(sketch.Name, "\\", "\\\\", -1) + "\"\n"
6478
source += sketch.Source
6579
source += "\n"
6680

6781
return source
6882
}
69-
70-
func composeIncludeArduinoSection() string {
71-
str := "#include <Arduino.h>\n"
72-
str += "#line 1\n"
73-
74-
return str
75-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Arduino.h should not be automatically included by the Arduino
2+
// preprocessor before the explicit include line in this sketch.
3+
4+
#if defined(HIGH)
5+
#error Arduino.h seems to be automatically included
6+
#endif
7+
8+
# include <Arduino.h>
9+
10+
void setup() {
11+
}
12+
13+
void loop() {
14+
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ func TestTryBuild032(t *testing.T) {
173173
tryBuild(t, "sketch10", "sketch.ino")
174174
}
175175

176+
func TestTryBuild033(t *testing.T) {
177+
tryBuild(t, "sketch_that_includes_arduino_h", "sketch_that_includes_arduino_h.ino")
178+
}
179+
176180
func makeDefaultContext(t *testing.T) map[string]interface{} {
177181
DownloadCoresAndToolsAndLibraries(t)
178182

0 commit comments

Comments
 (0)