8000 Merge branch 'preprocessor' into cmake_export_parallel · arduino/arduino-builder@5264c7b · GitHub
[go: up one dir, main page]

Skip to content

Commit 5264c7b

Browse files
committed
Merge branch 'preprocessor' into cmake_export_parallel
2 parents a66bcc7 + 9bb2743 commit 5264c7b

File tree

7 files changed

+162
-10
lines changed

7 files changed

+162
-10
lines changed

src/arduino.cc/arduino-builder/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const VERSION = "1.3.25"
5555
const FLAG_ACTION_COMPILE = "compile"
5656
const FLAG_ACTION_PREPROCESS = "preprocess"
5757
const FLAG_ACTION_DUMP_PREFS = "dump-prefs"
58+
const FLAG_ACTION_CODE_COMPLETE_AT = "code-complete-at"
5859
const FLAG_BUILD_OPTIONS_FILE = "build-options-file"
5960
const FLAG_HARDWARE = "hardware"
6061
const FLAG_TOOLS = "tools"
@@ -120,6 +121,7 @@ func (h *propertiesFlag) Set(value string) error {
120121
var compileFlag *bool
121122
var preprocessFlag *bool
122123
var dumpPrefsFlag *bool
124+
var codeCompleteAtFlag *string
123125
var buildOptionsFileFlag *string
124126
var hardwareFoldersFlag foldersFlag
125127
var toolsFoldersFlag foldersFlag
@@ -144,6 +146,7 @@ func init() {
144146
compileFlag = flag.Bool(FLAG_ACTION_COMPILE, false, "compiles the given sketch")
145147
preprocessFlag = flag.Bool(FLAG_ACTION_PREPROCESS, false, "preprocess the given sketch")
146148
dumpPrefsFlag = flag.Bool(FLAG_ACTION_DUMP_PREFS, false, "dumps build properties used when compiling")
149+
codeCompleteAtFlag = flag.String(FLAG_ACTION_CODE_COMPLETE_AT, "", "output code completions for sketch at a specific location. Location format is \"file:line:col\"")
147150
buildOptionsFileFlag = flag.String(FLAG_BUILD_OPTIONS_FILE, "", "Instead of specifying --"+FLAG_HARDWARE+", --"+FLAG_TOOLS+" etc every time, you can load all such options from a file")
148151
flag.Var(&hardwareFoldersFlag, FLAG_HARDWARE, "Specify a 'hardware' folder. Can be added multiple times for specifying multiple 'hardware' folders")
149152
flag.Var(&toolsFoldersFlag, FLAG_TOOLS, "Specify a 'tools' folder. Can be added multiple times for specifying multiple 'tools' folders")
@@ -340,7 +343,8 @@ func main() {
340343

341344
if *dumpPrefsFlag {
342345
err = builder.RunParseHardwareAndDumpBuildProperties(ctx)
343-
} else if *preprocessFlag {
346+
} else if *preprocessFlag || *codeCompleteAtFlag != "" {
347+
ctx.CodeCompleteAt = *codeCompleteAtFlag
344348
err = builder.RunPreprocess(ctx)
345349
} else {
346350
if flag.NArg() == 0 {

src/arduino.cc/builder/builder.go

Lines changed: 2 additions & 2 deletions
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},
@@ -160,7 +160,7 @@ func (s *Preprocess) Run(ctx *types.Context) error {
160160

161161
&WarnAboutArchIncompatibleLibraries{},
162162

163-
&ContainerAddPrototypes{},
163+
&PreprocessSketch{},
164164

165165
&PrintPreprocessedSource{},
166166
}

src/arduino.cc/builder/hardware/platform.txt

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

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,17 @@ func DownloadCoresAndToolsAndLibraries(t *testing.T) {
112112
OsUrl{Os: "i686-mingw32", Url: "http://downloads.arduino.cc/tools/ctags-5.8-arduino11-i686-mingw32.zip"},
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"},
115-
}},
115+
},
116+
},
117+
Tool{Name: "arduino-preprocessor", Version: "0.1.1",
118+
OsUrls: []OsUrl{
119+
OsUrl{Os: "i686-pc-linux-gnu", Url: "https://github.com/arduino/arduino-preprocessor/releases/download/0.1.1/arduino-preprocessor-0.1.1-i686-pc-linux-gnu.tar.bz2"},
120+
OsUrl{Os: "x86_64-pc-linux-gnu", Url: "https://github.com/arduino/arduino-preprocessor/releases/download/0.1.1/arduino-preprocessor-0.1.1-x86_64-pc-linux-gnu.tar.bz2"},
121+
OsUrl{Os: "i686-mingw32", Url: "https://github.com/arduino/arduino-preprocessor/releases/download/0.1.1/arduino-preprocessor-0.1.1-i686-pc-cygwin.tar.bz2"},
122+
OsUrl{Os: "x86_64-apple-darwin", Url: "https://github.com/arduino/arduino-preprocessor/releases/download/0.1.1/arduino-preprocessor-0.1.1-x86_64-apple-darwin11.tar.bz2"},
123+
OsUrl{Os: "arm-linux-gnueabihf", Url: "https://github.com/arduino/arduino-preprocessor/releases/download/0.1.1/arduino-preprocessor-0.1.1-armhf-pc-linux-gnu.tar.bz2"},
124+
},
125+
},
116126
}
117127

118128
boardsManagerTools := []Tool{
@@ -698,9 +708,9 @@ func translateGOOSGOARCHToPackageIndexValue() []string {
698708
case "linux-386":
699709
return []string{"i686-pc-linux-gnu", "i686-linux-gnu"}
700710
case "windows-amd64":
701-
return []string{"i686-mingw32"}
711+
return []string{"i686-mingw32", "i686-cygwin"}
702712
case "windows-386":
703-
return []string{"i686-mingw32"}
713+
return []string{"i686-mingw32", "i686-cygwin"}
704714
case "darwin-amd64":
705715
return []string{"i386-apple-darwin11", "x86_64-apple-darwin"}
706716
case "linux-arm":

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

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

3232
import (
33+
"sort"
34+
"testing"
35+
3336
"arduino.cc/builder"
3437
"arduino.cc/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.1.1", tools[idx].Version)
75+
require.Equal(t, Abs(t, "./downloaded_tools/arduino-preprocessor/0.1.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.1.1", tools[idx].Version)
155+
require.Equal(t, Abs(t, "./downloaded_tools/arduino-preprocessor/0.1.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)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Context struct {
1818
SketchLocation string
1919
ArduinoAPIVersion string
2020
FQBN string
21+
CodeCompleteAt string
2122

2223
// Build options are serialized here
2324
BuildOptionsJson string
@@ -53,6 +54,7 @@ type Context struct {
5354
Sketch *Sketch
5455
Source string
5556
SourceGccMinusE string
57+
CodeCompletions string
5658

5759
WarningsLevel string
5860

0 commit comments

Comments
 (0)
0