From 212a3dfd12c167aec7f644b7bcc92327708dccce Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Mon, 10 Apr 2017 15:49:24 -0500 Subject: [PATCH 1/2] 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. --- tools/{config.py => config/__init__.py} | 71 ++++++++----------------- tools/config/header.tmpl | 26 +++++++++ 2 files changed, 47 insertions(+), 50 deletions(-) rename tools/{config.py => config/__init__.py} (93%) create mode 100644 tools/config/header.tmpl diff --git a/tools/config.py b/tools/config/__init__.py similarity index 93% rename from tools/config.py rename to tools/config/__init__.py index 97758c30a4..fd4a2d3d86 100644 --- a/tools/config.py +++ b/tools/config/__init__.py @@ -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: diff --git a/tools/config/header.tmpl b/tools/config/header.tmpl new file mode 100644 index 0000000000..de4945b19a --- /dev/null +++ b/tools/config/header.tmpl @@ -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 From 157023342f58e3d2dd01a3b2082621d402722248 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Tue, 11 Apr 2017 11:23:53 -0500 Subject: [PATCH 2/2] Add license to configuration header --- tools/config/header.tmpl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tools/config/header.tmpl b/tools/config/header.tmpl index de4945b19a..cfbf5647cd 100644 --- a/tools/config/header.tmpl +++ b/tools/config/header.tmpl @@ -1,3 +1,20 @@ +/* + * 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.