@@ -132,8 +132,10 @@ def __init__(self, name, unit_name, unit_kind):
132
132
if len (tmp ) != 2 :
133
133
raise ValueError ("Invalid macro definition '%s' in '%s'" % (name , self .defined_by ))
134
134
self .macro_name = tmp [0 ]
135
+ self .macro_value = tmp [1 ]
135
136
else :
136
137
self .macro_name = name
138
+ self .macro_value = None
137
139
138
140
# 'Config' implements the mbed configuration mechanism
139
141
class Config :
@@ -343,13 +345,13 @@ def get_app_config_data(self, params, macros):
343
345
344
346
# Return the configuration data in two parts:
345
347
# - params: a dictionary with (name, ConfigParam) entries
346
- # - macros: the list of macros defined with "macros" in libraries and in the application
348
+ # - macros: the list of macros defined with "macros" in libraries and in the application (as ConfigMacro instances)
347
349
def get_config_data (self ):
348
350
all_params = self .get_target_config_data ()
349
351
lib_params , macros = self .get_lib_config_data ()
350
352
all_params .update (lib_params )
351
353
self .get_app_config_data (all_params , macros )
352
- return all_params , [ m . name for m in macros . values ()]
354
+ return all_params , macros
353
355
354
356
# Helper: verify if there are any required parameters without a value in 'params'
355
357
def _check_required_parameters (self , params ):
@@ -363,11 +365,17 @@ def _check_required_parameters(self, params):
363
365
def parameters_to_macros (params ):
364
366
return ['%s=%s' % (m .macro_name , m .value ) for m in params .values () if m .value is not None ]
365
367
368
+ # Return the macro definitions generated for a dictionary of ConfigMacros (as returned by get_config_data)
369
+ # params: a dictionary of (name, ConfigMacro instance) mappings
370
+ @staticmethod
371
+ def config_macros_to_macros (macros ):
372
+ return [m .name for m in macros .values ()]
373
+
366
374
# Return the configuration data converted to a list of C macros
367
375
def get_config_data_macros (self ):
368
376
params , macros = self .get_config_data ()
369
377
self ._check_required_parameters (params )
370
- return macros + self .parameters_to_macros (params )
378
+ return self . config_macros_to_macros ( macros ) + self .parameters_to_macros (params )
371
379
372
380
# Returns any features in the configuration data
373
381
def get_features (self ):
@@ -387,4 +395,43 @@ def validate_config(self):
387
395
if self .config_errors :
388
396
raise self .config_errors [0 ]
389
397
return True
390
-
398
+
399
+ # Return the configuration data converted to the content of a C header file,
400
+ # meant to be included to a C/C++ file. The content is returned as a string.
401
+ # If 'fname' is given, the content is also written to the file called "fname".
402
+ # WARNING: if 'fname' names an existing file, that file will be overwritten!
403
+ def get_config_data_header (self , fname = None ):
404
+ params , macros = self .get_config_data ()
405
+ self ._check_required_parameters (params )
406
+ header_data = "// Automatically generated configuration file.\n "
407
+ header_data += "// DO NOT EDIT, content will be overwritten.\n \n "
408
+ header_data += "#ifndef __MBED_CONFIG_DATA__\n "
409
+ header_data += "#define __MBED_CONFIG_DATA__\n \n "
410
+ # Compute maximum length of macro names for proper alignment
411
+ max_param_macro_name_len = max ([len (m .macro_name ) for m in params .values () if m .value is not None ]) if params else 0
412
+ max_direct_macro_name_len = max ([len (m .macro_name ) for m in macros .values ()]) if macros else 0
413
+ max_macro_name_len = max (max_param_macro_name_len , max_direct_macro_name_len )
414
+ # Compute maximum length of macro values for proper alignment
415
+ max_param_macro_val_len = max ([len (str (m .value )) for m in params .values () if m .value is not None ]) if params else 0
416
+ max_direct_macro_val_len = max ([len (m .macro_value or "" ) for m in macros .values ()]) if macros else 0
417
+ max_macro_val_len = max (max_param_macro_val_len , max_direct_macro_val_len )
418
+ # Generate config parameters first
419
+ if params :
420
+ header_data += "// Configuration parameters\n "
421
+ for m in params .values ():
422
+ if m .value is not None :
423
+ header_data += "#define {0:<{1}} {2!s:<{3}} // set by {4}\n " .format (m .macro_name , max_macro_name_len , m .value , max_macro_val_len , m .set_by )
424
+ # Then macros
425
+ if macros :
426
+ header_data += "// Macros\n "
427
+ for m in macros .values ():
428
+ if m .macro_value :
429
+ header_data += "#define {0:<{1}} {2!s:<{3}} // defined by {4}\n " .format (m .macro_name , max_macro_name_len , m .macro_value , max_macro_val_len , m .defined_by )
430
+ else :
431
+ header_data += "#define {0:<{1}} // defined by {2}\n " .format (m .macro_name , max_macro_name_len + max_macro_val_len + 1 , m .defined_by )
432
+ header_data += "\n #endif\n "
433
+ # If fname is given, write "header_data" to it
434
+ if fname :
435
+ with open (fname , "wt" ) as f :
436
+ f .write (header_data )
437
+ return header_data
0 commit comments