Skip to content

Commit c2c1bf1

Browse files
committed
First experiments with arduino-preprocessor
1 parent 0d042f0 commit c2c1bf1

File tree

5 files changed

+139
-6
lines changed

5 files changed

+139
-6
lines changed

builder.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (s *Builder) Run(ctx *types.Context) error {
8989
&WarnAboutArchIncompatibleLibraries{},
9090

9191
utils.LogIfVerbose(constants.LOG_LEVEL_INFO, "Generating function prototypes..."),
92-
&ContainerAddPrototypes{},
92+
&PreprocessSketch{},
9393

9494
utils.LogIfVerbose(constants.LOG_LEVEL_INFO, "Compiling sketch..."),
9595
&RecipeByPrefixSuffixRunner{Prefix: constants.HOOKS_SKETCH_PREBUILD, Suffix: constants.HOOKS_PATTERN_SUFFIX},
@@ -158,7 +158,7 @@ func (s *Preprocess) Run(ctx *types.Context) error {
158158

159159
&WarnAboutArchIncompatibleLibraries{},
160160

161-
&ContainerAddPrototypes{},
161+
&PreprocessSketch{},
162162

163163
&PrintPreprocessedSource{},
164164
}

hardware/platform.txt

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# arduino-preprocessor
2+
# --------------------
3+
4+
tools.arduino-preprocessor.path={runtime.tools.arduino-preprocessor.path}
5+
tools.arduino-preprocessor.cmd.path={path}/bin/arduino-preprocessor
6+
tools.arduino-preprocessor.pattern="{cmd.path}" "{source_file}" -- -std=gnu++11
7+
18
# ctags
29
# ------------------------------
310
tools.ctags.path={runtime.tools.ctags.path}
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
"fmt"
34+
"path/filepath"
35+
36+
"io/ioutil"
37+
38+
"arduino.cc/builder/constants"
39+
"arduino.cc/builder/i18n"
40+
"arduino.cc/builder/types"
41+
"arduino.cc/builder/utils"
42+
)
43+
44+
type PreprocessSketch struct{}
45+
46+
func (s *PreprocessSketch) Run(ctx *types.Context) error {
47+
sourceFile := filepath.Join(ctx.SketchBuildPath, filepath.Base(ctx.Sketch.MainFile.Name)+".cpp")
48+
commands := []types.Command{
49+
&GCCPreprocRunner{SourceFilePath: sourceFile, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E, Includes: ctx.IncludeFolders},
50+
&ArduinoPreprocessorRunner{},
51+
&SketchSaver{},
52+
}
53+
54+
for _, command := range commands {
55+
PrintRingNameIfDebug(ctx, command)
56+
err := command.Run(ctx)
57+
if err != nil {
58+
return i18n.WrapError(err)
59+
}
60+
}
61+
62+
return nil
63+
}
64+
65+
type ArduinoPreprocessorRunner struct{}
66+
67+
func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
68+
buildProperties := ctx.BuildProperties
69+
targetFilePath := ctx.FileToRead
70+
logger := ctx.GetLogger()
71+
72+
properties := buildProperties.Clone()
73+
toolProps := buildProperties.SubTree("tools").SubTree("arduino-preprocessor")
74+
properties.Merge(toolProps)
75+
properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = targetFilePath
76+
77+
pattern := properties[constants.BUILD_PROPERTIES_PATTERN]
78+
if pattern == constants.EMPTY_STRING {
79+
return i18n.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, "arduino-preprocessor")
80+
}
81+
82+
commandLine := properties.ExpandPropsInString(pattern)
83+
command, err := utils.PrepareCommand(commandLine, logger)
84+
if err != nil {
85+
return i18n.WrapError(err)
86+
}
87+
88+
verbose := ctx.Verbose
89+
if verbose {
90+
fmt.Println(commandLine)
91+
}
92+
93+
sourceBytes, err := command.Output()
94+
if err != nil {
95+
return i18n.WrapError(err)
96+
}
97+
98+
ctx.Source = string(sourceBytes)
99+
if ctx.Source == "" {
100+
// If the output is empty the original sketch may pass through
101+
buf, err := ioutil.ReadFile(targetFilePath)
102+
if err != nil {
103+
return i18n.WrapError(err)
104+
}
105+
ctx.Source = string(buf)
106+
}
107+
//fmt.Printf("PREPROCESSOR OUTPUT:\n%s\n", ctx.Source)
108+
return nil
109+
}

