Move config to own dir

I also broke the config header template into it's own file. Further, I
fixed a bug in the config header generation where if no macros, builds
would crash.
pull/4150/head
Jimmy Brisson 2017-04-10 15:49:24 -05:00
parent 65adf446c5
commit 212a3dfd12
2 changed files with 47 additions and 50 deletions

View File

@ -17,10 +17,13 @@ limitations under the License.
from copy import deepcopy
import os
from os.path import dirname, abspath
import sys
from collections import namedtuple
from os.path import splitext
from intelhex import IntelHex
from jinja2 import FileSystemLoader, StrictUndefined
from jinja2.environment import Environment
# Implementation of mbed configuration mechanism
from tools.utils import json_file_to_dict, intelhex_offset
from tools.arm_pack_manager import Cache
@ -852,57 +855,25 @@ class Config(object):
WARNING: if 'fname' names an existing file, it will be
overwritten!
"""
params, macros = config[0], config[1]
params, macros = config[0] or {}, config[1] or {}
Config._check_required_parameters(params)
header_data = "// Automatically generated configuration file.\n"
header_data += "// DO NOT EDIT, content will be overwritten.\n\n"
header_data += "#ifndef __MBED_CONFIG_DATA__\n"
header_data += "#define __MBED_CONFIG_DATA__\n\n"
# Compute maximum length of macro names for proper alignment
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)
max_direct_macro_name_len = (max([len(m.macro_name) for m
in macros.values()])
if macros else 0)
max_macro_name_len = max(max_param_macro_name_len,
max_direct_macro_name_len)
# Compute maximum length of macro values for proper alignment
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)
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"
params_with_values = [p for p in params.values() if p.value is not None]
ctx = {
"cfg_params" : [(p.macro_name, str(p.value), p.set_by)
for p in params_with_values],
"macros": [(m.macro_name, str(m.macro_value or ""), m.defined_by)
for m in macros.values()],
"name_len": max([len(m.macro_name) for m in macros.values()] +
[len(m.macro_name) for m in params_with_values]
+ [0]),
"val_len" : max([len(str(m.value)) for m in params_with_values] +
[len(m.macro_value or "") for m in macros.values()]
+ [0]),
}
jinja_loader = FileSystemLoader(dirname(abspath(__file__)))
jinja_environment = Environment(loader=jinja_loader,
undefined=StrictUndefined)
header_data = jinja_environment.get_template("header.tmpl").render(ctx)
# If fname is given, write "header_data" to it
if fname:
with open(fname, "w+") as file_desc:

26
tools/config/header.tmpl Normal file
View File

@ -0,0 +1,26 @@
// 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