Skip to content

Commit a0dfb78

Browse files
author
Federico Fissore
committed
Added --preprocess action: preprocess (doesn't compile) given sketch. Better
call arduino-compiler with the new -quite flag, otherwise output will get cluttered Signed-off-by: Federico Fissore <f.fissore@arduino.cc>
1 parent d17b4a8 commit a0dfb78

File tree

4 files changed

+118
-28
lines changed

4 files changed

+118
-28
lines changed

main.go

+29-28
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ import (
4949

5050
const VERSION = "1.0.7"
5151

52-
const FLAG_COMPILE = "compile"
53-
const FLAG_DUMP_PREFS = "dump-prefs"
52+
const FLAG_ACTION_COMPILE = "compile"
53+
const FLAG_ACTION_PREPROCESS = "preprocess"
54+
const FLAG_ACTION_DUMP_PREFS = "dump-prefs"
5455
const FLAG_BUILD_OPTIONS_FILE = "build-options-file"
5556
const FLAG_HARDWARE = "hardware"
5657
const FLAG_TOOLS = "tools"
@@ -61,6 +62,7 @@ const FLAG_FQBN = "fqbn"
6162
const FLAG_IDE_VERSION = "ide-version"
6263
const FLAG_BUILD_PATH = "build-path"
6364
const FLAG_VERBOSE = "verbose"
65+
const FLAG_QUITE = "quite"
6466
const FLAG_DEBUG_LEVEL = "debug-level"
6567
const FLAG_WARNINGS = "warnings"
6668
const FLAG_WARNINGS_NONE = "none"
@@ -97,6 +99,7 @@ func (h *slice) Set(csv string) error {
9799
}
98100

99101
var compileFlag *bool
102+
var preprocessFlag *bool
100103
var dumpPrefsFlag *bool
101104
var buildOptionsFileFlag *string
102105
var hardwareFoldersFlag slice
@@ -108,6 +111,7 @@ var fqbnFlag *string
108111
var ideVersionFlag *string
109112
var buildPathFlag *string
110113
var verboseFlag *bool
114+
var quiteFlag *bool
111115
var debugLevelFlag *int
112116
var libraryDiscoveryRecursionDepthFlag *int
113117
var warningsLevelFlag *string
@@ -116,8 +120,9 @@ var versionFlag *bool
116120
var vidPidFlag *string
117121

118122
func init() {
119-
compileFlag = flag.Bool(FLAG_COMPILE, false, "compiles the given sketch")
120-
dumpPrefsFlag = flag.Bool(FLAG_DUMP_PREFS, false, "dumps build properties used when compiling")
123+
compileFlag = flag.Bool(FLAG_ACTION_COMPILE, false, "compiles the given sketch")
124+
preprocessFlag = flag.Bool(FLAG_ACTION_PREPROCESS, false, "preprocess the given sketch")
125+
dumpPrefsFlag = flag.Bool(FLAG_ACTION_DUMP_PREFS, false, "dumps build properties used when compiling")
121126
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")
122127
flag.Var(&hardwareFoldersFlag, FLAG_HARDWARE, "Specify a 'hardware' folder. Can be added multiple times for specifying multiple 'hardware' folders")
123128
flag.Var(&toolsFoldersFlag, FLAG_TOOLS, "Specify a 'tools' folder. Can be added multiple times for specifying multiple 'tools' folders")
@@ -128,6 +133,7 @@ func init() {
128133
ideVersionFlag = flag.String(FLAG_IDE_VERSION, "10600", "fake IDE version")
129134
buildPathFlag = flag.String(FLAG_BUILD_PATH, "", "build path")
130135
verboseFlag = flag.Bool(FLAG_VERBOSE, false, "if 'true' prints lots of stuff")
136+
quiteFlag = flag.Bool(FLAG_QUITE, false, "if 'true' doesn't print any warnings or progress or whatever")
131137
debugLevelFlag = flag.Int(FLAG_DEBUG_LEVEL, builder.DEFAULT_DEBUG_LEVEL, "Turns on debugging messages. The higher, the chattier")
132138
warningsLevelFlag = flag.String(FLAG_WARNINGS, "", "Sets warnings level. Available values are '"+FLAG_WARNINGS_NONE+"', '"+FLAG_WARNINGS_DEFAULT+"', '"+FLAG_WARNINGS_MORE+"' and '"+FLAG_WARNINGS_ALL+"'")
133139
loggerFlag = flag.String(FLAG_LOGGER, FLAG_LOGGER_HUMAN, "Sets type of logger. Available values are '"+FLAG_LOGGER_HUMAN+"', '"+FLAG_LOGGER_MACHINE+"'")
@@ -149,19 +155,6 @@ func main() {
149155
return
150156
}
151157

152-
compile := *compileFlag
153-
dumpPrefs := *dumpPrefsFlag
154-
155-
if compile && dumpPrefs {
156-
fmt.Fprintln(os.Stderr, "You can either specify --"+FLAG_COMPILE+" or --"+FLAG_DUMP_PREFS+", not both")
157-
defer os.Exit(1)
158-
return
159-
}
160-
161-
if !compile && !dumpPrefs {
162-
compile = true
163-
}
164-
165158
context := make(map[string]interface{})
166159

167160
buildOptions := make(map[string]string)
@@ -265,13 +258,6 @@ func main() {
265258
context[constants.CTX_VIDPID] = *vidPidFlag
266259
}
267260

268-
if compile && flag.NArg() == 0 {
269-
fmt.Fprintln(os.Stderr, "Last parameter must be the sketch to compile")
270-
flag.Usage()
271-
defer os.Exit(1)
272-
return
273-
}
274-
275261
if flag.NArg() > 0 {
276262
sketchLocation := flag.Arg(0)
277263
sketchLocation, err := gohasissues.Unquote(sketchLocation)
@@ -283,6 +269,11 @@ func main() {
283269
context[constants.CTX_SKETCH_LOCATION] = sketchLocation
284270
}
285271

272+
if *verboseFlag && *quiteFlag {
273+
*verboseFlag = false
274+
*quiteFlag = false
275+
}
276+
286277
context[constants.CTX_VERBOSE] = *verboseFlag
287278

288279
ideVersion := ""
@@ -305,16 +296,26 @@ func main() {
305296
context[constants.CTX_LIBRARY_DISCOVERY_RECURSION_DEPTH] = *libraryDiscoveryRecursionDepthFlag
306297
}
307298

308-
if *loggerFlag == FLAG_LOGGER_MACHINE {
299+
if *quiteFlag {
300+
context[constants.CTX_LOGGER] = i18n.NoopLogger{}
301+
} else if *loggerFlag == FLAG_LOGGER_MACHINE {
309302
context[constants.CTX_LOGGER] = i18n.MachineLogger{}
310303
} else {
311304
context[constants.CTX_LOGGER] = i18n.HumanLogger{}
312305
}
313306

314-
if compile {
315-
err = builder.RunBuilder(context)
316-
} else if dumpPrefs {
307+
if *dumpPrefsFlag {
317308
err = builder.RunParseHardwareAndDumpBuildProperties(context)
309+
} else if *preprocessFlag {
310+
err = builder.RunPreprocess(context)
311+
} else {
312+
if flag.NArg() == 0 {
313+
fmt.Fprintln(os.Stderr, "Last parameter must be the sketch to compile")
314+
flag.Usage()
315+
defer os.Exit(1)
316+
return
317+
}
318+
err = builder.RunBuilder(context)
318319
}
319320

320321
exitCode := 0

src/arduino.cc/builder/builder.go

+34
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,35 @@ func (s *Builder) Run(context map[string]interface{}) error {
128128
return otherErr
129129
}
130130

131+
type Preprocess struct{}
132+
133+
func (s *Preprocess) Run(context map[string]interface{}) error {
134+
commands := []types.Command{
135+
&SetupHumanLoggerIfMissing{},
136+
137+
&GenerateBuildPathIfMissing{},
138+
&EnsureBuildPathExists{},
139+
140+
&ContainerSetupHardwareToolsLibsSketchAndProps{},
141+
142+
&ContainerBuildOptions{},
143+
144+
&RecipeByPrefixSuffixRunner{Prefix: constants.HOOKS_PREBUILD, Suffix: constants.HOOKS_PATTERN_SUFFIX},
145+
146+
&ContainerMergeCopySketchFiles{},
147+
148+
&ContainerFindIncludes{},
149+
150+
&WarnAboutArchIncompatibleLibraries{},
151+
152+
&ContainerAddPrototypes{},
153+
154+
&PrintPreprocessedSource{},
155+
}
156+
157+
return runCommands(context, commands, true)
158+
}
159+
131160
type ParseHardwareAndDumpBuildProperties struct{}
132161

133162
func (s *ParseHardwareAndDumpBuildProperties) Run(context map[string]interface{}) error {
@@ -190,3 +219,8 @@ func RunParseHardwareAndDumpBuildProperties(context map[string]interface{}) erro
190219
command := ParseHardwareAndDumpBuildProperties{}
191220
return command.Run(context)
192221
}
222+
223+
func RunPreprocess(context map[string]interface{}) error {
224+
command := Preprocess{}
225+
return command.Run(context)
226+
}

src/arduino.cc/builder/i18n/i18n.go

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ type Logger interface {
4848
Name() string
4949
}
5050

51+
type NoopLogger struct{}
52+
53+
func (s NoopLogger) Fprintln(w io.Writer, format string, a ...interface{}) {}
54+
55+
func (s NoopLogger) Println(format string, a ...interface{}) {}
56+
57+
func (s NoopLogger) Name() string {
58+
return "noop"
59+
}
60+
5161
type HumanLogger struct{}
5262

5363
func (s HumanLogger) Fprintln(w io.Writer, format string, a ...interface{}) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
"fmt"
35+
)
36+
37+
type PrintPreprocessedSource struct{}
38+
39+
func (s *PrintPreprocessedSource) Run(context map[string]interface{}) error {
40+
source := context[constants.CTX_GCC_MINUS_E_SOURCE].(string)
41+
42+
fmt.Println(source)
43+
44+
return nil
45+
}

0 commit comments

Comments
 (0)