Skip to content

Commit e2b2496

Browse files
committed
Factored sketch-related build properties creation
1 parent 0616fae commit e2b2496

17 files changed

+201
-278
lines changed

Diff for: commands/compile/compile.go

+43-31
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,52 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
104104
r.BoardPlatform = targetPlatform.ToRPCPlatformReference()
105105
r.BuildPlatform = buildPlatform.ToRPCPlatformReference()
106106

107+
// Setup sign keys if requested
108+
if req.KeysKeychain != "" {
109+
buildProperties.Set("build.keys.keychain", req.GetKeysKeychain())
110+
}
111+
if req.SignKey != "" {
112+
buildProperties.Set("build.keys.sign_key", req.GetSignKey())
113+
}
114+
if req.EncryptKey != "" {
115+
buildProperties.Set("build.keys.encrypt_key", req.GetEncryptKey())
116+
}
107117
// At the current time we do not have a way of knowing if a board supports the secure boot or not,
108118
// so, if the flags to override the default keys are used, we try override the corresponding platform property nonetheless.
109119
// It's not possible to use the default name for the keys since there could be more tools to sign and encrypt.
110120
// So it's mandatory to use all three flags to sign and encrypt the binary
111-
securityKeysOverride := []string{}
112-
if req.KeysKeychain != "" && req.SignKey != "" && req.EncryptKey != "" {
113-
securityKeysOverride = append(securityKeysOverride, "build.keys.keychain="+req.KeysKeychain, "build.keys.sign_key="+req.GetSignKey(), "build.keys.encrypt_key="+req.EncryptKey)
121+
keychainProp := buildProperties.ContainsKey("build.keys.keychain")
122+
signProp := buildProperties.ContainsKey("build.keys.sign_key")
123+
encryptProp := buildProperties.ContainsKey("build.keys.encrypt_key")
124+
// we verify that all the properties for the secure boot keys are defined or none of them is defined.
125+
if !(keychainProp == signProp && signProp == encryptProp) {
126+
return nil, fmt.Errorf(tr("Firmware encryption/signing requires all the following properties to be defined: %s", "build.keys.keychain, build.keys.sign_key, build.keys.encrypt_key"))
127+
}
128+
129+
// Generate or retrieve build path
130+
var buildPath *paths.Path
131+
if buildPathArg := req.GetBuildPath(); buildPathArg != "" {
132+
buildPath = paths.New(req.GetBuildPath()).Canonical()
133+
if in, err := buildPath.IsInsideDir(sk.FullPath); err != nil {
134+
return nil, &arduino.NotFoundError{Message: tr("Cannot find build path"), Cause: err}
135+
} else if in && buildPath.IsDir() {
136+
if sk.AdditionalFiles, err = removeBuildFromSketchFiles(sk.AdditionalFiles, buildPath); err != nil {
137+
return nil, err
138+
}
139+
}
140+
}
141+
if buildPath == nil {
142+
buildPath = sk.DefaultBuildPath()
143+
}
144+
if err = buildPath.MkdirAll(); err != nil {
145+
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build directory"), Cause: err}
114146
}
147+
buildcache.New(buildPath.Parent()).GetOrCreate(buildPath.Base())
148+
// cache is purged after compilation to not remove entries that might be required
149+
defer maybePurgeBuildCache()
150+
151+
// Add build properites related to sketch data
152+
buildProperties = builder.SetupBuildProperties(buildProperties, buildPath, sk, req.GetOptimizeForDebug())
115153

116154
requiredTools, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform)
117155
if err != nil {
@@ -124,59 +162,33 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
124162
builderCtx.LibrariesManager = lm
125163
}
126164
builderCtx.TargetBoard = targetBoard
127-
builderCtx.TargetBoardBuildProperties = buildProperties
128165
builderCtx.TargetPlatform = targetPlatform
129166
builderCtx.TargetPackage = targetPackage
130167
builderCtx.ActualPlatform = buildPlatform
131168
builderCtx.RequiredTools = requiredTools
169+
builderCtx.BuildProperties = buildProperties
132170
builderCtx.UseCachedLibrariesResolution = req.GetSkipLibrariesDiscovery()
133171
builderCtx.FQBN = fqbn
134172
builderCtx.Sketch = sk
173+
builderCtx.BuildPath = buildPath
135174
builderCtx.ProgressCB = progressCB
136175

