Skip to content

Commit e12ebf1

Browse files
[nrfconnect] Fix build examples script for nRFConnect (project-chip#40149)
Some adjustments and fixes in the nrf.py script are required for the newest nRFConnect toolchain. Updated also expected dry_run output for nrfconnect generate and build. Refactored generate and build functions to be ready to work with NCS 3.0.0. Signed-off-by: Arkadiusz Balys <[email protected]>
1 parent 42929d0 commit e12ebf1

File tree

2 files changed

+47
-46
lines changed

2 files changed

+47
-46
lines changed

scripts/build/builders/nrf.py

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ class NrfBoard(Enum):
122122

123123
def GnArgName(self):
124124
if self == NrfBoard.NRF52840DK:
125-
return 'nrf52840dk_nrf52840'
125+
return 'nrf52840dk/nrf52840'
126126
elif self == NrfBoard.NRF52840DONGLE:
127-
return 'nrf52840dongle_nrf52840'
127+
return 'nrf52840dongle/nrf52840'
128128
elif self == NrfBoard.NRF5340DK:
129-
return 'nrf5340dk_nrf5340_cpuapp'
129+
return 'nrf5340dk/nrf5340cpuapp'
130130
elif self == NrfBoard.NATIVE_SIM:
131131
return 'native_sim'
132132
else:
@@ -147,74 +147,73 @@ def __init__(self,
147147
self.board = board
148148
self.enable_rpcs = enable_rpcs
149149

150+
def _check_ncs_version(self):
151+
# validate the the ZEPHYR_BASE is up to date (generally the case in docker images)
152+
try:
153+
self._Execute(
154+
['python3', 'scripts/setup/nrfconnect/update_ncs.py', '--check'])
155+
except Exception:
156+
logging.exception('Failed to validate ZEPHYR_BASE status')
157+
logging.error(
158+
'To update $ZEPHYR_BASE run: python3 scripts/setup/nrfconnect/update_ncs.py --update --shallow')
159+
160+
raise Exception('ZEPHYR_BASE validation failed')
161+
162+
def _prepare_environment(self):
163+
# Source the zephyr-env.sh script to set up the environment
164+
# The zephyr-env.sh script changes the python environment, so we need to
165+
# source the activate.sh script after zephyr-env.sh to ensure that the
166+
# all python packages and dependencies are available.
167+
return 'source "$ZEPHYR_BASE/zephyr-env.sh";\nsource scripts/activate.sh;\n'
168+
169+
def _get_build_flags(self):
170+
flags = []
171+
if self.enable_rpcs:
172+
flags.append("-DOVERLAY_CONFIG=rpc.overlay")
173+
174+
if self.options.pregen_dir:
175+
flags.append(f"-DCHIP_CODEGEN_PREGEN_DIR={shlex.quote(self.options.pregen_dir)}")
176+
177+
build_flags = " -- " + " ".join(flags) if len(flags) > 0 else ""
178+
179+
return build_flags
180+
150181
def generate(self):
151182
if not os.path.exists(self.output_dir):
152-
zephyr_sdk_dir = None
153-
154183
if not self._runner.dry_run:
155-
if 'ZEPHYR_BASE' not in os.environ:
156-
raise Exception("NRF builds require ZEPHYR_BASE to be set")
157-
158-
# Users are expected to set ZEPHYR_SDK_INSTALL_DIR but additionally cover the Docker
159-
# case by inferring ZEPHYR_SDK_INSTALL_DIR from NRF5_TOOLS_ROOT.
160-
if not os.environ.get('ZEPHYR_SDK_INSTALL_DIR') and not os.environ.get('NRF5_TOOLS_ROOT'):
161-
raise Exception("NRF buils require ZEPHYR_SDK_INSTALL_DIR to be set")
184+
self._check_ncs_version()
162185

163186
zephyr_base = os.environ['ZEPHYR_BASE']
164187
nrfconnect_sdk = os.path.dirname(zephyr_base)
165-
zephyr_sdk_dir = os.environ.get('ZEPHYR_SDK_INSTALL_DIR') or os.path.join(
166-
os.environ['NRF5_TOOLS_ROOT'], 'zephyr-sdk-0.16.1')
167188

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

174-
# validate the the ZEPHYR_BASE is up to date (generally the case in docker images)
175-
try:
176-
self._Execute(
177-
['python3', 'scripts/setup/nrfconnect/update_ncs.py', '--check'])
178-
except Exception:
179-
logging.exception('Failed to validate ZEPHYR_BASE status')
180-
logging.error(
181-
'To update $ZEPHYR_BASE run: python3 scripts/setup/nrfconnect/update_ncs.py --update --shallow')
182-
183-
raise Exception('ZEPHYR_BASE validation failed')
184-
185-
flags = []
186-
if self.enable_rpcs:
187-
flags.append("-DOVERLAY_CONFIG=rpc.overlay")
188-
189-
if self.options.pregen_dir:
190-
flags.append(f"-DCHIP_CODEGEN_PREGEN_DIR={shlex.quote(self.options.pregen_dir)}")
191-
192-
build_flags = " -- " + " ".join(flags) if len(flags) > 0 else ""
193-
194-
cmd = 'source "$ZEPHYR_BASE/zephyr-env.sh";\nexport ZEPHYR_TOOLCHAIN_VARIANT=zephyr;'
195-
196-
if zephyr_sdk_dir:
197-
cmd += f'\nexport ZEPHYR_SDK_INSTALL_DIR={zephyr_sdk_dir};'
195+
cmd = self._prepare_environment()
198196

199-
cmd += '\nwest build --cmake-only -d {outdir} -b {board} --sysbuild {sourcedir}{build_flags}\n'.format(
197+
cmd += 'west build --cmake-only -d {outdir} -b {board} --sysbuild {sourcedir}{build_flags}\n'.format(
200198
outdir=shlex.quote(self.output_dir),
201199
board=self.board.GnArgName(),
202200
sourcedir=shlex.quote(os.path.join(
203201
self.root, self.app.AppPath(), 'nrfconnect')),
204-
build_flags=build_flags
202+
build_flags=self._get_build_flags()
205203
)
206204
self._Execute(['bash', '-c', cmd.strip()],
207205
title='Generating ' + self.identifier)
208206

209207
def _build(self):
210208
logging.info('Compiling NrfConnect at %s', self.output_dir)
211209

212-
cmd = ['ninja', '-C', self.output_dir]
210+
cmd = self._prepare_environment()
211+
cmd += f'ninja -C {self.output_dir}'
213212

214213
if self.ninja_jobs is not None:
215-
cmd.append('-j' + str(self.ninja_jobs))
214+
cmd += '-j' + str(self.ninja_jobs)
216215

217-
self._Execute(cmd, title='Building ' + self.identifier)
216+
self._Execute(['bash', '-c', cmd.strip()], title='Building ' + self.identifier)
218217

219218
if self.app == NrfApp.UNIT_TESTS:
220219
# Note: running zephyr/zephyr.elf has the same result except it creates

scripts/build/testdata/dry_run_nrf-nrf52840dk-pump.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ cd "{root}"
33

44
# Generating nrf-nrf52840dk-pump
55
bash -c 'source "$ZEPHYR_BASE/zephyr-env.sh";
6-
export ZEPHYR_TOOLCHAIN_VARIANT=zephyr;
7-
west build --cmake-only -d {out}/nrf-nrf52840dk-pump -b nrf52840dk_nrf52840 --sysbuild {root}/examples/pump-app/nrfconnect'
6+
source scripts/activate.sh;
7+
west build --cmake-only -d {out}/nrf-nrf52840dk-pump -b nrf52840dk/nrf52840 --sysbuild {root}/examples/pump-app/nrfconnect'
88

99
# Building nrf-nrf52840dk-pump
10-
ninja -C {out}/nrf-nrf52840dk-pump
10+
bash -c 'source "$ZEPHYR_BASE/zephyr-env.sh";
11+
source scripts/activate.sh;
12+
ninja -C {out}/nrf-nrf52840dk-pump'

0 commit comments

Comments
 (0)