linker_script.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. # Copyright 2021 Espressif Systems (Shanghai) CO LTD
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import collections
  17. import os
  18. from fragments import Fragment
  19. from generation import GenerationException
  20. from pyparsing import ParseException, Suppress, White
  21. class LinkerScript:
  22. """
  23. Process a linker script template, which contains markers with grammar:
  24. [<target>]
  25. The <target> is where output commands (see output_commands.py) are placed.
  26. """
  27. Marker = collections.namedtuple('Marker', 'target indent rules')
  28. def __init__(self, template_file):
  29. self.members = []
  30. self.file = os.path.realpath(template_file.name)
  31. self._generate_members(template_file)
  32. def _generate_members(self, template_file):
  33. lines = template_file.readlines()
  34. target = Fragment.IDENTIFIER
  35. reference = Suppress('mapping') + Suppress('[') + target.setResultsName('target') + Suppress(']')
  36. pattern = White(' \t').setResultsName('indent') + reference
  37. # Find the markers in the template file line by line. If line does not match marker grammar,
  38. # set it as a literal to be copied as is to the output file.
  39. for line in lines:
  40. try:
  41. parsed = pattern.parseString(line)
  42. indent = parsed.indent
  43. target = parsed.target
  44. marker = LinkerScript.Marker(target, indent, [])
  45. self.members.append(marker)
  46. except ParseException:
  47. # Does not match marker syntax
  48. self.members.append(line)
  49. def fill(self, mapping_rules):
  50. for member in self.members:
  51. target = None
  52. try:
  53. target = member.target
  54. rules = member.rules
  55. del rules[:]
  56. rules.extend(mapping_rules[target])
  57. except KeyError:
  58. message = GenerationException.UNDEFINED_REFERENCE + " to target '" + target + "'."
  59. raise GenerationException(message)
  60. except AttributeError:
  61. pass
  62. def write(self, output_file):
  63. # Add information that this is a generated file.
  64. output_file.write('/* Automatically generated file; DO NOT EDIT */\n')
  65. output_file.write('/* Espressif IoT Development Framework Linker Script */\n')
  66. output_file.write('/* Generated from: %s */\n' % self.file)
  67. output_file.write('\n')
  68. # Do the text replacement
  69. for member in self.members:
  70. try:
  71. indent = member.indent
  72. rules = member.rules
  73. for rule in rules:
  74. generated_line = ''.join([indent, str(rule), '\n'])
  75. output_file.write(generated_line)
  76. except AttributeError:
  77. output_file.write(member)