Merge pull request #4150 from theotherjimmy/refactor-config-header

Move config system into it's own folder and refactor header generation
pull/3066/merge
Anna Bridge 2017-04-20 16:14:38 +01:00 committed by GitHub
commit 41ff084f0c
2 changed files with 64 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:

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

@ -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