@@ -104,14 +104,52 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
104
104
r .BoardPlatform = targetPlatform .ToRPCPlatformReference ()
105
105
r .BuildPlatform = buildPlatform .ToRPCPlatformReference ()
106
106
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
+ }
107
117
// At the current time we do not have a way of knowing if a board supports the secure boot or not,
108
118
// so, if the flags to override the default keys are used, we try override the corresponding platform property nonetheless.
109
119
// It's not possible to use the default name for the keys since there could be more tools to sign and encrypt.
110
120
// 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 }
114
146
}
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 ())
115
153
116
154
requiredTools , err := pme .FindToolsRequiredForBuild (targetPlatform , buildPlatform )
117
155
if err != nil {
@@ -124,59 +162,33 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
124
162
builderCtx .LibrariesManager = lm
125
163
}
126
164
builderCtx .TargetBoard = targetBoard
127
- builderCtx .TargetBoardBuildProperties = buildProperties
128
165
builderCtx .TargetPlatform = targetPlatform
129
166
builderCtx .TargetPackage = targetPackage
130
167
builderCtx .ActualPlatform = buildPlatform
131
168
builderCtx .RequiredTools = requiredTools
169
+ builderCtx .BuildProperties = buildProperties
132
170
builderCtx .UseCachedLibrariesResolution = req .GetSkipLibrariesDiscovery ()
133
171
builderCtx .FQBN = fqbn
134
172
builderCtx .Sketch = sk
173
+ builderCtx .BuildPath = buildPath
135
174
builderCtx .ProgressCB = progressCB
136
175
137
176
// FIXME: This will be redundant when arduino-builder will be part of the cli
138
177
builderCtx .HardwareDirs = configuration .HardwareDirectories (configuration .Settings )
139
178
builderCtx .BuiltInToolsDirs = configuration .BuiltinToolsDirectories (configuration .Settings )
140
-
141
- // FIXME: This will be redundant when arduino-builder will be part of the cli
142
179
builderCtx .OtherLibrariesDirs = paths .NewPathList (req .GetLibraries ()... )
143
180
builderCtx .OtherLibrariesDirs .Add (configuration .LibrariesDir (configuration .Settings ))
144
181
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 ()
164
182
165
183
builderCtx .CompilationDatabase = bldr .NewCompilationDatabase (
166
184
builderCtx .BuildPath .Join ("compile_commands.json" ),
167
185
)
168
186
169
187
builderCtx .Verbose = req .GetVerbose ()
170
-
171
- // Optimize for debug
172
- builderCtx .OptimizeForDebug = req .GetOptimizeForDebug ()
173
-
174
188
builderCtx .Jobs = int (req .GetJobs ())
175
-
176
189
builderCtx .WarningsLevel = req .GetWarnings ()
177
190
178
191
builderCtx .CustomBuildProperties = append (req .GetBuildProperties (), "build.warn_data_percentage=75" )
179
- builderCtx .CustomBuildProperties = append (req .GetBuildProperties (), securityKeysOverride ... )
180
192
181
193
if req .GetBuildCachePath () == "" {
182
194
builderCtx .CoreBuildCachePath = paths .TempDir ().Join ("arduino" , "cores" )
0 commit comments