Skip to content

Commit 66fda82

Browse files
committed
shields: scripts: cmake: use list_shields.py in shields.cmake
The logic to "guess" shield names/dirs was duplicated between list_shields.py (which is used by e.g. west shields) and shields.cmake. This commit moves the logic to list_shields.py, and updates shields.cmake to call the script and process its JSON output. Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
1 parent 93ced04 commit 66fda82

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

cmake/modules/shields.cmake

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
include_guard(GLOBAL)
3232

3333
include(extensions)
34+
include(python)
3435

3536
# Check that SHIELD has not changed.
3637
zephyr_check_cache(SHIELD WATCH)
@@ -46,31 +47,42 @@ endif()
4647
# After processing all shields, only invalid shields will be left in this list.
4748
set(SHIELD-NOTFOUND ${SHIELD_AS_LIST})
4849

49-
foreach(root ${BOARD_ROOT})
50-
set(shield_dir ${root}/boards/shields)
51-
# Match the Kconfig.shield files in the shield directories to make sure we are
52-
# finding shields, e.g. x_nucleo_iks01a1/Kconfig.shield
53-
file(GLOB_RECURSE shields_refs_list ${shield_dir}/*/Kconfig.shield)
54-
55-
# The above gives a list of Kconfig.shield files, like this:
56-
#
57-
# x_nucleo_iks01a1/Kconfig.shield;x_nucleo_iks01a2/Kconfig.shield
58-
#
59-
# we construct a list of shield names by extracting the directories
60-
# from each file and looking for <shield>.overlay files in there.
61-
# Each overlay corresponds to a shield. We obtain the shield name by
62-
# removing the .overlay extension.
63-
# We also create a SHIELD_DIR_${name} variable for each shield's directory.
64-
foreach(shields_refs ${shields_refs_list})
65-
get_filename_component(shield_path ${shields_refs} DIRECTORY)
66-
file(GLOB shield_overlays RELATIVE ${shield_path} ${shield_path}/*.overlay)
67-
foreach(overlay ${shield_overlays})
68-
get_filename_component(shield ${overlay} NAME_WE)
69-
list(APPEND SHIELD_LIST ${shield})
70-
set(SHIELD_DIR_${shield} ${shield_path})
71-
endforeach()
50+
# Prepare list shields command.
51+
# This command is used for locating the shield dir as well as printing all shields
52+
# in the system in the following cases:
53+
# - User specifies an invalid SHIELD
54+
# - User invokes '<build-command> shields' target
55+
list(TRANSFORM BOARD_ROOT PREPEND "--board-root=" OUTPUT_VARIABLE board_root_args)
56+
57+
set(list_shields_commands
58+
COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/list_shields.py
59+
${board_root_args} --json
60+
)
61+
62+
# Get list of shields in JSON format
63+
execute_process(${list_shields_commands}
64+
OUTPUT_VARIABLE shields_json
65+
ERROR_VARIABLE err_shields
66+
RESULT_VARIABLE ret_val
67+
)
68+
69+
if(ret_val)
70+
message(FATAL_ERROR "Error finding shields\nError message: ${err_shields}")
71+
endif()
72+
73+
string(JSON shields_length LENGTH ${shields_json})
74+
75+
if(shields_length GREATER 0)
76+
math(EXPR shields_length "${shields_length} - 1")
77+
78+
foreach(i RANGE ${shields_length})
79+
string(JSON shield GET "${shields_json}" "${i}")
80+
string(JSON shield_name GET ${shield} name)
81+
string(JSON shield_dir GET ${shield} dir)
82+
list(APPEND SHIELD_LIST ${shield_name})
83+
set(SHIELD_DIR_${shield_name} ${shield_dir})
7284
endforeach()
73-
endforeach()
85+
endif()
7486

7587
# Process shields in-order
7688
if(DEFINED SHIELD)

scripts/list_shields.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# SPDX-License-Identifier: Apache-2.0
66

77
import argparse
8+
import json
89
from dataclasses import dataclass
910
from pathlib import Path
1011

@@ -57,6 +58,7 @@ def find_shields_in(root):
5758
def parse_args():
5859
parser = argparse.ArgumentParser(allow_abbrev=False)
5960
add_args(parser)
61+
add_args_formatting(parser)
6062
return parser.parse_args()
6163

6264
def add_args(parser):
@@ -66,9 +68,19 @@ def add_args(parser):
6668
type=Path, action='append',
6769
help='add a board root, may be given more than once')
6870

71+
def add_args_formatting(parser):
72+
parser.add_argument("--json", action='store_true',
73+
help='''output list of shields in JSON format''')
74+
6975
def dump_shields(shields):
70-
for shield in shields:
71-
print(f' {shield.name}')
76+
if args.json:
77+
print(
78+
json.dumps([{'dir': shield.dir.as_posix(), 'name': shield.name} for shield in shields])
79+
)
80+
else:
81+
for shield in shields:
82+
print(f' {shield.name}')
7283

7384
if __name__ == '__main__':
74-
dump_shields(find_shields(parse_args()))
85+
args = parse_args()
86+
dump_shields(find_shields(args))

0 commit comments

Comments
 (0)