|
| 1 | +# Copyright (c) 2024 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 | + |
| 15 | +import logging |
| 16 | +import os |
| 17 | +from enum import Enum, auto |
| 18 | + |
| 19 | +from .gn import Builder |
| 20 | + |
| 21 | + |
| 22 | +class NuttXApp(Enum): |
| 23 | + LIGHT = auto() |
| 24 | + |
| 25 | + def ExampleName(self): |
| 26 | + if self == NuttXApp.LIGHT: |
| 27 | + return 'lighting-app' |
| 28 | + else: |
| 29 | + raise Exception('Unknown app type: %r' % self) |
| 30 | + |
| 31 | + def AppNamePrefix(self, chip_name): |
| 32 | + if self == NuttXApp.LIGHT: |
| 33 | + return ('chip-%s-lighting-example' % chip_name) |
| 34 | + else: |
| 35 | + raise Exception('Unknown app type: %r' % self) |
| 36 | + |
| 37 | + |
| 38 | +class NuttXBoard(Enum): |
| 39 | + SIM = auto() |
| 40 | + |
| 41 | + |
| 42 | +def NuttXTarget(board, app): |
| 43 | + if board == NuttXBoard.SIM: |
| 44 | + if app == NuttXApp.LIGHT: |
| 45 | + return 'sim:matter' |
| 46 | + return 'none' |
| 47 | + |
| 48 | + |
| 49 | +class NuttXBuilder(Builder): |
| 50 | + |
| 51 | + def __init__(self, |
| 52 | + root, |
| 53 | + runner, |
| 54 | + app: NuttXApp = NuttXApp.LIGHT, |
| 55 | + board: NuttXBoard = NuttXBoard.SIM, |
| 56 | + ): |
| 57 | + |
| 58 | + nuttx_chip = 'nuttx' |
| 59 | + |
| 60 | + super(NuttXBuilder, self).__init__( |
| 61 | + root=os.path.join(root, 'examples', |
| 62 | + app.ExampleName(), nuttx_chip), |
| 63 | + runner=runner |
| 64 | + ) |
| 65 | + |
| 66 | + self.chip_name = nuttx_chip |
| 67 | + self.app = app |
| 68 | + self.board = board |
| 69 | + |
| 70 | + def generate(self): |
| 71 | + self._Execute(['mkdir', '-p', self.output_dir], |
| 72 | + title='Generating ' + self.identifier) |
| 73 | + |
| 74 | + def _build(self): |
| 75 | + logging.info('Compiling NuttX %s at %s, ', |
| 76 | + NuttXTarget(self.board, self.app), self.output_dir) |
| 77 | + nuttx_dir = os.path.join(os.sep, 'opt', 'nuttx', 'nuttx') |
| 78 | + |
| 79 | + self._Execute(['cmake', '-S', nuttx_dir, '-B', self.output_dir, '-DCHIP_ROOT=' + os.getenv('PW_PROJECT_ROOT'), |
| 80 | + '-DBOARD_CONFIG=' + NuttXTarget(self.board, self.app), |
| 81 | + '-DCMAKE_C_COMPILER=/opt/nuttx/gcc-13/bin/gcc', |
| 82 | + '-DCMAKE_CXX_COMPILER=/opt/nuttx/gcc-13/bin/g++', |
| 83 | + '-GNinja'], |
| 84 | + title='Building ' + self.identifier) |
| 85 | + self._Execute(['cmake', '--build', self.output_dir]) |
| 86 | + |
| 87 | + def build_outputs(self): |
| 88 | + logging.info('Compiling outputs NuttX at %s', self.output_dir) |
| 89 | + items = { |
| 90 | + '%s.out' % self.app.AppNamePrefix(self.chip_name): |
| 91 | + os.path.join(self.output_dir, '%s.out' % |
| 92 | + self.app.AppNamePrefix(self.chip_name)), |
| 93 | + '%s.out.map' % self.app.AppNamePrefix(self.chip_name): |
| 94 | + os.path.join(self.output_dir, |
| 95 | + '%s.out.map' % self.app.AppNamePrefix(self.chip_name)), |
| 96 | + } |
| 97 | + |
| 98 | + return items |
0 commit comments