|  | 
|  | 1 | +#!/usr/bin/env python3 | 
|  | 2 | + | 
|  | 3 | +import os | 
|  | 4 | +import dataclasses | 
|  | 5 | +import json | 
|  | 6 | + | 
|  | 7 | +from enum import Enum | 
|  | 8 | +from typing import Any, Dict, List, Final, Set, Union | 
|  | 9 | + | 
|  | 10 | +MANAGED_OWNER: Final[str] = "kernel-patches" | 
|  | 11 | +MANAGED_REPOS: Final[Set[str]] = { | 
|  | 12 | +    f"{MANAGED_OWNER}/bpf", | 
|  | 13 | +    f"{MANAGED_OWNER}/vmtest", | 
|  | 14 | +} | 
|  | 15 | +# We need to run on ubuntu 20.04 because our rootfs is based on debian buster and we | 
|  | 16 | +# otherwise get library versioning issue such as | 
|  | 17 | +# `./test_verifier: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by ./test_verifier)` | 
|  | 18 | +DEFAULT_RUNNER: Final[str] = "ubuntu-20.04" | 
|  | 19 | +DEFAULT_LLVM_VERSION: Final[int] = 17 | 
|  | 20 | + | 
|  | 21 | + | 
|  | 22 | +class Arch(str, Enum): | 
|  | 23 | +    """ | 
|  | 24 | +    CPU architecture supported by CI. | 
|  | 25 | +    """ | 
|  | 26 | + | 
|  | 27 | +    AARCH64 = "aarch64" | 
|  | 28 | +    S390X = "s390x" | 
|  | 29 | +    X86_64 = "x86_64" | 
|  | 30 | + | 
|  | 31 | + | 
|  | 32 | +class Compiler(str, Enum): | 
|  | 33 | +    GCC = "gcc" | 
|  | 34 | +    LLVM = "llvm" | 
|  | 35 | + | 
|  | 36 | + | 
|  | 37 | +@dataclasses.dataclass | 
|  | 38 | +class Toolchain: | 
|  | 39 | +    compiler: Compiler | 
|  | 40 | +    # This is relevant ONLY for LLVM and should not be required for GCC | 
|  | 41 | +    version: int | 
|  | 42 | + | 
|  | 43 | +    @property | 
|  | 44 | +    def short_name(self) -> str: | 
|  | 45 | +        return str(self.compiler.value) | 
|  | 46 | + | 
|  | 47 | +    @property | 
|  | 48 | +    def full_name(self) -> str: | 
|  | 49 | +        if self.compiler == Compiler.GCC: | 
|  | 50 | +            return self.short_name | 
|  | 51 | + | 
|  | 52 | +        return f"{self.short_name}-{self.version}" | 
|  | 53 | + | 
|  | 54 | +    def to_dict(self) -> Dict[str, Union[str, int]]: | 
|  | 55 | +        return { | 
|  | 56 | +            "name": self.short_name, | 
|  | 57 | +            "fullname": self.full_name, | 
|  | 58 | +            "version": self.version, | 
|  | 59 | +        } | 
|  | 60 | + | 
|  | 61 | + | 
|  | 62 | +@dataclasses.dataclass | 
|  | 63 | +class BuildConfig: | 
|  | 64 | +    arch: Arch | 
|  | 65 | +    toolchain: Toolchain | 
|  | 66 | +    kernel: str = "LATEST" | 
|  | 67 | +    run_veristat: bool = False | 
|  | 68 | +    parallel_tests: bool = False | 
|  | 69 | +    build_release: bool = False | 
|  | 70 | + | 
|  | 71 | +    @property | 
|  | 72 | +    def runs_on(self) -> List[str]: | 
|  | 73 | +        if is_managed_repo(): | 
|  | 74 | +            return ["self-hosted", self.arch.value] | 
|  | 75 | +        return [DEFAULT_RUNNER] | 
|  | 76 | + | 
|  | 77 | +    @property | 
|  | 78 | +    def tests(self) -> Dict[str, Any]: | 
|  | 79 | +        tests_list = [ | 
|  | 80 | +            "test_progs", | 
|  | 81 | +            "test_progs_parallel", | 
|  | 82 | +            "test_progs_no_alu32", | 
|  | 83 | +            "test_progs_no_alu32_parallel", | 
|  | 84 | +            "test_maps", | 
|  | 85 | +            "test_verifier", | 
|  | 86 | +        ] | 
|  | 87 | + | 
|  | 88 | +        if self.toolchain.version >= 18: | 
|  | 89 | +            tests_list.append("test_progs_cpuv4") | 
|  | 90 | + | 
|  | 91 | +        if not self.parallel_tests: | 
|  | 92 | +            tests_list = [test for test in tests_list if not test.endswith("parallel")] | 
|  | 93 | + | 
|  | 94 | +        return {"include": [generate_test_config(test) for test in tests_list]} | 
|  | 95 | + | 
|  | 96 | +    def to_dict(self) -> Dict[str, Any]: | 
|  | 97 | +        return { | 
|  | 98 | +            "arch": self.arch.value, | 
|  | 99 | +            "toolchain": self.toolchain.to_dict(), | 
|  | 100 | +            "kernel": self.kernel, | 
|  | 101 | +            "run_veristat": self.run_veristat, | 
|  | 102 | +            "parallel_tests": self.parallel_tests, | 
|  | 103 | +            "build_release": self.build_release, | 
|  | 104 | +            "runs_on": self.runs_on, | 
|  | 105 | +            "tests": self.tests, | 
|  | 106 | +        } | 
|  | 107 | + | 
|  | 108 | + | 
|  | 109 | +def is_managed_repo() -> bool: | 
|  | 110 | +    return ( | 
|  | 111 | +        os.environ["GITHUB_REPOSITORY_OWNER"] == MANAGED_OWNER | 
|  | 112 | +        and os.environ["GITHUB_REPOSITORY"] in MANAGED_REPOS | 
|  | 113 | +    ) | 
|  | 114 | + | 
|  | 115 | + | 
|  | 116 | +def set_output(name, value): | 
|  | 117 | +    """Write an output variable to the GitHub output file.""" | 
|  | 118 | +    with open(os.getenv("GITHUB_OUTPUT"), "a", encoding="utf-8") as file: | 
|  | 119 | +        file.write(f"{name}={value}\n") | 
|  | 120 | + | 
|  | 121 | + | 
|  | 122 | +def generate_test_config(test: str) -> Dict[str, Union[str, int]]: | 
|  | 123 | +    """Create the configuration for the provided test.""" | 
|  | 124 | +    is_parallel = test.endswith("_parallel") | 
|  | 125 | +    config = { | 
|  | 126 | +        "test": test, | 
|  | 127 | +        "continue_on_error": is_parallel, | 
|  | 128 | +        # While in experimental mode, parallel jobs may get stuck | 
|  | 129 | +        # anywhere, including in user space where the kernel won't detect | 
|  | 130 | +        # a problem and panic. We add a second layer of (smaller) timeouts | 
|  | 131 | +        # here such that if we get stuck in a parallel run, we hit this | 
|  | 132 | +        # timeout and fail without affecting the overall job success (as | 
|  | 133 | +        # would be the case if we hit the job-wide timeout). For | 
|  | 134 | +        # non-experimental jobs, 360 is the default which will be | 
|  | 135 | +        # superseded by the overall workflow timeout (but we need to | 
|  | 136 | +        # specify something). | 
|  | 137 | +        "timeout_minutes": 30 if is_parallel else 360, | 
|  | 138 | +    } | 
|  | 139 | +    return config | 
|  | 140 | + | 
|  | 141 | + | 
|  | 142 | +if __name__ == "__main__": | 
|  | 143 | +    matrix = [ | 
|  | 144 | +        BuildConfig( | 
|  | 145 | +            arch=Arch.X86_64, | 
|  | 146 | +            toolchain=Toolchain(compiler=Compiler.GCC, version=DEFAULT_LLVM_VERSION), | 
|  | 147 | +            run_veristat=True, | 
|  | 148 | +            parallel_tests=True, | 
|  | 149 | +        ), | 
|  | 150 | +        BuildConfig( | 
|  | 151 | +            arch=Arch.X86_64, | 
|  | 152 | +            toolchain=Toolchain(compiler=Compiler.LLVM, version=DEFAULT_LLVM_VERSION), | 
|  | 153 | +            build_release=True, | 
|  | 154 | +        ), | 
|  | 155 | +        BuildConfig( | 
|  | 156 | +            arch=Arch.X86_64, | 
|  | 157 | +            toolchain=Toolchain(compiler=Compiler.LLVM, version=18), | 
|  | 158 | +            build_release=True, | 
|  | 159 | +        ), | 
|  | 160 | +        BuildConfig( | 
|  | 161 | +            arch=Arch.AARCH64, | 
|  | 162 | +            toolchain=Toolchain(compiler=Compiler.GCC, version=DEFAULT_LLVM_VERSION), | 
|  | 163 | +        ), | 
|  | 164 | +        # BuildConfig( | 
|  | 165 | +        #     arch=Arch.AARCH64, | 
|  | 166 | +        #     toolchain=Toolchain( | 
|  | 167 | +        #         compiler=Compiler.LLVM, | 
|  | 168 | +        #         version=DEFAULT_LLVM_VERSION | 
|  | 169 | +        #     ), | 
|  | 170 | +        # ), | 
|  | 171 | +        BuildConfig( | 
|  | 172 | +            arch=Arch.S390X, | 
|  | 173 | +            toolchain=Toolchain(compiler=Compiler.GCC, version=DEFAULT_LLVM_VERSION), | 
|  | 174 | +        ), | 
|  | 175 | +    ] | 
|  | 176 | + | 
|  | 177 | +    # Outside of those repositories we only run on x86_64 | 
|  | 178 | +    if not is_managed_repo(): | 
|  | 179 | +        matrix = [config for config in matrix if config.arch == Arch.X86_64] | 
|  | 180 | + | 
|  | 181 | +    json_matrix = json.dumps({"include": [config.to_dict() for config in matrix]}) | 
|  | 182 | +    print(json_matrix) | 
|  | 183 | +    set_output("build_matrix", json_matrix) | 
0 commit comments