mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			Merge pull request #4150 from theotherjimmy/refactor-config-header
Move config system into it's own folder and refactor header generationpull/3066/merge
						commit
						41ff084f0c
					
				| 
						 | 
					@ -17,10 +17,13 @@ limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from copy import deepcopy
 | 
					from copy import deepcopy
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					from os.path import dirname, abspath
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
from collections import namedtuple
 | 
					from collections import namedtuple
 | 
				
			||||||
from os.path import splitext
 | 
					from os.path import splitext
 | 
				
			||||||
from intelhex import IntelHex
 | 
					from intelhex import IntelHex
 | 
				
			||||||
 | 
					from jinja2 import FileSystemLoader, StrictUndefined
 | 
				
			||||||
 | 
					from jinja2.environment import Environment
 | 
				
			||||||
# Implementation of mbed configuration mechanism
 | 
					# Implementation of mbed configuration mechanism
 | 
				
			||||||
from tools.utils import json_file_to_dict, intelhex_offset
 | 
					from tools.utils import json_file_to_dict, intelhex_offset
 | 
				
			||||||
from tools.arm_pack_manager import Cache
 | 
					from tools.arm_pack_manager import Cache
 | 
				
			||||||
| 
						 | 
					@ -852,57 +855,25 @@ class Config(object):
 | 
				
			||||||
                 WARNING: if 'fname' names an existing file, it will be
 | 
					                 WARNING: if 'fname' names an existing file, it will be
 | 
				
			||||||
                 overwritten!
 | 
					                 overwritten!
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        params, macros = config[0], config[1]
 | 
					        params, macros = config[0] or {}, config[1] or {}
 | 
				
			||||||
        Config._check_required_parameters(params)
 | 
					        Config._check_required_parameters(params)
 | 
				
			||||||
        header_data = "// Automatically generated configuration file.\n"
 | 
					        params_with_values = [p for p in params.values() if p.value is not None]
 | 
				
			||||||
        header_data += "// DO NOT EDIT, content will be overwritten.\n\n"
 | 
					        ctx = {
 | 
				
			||||||
        header_data += "#ifndef __MBED_CONFIG_DATA__\n"
 | 
					            "cfg_params" : [(p.macro_name, str(p.value), p.set_by)
 | 
				
			||||||
        header_data += "#define __MBED_CONFIG_DATA__\n\n"
 | 
					                            for p in params_with_values],
 | 
				
			||||||
        # Compute maximum length of macro names for proper alignment
 | 
					            "macros": [(m.macro_name, str(m.macro_value or ""), m.defined_by)
 | 
				
			||||||
        max_param_macro_name_len = (max([len(m.macro_name) for m
 | 
					                       for m in macros.values()],
 | 
				
			||||||
                                         in params.values()
 | 
					            "name_len":  max([len(m.macro_name) for m in macros.values()] +
 | 
				
			||||||
                                         if m.value is not None])
 | 
					                             [len(m.macro_name) for m in params_with_values]
 | 
				
			||||||
                                    if params else 0)
 | 
					                             + [0]),
 | 
				
			||||||
        max_direct_macro_name_len = (max([len(m.macro_name) for m
 | 
					            "val_len" : max([len(str(m.value)) for m in params_with_values] +
 | 
				
			||||||
                                         in macros.values()])
 | 
					                            [len(m.macro_value or "") for m in macros.values()]
 | 
				
			||||||
                                     if macros else 0)
 | 
					                            + [0]),
 | 
				
			||||||
        max_macro_name_len = max(max_param_macro_name_len,
 | 
					        }
 | 
				
			||||||
                                 max_direct_macro_name_len)
 | 
					        jinja_loader = FileSystemLoader(dirname(abspath(__file__)))
 | 
				
			||||||
        # Compute maximum length of macro values for proper alignment
 | 
					        jinja_environment = Environment(loader=jinja_loader,
 | 
				
			||||||
        max_param_macro_val_len = (max([len(str(m.value)) for m
 | 
					                                        undefined=StrictUndefined)
 | 
				
			||||||
                                       in params.values()
 | 
					        header_data = jinja_environment.get_template("header.tmpl").render(ctx)
 | 
				
			||||||
                                       if m.value is not None])
 | 
					 | 
				
			||||||
                                   if params else 0)
 | 
					 | 
				
			||||||
        max_direct_macro_val_len = max([len(m.macro_value or "") for m
 | 
					 | 
				
			||||||
                                        in macros.values()]) if macros else 0
 | 
					 | 
				
			||||||
        max_macro_val_len = max(max_param_macro_val_len,
 | 
					 | 
				
			||||||
                                max_direct_macro_val_len)
 | 
					 | 
				
			||||||
        # Generate config parameters first
 | 
					 | 
				
			||||||
        if params:
 | 
					 | 
				
			||||||
            header_data += "// Configuration parameters\n"
 | 
					 | 
				
			||||||
            for macro in params.values():
 | 
					 | 
				
			||||||
                if macro.value is not None:
 | 
					 | 
				
			||||||
                    header_data += ("#define {0:<{1}} {2!s:<{3}} " +
 | 
					 | 
				
			||||||
                                    "// set by {4}\n")\
 | 
					 | 
				
			||||||
                        .format(macro.macro_name, max_macro_name_len,
 | 
					 | 
				
			||||||
                                macro.value, max_macro_val_len, macro.set_by)
 | 
					 | 
				
			||||||
        # Then macros
 | 
					 | 
				
			||||||
        if macros:
 | 
					 | 
				
			||||||
            header_data += "// Macros\n"
 | 
					 | 
				
			||||||
            for macro in macros.values():
 | 
					 | 
				
			||||||
                if macro.macro_value:
 | 
					 | 
				
			||||||
                    header_data += ("#define {0:<{1}} {2!s:<{3}}" +
 | 
					 | 
				
			||||||
                                    " // defined by {4}\n")\
 | 
					 | 
				
			||||||
                        .format(macro.macro_name, max_macro_name_len,
 | 
					 | 
				
			||||||
                                macro.macro_value, max_macro_val_len,
 | 
					 | 
				
			||||||
                                macro.defined_by)
 | 
					 | 
				
			||||||
                else:
 | 
					 | 
				
			||||||
                    header_data += ("#define {0:<{1}}" +
 | 
					 | 
				
			||||||
                                    " // defined by {2}\n")\
 | 
					 | 
				
			||||||
                        .format(macro.macro_name,
 | 
					 | 
				
			||||||
                                max_macro_name_len + max_macro_val_len + 1,
 | 
					 | 
				
			||||||
                                macro.defined_by)
 | 
					 | 
				
			||||||
        header_data += "\n#endif\n"
 | 
					 | 
				
			||||||
        # If fname is given, write "header_data" to it
 | 
					        # If fname is given, write "header_data" to it
 | 
				
			||||||
        if fname:
 | 
					        if fname:
 | 
				
			||||||
            with open(fname, "w+") as file_desc:
 | 
					            with open(fname, "w+") as file_desc:
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,43 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * mbed SDK
 | 
				
			||||||
 | 
					 * Copyright (c) 2017 ARM Limited
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					 * you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					 * You may obtain a copy of the License at
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					 * See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					 * limitations under the License.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Automatically generated configuration file.
 | 
				
			||||||
 | 
					// DO NOT EDIT, content will be overwritten.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef __MBED_CONFIG_DATA__
 | 
				
			||||||
 | 
					#define __MBED_CONFIG_DATA__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{% if cfg_params -%}
 | 
				
			||||||
 | 
					// Configuration parameters
 | 
				
			||||||
 | 
					{% for name, value, set_by in cfg_params -%}
 | 
				
			||||||
 | 
					{% if value is not none -%}
 | 
				
			||||||
 | 
					#define {{name.ljust(name_len)}} {{value.ljust(val_len)}} // set by {{set_by}}
 | 
				
			||||||
 | 
					{%- endif %}
 | 
				
			||||||
 | 
					{% endfor %}
 | 
				
			||||||
 | 
					{%- endif -%}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{%- if macros -%}
 | 
				
			||||||
 | 
					// Macros
 | 
				
			||||||
 | 
					{% for name, value, set_by in macros -%}
 | 
				
			||||||
 | 
					{% if value is not none -%}
 | 
				
			||||||
 | 
					#define {{name.ljust(name_len)}} {{value.ljust(val_len)}} // defined by {{set_by}}
 | 
				
			||||||
 | 
					{%- else -%}
 | 
				
			||||||
 | 
					#define {{name.ljust(name_len + val_len + 1)}} // defined by {{set_by}}
 | 
				
			||||||
 | 
					{%- endif %}
 | 
				
			||||||
 | 
					{% endfor %}
 | 
				
			||||||
 | 
					{%- endif %}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		Loading…
	
		Reference in New Issue