ldgen.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. import argparse
  18. import sys
  19. import tempfile
  20. from fragments import FragmentFileModel
  21. from sdkconfig import SDKConfig
  22. from generation import GenerationModel, TemplateModel, SectionsInfo
  23. from common import LdGenFailure
  24. def main():
  25. argparser = argparse.ArgumentParser(description="ESP-IDF linker script generator")
  26. argparser.add_argument(
  27. "--input", "-i",
  28. help="Linker template file",
  29. type=argparse.FileType("r"))
  30. argparser.add_argument(
  31. "--fragments", "-f",
  32. type=argparse.FileType("r"),
  33. help="Input fragment files",
  34. nargs="+")
  35. argparser.add_argument(
  36. "--sections", "-s",
  37. type=argparse.FileType("r"),
  38. help="Library sections info")
  39. argparser.add_argument(
  40. "--output", "-o",
  41. help="Output linker script",
  42. type=str)
  43. argparser.add_argument(
  44. "--config", "-c",
  45. help="Project configuration",
  46. type=argparse.FileType("r"))
  47. argparser.add_argument(
  48. "--kconfig", "-k",
  49. help="IDF Kconfig file",
  50. type=argparse.FileType("r"))
  51. argparser.add_argument(
  52. "--env", "-e",
  53. action='append', default=[],
  54. help='Environment to set when evaluating the config file', metavar='NAME=VAL')
  55. args = argparser.parse_args()
  56. input_file = args.input
  57. fragment_files = [] if not args.fragments else args.fragments
  58. config_file = args.config
  59. output_path = args.output
  60. kconfig_file = args.kconfig
  61. sections = args.sections
  62. try:
  63. sections_infos = SectionsInfo()
  64. if sections:
  65. section_info_contents = [s.strip() for s in sections.read().split("\n")]
  66. section_info_contents = [s for s in section_info_contents if s]
  67. else:
  68. section_info_contents = []
  69. for sections_info_file in section_info_contents:
  70. with open(sections_info_file) as sections_info_file_obj:
  71. sections_infos.add_sections_info(sections_info_file_obj)
  72. generation_model = GenerationModel()
  73. for fragment_file in fragment_files:
  74. fragment_file = FragmentFileModel(fragment_file)
  75. generation_model.add_fragments_from_file(fragment_file)
  76. sdkconfig = SDKConfig(kconfig_file, config_file, args.env)
  77. mapping_rules = generation_model.generate_rules(sdkconfig, sections_infos)
  78. script_model = TemplateModel(input_file)
  79. script_model.fill(mapping_rules, sdkconfig)
  80. with tempfile.TemporaryFile("w+") as output:
  81. script_model.write(output)
  82. output.seek(0)
  83. with open(output_path, "w") as f: # only create output file after generation has suceeded
  84. f.write(output.read())
  85. except LdGenFailure as e:
  86. print("linker script generation failed for %s\nERROR: %s" % (input_file.name, e))
  87. sys.exit(1)
  88. if __name__ == "__main__":
  89. main()