objcopy.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/python
  2. # Copyright 2024 Google LLC
  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. # Grygoriy Fuchedzhy 2010
  16. """
  17. Support for converting linked targets to ihex, srec or binary files using
  18. objcopy. Use the 'objcopy' feature in conjuction with the 'cc' or 'cxx'
  19. feature. The 'objcopy' feature uses the following attributes:
  20. objcopy_bfdname Target object format name (eg. ihex, srec, binary).
  21. Defaults to ihex.
  22. objcopy_target File name used for objcopy output. This defaults to the
  23. target name with objcopy_bfdname as extension.
  24. objcopy_install_path Install path for objcopy_target file. Defaults to ${PREFIX}/fw.
  25. objcopy_flags Additional flags passed to objcopy.
  26. """
  27. from waflib.Utils import def_attrs
  28. from waflib import Task
  29. from waflib.TaskGen import feature, after_method
  30. class objcopy(Task.Task):
  31. run_str = '${OBJCOPY} -O ${TARGET_BFDNAME} ${OBJCOPYFLAGS} ${SRC} ${TGT}'
  32. color = 'CYAN'
  33. @feature('objcopy')
  34. @after_method('apply_link')
  35. def objcopy(self):
  36. def_attrs(self,
  37. objcopy_bfdname='ihex',
  38. objcopy_target=None,
  39. objcopy_install_path="${PREFIX}/firmware",
  40. objcopy_flags='')
  41. link_output = self.link_task.outputs[0]
  42. if not self.objcopy_target:
  43. self.objcopy_target = link_output.change_ext('.' + self.objcopy_bfdname).name
  44. elif isinstance(self.objcopy_target, str):
  45. self.objcopy_target = self.path.find_or_declare(self.objcopy_target)
  46. task = self.create_task('objcopy',
  47. src=link_output,
  48. tgt=self.objcopy_target)
  49. task.env.append_unique('TARGET_BFDNAME', self.objcopy_bfdname)
  50. try:
  51. task.env.append_unique('OBJCOPYFLAGS', getattr(self, 'objcopy_flags'))
  52. except AttributeError:
  53. pass
  54. if self.objcopy_install_path:
  55. self.bld.install_files(self.objcopy_install_path,
  56. task.outputs[0],
  57. env=task.env.derive())
  58. def configure(ctx):
  59. objcopy = ctx.find_program('objcopy', var='OBJCOPY', mandatory=True)
  60. def objcopy_simple(task, mode):
  61. return task.exec_command('arm-none-eabi-objcopy -S -R .stack -R .priv_bss'
  62. ' -R .bss -O %s "%s" "%s"' %
  63. (mode, task.inputs[0].abspath(), task.outputs[0].abspath()))
  64. def objcopy_simple_bin(task):
  65. return objcopy_simple(task, 'binary')