test/helper_tools_downloader.go

+8
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ func DownloadCoresAndToolsAndLibraries(t *testing.T) {
113113
OsUrl{Os: "x86_64-apple-darwin", Url: "http://downloads.arduino.cc/tools/ctags-5.8-arduino11-x86_64-apple-darwin.zip"},
114114
OsUrl{Os: "arm-linux-gnueabihf", Url: "http://downloads.arduino.cc/tools/ctags-5.8-arduino11-armv6-linux-gnueabihf.tar.bz2"},
115115
}},
116+
Tool{Name: "arduino-preprocessor", Version: "0.0.1"}, //OsUrls: []OsUrl{
117+
// OsUrl{Os: "i686-pc-linux-gnu", Url: "http://downloads.arduino.cc/tools/ctags-5.8-arduino11-i686-pc-linux-gnu.tar.bz2"},
118+
// OsUrl{Os: "x86_64-pc-linux-gnu", Url: "http://downloads.arduino.cc/tools/ctags-5.8-arduino11-x86_64-pc-linux-gnu.tar.bz2"},
119+
// OsUrl{Os: "i686-mingw32", Url: "http://downloads.arduino.cc/tools/ctags-5.8-arduino11-i686-mingw32.zip"},
120+
// OsUrl{Os: "x86_64-apple-darwin", Url: "http://downloads.arduino.cc/tools/ctags-5.8-arduino11-x86_64-apple-darwin.zip"},
121+
// OsUrl{Os: "arm-linux-gnueabihf", Url: "http://downloads.arduino.cc/tools/ctags-5.8-arduino11-armv6-linux-gnueabihf.tar.bz2"},
122+
//}
123+
116124
}
117125

118126
boardsManagerTools := []Tool{

test/tools_loader_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
package test
3131

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

4041
type ByToolIDAndVersion []*types.Tool
@@ -64,11 +65,15 @@ func TestLoadTools(t *testing.T) {
6465
NoError(t, err)
6566

6667
tools := ctx.Tools
67-
require.Equal(t, 6, len(tools))
68+
require.Equal(t, 7, len(tools))
6869

6970
sort.Sort(ByToolIDAndVersion(tools))
7071

7172
idx := 0
73+
require.Equal(t, "arduino-preprocessor", tools[idx].Name)
74+
require.Equal(t, "0.0.1", tools[idx].Version)
75+
require.Equal(t, Abs(t, "./downloaded_tools/arduino-preprocessor/0.0.1"), tools[idx].Folder)
76+
idx++
7277
require.Equal(t, "arm-none-eabi-gcc", tools[idx].Name)
7378
require.Equal(t, "4.8.3-2014q1", tools[idx].Version)
7479
require.Equal(t, Abs(t, "./downloaded_tools/arm-none-eabi-gcc/4.8.3-2014q1"), tools[idx].Folder)
@@ -136,7 +141,7 @@ func TestLoadLotsOfTools(t *testing.T) {
136141
NoError(t, err)
137142

138143
tools := ctx.Tools
139-
require.Equal(t, 8, len(tools))
144+
require.Equal(t, 9, len(tools))
140145

141146
sort.Sort(ByToolIDAndVersion(tools))
142147

@@ -145,6 +150,10 @@ func TestLoadLotsOfTools(t *testing.T) {
145150
require.Equal(t, "4.0.0-atmel", tools[idx].Version)
146151
require.Equal(t, Abs(t, "./downloaded_board_manager_stuff/arduino/tools/CMSIS/4.0.0-atmel"), tools[idx].Folder)
147152
idx++
153+
require.Equal(t, "arduino-preprocessor", tools[idx].Name)
154+
require.Equal(t, "0.0.1", tools[idx].Version)
155+
require.Equal(t, Abs(t, "./downloaded_tools/arduino-preprocessor/0.0.1"), tools[idx].Folder)
156+
idx++
148157
require.Equal(t, "arm-none-eabi-gcc", tools[idx].Name)
149158
require.Equal(t, "4.8.3-2014q1", tools[idx].Version)
150159
require.Equal(t, Abs(t, "./downloaded_tools/arm-none-eabi-gcc/4.8.3-2014q1"), tools[idx].Folder)

0 commit comments

Comments
 (0)