Skip to content

Commit 3f9e2f0

Browse files
committed
Merge remote-tracking branch 'facchinm/rpc_experiments'
2 parents 608df9f + 14b777f commit 3f9e2f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2093
-355
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ install:
1414
- go get github.com/arduino/go-timeutils
1515

1616
script:
17+
- go get github.com/arduino/arduino-builder/arduino-builder
1718
- go build -o $HOME/arduino-builder -v github.com/arduino/arduino-builder/arduino-builder
1819
- export TEST_PACKAGES=`go list github.com/arduino/arduino-builder/...`
1920
- RES=0; I=0; for PKG in $TEST_PACKAGES; do go test -v -timeout 30m -covermode=count -coverprofile=coverage.$I.out $PKG; ((RES=RES+$?)); ((I++)); done; ( exit $RES )

arduino-builder/main.go

+66-1
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ import (
3737
"io/ioutil"
3838
"os"
3939
"os/exec"
40+
"runtime"
4041
"strings"
4142
"syscall"
4243

44+
"runtime/pprof"
45+
"runtime/trace"
46+
4347
"github.com/arduino/arduino-builder"
4448
"github.com/arduino/arduino-builder/gohasissues"
49+
"github.com/arduino/arduino-builder/grpc"
4550
"github.com/arduino/arduino-builder/i18n"
4651
"github.com/arduino/arduino-builder/types"
4752
"github.com/arduino/arduino-builder/utils"
@@ -54,6 +59,7 @@ const VERSION = "1.3.25"
5459
const FLAG_ACTION_COMPILE = "compile"
5560
const FLAG_ACTION_PREPROCESS = "preprocess"
5661
const FLAG_ACTION_DUMP_PREFS = "dump-prefs"
62+
const FLAG_ACTION_CODE_COMPLETE_AT = "code-complete-at"
5763
const FLAG_BUILD_OPTIONS_FILE = "build-options-file"
5864
const FLAG_HARDWARE = "hardware"
5965
const FLAG_TOOLS = "tools"
@@ -78,7 +84,11 @@ const FLAG_LOGGER_HUMAN = "human"
7884
const FLAG_LOGGER_HUMANTAGS = "humantags"
7985
const FLAG_LOGGER_MACHINE = "machine"
8086
const FLAG_VERSION = "version"
87+
const FLAG_DAEMON = "daemon"
8188
const FLAG_VID_PID = "vid-pid"
89+
const FLAG_JOBS = "jobs"
90+
const FLAG_TRACE = "trace"
91+
const FLAG_EXPERIMENTAL = "experimental"
8292

8393
type foldersFlag []string
8494

@@ -118,6 +128,7 @@ func (h *propertiesFlag) Set(value string) error {
118128
var compileFlag *bool
119129
var preprocessFlag *bool
120130
var dumpPrefsFlag *bool
131+
var codeCompleteAtFlag *string
121132
var buildOptionsFileFlag *string
122133
var hardwareFoldersFlag foldersFlag
123134
var toolsFoldersFlag foldersFlag
@@ -135,12 +146,17 @@ var debugLevelFlag *int
135146
var warningsLevelFlag *string
136147
var loggerFlag *string
137148
var versionFlag *bool
149+
var daemonFlag *bool
138150
var vidPidFlag *string
151+
var jobsFlag *int
152+
var traceFlag *bool
153+
var experimentalFeatures *bool
139154

140155
func init() {
141156
compileFlag = flag.Bool(FLAG_ACTION_COMPILE, false, "compiles the given sketch")
142157
preprocessFlag = flag.Bool(FLAG_ACTION_PREPROCESS, false, "preprocess the given sketch")
143158
dumpPrefsFlag = flag.Bool(FLAG_ACTION_DUMP_PREFS, false, "dumps build properties used when compiling")
159+
codeCompleteAtFlag = flag.String(FLAG_ACTION_CODE_COMPLETE_AT, "", "output code completions for sketch at a specific location. Location format is \"file:line:col\"")
144160
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")
145161
flag.Var(&hardwareFoldersFlag, FLAG_HARDWARE, "Specify a 'hardware' folder. Can be added multiple times for specifying multiple 'hardware' folders")
146162
flag.Var(&toolsFoldersFlag, FLAG_TOOLS, "Specify a 'tools' folder. Can be added multiple times for specifying multiple 'tools' folders")
@@ -158,12 +174,40 @@ func init() {
158174
warningsLevelFlag = flag.String(FLAG_WARNINGS, "", "Sets warnings level. Available values are '"+FLAG_WARNINGS_NONE+"', '"+FLAG_WARNINGS_DEFAULT+"', '"+FLAG_WARNINGS_MORE+"' and '"+FLAG_WARNINGS_ALL+"'")
159175
loggerFlag = flag.String(FLAG_LOGGER, FLAG_LOGGER_HUMAN, "Sets type of logger. Available values are '"+FLAG_LOGGER_HUMAN+"', '"+FLAG_LOGGER_HUMANTAGS+"', '"+FLAG_LOGGER_MACHINE+"'")
160176
versionFlag = flag.Bool(FLAG_VERSION, false, "prints version and exits")
177+
daemonFlag = flag.Bool(FLAG_DAEMON, false, "daemonizes and serves its functions via rpc")
161178
vidPidFlag = flag.String(FLAG_VID_PID, "", "specify to use vid/pid specific build properties, as defined in boards.txt")
179+
jobsFlag = flag.Int(FLAG_JOBS, 0, "specify how many concurrent gcc processes should run at the same time. Defaults to the number of available cores on the running machine")
180+
traceFlag = flag.Bool(FLAG_TRACE, false, "traces the whole process lifecycle")
181+
experimentalFeatures = flag.Bool(FLAG_EXPERIMENTAL, false, "enables experimental features")
162182
}
163183

164184
func main() {
185+
165186
flag.Parse()
166187

188+
if *traceFlag {
189+
f, err := os.Create("trace.out")
190+
if err != nil {
191+
panic(err)
192+
}
193+
defer f.Close()
194+
195+
f2, err := os.Create("profile.out")
196+
if err != nil {
197+
panic(err)
198+
}
199+
defer f2.Close()
200+
201+
pprof.StartCPUProfile(f2)
202+
defer pprof.StopCPUProfile()
203+
204+
err = trace.Start(f)
205+
if err != nil {
206+
panic(err)
207+
}
208+
defer trace.Stop()
209+
}
210+
167211
if *versionFlag {
168212
fmt.Println("Arduino Builder " + VERSION)
169213
fmt.Println("Copyright (C) 2015 Arduino LLC and contributors")
@@ -173,8 +217,28 @@ func main() {
173217
return
174218
}
175219

220+
if *jobsFlag > 0 {
221+
runtime.GOMAXPROCS(*jobsFlag)
222+
} else {
223+
runtime.GOMAXPROCS(runtime.NumCPU())
224+
}
225+
176226
ctx := &types.Context{}
177227

228+
// place here all experimental features that should live under this flag
229+
if *experimentalFeatures {
230+
ctx.UseArduinoPreprocessor = true
231+
}
232+
233+
if *daemonFlag {
234+
var loggerBuffer []string
235+
logger := i18n.AccumulatorLogger{}
236+
logger.Buffer = &loggerBuffer
237+
//logger := i18n.HumanLogger{}
238+
ctx.SetLogger(logger)
239+
jsonrpc.RegisterAndServeJsonRPC(ctx)
240+
}
241+
178242
if *buildOptionsFileFlag != "" {
179243
buildOptions := make(properties.Map)
180244
if _, err := os.Stat(*buildOptionsFileFlag); err == nil {
@@ -330,7 +394,8 @@ func main() {
330394

331395
if *dumpPrefsFlag {
332396
err = builder.RunParseHardwareAndDumpBuildProperties(ctx)
333-
} else if *preprocessFlag {
397+
} else if *preprocessFlag || *codeCompleteAtFlag != "" {
398+
ctx.CodeCompleteAt = *codeCompleteAtFlag
334399
err = builder.RunPreprocess(ctx)
335400
} else {
336401
if flag.NArg() == 0 {

builder.go

+22-21
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"strconv"
3636
"time"
3737

38+
"github.com/arduino/arduino-builder/builder_utils"
3839
"github.com/arduino/arduino-builder/constants"
3940
"github.com/arduino/arduino-builder/i18n"
4041
"github.com/arduino/arduino-builder/phases"
@@ -89,7 +90,7 @@ func (s *Builder) Run(ctx *types.Context) error {
8990
&WarnAboutArchIncompatibleLibraries{},
9091

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

9495
utils.LogIfVerbose(constants.LOG_LEVEL_INFO, "Compiling sketch..."),
9596
&RecipeByPrefixSuffixRunner{Prefix: constants.HOOKS_SKETCH_PREBUILD, Suffix: constants.HOOKS_PATTERN_SUFFIX},
@@ -128,6 +129,8 @@ func (s *Builder) Run(ctx *types.Context) error {
128129

129130
&PrintUsedLibrariesIfVerbose{},
130131

132+
&ExportProjectCMake{SketchError: mainErr != nil},
133+
131134
&phases.Sizer{SketchError: mainErr != nil},
132135
}
133136
otherErr := runCommands(ctx, commands, false)
@@ -139,6 +142,18 @@ func (s *Builder) Run(ctx *types.Context) error {
139142
return otherErr
140143
}
141144

145+
type PreprocessSketch struct{}
146+
147+
func (s *PreprocessSketch) Run(ctx *types.Context) error {
148+
var commands []types.Command
149+
if ctx.UseArduinoPreprocessor {
150+
commands = append(commands, &PreprocessSketchArduino{})
151+
} else {
152+
commands = append(commands, &ContainerAddPrototypes{})
153+
}
154+
return runCommands(ctx, commands, true)
155+
}
156+
142157
type Preprocess struct{}
143158

144159
func (s *Preprocess) Run(ctx *types.Context) error {
@@ -158,7 +173,7 @@ func (s *Preprocess) Run(ctx *types.Context) error {
158173

159174
&WarnAboutArchIncompatibleLibraries{},
160175

161-
&ContainerAddPrototypes{},
176+
&PreprocessSketch{},
162177

163178
&PrintPreprocessedSource{},
164179
}
@@ -181,36 +196,22 @@ func (s *ParseHardwareAndDumpBuildProperties) Run(ctx *types.Context) error {
181196
}
182197

183198
func runCommands(ctx *types.Context, commands []types.Command, progressEnabled bool) error {
184-
commandsLength := len(commands)
185-
progressForEachCommand := float32(100) / float32(commandsLength)
186199

187-
progress := float32(0)
200+
ctx.Progress.PrintEnabled = progressEnabled
201+
ctx.Progress.Progress = 0
202+
188203
for _, command := range commands {
189204
PrintRingNameIfDebug(ctx, command)
190-
printProgressIfProgressEnabledAndMachineLogger(progressEnabled, ctx, progress)
205+
ctx.Progress.Steps = 100.0 / float64(len(commands))
206+
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx)
191207
err := command.Run(ctx)
192208
if err != nil {
193209
return i18n.WrapError(err)
194210
}
195-
progress += progressForEachCommand
196211
}
197-
198-
printProgressIfProgressEnabledAndMachineLogger(progressEnabled, ctx, 100)
199-
200212
return nil
201213
}
202214

203-
func printProgressIfProgressEnabledAndMachineLogger(progressEnabled bool, ctx *types.Context, progress float32) {
204-
if !progressEnabled {
205-
return
206-
}
207-
208-
log := ctx.GetLogger()
209-
if log.Name() == "machine" {
210-
log.Println(constants.LOG_LEVEL_INFO, constants.MSG_PROGRESS, strconv.FormatFloat(float64(progress), 'f', 2, 32))
211-
}
212-
}
213-
214215
func PrintRingNameIfDebug(ctx *types.Context, command types.Command) {
215216
if ctx.DebugLevel >= 10 {
216217
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_RUNNING_COMMAND, strconv.FormatInt(time.Now().Unix(), 10), reflect.Indirect(reflect.ValueOf(command)).Type().Name())

0 commit comments

Comments
 (0)