Skip to content

Commit

Permalink
add hil_ci_set_matrix.py, starting to support hil with additional bui…
Browse files Browse the repository at this point in the history
…ld flags
  • Loading branch information
hathach committed Oct 4, 2024
1 parent eda3cce commit 1b295de
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 47 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
with:
build-system: 'cmake'
toolchain: ${{ matrix.toolchain }}
build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain].family) }}
build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }}
one-per-family: ${{ github.event_name == 'push' }}

# ---------------------------------------
Expand All @@ -89,7 +89,7 @@ jobs:
with:
build-system: 'make'
toolchain: ${{ matrix.toolchain }}
build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain].family) }}
build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }}
one-per-family: true

# ---------------------------------------
Expand Down Expand Up @@ -117,7 +117,7 @@ jobs:
needs: set-matrix
runs-on: [self-hosted, Linux, X64, hifiphile]
env:
BUILD_ARGS: ${{ join(fromJSON(needs.set-matrix.outputs.json)['arm-iar'].family, ' ') }}
BUILD_ARGS: ${{ join(fromJSON(needs.set-matrix.outputs.json)['arm-iar'], ' ') }}
steps:
- name: Clean workspace
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_set_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def set_matrix_json():
hfp_boards = [f"-b{board['name']}" for board in hfp_data['boards']]
filtered_families = filtered_families + hfp_boards

matrix[toolchain] = {"family": filtered_families}
matrix[toolchain] = filtered_families

print(json.dumps(matrix))

Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/hil_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ jobs:
- name: Generate matrix json
id: set-matrix-json
run: |
MATRIX_ARMGCC=$(jq -c '{ "arm-gcc": { "family": [.boards[] | select(.flasher != "esptool" and .flasher != "openocd_wch") | "-b \(.name)"] } }' "${{ env.HIL_JSON }}")
MATRIX_ESP=$(jq -c '{ "esp-idf": { "family": [.boards[] | select(.flasher == "esptool") | "-b \(.name)"] } }' "${{ env.HIL_JSON }}")
MATRIX_RISCV=$(jq -c '{ "riscv-gcc": { "family": [.boards[] | select(.flasher == "openocd_wch") | "-b \(.name)"] } }' "${{ env.HIL_JSON }}")
MATRIX_JSON=$(jq -nc --argjson arm "$MATRIX_ARMGCC" --argjson esp "$MATRIX_ESP" --argjson riscv "$MATRIX_RISCV" '$arm + $esp + $riscv')
MATRIX_JSON=$(python test/hil/hil_ci_set_matrix.py)
echo "matrix=$MATRIX_JSON"
echo "matrix=$MATRIX_JSON" >> $GITHUB_OUTPUT
Expand All @@ -55,7 +52,7 @@ jobs:
with:
build-system: 'cmake'
toolchain: ${{ matrix.toolchain }}
build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain].family) }}
build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }}
one-per-family: true
upload-artifacts: true

Expand Down
42 changes: 42 additions & 0 deletions test/hil/hil_ci_set_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import argparse
import json
import os

def main():
parser = argparse.ArgumentParser()
parser.add_argument('config_file', help='Configuration JSON file')
args = parser.parse_args()

config_file = args.config_file

# if config file is not found, try to find it in the same directory as this script
if not os.path.exists(config_file):
config_file = os.path.join(os.path.dirname(__file__), config_file)
with open(config_file) as f:
config = json.load(f)

matrix = {
'arm-gcc': [],
'esp-idf': []
}
for board in config['boards']:
name = board['name']
if board['flasher'] == 'esptool':
toolchain = 'esp-idf'
else:
toolchain = 'arm-gcc'

if 'build_flags_on' in board:
for f in board['build_flags_on']:
if f == '':
matrix[toolchain].append(f'-b {name}')
else:
matrix[toolchain].append(f'-b {name}-{f.replace(" ", "_")}')
else:
matrix[toolchain].append(f'-b {name}')