137176
// FIXME: This will be redundant when arduino-builder will be part of the cli
138177
builderCtx.HardwareDirs = configuration.HardwareDirectories(configuration.Settings)
139178
builderCtx.BuiltInToolsDirs = configuration.BuiltinToolsDirectories(configuration.Settings)
140-
141-
// FIXME: This will be redundant when arduino-builder will be part of the cli
142179
builderCtx.OtherLibrariesDirs = paths.NewPathList(req.GetLibraries()...)
143180
builderCtx.OtherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings))
144181
builderCtx.LibraryDirs = paths.NewPathList(req.Library...)
145-
if req.GetBuildPath() == "" {
146-
builderCtx.BuildPath = sk.DefaultBuildPath()
147-
} else {
148-
builderCtx.BuildPath = paths.New(req.GetBuildPath()).Canonical()
149-
if in, err := builderCtx.BuildPath.IsInsideDir(sk.FullPath); err != nil {
150-
return nil, &arduino.NotFoundError{Message: tr("Cannot find build path"), Cause: err}
151-
} else if in && builderCtx.BuildPath.IsDir() {
152-
if sk.AdditionalFiles, err = removeBuildFromSketchFiles(sk.AdditionalFiles, builderCtx.BuildPath); err != nil {
153-
return nil, err
154-
}
155-
}
156-
}
157-
if err = builderCtx.BuildPath.MkdirAll(); err != nil {
158-
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build directory"), Cause: err}
159-
}
160-
161-
buildcache.New(builderCtx.BuildPath.Parent()).GetOrCreate(builderCtx.BuildPath.Base())
162-
// cache is purged after compilation to not remove entries that might be required
163-
defer maybePurgeBuildCache()
164182

165183
builderCtx.CompilationDatabase = bldr.NewCompilationDatabase(
166184
builderCtx.BuildPath.Join("compile_commands.json"),
167185
)
168186

169187
builderCtx.Verbose = req.GetVerbose()
170-
171-
// Optimize for debug
172-
builderCtx.OptimizeForDebug = req.GetOptimizeForDebug()
173-
174188
builderCtx.Jobs = int(req.GetJobs())
175-
176189
builderCtx.WarningsLevel = req.GetWarnings()
177190

178191
builderCtx.CustomBuildProperties = append(req.GetBuildProperties(), "build.warn_data_percentage=75")
179-
builderCtx.CustomBuildProperties = append(req.GetBuildProperties(), securityKeysOverride...)
180192

