|
17 | 17 |
|
18 | 18 | from copy import deepcopy
|
19 | 19 | import os
|
| 20 | +from os.path import dirname, abspath |
20 | 21 | import sys
|
21 | 22 | from collections import namedtuple
|
22 | 23 | from os.path import splitext
|
23 | 24 | from intelhex import IntelHex
|
| 25 | +from jinja2 import FileSystemLoader, StrictUndefined |
| 26 | +from jinja2.environment import Environment |
24 | 27 | # Implementation of mbed configuration mechanism
|
25 | 28 | from tools.utils import json_file_to_dict, intelhex_offset
|
26 | 29 | from tools.arm_pack_manager import Cache
|
@@ -852,57 +855,25 @@ def config_to_header(config, fname=None):
|
852 | 855 | WARNING: if 'fname' names an existing file, it will be
|
853 | 856 | overwritten!
|
854 | 857 | """
|
855 |
| - params, macros = config[0], config[1] |
| 858 | + params, macros = config[0] or {}, config[1] or {} |
856 | 859 | Config._check_required_parameters(params)
|
857 |
| - header_data = "// Automatically generated configuration file.\n" |
858 |
| - header_data += "// DO NOT EDIT, content will be overwritten.\n\n" |
859 |
| - header_data += "#ifndef __MBED_CONFIG_DATA__\n" |
860 |
| - header_data += "#define __MBED_CONFIG_DATA__\n\n" |
861 |
| - # Compute maximum length of macro names for proper alignment |
862 |
| - max_param_macro_name_len = (max([len(m.macro_name) for m |
863 |
| - in params.values() |
864 |
| - if m.value is not None]) |
865 |
| - if params else 0) |
866 |
| - max_direct_macro_name_len = (max([len(m.macro_name) for m |
867 |
| - in macros.values()]) |
868 |
| - if macros else 0) |
869 |
| - max_macro_name_len = max(max_param_macro_name_len, |
870 |
| - max_direct_macro_name_len) |
871 |
| - # Compute maximum length of macro values for proper alignment |
872 |
| - max_param_macro_val_len = (max([len(str(m.value)) for m |
873 |
| - in params.values() |
874 |
| - if m.value is not None]) |
875 |
| - if params else 0) |
876 |
| - max_direct_macro_val_len = max([len(m.macro_value or "") for m |
877 |
| - in macros.values()]) if macros else 0 |
878 |
| - max_macro_val_len = max(max_param_macro_val_len, |
879 |
| - max_direct_macro_val_len) |
880 |
| - # Generate config parameters first |
881 |
| - if params: |
882 |
| - header_data += "// Configuration parameters\n" |
883 |
| - for macro in params.values(): |
884 |
| - if macro.value is not None: |
885 |
| - header_data += ("#define {0:<{1}} {2!s:<{3}} " + |
886 |
| - "// set by {4}\n")\ |
887 |
| - .format(macro.macro_name, max_macro_name_len, |
888 |
| - macro.value, max_macro_val_len, macro.set_by) |
889 |
| - # Then macros |
890 |
| - if macros: |
891 |
| - header_data += "// Macros\n" |
892 |
| - for macro in macros.values(): |
893 |
| - if macro.macro_value: |
894 |
| - header_data += ("#define {0:<{1}} {2!s:<{3}}" + |
895 |
| - " // defined by {4}\n")\ |
896 |
| - .format(macro.macro_name, max_macro_name_len, |
897 |
| - macro.macro_value, max_macro_val_len, |
898 |
| - macro.defined_by) |
899 |
| - else: |
900 |
| - header_data += ("#define {0:<{1}}" + |
901 |
| - " // defined by {2}\n")\ |
902 |
| - .format(macro.macro_name, |
903 |
| - max_macro_name_len + max_macro_val_len + 1, |
904 |
| - macro.defined_by) |
905 |
| - header_data += "\n#endif\n" |
| 860 | + params_with_values = [p for p in params.values() if p.value is not None] |
| 861 | + ctx = { |
| 862 | + "cfg_params" : [(p.macro_name, str(p.value), p.set_by) |
| 863 | + for p in params_with_values], |
| 864 | + "macros": [(m.macro_name, str(m.macro_value or ""), m.defined_by) |
| 865 | + for m in macros.values()], |
| 866 | + "name_len": max([len(m.macro_name) for m in macros.values()] + |
| 867 | + [len(m.macro_name) for m in params_with_values] |
| 868 | + + [0]), |
| 869 | + "val_len" : max([len(str(m.value)) for m in params_with_values] + |
| 870 | + [len(m.macro_value or "") for m in macros.values()] |
| 871 | + + [0]), |
| 872 | + } |
| 873 | + jinja_loader = FileSystemLoader(dirname(abspath(__file__))) |
| 874 | + jinja_environment = Environment(loader=jinja_loader, |
| 875 | + undefined=StrictUndefined) |
| 876 | + header_data = jinja_environment.get_template("header.tmpl").render(ctx) |
906 | 877 | # If fname is given, write "header_data" to it
|
907 | 878 | if fname:
|
908 | 879 | with open(fname, "w+") as file_desc:
|
|
0 commit comments