xref: /openbmc/qemu/tests/lcitool/refresh (revision b8116f4c)
1#!/usr/bin/env python3
2#
3# Re-generate container recipes
4#
5# This script uses the "lcitool" available from
6#
7#   https://gitlab.com/libvirt/libvirt-ci
8#
9# Copyright (c) 2020 Red Hat Inc.
10#
11# This work is licensed under the terms of the GNU GPL, version 2
12# or (at your option) any later version. See the COPYING file in
13# the top-level directory.
14
15import sys
16import subprocess
17
18from pathlib import Path
19
20if len(sys.argv) != 1:
21    print("syntax: %s" % sys.argv[0], file=sys.stderr)
22    sys.exit(1)
23
24self_dir = Path(__file__).parent
25src_dir = self_dir.parent.parent
26dockerfiles_dir = Path(src_dir, "tests", "docker", "dockerfiles")
27
28lcitool_path = Path(self_dir, "libvirt-ci", "bin", "lcitool")
29
30lcitool_cmd = [lcitool_path, "--data-dir", self_dir]
31
32
33def atomic_write(filename, content):
34    tmp = filename.with_suffix(filename.suffix + ".tmp")
35    try:
36        with tmp.open("w") as fp:
37            print(content, file=fp, end="")
38            tmp.rename(filename)
39    except Exception as ex:
40        tmp.unlink()
41        raise
42
43
44def generate(filename, cmd, trailer):
45    print("Generate %s" % filename)
46    lcitool = subprocess.run(cmd, capture_output=True, encoding='utf8')
47
48    if lcitool.returncode != 0:
49        raise Exception("Failed to generate %s: %s" % (filename, lcitool.stderr))
50
51    content = lcitool.stdout
52    if trailer is not None:
53        content += trailer
54    atomic_write(filename, content)
55
56# Optional user setting, this will always be the last thing added
57# so maximise the number of layers that are cached
58add_user_mapping = [
59    "# As a final step configure the user (if env is defined)",
60    "ARG USER",
61    "ARG UID",
62    "RUN if [ \"${USER}\" ]; then \\",
63    "  id ${USER} 2>/dev/null || useradd -u ${UID} -U ${USER}; fi\n"
64]
65
66def generate_dockerfile(host, target, project="qemu", cross=None, trailer=None):
67    filename = Path(src_dir, "tests", "docker", "dockerfiles", host + ".docker")
68    cmd = lcitool_cmd + ["dockerfile"]
69    if cross is not None:
70        cmd.extend(["--cross", cross])
71    cmd.extend([target, project])
72
73    if trailer is not None:
74        trailer += "\n".join(add_user_mapping)
75    else:
76        trailer = "\n".join(add_user_mapping)
77
78    generate(filename, cmd, trailer)
79
80
81def generate_cirrus(target, trailer=None):
82    filename = Path(src_dir, ".gitlab-ci.d", "cirrus", target + ".vars")
83    cmd = lcitool_cmd + ["variables", target, "qemu"]
84    generate(filename, cmd, trailer)
85
86
87def generate_pkglist(vm, target):
88    filename = Path(src_dir, "tests", "vm", "generated", vm + ".json")
89    cmd = lcitool_cmd + ["variables", "--format", "json", target, "qemu"]
90    generate(filename, cmd, None)
91
92
93# Netmap still needs to be manually built as it is yet to be packaged
94# into a distro. We also add cscope and gtags which are used in the CI
95# test
96debian12_extras = [
97    "# netmap/cscope/global\n",
98    "RUN DEBIAN_FRONTEND=noninteractive eatmydata \\\n",
99    "  apt install -y --no-install-recommends \\\n",
100    "  cscope\\\n",
101    "  global\\\n",
102    "  linux-headers-generic\n",
103    "RUN git clone https://github.com/luigirizzo/netmap.git /usr/src/netmap\n",
104    "RUN cd /usr/src/netmap && git checkout v11.3\n",
105    "RUN cd /usr/src/netmap/LINUX && \\\n",
106    "  ./configure --no-drivers --no-apps \\\n",
107    "  --kernel-dir=$(ls -d /usr/src/linux-headers-*-$(dpkg --print-architecture)) \\\n",
108    "  && make install\n",
109    "ENV QEMU_CONFIGURE_OPTS --enable-netmap\n"
110]
111
112
113def cross_build(prefix, targets):
114    conf = "ENV QEMU_CONFIGURE_OPTS --cross-prefix=%s\n" % (prefix)
115    targets = "ENV DEF_TARGET_LIST %s\n" % (targets)
116    return "".join([conf, targets])
117
118#
119# Update all the various build configurations.
120# Please keep each group sorted alphabetically for easy reading.
121#
122
123try:
124    #
125    # Standard native builds
126    #
127    generate_dockerfile("alpine", "alpine-318")
128    generate_dockerfile("centos9", "centos-stream-9")
129    generate_dockerfile("debian", "debian-12",
130                        trailer="".join(debian12_extras))
131    generate_dockerfile("fedora", "fedora-38")
132    generate_dockerfile("opensuse-leap", "opensuse-leap-15")
133    generate_dockerfile("ubuntu2204", "ubuntu-2204")
134
135    #
136    # Cross compiling builds
137    #
138    generate_dockerfile("debian-amd64-cross", "debian-12",
139                        cross="x86_64",
140                        trailer=cross_build("x86_64-linux-gnu-",
141                                            "x86_64-softmmu,"
142                                            "x86_64-linux-user,"
143                                            "i386-softmmu,i386-linux-user"))
144
145    generate_dockerfile("debian-arm64-cross", "debian-12",
146                        cross="aarch64",
147                        trailer=cross_build("aarch64-linux-gnu-",
148                                            "aarch64-softmmu,aarch64-linux-user"))
149
150    # migration to bookworm stalled: https://lists.debian.org/debian-arm/2023/09/msg00006.html
151    generate_dockerfile("debian-armel-cross", "debian-11",
152                        cross="armv6l",
153                        trailer=cross_build("arm-linux-gnueabi-",
154                                            "arm-softmmu,arm-linux-user,armeb-linux-user"))
155
156    generate_dockerfile("debian-armhf-cross", "debian-12",
157                        cross="armv7l",
158                        trailer=cross_build("arm-linux-gnueabihf-",
159                                            "arm-softmmu,arm-linux-user"))
160
161    generate_dockerfile("debian-i686-cross", "debian-11",
162                        cross="i686",
163                        trailer=cross_build("x86_64-linux-gnu-",
164                                            "x86_64-softmmu,"
165                                            "x86_64-linux-user,"
166                                            "i386-softmmu,i386-linux-user"))
167
168    generate_dockerfile("debian-mips64el-cross", "debian-11",
169                        cross="mips64el",
170                        trailer=cross_build("mips64el-linux-gnuabi64-",
171                                            "mips64el-softmmu,mips64el-linux-user"))
172
173    generate_dockerfile("debian-mipsel-cross", "debian-11",
174                        cross="mipsel",
175                        trailer=cross_build("mipsel-linux-gnu-",
176                                            "mipsel-softmmu,mipsel-linux-user"))
177
178    generate_dockerfile("debian-ppc64el-cross", "debian-12",
179                        cross="ppc64le",
180                        trailer=cross_build("powerpc64le-linux-gnu-",
181                                            "ppc64-softmmu,ppc64-linux-user"))
182
183    generate_dockerfile("debian-riscv64-cross", "debian-sid",
184                        project="qemu-minimal",
185                        cross="riscv64",
186                        trailer=cross_build("riscv64-linux-gnu-",
187                                            "riscv64-softmmu,riscv64-linux-user"))
188
189    generate_dockerfile("debian-s390x-cross", "debian-12",
190                        cross="s390x",
191                        trailer=cross_build("s390x-linux-gnu-",
192                                            "s390x-softmmu,s390x-linux-user"))
193
194    generate_dockerfile("fedora-win64-cross", "fedora-38",
195                        project='qemu,qemu-win-installer',
196                        cross="mingw64",
197                        trailer=cross_build("x86_64-w64-mingw32-",
198                                            "x86_64-softmmu"))
199
200    #
201    # Cirrus packages lists for GitLab
202    #
203    generate_cirrus("freebsd-13")
204    generate_cirrus("macos-13")
205    generate_cirrus("macos-14")
206
207    #
208    # VM packages lists
209    #
210    generate_pkglist("freebsd", "freebsd-13")
211
212    sys.exit(0)
213except Exception as ex:
214    print(str(ex), file=sys.stderr)
215    sys.exit(1)
216