@@ -248,6 +248,9 @@ def update(debug, board, update_all):
248248 default_settings = []
249249 last_default_group = None
250250
251+ target_kconfig_snippets = set ()
252+ target_symbols = set ()
253+
251254 current_group = []
252255
253256 for sym in kconfig .unique_defined_syms :
@@ -282,26 +285,63 @@ def update(debug, board, update_all):
282285
283286 config_string = item .config_string .strip ()
284287 if not config_string :
285- if matches_group ("CONFIG_" + item .name , ALWAYS_INCLUDE ):
288+ cp_sym = cp_kconfig_defaults .syms [item .name ]
289+ if cp_sym .str_value == "n" :
286290 config_string = f"# CONFIG_{ item .name } is not set"
287- print (config_string )
288291 else :
289- continue
292+ config_string = ""
290293
291294 if node .list :
292295 pending_nodes .append (node .list )
293296
294297 matches_cp_default = cp_kconfig_defaults .syms [item .name ].str_value == item .str_value
295298 matches_esp_default = sym_default (item )
296299
297- if not matches_esp_default :
300+ print_debug = not matches_esp_default
301+ if print_debug :
298302 print (" " * len (current_group ), i , config_string .strip ())
299303
300- target_reference = False
304+ # Some files are `rsource`d into another kconfig with $IDF_TARGET as
305+ # part of the path. kconfiglib doesn't show this as a reference so
306+ # we have to look ourselves.
307+ target_reference = target in item .name_and_loc
308+ if target_reference :
309+ loc = item .name_and_loc .split ("defined at " )[1 ].split (":" )[0 ].replace (target , "*" )
310+ if loc not in target_kconfig_snippets :
311+ differing_keys = set ()
312+ shared_keys = {}
313+ first = True
314+ for path in pathlib .Path ("." ).glob (loc ):
315+ kc = kconfiglib .Kconfig (path )
316+ all_file_syms = set ()
317+ for sym in kc .unique_defined_syms :
318+ all_file_syms .add (sym )
319+ if sym .name in differing_keys :
320+ continue
321+ if first :
322+ shared_keys [sym .name ] = sym .str_value
323+ elif (
324+ sym .name not in shared_keys
325+ or shared_keys [sym .name ] != sym .str_value
326+ ):
327+ differing_keys .add (sym .name )
328+ if sym .name in shared_keys :
329+ del shared_keys [sym .name ]
330+ # Any settings missing from a file are *not* shared.
331+ shared_syms = set (shared_keys .keys ())
332+ for missing in shared_syms - all_file_syms :
333+ differing_keys .add (missing )
334+ del shared_keys [missing ]
335+ first = False
336+ target_kconfig_snippets .add (loc )
337+ target_symbols = target_symbols .union (differing_keys )
301338 psram_reference = False
302339 for referenced in item .referenced :
303340 if referenced .name .startswith ("IDF_TARGET" ):
304- # print(item.name, "references", referenced.name)
341+ target_reference = True
342+ break
343+ if referenced .name in target_symbols :
344+ # Implicit target symbols
305345 target_reference = True
306346 break
307347 if referenced .name == "SPIRAM" :
@@ -335,20 +375,22 @@ def update(debug, board, update_all):
335375 elif matches_group (config_string , PSRAM_MODE_SETTINGS ):
336376 print (" " * (len (current_group ) + 1 ), "psram mode" )
337377 psram_mode_settings .append (config_string )
338- elif matches_group (config_string , PSRAM_FREQ_SETTINGS ) and not target_setting :
339- # The ESP32S2 has two frequencies that aren't on the S3 or ESP32. So, put those
340- # in target settings.
378+ elif (
379+ matches_group (config_string , PSRAM_FREQ_SETTINGS )
380+ and "26M" not in config_string
381+ and "20M" not in config_string
382+ ):
383+ # The ESP32S2 has two frequencies (20M and 26M) that aren't on the S3 or ESP32.
384+ # So, put those in target settings.
341385 print (" " * (len (current_group ) + 1 ), "psram freq" )
342386 psram_freq_settings .append (config_string )
343387 elif matches_esp_default :
388+ if print_debug :
389+ print (" " * (len (current_group ) + 1 ), "default" )
344390 # Always document the above settings. Settings below should
345391 # be non-default.
346392 pass
347- elif (
348- (matches_group (config_string , PSRAM_SETTINGS ) or psram_reference )
349- and not target_reference
350- and not target_setting
351- ):
393+ elif matches_group (config_string , PSRAM_SETTINGS ) or psram_reference :
352394 print (" " * (len (current_group ) + 1 ), "psram shared" )
353395 last_psram_group = add_group (psram_settings , last_psram_group , current_group )
354396 psram_settings .append (config_string )
@@ -373,12 +415,11 @@ def update(debug, board, update_all):
373415 )
374416 default_settings .append (config_string )
375417
376- elif kconfiglib . expr_value ( node . dep ) :
418+ else :
377419 if item is kconfiglib .COMMENT :
378420 print ("comment" , repr (item ))
379421 elif item is kconfiglib .MENU :
380- # This menu isn't visible so skip to the next node.
381- if kconfiglib .expr_value (node .visibility ) and node .list :
422+ if node .list :
382423 current_group .append (node .prompt [0 ])
383424 pending_nodes .append (None )
384425 pending_nodes .append (node .list )
@@ -398,18 +439,25 @@ def update(debug, board, update_all):
398439
399440 board_config .write_text ("\n " .join (board_settings ))
400441 if update_all :
442+ # Add empty strings to get trailing newlines
443+ flash_mode_settings .append ("" )
444+ flash_freq_settings .append ("" )
401445 flash_size_config .write_text ("\n " .join (flash_size_settings ))
402446 flash_mode_config .write_text ("\n " .join (flash_mode_settings ))
403447 flash_freq_config .write_text ("\n " .join (flash_freq_settings ))
404448 if psram_size != "0" :
449+ psram_size_settings .append ("" )
450+ psram_mode_settings .append ("" )
451+ psram_freq_settings .append ("" )
405452 psram_config .write_text ("\n " .join (psram_settings ))
406453 psram_size_config .write_text ("\n " .join (psram_size_settings ))
407454 psram_mode_config .write_text ("\n " .join (psram_mode_settings ))
408455 psram_freq_config .write_text ("\n " .join (psram_freq_settings ))
409456 opt_config .write_text ("\n " .join (opt_settings ))
410457 default_config .write_text ("\n " .join (default_settings ))
411458 target_config .write_text ("\n " .join (target_settings ))
412- ble_config .write_text ("\n " .join (ble_settings ))
459+ if ble_settings :
460+ ble_config .write_text ("\n " .join (ble_settings ))
413461
414462
415463if __name__ == "__main__" :
0 commit comments