print(json.dumps(matrix))


if __name__ == '__main__':
main()
64 changes: 36 additions & 28 deletions test/hil/hil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,37 +424,45 @@ def test_board(board):
test_list.append('device/board_test')

err_count = 0
for test in test_list:
fw_dir = f'cmake-build/cmake-build-{name}/{test}'
if not os.path.exists(fw_dir):
fw_dir = f'examples/cmake-build-{name}/{test}'
fw_name = f'{fw_dir}/{os.path.basename(test)}'
print(f'{name:25} {test:30} ... ', end='')

if not os.path.exists(fw_dir):
print('Skip (no binary)')
continue

# flash firmware. It may fail randomly, retry a few times
for i in range(3):
ret = globals()[f'flash_{flasher}'](board, fw_name)
flags_opt_list = [""]
if 'build_flags_on' in board:
flags_opt_list = board['build_flags_on']

for flags_opt in flags_opt_list:
flags_opt_str = ""
if flags_opt != "":
flags_opt_str = '-' + flags_opt.replace(' ', '-')
for test in test_list:
fw_dir = f'cmake-build/cmake-build-{name}{flags_opt_str}/{test}'
if not os.path.exists(fw_dir):
fw_dir = f'examples/cmake-build-{name}{flags_opt_str}/{test}'
fw_name = f'{fw_dir}/{os.path.basename(test)}'
print(f'{name+flags_opt_str:40} {test:30} ... ', end='')

if not os.path.exists(fw_dir):
print('Skip (no binary)')
continue

# flash firmware. It may fail randomly, retry a few times
for i in range(3):
ret = globals()[f'flash_{flasher}'](board, fw_name)
if ret.returncode == 0:
break
else:
print(f'Flashing failed, retry {i+1}')
time.sleep(1)

if ret.returncode == 0:
break
try:
ret = globals()[f'test_{test.replace("/", "_")}'](board)
print('OK')
except Exception as e:
err_count += 1
print(STATUS_FAILED)
print(f' {e}')
else:
print(f'Flashing failed, retry {i+1}')
time.sleep(1)

if ret.returncode == 0:
try:
ret = globals()[f'test_{test.replace("/", "_")}'](board)
print('OK')
except Exception as e:
err_count += 1
print(STATUS_FAILED)
print(f' {e}')
else:
err_count += 1
print(f'Flash {STATUS_FAILED}')
print(f'Flash {STATUS_FAILED}')
return err_count


Expand Down
39 changes: 29 additions & 10 deletions tools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
build_separator = '-' * 95
build_status = [STATUS_OK, STATUS_FAILED, STATUS_SKIPPED]

verbose = False

# -----------------------------
# Helper
Expand All @@ -38,6 +39,9 @@ def run_cmd(cmd):
else:
print(title)
print(r.stdout.decode("utf-8"))
elif verbose:
print(cmd)
print(r.stdout.decode("utf-8"))
return r


Expand Down Expand Up @@ -75,10 +79,17 @@ def print_build_result(board, example, status, duration):
# -----------------------------
# CMake
# -----------------------------
def cmake_board(board, toolchain):
def cmake_board(board, toolchain, build_flags_on):
ret = [0, 0, 0]
start_time = time.monotonic()
build_dir = f"cmake-build/cmake-build-{board}"

build_dir = f'cmake-build/cmake-build-{board}'
build_flags = ''
if len(build_flags_on) > 0:
build_flags = ' '.join(f'-D{flag}=1' for flag in build_flags_on)
build_flags = f'-DCFLAGS_CLI="{build_flags}"'
build_dir += '-' + '-'.join(build_flags_on)

