Skip to content

Commit 41ff084

Browse files
authored
Merge pull request #4150 from theotherjimmy/refactor-config-header
Move config system into it's own folder and refactor header generation
2 parents 5b86a10 + 1570233 commit 41ff084

File tree

2 files changed

+64
-50
lines changed

2 files changed

+64
-50
lines changed

tools/config.py renamed to tools/config/__init__.py

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717

1818
from copy import deepcopy
1919
import os
20+
from os.path import dirname, abspath
2021
import sys
2122
from collections import namedtuple
2223
from os.path import splitext
2324
from intelhex import IntelHex
25+
from jinja2 import FileSystemLoader, StrictUndefined
26+
from jinja2.environment import Environment
2427
# Implementation of mbed configuration mechanism
2528
from tools.utils import json_file_to_dict, intelhex_offset
2629
from tools.arm_pack_manager import Cache
@@ -852,57 +855,25 @@ def config_to_header(config, fname=None):
852855
WARNING: if 'fname' names an existing file, it will be
853856
overwritten!
854857
"""
855-
params, macros = config[0], config[1]
858+
params, macros = config[0] or {}, config[1] or {}
856859
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)
906877
# If fname is given, write "header_data" to it
907878
if fname:
908879
with open(fname, "w+") as file_desc:

tools/config/header.tmpl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* mbed SDK
3+
* Copyright (c) 2017 ARM Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Automatically generated configuration file.
19+
// DO NOT EDIT, content will be overwritten.
20+
21+
#ifndef __MBED_CONFIG_DATA__
22+
#define __MBED_CONFIG_DATA__
23+
24+
{% if cfg_params -%}
25+
// Configuration parameters
26+
{% for name, value, set_by in cfg_params -%}
27+
{% if value is not none -%}
28+
#define {{name.ljust(name_len)}} {{value.ljust(val_len)}} // set by {{set_by}}
29+
{%- endif %}
30+
{% endfor %}
31+
{%- endif -%}
32+
33+
{%- if macros -%}
34+
// Macros
35+
{% for name, value, set_by in macros -%}
36+
{% if value is not none -%}
37+
#define {{name.ljust(name_len)}} {{value.ljust(val_len)}} // defined by {{set_by}}
38+
{%- else -%}
39+
#define {{name.ljust(name_len + val_len + 1)}} // defined by {{set_by}}
40+
{%- endif %}
41+
{% endfor %}
42+
{%- endif %}
43+
#endif

0 commit comments

Comments
 (0)