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      - 'develop'
43      - 'master'
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@v2
59
60    - name: Checkout submodules
61      run: git submodule update --init
62
63    - name: Get core count
64      id: core_count
65      run : cat /proc/cpuinfo  | grep processor | wc -l
66'''
67
68for gcc_version, toolchain_path in gcc_versions_sorted.items():
69    for build_type in ["Debug", "Release"]:
70        output += "\n"
71        output += "    - name: GCC {} {}\n".format(gcc_version, build_type)
72        output += "      if: always()\n"
73        output += "      shell: bash\n"
74        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 -j ${{{{steps.core_count.outputs.output}}}}\n".format(build_type, toolchain_path)
75
76print(output)
77