Skip to content

Commit

Permalink
Add nrf support for scripts/build/build_examples.py (#8642)
Browse files Browse the repository at this point in the history
* Add nrf  builds to the example_build.py

* Added a bunch of licenses

* Detect dry run and skip validation of environment variables

* Fix typo in runner naming for dry run detect

* Add validation for nrf version in the compile script

* Fix unit tests

* Remove the double-generate output in the dry run of commands
  • Loading branch information
andy31415 authored and pull[bot] committed Sep 1, 2021
1 parent 7292692 commit c5cd770
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 33 deletions.
22 changes: 22 additions & 0 deletions scripts/build/build/factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from typing import Set
Expand All @@ -7,6 +21,7 @@
from builders.qpg import QpgBuilder
from builders.esp32 import Esp32Builder, Esp32Board, Esp32App
from builders.efr32 import Efr32Builder, Efr32App, Efr32Board
from builders.nrf import NrfApp, NrfBoard, NrfConnectBuilder

from .targets import Application, Board, Platform

Expand Down Expand Up @@ -68,6 +83,7 @@ def Create(self, runner, __board_key: Board, __app_key: Application,
Platform.ESP32: Matcher(Esp32Builder),
Platform.QPG: Matcher(QpgBuilder),
Platform.EFR32: Matcher(Efr32Builder),
Platform.NRF: Matcher(NrfConnectBuilder),
}

# Matrix of what can be compiled and what build options are required
Expand All @@ -93,6 +109,12 @@ def Create(self, runner, __board_key: Board, __app_key: Application,
Application.WINDOW_COVERING, app=Efr32App.WINDOW_COVERING)


_MATCHERS[Platform.NRF].AcceptBoard(Board.NRF5340, board=NrfBoard.NRF5340)
_MATCHERS[Platform.NRF].AcceptBoard(Board.NRF52840, board=NrfBoard.NRF52840)
_MATCHERS[Platform.NRF].AcceptApplication(Application.LOCK, app=NrfApp.LOCK)
_MATCHERS[Platform.NRF].AcceptApplication(Application.LIGHT, app=NrfApp.LIGHT)
_MATCHERS[Platform.NRF].AcceptApplication(Application.SHELL, app=NrfApp.SHELL)

class BuilderFactory:
"""Creates application builders."""

Expand Down
20 changes: 20 additions & 0 deletions scripts/build/build/targets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import shutil
Expand All @@ -11,6 +25,7 @@ class Platform(IntEnum):
QPG = auto()
ESP32 = auto()
EFR32 = auto()
NRF = auto()

@property
def ArgName(self):
Expand Down Expand Up @@ -39,6 +54,10 @@ class Board(IntEnum):
# EFR32 platform
BRD4161A = auto()

# NRF platform
NRF52840 = auto()
NRF5340 = auto()

@property
def ArgName(self):
return self.name.lower()
Expand All @@ -57,6 +76,7 @@ class Application(IntEnum):
LIGHT = auto()
LOCK = auto()
WINDOW_COVERING = auto()
SHELL = auto()

@property
def ArgName(self):
Expand Down
14 changes: 14 additions & 0 deletions scripts/build/build_examples.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#!/usr/bin/env -S python3 -B

# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import coloredlogs
import click
import logging
Expand Down
14 changes: 13 additions & 1 deletion scripts/build/builders/builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
#!/usr/bin/env python3
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
Expand Down
14 changes: 14 additions & 0 deletions scripts/build/builders/efr32.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
from enum import Enum, auto
Expand Down
15 changes: 14 additions & 1 deletion scripts/build/builders/esp32.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import shlex
Expand Down Expand Up @@ -86,7 +100,6 @@ def generate(self):
def build(self):
logging.info('Compiling Esp32 at %s', self.output_dir)

self.generate()
self._IdfEnvExecute(
"ninja -C '%s'" % self.output_dir, title='Building ' + self.identifier)

Expand Down
15 changes: 14 additions & 1 deletion scripts/build/builders/gn.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os

Expand Down Expand Up @@ -33,6 +47,5 @@ def generate(self):
self._Execute(cmd, title='Generating ' + self.identifier)

def build(self):
self.generate()
self._Execute(['ninja', '-C', self.output_dir],
title='Building ' + self.identifier)
14 changes: 14 additions & 0 deletions scripts/build/builders/linux.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os

Expand Down
121 changes: 121 additions & 0 deletions scripts/build/builders/nrf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import shlex

from enum import Enum, auto

from .builder import Builder


class NrfApp(Enum):
LIGHT = auto()
LOCK = auto()
SHELL=auto()

def ExampleName(self):
if self == NrfApp.LIGHT:
return 'lighting-app'
elif self == NrfApp.LOCK:
return 'lock-app'
elif self == NrfApp.SHELL:
return 'shell'
else:
raise Exception('Unknown app type: %r' % self)

def AppNamePrefix(self):
if self == NrfApp.LIGHT:
return 'chip-nrf-lighting-example'
elif self == NrfApp.LOCK:
return 'chip-nrf-lock-example'
elif self == NrfApp.SHELL:
return 'chip-nrf-shell'
else:
raise Exception('Unknown app type: %r' % self)


class NrfBoard(Enum):
NRF52840 = auto()
NRF5340 = auto()

def GnArgName(self):
if self == NrfBoard.NRF52840:
return 'nrf52840dk_nrf52840'
elif self == NrfBoard.NRF5340:
return 'nrf5340dk_nrf5340_cpuapp'
else:
raise Exception('Unknown board type: %r' % self)


class NrfConnectBuilder(Builder):

def __init__(self,
root,
runner,
output_dir: str,
app: NrfApp = NrfApp.LIGHT,
board: NrfBoard = NrfBoard.NRF52840):
super(NrfConnectBuilder, self).__init__(root, runner, output_dir)
self.app = app
self.board = board

def generate(self):
if not os.path.exists(self.output_dir):
# NRF does a in-place update of SDK tools
if not self._runner.dry_run:
if 'ZEPHYR_BASE' not in os.environ:
raise Exception("NRF builds require ZEPHYR_BASE to be set")

zephyr_base = os.environ['ZEPHYR_BASE']
nrfconnect_sdk = os.path.dirname(zephyr_base)

# NRF builds will both try to change .west/config in nrfconnect and
# overall perform a git fetch on that location
if not os.access(nrfconnect_sdk, os.W_OK):
raise Exception("Directory %s not writable. NRFConnect builds require updates to this directory." % nrfconnect_sdk)

# validate the the ZEPHYR_BASE is up to date (generally the case in docker images)
try:
self._Execute(['python3', 'scripts/setup/nrfconnect/update_ncs.py', '--check'])
except Exception as e:
logging.exception('Failed to validate ZEPHYR_BASE status')
logging.error('To update $ZEPHYR_BASE run: python3 scripts/setup/nrfconnect/update_ncs.py --update --shallow')

raise Exception('ZEPHYR_BASE validation failed')

cmd = '''
source "$ZEPHYR_BASE/zephyr-env.sh";
export GNUARMEMB_TOOLCHAIN_PATH="$PW_PIGWEED_CIPD_INSTALL_DIR";
west build --cmake-only -d {outdir} -b {board} {sourcedir}
'''.format(
outdir = shlex.quote(self.output_dir),
board = self.board.GnArgName(),
sourcedir=shlex.quote(os.path.join(self.root, 'examples', self.app.ExampleName(), 'nrfconnect'))
).strip()

self._Execute(['bash', '-c', cmd], title='Generating ' + self.identifier)


def build(self):
logging.info('Compiling NrfConnect at %s', self.output_dir)

self._Execute(['ninja', '-C', self.output_dir], title='Building ' + self.identifier)

def outputs(self):
return {
'%s.elf' % self.app.AppNamePrefix(): os.path.join(self.output_dir, 'zephyr', 'zephyr.elf'),
'%s.map' % self.app.AppNamePrefix(): os.path.join(self.output_dir, 'zephyr', 'zephyr.map'),
}
14 changes: 13 additions & 1 deletion scripts/build/builders/qpg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
#!/usr/bin/env python3
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
Expand Down
Loading

0 comments on commit c5cd770

Please sign in to comment.