nrf.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # Copyright (c) 2021 Project CHIP Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. import os
  16. import shlex
  17. from enum import Enum, auto
  18. from .builder import Builder
  19. class NrfApp(Enum):
  20. ALL_CLUSTERS = auto()
  21. ALL_CLUSTERS_MINIMAL = auto()
  22. LIGHT = auto()
  23. LOCK = auto()
  24. SHELL = auto()
  25. PUMP = auto()
  26. PUMP_CONTROLLER = auto()
  27. SWITCH = auto()
  28. WINDOW_COVERING = auto()
  29. UNIT_TESTS = auto()
  30. def AppPath(self):
  31. if self == NrfApp.ALL_CLUSTERS:
  32. return 'examples/all-clusters-app'
  33. elif self == NrfApp.ALL_CLUSTERS_MINIMAL:
  34. return 'examples/all-clusters-minimal-app'
  35. elif self == NrfApp.LIGHT:
  36. return 'examples/lighting-app'
  37. elif self == NrfApp.SWITCH:
  38. return 'examples/light-switch-app'
  39. elif self == NrfApp.LOCK:
  40. return 'examples/lock-app'
  41. elif self == NrfApp.SHELL:
  42. return 'examples/shell'
  43. elif self == NrfApp.PUMP:
  44. return 'examples/pump-app'
  45. elif self == NrfApp.PUMP_CONTROLLER:
  46. return 'examples/pump-controller-app'
  47. elif self == NrfApp.WINDOW_COVERING:
  48. return 'examples/window-app'
  49. elif self == NrfApp.UNIT_TESTS:
  50. return 'src/test_driver'
  51. else:
  52. raise Exception('Unknown app type: %r' % self)
  53. def AppNamePrefix(self):
  54. if self == NrfApp.ALL_CLUSTERS:
  55. return 'chip-nrf-all-clusters-example'
  56. elif self == NrfApp.ALL_CLUSTERS_MINIMAL:
  57. return 'chip-nrf-all-clusters-minimal-example'
  58. elif self == NrfApp.LIGHT:
  59. return 'chip-nrf-lighting-example'
  60. elif self == NrfApp.SWITCH:
  61. return 'chip-nrf-light-switch-example'
  62. elif self == NrfApp.LOCK:
  63. return 'chip-nrf-lock-example'
  64. elif self == NrfApp.SHELL:
  65. return 'chip-nrf-shell'
  66. elif self == NrfApp.PUMP:
  67. return 'chip-nrf-pump-example'
  68. elif self == NrfApp.PUMP_CONTROLLER:
  69. return 'chip-nrf-pump-controller-example'
  70. elif self == NrfApp.WINDOW_COVERING:
  71. return 'chip-nrf-window-example'
  72. elif self == NrfApp.UNIT_TESTS:
  73. return 'chip-nrf-unit-tests'
  74. else:
  75. raise Exception('Unknown app type: %r' % self)
  76. def _FlashBundlePrefix(self):
  77. if self == NrfApp.ALL_CLUSTERS:
  78. return 'chip-nrfconnect-all-clusters-app-example'
  79. elif self == NrfApp.ALL_CLUSTERS_MINIMAL:
  80. return 'chip-nrfconnect-all-clusters-minimal-app-example'
  81. elif self == NrfApp.LIGHT:
  82. return 'chip-nrfconnect-lighting-example'
  83. elif self == NrfApp.SWITCH:
  84. return 'chip-nrfconnect-switch-example'
  85. elif self == NrfApp.LOCK:
  86. return 'chip-nrfconnect-lock-example'
  87. elif self == NrfApp.SHELL:
  88. return 'chip-nrfconnect-shell-example'
  89. elif self == NrfApp.PUMP:
  90. return 'chip-nrfconnect-pump-example'
  91. elif self == NrfApp.PUMP_CONTROLLER:
  92. return 'chip-nrfconnect-pump-controller-example'
  93. elif self == NrfApp.WINDOW_COVERING:
  94. return 'chip-nrfconnect-window-example'
  95. elif self == NrfApp.UNIT_TESTS:
  96. raise Exception(
  97. 'Unit tests compile natively and do not have a flashbundle')
  98. else:
  99. raise Exception('Unknown app type: %r' % self)
  100. def FlashBundleName(self):
  101. '''
  102. Nrf build script will generate a file naming <project_name>.flashbundle.txt,
  103. go through the output dir to find the file and return it.
  104. '''
  105. return self._FlashBundlePrefix() + '.flashbundle.txt'
  106. class NrfBoard(Enum):
  107. NRF52840DK = auto()
  108. NRF52840DONGLE = auto()
  109. NRF5340DK = auto()
  110. NATIVE_POSIX_64 = auto()
  111. def GnArgName(self):
  112. if self == NrfBoard.NRF52840DK:
  113. return 'nrf52840dk_nrf52840'
  114. elif self == NrfBoard.NRF52840DONGLE:
  115. return 'nrf52840dongle_nrf52840'
  116. elif self == NrfBoard.NRF5340DK:
  117. return 'nrf5340dk_nrf5340_cpuapp'
  118. elif self == NrfBoard.NATIVE_POSIX_64:
  119. return 'native_posix_64'
  120. else:
  121. raise Exception('Unknown board type: %r' % self)
  122. class NrfConnectBuilder(Builder):
  123. def __init__(self,
  124. root,
  125. runner,
  126. app: NrfApp = NrfApp.LIGHT,
  127. board: NrfBoard = NrfBoard.NRF52840DK,
  128. enable_rpcs: bool = False):
  129. super(NrfConnectBuilder, self).__init__(root, runner)
  130. self.app = app
  131. self.board = board
  132. self.enable_rpcs = enable_rpcs
  133. def generate(self):
  134. if not os.path.exists(self.output_dir):
  135. zephyr_sdk_dir = None
  136. if not self._runner.dry_run:
  137. if 'ZEPHYR_BASE' not in os.environ:
  138. raise Exception("NRF builds require ZEPHYR_BASE to be set")
  139. # Users are expected to set ZEPHYR_SDK_INSTALL_DIR but additionally cover the Docker
  140. # case by inferring ZEPHYR_SDK_INSTALL_DIR from NRF5_TOOLS_ROOT.
  141. if not os.environ.get('ZEPHYR_SDK_INSTALL_DIR') and not os.environ.get('NRF5_TOOLS_ROOT'):
  142. raise Exception("NRF buils require ZEPHYR_SDK_INSTALL_DIR to be set")
  143. zephyr_base = os.environ['ZEPHYR_BASE']
  144. nrfconnect_sdk = os.path.dirname(zephyr_base)
  145. zephyr_sdk_dir = os.environ.get('ZEPHYR_SDK_INSTALL_DIR') or os.path.join(
  146. os.environ['NRF5_TOOLS_ROOT'], 'zephyr-sdk-0.16.0')
  147. # NRF builds will both try to change .west/config in nrfconnect and
  148. # overall perform a git fetch on that location
  149. if not os.access(nrfconnect_sdk, os.W_OK):
  150. raise Exception(
  151. "Directory %s not writable. NRFConnect builds require updates to this directory." % nrfconnect_sdk)
  152. # validate the the ZEPHYR_BASE is up to date (generally the case in docker images)
  153. try:
  154. self._Execute(
  155. ['python3', 'scripts/setup/nrfconnect/update_ncs.py', '--check'])
  156. except Exception:
  157. logging.exception('Failed to validate ZEPHYR_BASE status')
  158. logging.error(
  159. 'To update $ZEPHYR_BASE run: python3 scripts/setup/nrfconnect/update_ncs.py --update --shallow')
  160. raise Exception('ZEPHYR_BASE validation failed')
  161. flags = []
  162. if self.enable_rpcs:
  163. flags.append("-DOVERLAY_CONFIG=rpc.overlay")
  164. if (self.board == NrfBoard.NRF52840DONGLE and
  165. self.app != NrfApp.ALL_CLUSTERS and self.app != NrfApp.ALL_CLUSTERS_MINIMAL):
  166. flags.append("-DCONF_FILE=prj_no_dfu.conf")
  167. if self.options.pregen_dir:
  168. flags.append(f"-DCHIP_CODEGEN_PREGEN_DIR={shlex.quote(self.options.pregen_dir)}")
  169. build_flags = " -- " + " ".join(flags) if len(flags) > 0 else ""
  170. cmd = '''
  171. source "$ZEPHYR_BASE/zephyr-env.sh";
  172. export ZEPHYR_TOOLCHAIN_VARIANT=zephyr;'''
  173. if zephyr_sdk_dir:
  174. cmd += f'''
  175. export ZEPHYR_SDK_INSTALL_DIR={zephyr_sdk_dir};'''
  176. cmd += '''
  177. west build --cmake-only -d {outdir} -b {board} {sourcedir}{build_flags}
  178. '''.format(
  179. outdir=shlex.quote(self.output_dir),
  180. board=self.board.GnArgName(),
  181. sourcedir=shlex.quote(os.path.join(
  182. self.root, self.app.AppPath(), 'nrfconnect')),
  183. build_flags=build_flags
  184. )
  185. self._Execute(['bash', '-c', cmd.strip()],
  186. title='Generating ' + self.identifier)
  187. def _build(self):
  188. logging.info('Compiling NrfConnect at %s', self.output_dir)
  189. self._Execute(['ninja', '-C', self.output_dir],
  190. title='Building ' + self.identifier)
  191. if self.app == NrfApp.UNIT_TESTS:
  192. # Note: running zephyr/zephyr.elf has the same result except it creates
  193. # a flash.bin in the current directory. ctest has more options and does not
  194. # pollute the source directory
  195. self._Execute(['ctest', '--build-nocmake', '-V', '--output-on-failure', '--test-dir', self.output_dir],
  196. title='Run Tests ' + self.identifier)
  197. def _generate_flashbundle(self):
  198. logging.info(f'Generating flashbundle at {self.output_dir}')
  199. self._Execute(['ninja', '-C', self.output_dir, 'flashing_script'],
  200. title='Generating flashable files of ' + self.identifier)
  201. def build_outputs(self):
  202. return {
  203. '%s.elf' % self.app.AppNamePrefix(): os.path.join(self.output_dir, 'zephyr', 'zephyr.elf'),
  204. '%s.map' % self.app.AppNamePrefix(): os.path.join(self.output_dir, 'zephyr', 'zephyr.map'),
  205. }
  206. def flashbundle(self):
  207. if self.app == NrfApp.UNIT_TESTS:
  208. return dict()
  209. with open(os.path.join(self.output_dir, self.app.FlashBundleName()), 'r') as fp:
  210. return {
  211. line.strip(): os.path.join(self.output_dir, line.strip()) for line in fp.readlines() if line.strip()
  212. }