linker_script.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. rules.extend(mapping_rules[target])
  55. except KeyError:
  56. message = GenerationException.UNDEFINED_REFERENCE + " to target '" + target + "'."
  57. raise GenerationException(message)
  58. except AttributeError:
  59. pass
  60. def write(self, output_file):
  61. # Add information that this is a generated file.
  62. output_file.write('/* Automatically generated file; DO NOT EDIT */\n')
  63. output_file.write('/* Espressif IoT Development Framework Linker Script */\n')
  64. output_file.write('/* Generated from: %s */\n' % self.file)
  65. output_file.write('\n')
  66. # Do the text replacement
  67. for member in self.members:
  68. try:
  69. indent = member.indent
  70. rules = member.rules
  71. for rule in rules:
  72. generated_line = ''.join([indent, str(rule), '\n'])
  73. output_file.write(generated_line)
  74. except AttributeError:
  75. output_file.write(member)