Skip to content

Move config system into it's own folder and refactor header generation #4150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 21 additions & 50 deletions tools/config.py → tools/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

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
Expand Down Expand Up @@ -852,57 +855,25 @@ def config_to_header(config, fname=None):
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:
Expand Down
43 changes: 43 additions & 0 deletions tools/config/header.tmpl
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this file contain license header? I would say yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also say yes. now it has one.

// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should have a new line at the end of file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