family = find_family(board)
if family == 'espressif':
# for espressif, we have to build example individually
Expand All @@ -87,12 +98,14 @@ def cmake_board(board, toolchain):
if build_utils.skip_example(example, board):
ret[2] += 1
else:
rcmd = run_cmd(f'cmake examples/{example} -B {build_dir}/{example} -G "Ninja" -DBOARD={board}')
rcmd = run_cmd(f'cmake examples/{example} -B {build_dir}/{example} -G "Ninja" '
f'-DBOARD={board} {build_flags}')
if rcmd.returncode == 0:
rcmd = run_cmd(f'cmake --build {build_dir}/{example}')
ret[0 if rcmd.returncode == 0 else 1] += 1
else:
rcmd = run_cmd(f'cmake examples -B {build_dir} -G "Ninja" -DBOARD={board} -DCMAKE_BUILD_TYPE=MinSizeRel -DTOOLCHAIN={toolchain}')
rcmd = run_cmd(f'cmake examples -B {build_dir} -G "Ninja" -DBOARD={board} -DCMAKE_BUILD_TYPE=MinSizeRel '
f'-DTOOLCHAIN={toolchain} {build_flags}')
if rcmd.returncode == 0:
rcmd = run_cmd(f"cmake --build {build_dir}")
ret[0 if rcmd.returncode == 0 else 1] += 1
Expand Down Expand Up @@ -144,12 +157,12 @@ def make_board(board, toolchain):
# -----------------------------
# Build Family
# -----------------------------
def build_boards_list(boards, toolchain, build_system):
def build_boards_list(boards, toolchain, build_system, build_flags_on):
ret = [0, 0, 0]
for b in boards:
r = [0, 0, 0]
if build_system == 'cmake':
r = cmake_board(b, toolchain)
r = cmake_board(b, toolchain, build_flags_on)
elif build_system == 'make':
r = make_board(b, toolchain)
ret[0] += r[0]
Expand All @@ -158,7 +171,7 @@ def build_boards_list(boards, toolchain, build_system):
return ret


def build_family(family, toolchain, build_system, one_per_family, boards):
def build_family(family, toolchain, build_system, build_flags_on, one_per_family, boards):
all_boards = []
for entry in os.scandir(f"hw/bsp/{family}/boards"):
if entry.is_dir() and entry.name != 'pico_sdk':
Expand All @@ -174,27 +187,33 @@ def build_family(family, toolchain, build_system, one_per_family, boards):
return ret
all_boards = [random.choice(all_boards)]

ret = build_boards_list(all_boards, toolchain, build_system)
ret = build_boards_list(all_boards, toolchain, build_system, build_flags_on)
return ret


# -----------------------------
# Main
# -----------------------------
def main():
global verbose

parser = argparse.ArgumentParser()
parser.add_argument('families', nargs='*', default=[], help='Families to build')
parser.add_argument('-b', '--board', action='append', default=[], help='Boards to build')
parser.add_argument('-t', '--toolchain', default='gcc', help='Toolchain to use, default is gcc')
parser.add_argument('-s', '--build-system', default='cmake', help='Build system to use, default is cmake')
parser.add_argument('-f1', '--build-flags-on', action='append', default=[], help='Build flag to pass to build system')
parser.add_argument('-1', '--one-per-family', action='store_true', default=False, help='Build only one random board inside a family')
parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output')
args = parser.parse_args()

families = args.families
boards = args.board
toolchain = args.toolchain
build_system = args.build_system
build_flags_on = args.build_flags_on
one_per_family = args.one_per_family
verbose = args.verbose

if len(families) == 0 and len(boards) == 0:
print("Please specify families or board to build")
Expand All @@ -217,13 +236,13 @@ def main():

# succeeded, failed, skipped
for f in all_families:
r = build_family(f, toolchain, build_system, one_per_family, boards)
r = build_family(f, toolchain, build_system, build_flags_on, one_per_family, boards)
result[0] += r[0]
result[1] += r[1]
result[2] += r[2]

# build boards
r = build_boards_list(boards, toolchain, build_system)
r = build_boards_list(boards, toolchain, build_system, build_flags_on)
result[0] += r[0]
result[1] += r[1]
result[2] += r[2]
Expand Down

0 comments on commit 1b295de

Please sign in to comment.