@@ -135,6 +135,13 @@ fn main() -> Result<()> {
135
135
}
136
136
}
137
137
138
+ #[ derive( Debug , Clone ) ]
139
+ struct BuildContext {
140
+ pub artifact_path : PathBuf ,
141
+ pub bootloader_path : Option < PathBuf > ,
142
+ pub partition_table_path : Option < PathBuf > ,
143
+ }
144
+
138
145
fn flash (
139
146
opts : EspFlashOpts ,
140
147
config : Config ,
@@ -143,7 +150,7 @@ fn flash(
143
150
) -> Result < ( ) > {
144
151
let mut flasher = connect ( & opts. connect_opts , & config) ?;
145
152
146
- let artifact_path = build ( & opts. build_opts , & cargo_config, Some ( flasher. chip ( ) ) )
153
+ let build_ctx = build ( & opts. build_opts , & cargo_config, Some ( flasher. chip ( ) ) )
147
154
. wrap_err ( "Failed to build project" ) ?;
148
155
149
156
// Print the board information once the project has successfully built. We do
@@ -152,7 +159,7 @@ fn flash(
152
159
flasher. board_info ( ) ?;
153
160
154
161
// Read the ELF data from the build path and load it to the target.
155
- let elf_data = fs:: read ( artifact_path) . into_diagnostic ( ) ?;
162
+ let elf_data = fs:: read ( build_ctx . artifact_path ) . into_diagnostic ( ) ?;
156
163
157
164
if opts. flash_opts . ram {
158
165
flasher. load_elf_to_ram ( & elf_data) ?;
@@ -161,13 +168,15 @@ fn flash(
161
168
. flash_opts
162
169
. bootloader
163
170
. as_deref ( )
164
- . or ( metadata. bootloader . as_deref ( ) ) ;
171
+ . or ( metadata. bootloader . as_deref ( ) )
172
+ . or ( build_ctx. bootloader_path . as_deref ( ) ) ;
165
173
166
174
let partition_table = opts
167
175
. flash_opts
168
176
. partition_table
169
177
. as_deref ( )
170
- . or ( metadata. partition_table . as_deref ( ) ) ;
178
+ . or ( metadata. partition_table . as_deref ( ) )
179
+ . or ( build_ctx. partition_table_path . as_deref ( ) ) ;
171
180
172
181
let image_format = opts
173
182
. build_opts
@@ -201,7 +210,7 @@ fn build(
201
210
build_options : & BuildOpts ,
202
211
cargo_config : & CargoConfig ,
203
212
chip : Option < Chip > ,
204
- ) -> Result < PathBuf > {
213
+ ) -> Result < BuildContext > {
205
214
let target = build_options
206
215
. target
207
216
. as_deref ( )
@@ -291,9 +300,31 @@ fn build(
291
300
292
301
// Find artifacts.
293
302
let mut target_artifact = None ;
303
+ let mut bootloader_path = None ;
304
+ let mut partition_table_path = None ;
294
305
295
306
for message in messages {
296
307
match message. into_diagnostic ( ) ? {
308
+ Message :: BuildScriptExecuted ( script)
309
+ if script. package_id . repr . starts_with ( "esp-idf-sys" ) =>
310
+ {
311
+ // If the `esp-idf-sys` package is being used, attempt to use the bootloader and
312
+ // partition table compiled by `embuild` instead.
313
+ let build_path = PathBuf :: from ( script. out_dir ) . join ( "build" ) ;
314
+
315
+ let bl_path = build_path. join ( "bootloader" ) . join ( "bootloader.bin" ) ;
316
+ let pt_path = build_path
317
+ . join ( "partition_table" )
318
+ . join ( "partition-table.bin" ) ;
319
+
320
+ if bl_path. exists ( ) && bl_path. is_file ( ) {
321
+ bootloader_path = Some ( bl_path) ;
322
+ }
323
+
324
+ if pt_path. exists ( ) && pt_path. is_file ( ) {
325
+ partition_table_path = Some ( pt_path) ;
326
+ }
327
+ }
297
328
Message :: CompilerArtifact ( artifact) => {
298
329
if artifact. executable . is_some ( ) {
299
330
if target_artifact. is_some ( ) {
@@ -324,7 +355,13 @@ fn build(
324
355
let target_artifact = target_artifact. ok_or ( Error :: NoArtifact ) ?;
325
356
let artifact_path = target_artifact. executable . unwrap ( ) . into ( ) ;
326
357
327
- Ok ( artifact_path)
358
+ let build_ctx = BuildContext {
359
+ artifact_path,
360
+ bootloader_path,
361
+ partition_table_path,
362
+ } ;
363
+
364
+ Ok ( build_ctx)
328
365
}
329
366
330
367
fn save_image (
@@ -342,8 +379,22 @@ fn save_image(
342
379
343
380
let chip = Chip :: from_target ( target) . ok_or_else ( || Error :: UnknownTarget ( target. into ( ) ) ) ?;
344
381
345
- let path = build ( & opts. build_opts , & cargo_config, Some ( chip) ) ?;
346
- let elf_data = fs:: read ( path) . into_diagnostic ( ) ?;
382
+ let build_ctx = build ( & opts. build_opts , & cargo_config, Some ( chip) ) ?;
383
+ let elf_data = fs:: read ( build_ctx. artifact_path ) . into_diagnostic ( ) ?;
384
+
385
+ let bootloader = opts
386
+ . bootloader
387
+ . as_deref ( )
388
+ . or ( metadata. bootloader . as_deref ( ) )
389
+ . or ( build_ctx. bootloader_path . as_deref ( ) )
390
+ . map ( |p| p. to_path_buf ( ) ) ;
391
+
392
+ let partition_table = opts
393
+ . partition_table
394
+ . as_deref ( )
395
+ . or ( metadata. partition_table . as_deref ( ) )
396
+ . or ( build_ctx. partition_table_path . as_deref ( ) )
397
+ . map ( |p| p. to_path_buf ( ) ) ;
347
398
348
399
let image_format = opts
349
400
. build_opts
@@ -362,8 +413,8 @@ fn save_image(
362
413
opts. build_opts . flash_config_opts . flash_size ,
363
414
opts. build_opts . flash_config_opts . flash_freq ,
364
415
opts. merge ,
365
- opts . bootloader ,
366
- opts . partition_table ,
416
+ bootloader,
417
+ partition_table,
367
418
) ?;
368
419
369
420
Ok ( ( ) )
0 commit comments