build-baremetal 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python3
  2. import os
  3. import common
  4. import thread_pool
  5. from shell_helpers import LF
  6. class Main(common.BuildCliFunction):
  7. def __init__(self):
  8. super().__init__(
  9. defaults={
  10. 'gcc_which': 'crosstool-ng',
  11. 'mode': 'baremetal',
  12. },
  13. description='''\
  14. Build the baremetal examples with crosstool-NG.
  15. ''',
  16. )
  17. self._add_argument('--ccflags')
  18. self._add_argument('--force-rebuild')
  19. self._add_argument('--optimization-level')
  20. self.add_argument(
  21. 'targets',
  22. default=[],
  23. help='Analogous to ./build-userland target selection',
  24. nargs='*',
  25. )
  26. def build(self):
  27. build_dir = self.get_build_dir()
  28. extra_obj_baremetal_bootloader = os.path.join(
  29. self.env['baremetal_build_lib_dir'],
  30. 'bootloader{}'.format(self.env['obj_ext'])
  31. )
  32. extra_obj_lkmc_common = os.path.join(
  33. self.env['baremetal_build_lib_dir'],
  34. self.env['common_basename_noext'] + self.env['obj_ext']
  35. )
  36. cc_flags = [
  37. '-I', self.env['root_dir'], LF,
  38. '-O{}'.format(self.env['optimization_level']), LF,
  39. '-nostartfiles', LF,
  40. ]
  41. if self.env['arch'] == 'arm':
  42. cc_flags.extend([
  43. '-mhard-float', LF,
  44. # This uses the soft float ABI for calling functions from objets in Newlib which
  45. # our crosstool-NG config compiles with soft floats, while emiting hard float
  46. # from C and allowing us to use it from assembly, e.g. for the VMRS instruction:
  47. # which would otherwise fail "with selected processor does not support XXX in ARM mode"
  48. # Bibliography:
  49. # - https://stackoverflow.com/questions/9753749/arm-compilation-error-vfp-registered-used-by-executable-not-object-file
  50. # - https://stackoverflow.com/questions/41131432/cross-compiling-error-selected-processor-does-not-support-fmrx-r3-fpexc-in/41131782#41131782
  51. # - https://embeddedartistry.com/blog/2017/10/9/r1q7pksku2q3gww9rpqef0dnskphtc
  52. '-mfloat-abi=softfp', LF,
  53. '-mfpu=crypto-neon-fp-armv8', LF,
  54. ])
  55. if self.env['emulator'] == 'gem5':
  56. if self.env['machine'] == 'VExpress_GEM5_V1':
  57. entry_address = 0x80000000
  58. uart_address = 0x1c090000
  59. elif self.env['machine'] == 'RealViewPBX':
  60. entry_address = 0x10000
  61. uart_address = 0x10009000
  62. else:
  63. raise Exception('unknown machine: ' + self.env['machine'])
  64. cc_flags.extend([
  65. '-DLKMC_GEM5=1', LF,
  66. '-DLKMC_M5OPS_ENABLE=1', LF,
  67. ])
  68. else:
  69. entry_address = 0x40000000
  70. uart_address = 0x09000000
  71. cc_flags.extend(['-D', 'LKMC_QEMU=1', LF])
  72. cc_flags.extend(['-D', 'LKMC_UART0_ADDR={:#x}'.format(uart_address), LF])
  73. cc_flags.extend(self.sh.shlex_split(self.env['ccflags']))
  74. bootloader_src = os.path.join(
  75. self.env['baremetal_source_lib_dir'],
  76. '{}{}'.format(
  77. self.env['arch'],
  78. self.env['asm_ext']
  79. )
  80. )
  81. for in_path, out_path in [
  82. (bootloader_src, extra_obj_baremetal_bootloader),
  83. (self.env['common_c'], extra_obj_lkmc_common),
  84. (
  85. self.env['baremetal_syscalls_src'],
  86. self.env['baremetal_syscalls_obj']
  87. ),
  88. (
  89. self.env['baremetal_syscalls_asm_src'],
  90. self.env['baremetal_syscalls_asm_obj']
  91. ),
  92. ]:
  93. self._build_one(
  94. in_path=in_path,
  95. out_path=out_path,
  96. cc_flags=cc_flags,
  97. extra_deps=[self.env['common_h']],
  98. link=False,
  99. )
  100. cc_flags.extend([
  101. '-Wl,--section-start=.text={:#x}'.format(entry_address), LF,
  102. '-T', self.env['baremetal_link_script'], LF,
  103. ])
  104. with thread_pool.ThreadPool(
  105. self._build_one,
  106. nthreads=self.env['nproc'],
  107. submit_raise_exit=self.env['quit_on_fail'],
  108. ) as my_thread_pool:
  109. for target in self.env['targets']:
  110. for path, in_dirnames, in_filenames in self.sh.walk(target):
  111. for in_filename in in_filenames:
  112. in_ext = os.path.splitext(in_filename)[1]
  113. if not in_ext in self.env['baremetal_build_in_exts']:
  114. continue
  115. in_path = os.path.join(path, in_filename)
  116. my_thread_pool.submit({
  117. 'cc_flags': cc_flags,
  118. 'extra_deps': [
  119. self.env['baremetal_link_script'],
  120. self.env['common_h']
  121. ],
  122. 'extra_objs': [
  123. self.env['baremetal_syscalls_obj'],
  124. self.env['baremetal_syscalls_asm_obj']
  125. ],
  126. 'extra_objs_baremetal_bootloader': [extra_obj_baremetal_bootloader],
  127. 'extra_objs_lkmc_common': [extra_obj_lkmc_common],
  128. 'in_path': in_path,
  129. 'out_path': self.resolve_baremetal_executable(in_path),
  130. })
  131. return self._handle_thread_pool_errors(my_thread_pool)
  132. def get_build_dir(self):
  133. return self.env['baremetal_build_dir']
  134. def setup_one(self):
  135. self.env['targets'] = self.resolve_targets(
  136. [
  137. self.env['baremetal_source_dir'],
  138. self.env['userland_source_dir']
  139. ],
  140. self.env['targets']
  141. )
  142. if __name__ == '__main__':
  143. Main().cli()