mbed.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 os
  15. import shlex
  16. from enum import Enum, auto
  17. from .builder import Builder
  18. class MbedApp(Enum):
  19. LOCK = auto()
  20. LIGHT = auto()
  21. ALL_CLUSTERS = auto()
  22. ALL_CLUSTERS_MINIMAL = auto()
  23. PIGWEED = auto()
  24. SHELL = auto()
  25. OTA_REQUESTOR = auto()
  26. @property
  27. def ExampleName(self):
  28. if self == MbedApp.LOCK:
  29. return 'lock-app'
  30. elif self == MbedApp.LIGHT:
  31. return 'lighting-app'
  32. elif self == MbedApp.ALL_CLUSTERS:
  33. return 'all-clusters-app'
  34. elif self == MbedApp.ALL_CLUSTERS_MINIMAL:
  35. return 'all-clusters-minimal-app'
  36. elif self == MbedApp.PIGWEED:
  37. return 'pigweed-app'
  38. elif self == MbedApp.OTA_REQUESTOR:
  39. return 'ota-requestor-app'
  40. elif self == MbedApp.SHELL:
  41. return 'shell'
  42. else:
  43. raise Exception('Unknown app type: %r' % self)
  44. @property
  45. def AppNamePrefix(self):
  46. if self == MbedApp.LOCK:
  47. return 'chip-mbed-lock-app-example'
  48. elif self == MbedApp.LIGHT:
  49. return 'chip-mbed-lighting-app-example'
  50. elif self == MbedApp.ALL_CLUSTERS:
  51. return 'chip-mbed-all-clusters-app-example'
  52. elif self == MbedApp.ALL_CLUSTERS_MINIMAL:
  53. return 'chip-mbed-all-clusters-minimal-app-example'
  54. elif self == MbedApp.PIGWEED:
  55. return 'chip-mbed-pigweed-app-example'
  56. elif self == MbedApp.OTA_REQUESTOR:
  57. return 'chip-mbed-ota-requestor-app-example'
  58. elif self == MbedApp.SHELL:
  59. return 'chip-mbed-shell-example'
  60. else:
  61. raise Exception('Unknown app type: %r' % self)
  62. class MbedBoard(Enum):
  63. CY8CPROTO_062_4343W = auto()
  64. @property
  65. def BoardName(self):
  66. if self == MbedBoard.CY8CPROTO_062_4343W:
  67. return 'CY8CPROTO_062_4343W'
  68. else:
  69. raise Exception('Unknown board type: %r' % self)
  70. class MbedProfile(Enum):
  71. RELEASE = auto()
  72. DEVELOP = auto()
  73. DEBUG = auto()
  74. @property
  75. def ProfileName(self):
  76. if self == MbedProfile.RELEASE:
  77. return 'release'
  78. elif self == MbedProfile.DEVELOP:
  79. return 'develop'
  80. elif self == MbedProfile.DEBUG:
  81. return 'debug'
  82. else:
  83. raise Exception('Unknown board type: %r' % self)
  84. class MbedBuilder(Builder):
  85. def __init__(self,
  86. root,
  87. runner,
  88. app: MbedApp = MbedApp.LOCK,
  89. board: MbedBoard = MbedBoard.CY8CPROTO_062_4343W,
  90. profile: MbedProfile = MbedProfile.RELEASE):
  91. super(MbedBuilder, self).__init__(root, runner)
  92. self.app = app
  93. self.board = board
  94. self.profile = profile
  95. self.toolchain = "GCC_ARM"
  96. self.mbed_os_path = os.path.join(
  97. self.root, 'third_party', 'mbed-os', 'repo')
  98. self.mbed_os_posix_socket_path = os.path.join(
  99. self.root, 'third_party', 'mbed-os-posix-socket', 'repo')
  100. @property
  101. def ExamplePath(self):
  102. return os.path.join(self.root, 'examples', self.app.ExampleName, 'mbed')
  103. def generate(self):
  104. if not os.path.exists(self.output_dir):
  105. self._Execute(['mbed-tools', 'configure',
  106. '-t', self.toolchain,
  107. '-m', self.board.BoardName,
  108. '-p', self.ExamplePath,
  109. '-o', self.output_dir,
  110. '--mbed-os-path', self.mbed_os_path,
  111. ], title='Generating config ' + self.identifier)
  112. flags = []
  113. flags.append(f"-DMBED_OS_PATH={shlex.quote(self.mbed_os_path)}")
  114. flags.append(f"-DMBED_OS_PATH={shlex.quote(self.mbed_os_path)}")
  115. flags.append(f"-DMBED_OS_POSIX_SOCKET_PATH={shlex.quote(self.mbed_os_posix_socket_path)}")
  116. if self.options.pregen_dir:
  117. flags.append(f"-DCHIP_CODEGEN_PREGEN_DIR={shlex.quote(self.options.pregen_dir)}")
  118. self._Execute(['cmake', '-S', shlex.quote(self.ExamplePath), '-B', shlex.quote(self.output_dir),
  119. '-GNinja'] + flags, title='Generating ' + self.identifier)
  120. def _build(self):
  121. # Remove old artifacts to force linking
  122. cmd = 'rm -rf {}/chip-*'.format(self.output_dir)
  123. self._Execute(['bash', '-c', cmd],
  124. title='Remove old artifacts ' + self.identifier)
  125. self._Execute(['cmake', '--build', shlex.quote(self.output_dir)],
  126. title='Building ' + self.identifier)
  127. def build_outputs(self):
  128. return {
  129. self.app.AppNamePrefix + '.elf':
  130. os.path.join(self.output_dir, self.app.AppNamePrefix + '.elf'),
  131. self.app.AppNamePrefix + '.hex':
  132. os.path.join(self.output_dir, self.app.AppNamePrefix + '.hex'),
  133. self.app.AppNamePrefix + '.map':
  134. os.path.join(self.output_dir,
  135. self.app.AppNamePrefix + '.elf.map'),
  136. }