diff options
author | Kevin Peng <kevin.peng@arm.com> | 2021-02-23 09:53:54 +0800 |
---|---|---|
committer | Kevin Peng <kevin.peng@arm.com> | 2021-02-23 09:53:54 +0800 |
commit | d65629def610abded9f8f242043eb6aefc5ee071 (patch) | |
tree | 764ea6f4d533af45d5e2485a857457cfac317c4c /bl2/ext | |
parent | 5ec7965124f4b6d0c1b1653d67da2171175c26da (diff) | |
download | trusted-firmware-m-d65629def610abded9f8f242043eb6aefc5ee071.tar.gz |
Introduce end-of-line normalization
Change-Id: Ia78df87957163ad6dd65b9b72cf539a96e57ddf4
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
Diffstat (limited to 'bl2/ext')
-rw-r--r-- | bl2/ext/mcuboot/scripts/macro_parser.py | 146 |
1 files changed, 73 insertions, 73 deletions
diff --git a/bl2/ext/mcuboot/scripts/macro_parser.py b/bl2/ext/mcuboot/scripts/macro_parser.py index 188c650949..5d9418a4e8 100644 --- a/bl2/ext/mcuboot/scripts/macro_parser.py +++ b/bl2/ext/mcuboot/scripts/macro_parser.py @@ -1,73 +1,73 @@ -#! /usr/bin/env python3
-#
-# -----------------------------------------------------------------------------
-# Copyright (c) 2019, Arm Limited. All rights reserved.
-#
-# SPDX-License-Identifier: BSD-3-Clause
-#
-# -----------------------------------------------------------------------------
-
-
-import re
-import os
-
-expression_re = re.compile(r"[(]?(([(]?(((0x)[0-9a-fA-F]+)|([0-9]+))[)]?)\s*([\+\-]\s*([(]?(((0x)[0-9a-fA-F]+)|([0-9]+))[)]?)\s*)*)[)]?")
-
-# Simple parser that takes a string and evaluates an expression from it.
-# The expression might contain additions and subtractions amongst numbers that
-# are written in decimal or hexadecimal form.
-# The parses can process expressions in which the parentheses does not change
-# the sign of the following number or numbers in an expression.
-# Thus the parser can process the following expression: (x + y)
-# However it will not calculate the correct sum for the expression below:
-# (x - (y + z))
-def parse_and_sum(text):
- m = expression_re.match(text)
- if m is None:
- msg = "The script was probably invoked manually"
- msg += " with having certain macros nested in flash_layouts.h.\n"
- msg += "Please revisit the flash_layout.h file and hardcode values"
- msg += " for the (NON-)SECURE_IMAGE_OFFSET and"
- msg += " (NON-)SECURE_IMAGE_MAX_SIZE macros"
- raise Exception(msg)
-
- nums = re.findall(r'(0x[A-Fa-f0-9]+)|[\d]+', m.group(0))
- for i in range(len(nums)):
- nums[i] = int(nums[i], 0)
- ops = re.findall(r'\+|\-', m.group(0))
- sum = nums[0]
- for i in range(len(ops)):
- if ops[i] == '+':
- sum += nums[i+1]
- else:
- sum -= nums[i+1]
- return sum
-
-
-# Opens a file that contains the macro of interest, then finds the macro with
-# a regular expression, parses the expression that is defined for the given
-# macro. Lastly it evaluates the expression with the parse_and_sum function
-def evaluate_macro(file, regexp, matchGroupKey, matchGroupData, bracketless=False):
- regexp_compiled = re.compile(regexp)
-
- if os.path.isabs(file):
- configFile = file
- else:
- scriptsDir = os.path.dirname(os.path.abspath(__file__))
- configFile = os.path.join(scriptsDir, file)
-
- macroValue = {}
- with open(configFile, 'r') as macros_preprocessed_file:
- for line in macros_preprocessed_file:
- if bracketless:
- line=line.replace("(","")
- line=line.replace(")","")
- m = regexp_compiled.match(line)
- if m is not None:
- macroValue[m.group(matchGroupKey)] = \
- parse_and_sum(m.group(matchGroupData))
-
- if (matchGroupKey == 0 and not macroValue):
- macroValue["None"] = None
-
- return list(macroValue.values())[0] if (matchGroupKey == 0) else macroValue
+#! /usr/bin/env python3 +# +# ----------------------------------------------------------------------------- +# Copyright (c) 2019, Arm Limited. All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause +# +# ----------------------------------------------------------------------------- + + +import re +import os + +expression_re = re.compile(r"[(]?(([(]?(((0x)[0-9a-fA-F]+)|([0-9]+))[)]?)\s*([\+\-]\s*([(]?(((0x)[0-9a-fA-F]+)|([0-9]+))[)]?)\s*)*)[)]?") + +# Simple parser that takes a string and evaluates an expression from it. +# The expression might contain additions and subtractions amongst numbers that +# are written in decimal or hexadecimal form. +# The parses can process expressions in which the parentheses does not change +# the sign of the following number or numbers in an expression. +# Thus the parser can process the following expression: (x + y) +# However it will not calculate the correct sum for the expression below: +# (x - (y + z)) +def parse_and_sum(text): + m = expression_re.match(text) + if m is None: + msg = "The script was probably invoked manually" + msg += " with having certain macros nested in flash_layouts.h.\n" + msg += "Please revisit the flash_layout.h file and hardcode values" + msg += " for the (NON-)SECURE_IMAGE_OFFSET and" + msg += " (NON-)SECURE_IMAGE_MAX_SIZE macros" + raise Exception(msg) + + nums = re.findall(r'(0x[A-Fa-f0-9]+)|[\d]+', m.group(0)) + for i in range(len(nums)): + nums[i] = int(nums[i], 0) + ops = re.findall(r'\+|\-', m.group(0)) + sum = nums[0] + for i in range(len(ops)): + if ops[i] == '+': + sum += nums[i+1] + else: + sum -= nums[i+1] + return sum + + +# Opens a file that contains the macro of interest, then finds the macro with +# a regular expression, parses the expression that is defined for the given +# macro. Lastly it evaluates the expression with the parse_and_sum function +def evaluate_macro(file, regexp, matchGroupKey, matchGroupData, bracketless=False): + regexp_compiled = re.compile(regexp) + + if os.path.isabs(file): + configFile = file + else: + scriptsDir = os.path.dirname(os.path.abspath(__file__)) + configFile = os.path.join(scriptsDir, file) + + macroValue = {} + with open(configFile, 'r') as macros_preprocessed_file: + for line in macros_preprocessed_file: + if bracketless: + line=line.replace("(","") + line=line.replace(")","") + m = regexp_compiled.match(line) + if m is not None: + macroValue[m.group(matchGroupKey)] = \ + parse_and_sum(m.group(matchGroupData)) + + if (matchGroupKey == 0 and not macroValue): + macroValue["None"] = None + + return list(macroValue.values())[0] if (matchGroupKey == 0) else macroValue |