1#!/usr/bin/env python3
2
3import os
4from collections import OrderedDict
5import subprocess
6import re
7
8toolchain_dir = "/opt/arm"
9toolchains = os.listdir(toolchain_dir)
10
11gcc_versions = OrderedDict()
12
13for toolchain in toolchains:
14    fullpath = os.path.join(toolchain_dir, toolchain)
15    gcc_path = os.path.join(fullpath, "bin/arm-none-eabi-gcc")
16    version = subprocess.run([gcc_path, "--version"], capture_output=True)
17    stdout = version.stdout.decode('utf-8')
18    stderr = version.stderr.decode('utf-8')
19    assert(len(stderr) == 0)
20    # Version should be on first line
21    version_line = stdout.split("\n")[0]
22    m = re.search("(\d+\.\d+\.\d+)", version_line)
23    assert(m is not None)
24    version = m.group(1)
25
26    if version in gcc_versions:
27        raise Exception("Already have version {} in versions current path {}, this path {}".format(version, gcc_versions[version], fullpath))
28
29    gcc_versions[version] = fullpath
30
31# Sort by major version
32gcc_versions_sorted = OrderedDict(sorted(gcc_versions.items(), key=lambda item: int(item[0].replace(".", ""))))
33
34
35# Create output
36output = '''
37name: Multi GCC
38on:
39  workflow_dispatch:
40  push:
41    branches:
42      - 'master'
43      - 'test_workflow'
44
45jobs:
46  build:
47    if: github.repository_owner == 'raspberrypi'
48    runs-on: [self-hosted, Linux, X64]
49
50    steps:
51    - name: Clean workspace
52      run: |
53        echo "Cleaning up previous run"
54        rm -rf "${{ github.workspace }}"
55        mkdir -p "${{ github.workspace }}"
56
57    - name: Checkout repo
58      uses: actions/checkout@v4
59
60    - name: Checkout submodules
61      run: git submodule update --init
62'''
63
64for gcc_version, toolchain_path in gcc_versions_sorted.items():
65    for build_type in ["Debug", "Release"]:
66        output += "\n"
67        output += "    - name: GCC {} {}\n".format(gcc_version, build_type)
68        output += "      if: always()\n"
69        output += "      shell: bash\n"
70        output += "      run: cd ${{{{github.workspace}}}}; mkdir -p build; rm -rf build/*; cd build; cmake ../ -DPICO_SDK_TESTS_ENABLED=1 -DCMAKE_BUILD_TYPE={} -DPICO_TOOLCHAIN_PATH={} -DPICO_BOARD=pico_w; make --output-sync=target --no-builtin-rules --no-builtin-variables -j$(nproc)\n".format(build_type, toolchain_path)
71
72print(output)
73