linker_script.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. Encapsulates a linker script template file. Finds marker syntax and handles replacement to generate the
  24. final output.
  25. """
  26. Marker = collections.namedtuple('Marker', 'target indent rules')
  27. def __init__(self, template_file):
  28. self.members = []
  29. self.file = os.path.realpath(template_file.name)
  30. self._generate_members(template_file)
  31. def _generate_members(self, template_file):
  32. lines = template_file.readlines()
  33. target = Fragment.IDENTIFIER
  34. reference = Suppress('mapping') + Suppress('[') + target.setResultsName('target') + Suppress(']')
  35. pattern = White(' \t').setResultsName('indent') + reference
  36. # Find the markers in the template file line by line. If line does not match marker grammar,
  37. # set it as a literal to be copied as is to the output file.
  38. for line in lines:
  39. try:
  40. parsed = pattern.parseString(line)
  41. indent = parsed.indent
  42. target = parsed.target
  43. marker = LinkerScript.Marker(target, indent, [])
  44. self.members.append(marker)
  45. except ParseException:
  46. # Does not match marker syntax
  47. self.members.append(line)
  48. def fill(self, mapping_rules):
  49. for member in self.members:
  50. target = None
  51. try:
  52. target = member.target
  53. rules = member.rules
  54. del rules[:]
  55. rules.extend(mapping_rules[target])
  56. except KeyError:
  57. message = GenerationException.UNDEFINED_REFERENCE + " to target '" + target + "'."
  58. raise GenerationException(message)
  59. except AttributeError:
  60. pass
  61. def write(self, output_file):
  62. # Add information that this is a generated file.
  63. output_file.write('/* Automatically generated file; DO NOT EDIT */\n')
  64. output_file.write('/* Espressif IoT Development Framework Linker Script */\n')
  65. output_file.write('/* Generated from: %s */\n' % self.file)
  66. output_file.write('\n')
  67. # Do the text replacement
  68. for member in self.members:
  69. try:
  70. indent = member.indent
  71. rules = member.rules
  72. for rule in rules:
  73. generated_line = ''.join([indent, str(rule), '\n'])
  74. output_file.write(generated_line)
  75. except AttributeError:
  76. output_file.write(member)