181193
if req.GetBuildCachePath() == "" {
182194
builderCtx.CoreBuildCachePath = paths.TempDir().Join("arduino", "cores")

Diff for: legacy/builder/container_setup.go

+2-22
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,15 @@ import (
2323
type ContainerSetupHardwareToolsLibsSketchAndProps struct{}
2424

2525
func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context) error {
26-
// total number of steps in this container: 14
27-
ctx.Progress.AddSubSteps(14)
26+
// total number of steps in this container: 4
27+
ctx.Progress.AddSubSteps(4)
2828
defer ctx.Progress.RemoveSubSteps()
29-
3029
commands := []types.Command{
3130
&AddAdditionalEntriesToContext{},
3231
&FailIfBuildPathEqualsSketchPath{},
3332
&LibrariesLoader{},
34-
}
35-
36-
for _, command := range commands {
37-
PrintRingNameIfDebug(ctx, command)
38-
err := command.Run(ctx)
39-
if err != nil {
40-
return errors.WithStack(err)
41-
}
42-
ctx.Progress.CompleteStep()
43-
ctx.PushProgress()
44-
}
45-
46-
ctx.Progress.CompleteStep()
47-
ctx.PushProgress()
48-
49-
commands = []types.Command{
50-
&SetupBuildProperties{},
5133
&SetCustomBuildProperties{},
5234
}
53-
5435
for _, command := range commands {
5536
PrintRingNameIfDebug(ctx, command)
5637
err := command.Run(ctx)
@@ -60,6 +41,5 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
6041
ctx.Progress.CompleteStep()
6142
ctx.PushProgress()
6243
}
63-
6444
return nil
6545
}

Diff for: legacy/builder/setup_build_properties.go

+19-30
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,35 @@
1616
package builder
1717

1818
import (
19-
"github.com/arduino/arduino-cli/legacy/builder/types"
19+
"github.com/arduino/arduino-cli/arduino/sketch"
20+
"github.com/arduino/go-paths-helper"
2021
properties "github.com/arduino/go-properties-orderedmap"
21-
"github.com/pkg/errors"
2222
)
2323

24-
type SetupBuildProperties struct{}
25-
26-
func (s *SetupBuildProperties) Run(ctx *types.Context) error {
24+
func SetupBuildProperties(boardBuildProperties *properties.Map,
25+
buildPath *paths.Path,
26+
sketch *sketch.Sketch,
27+
optimizeForDebug bool,
28+
) *properties.Map {
2729
buildProperties := properties.NewMap()
28-
buildProperties.Merge(ctx.TargetBoardBuildProperties)
30+
buildProperties.Merge(boardBuildProperties)
2931

30-
if ctx.BuildPath != nil {
31-
buildProperties.SetPath("build.path", ctx.BuildPath)
32+
if buildPath != nil {
33+
buildProperties.SetPath("build.path", buildPath)
3234
}
33-
if ctx.Sketch != nil {
34-
buildProperties.Set("build.project_name", ctx.Sketch.MainFile.Base())
35+
if sketch != nil {
36+
buildProperties.Set("build.project_name", sketch.MainFile.Base())
37+
buildProperties.SetPath("build.source.path", sketch.FullPath)
3538
}
36-
37-
if ctx.OptimizeForDebug {
38-
if buildProperties.ContainsKey("compiler.optimization_flags.debug") {
39-
buildProperties.Set("compiler.optimization_flags", buildProperties.Get("compiler.optimization_flags.debug"))
39+
if optimizeForDebug {
40+
if debugFlags, ok := buildProperties.GetOk("compiler.optimization_flags.debug"); ok {
41+
buildProperties.Set("compiler.optimization_flags", debugFlags)
4042
}
4143
} else {
42-
if buildProperties.ContainsKey("compiler.optimization_flags.release") {
43-
buildProperties.Set("compiler.optimization_flags", buildProperties.Get("compiler.optimization_flags.release"))
44+
if releaseFlags, ok := buildProperties.GetOk("compiler.optimization_flags.release"); ok {
45+
buildProperties.Set("compiler.optimization_flags", releaseFlags)
4446
}
4547
}
46-
ctx.OptimizationFlags = buildProperties.Get("compiler.optimization_flags")
47-
48-
buildProperties.SetPath("build.source.path", ctx.Sketch.FullPath)
49-
50-
keychainProp := buildProperties.ContainsKey("build.keys.keychain")
51-
signProp := buildProperties.ContainsKey("build.keys.sign_key")
52-
encryptProp := buildProperties.ContainsKey("build.keys.encrypt_key")
53-
// we verify that all the properties for the secure boot keys are defined or none of them is defined.
54-
if (keychainProp || signProp || encryptProp) && !(keychainProp && signProp && encryptProp) {
55-
return errors.Errorf("%s platform does not specify correctly default sign and encryption keys", ctx.TargetPlatform.Platform)
56-
}
57-
58-
ctx.BuildProperties = buildProperties
5948

60-
return nil
49+
return buildProperties
6150
}

0 commit comments

Comments
 (0)