blob: 1027945c4b27201e9550a0b9bf14de1b59037fb8 [file] [log] [blame]
Patrick Williams02871c92021-02-01 20:57:19 -06001#!/usr/bin/env python3
2#
3# Build the required docker image to run package unit tests
4#
5# Script Variables:
6# DOCKER_IMG_NAME: <optional, the name of the docker image to generate>
7# default is openbmc/ubuntu-unit-test
8# DISTRO: <optional, the distro to build a docker image against>
Patrick Williams50837432021-02-06 12:24:05 -06009# FORCE_DOCKER_BUILD: <optional, a non-zero value with force all Docker
10# images to be rebuilt rather than reusing caches.>
11# BUILD_URL: <optional, used to detect running under CI context
12# (ex. Jenkins)>
Patrick Williams02871c92021-02-01 20:57:19 -060013# BRANCH: <optional, branch to build from each of the openbmc/
14# repositories>
15# default is master, which will be used if input branch not
16# provided or not found
17# UBUNTU_MIRROR: <optional, the URL of a mirror of Ubuntu to override the
18# default ones in /etc/apt/sources.list>
19# default is empty, and no mirror is used.
Andrew Geisslerfe2768c2024-10-02 10:10:29 -040020# DOCKER_REG: <optional, the URL of a docker registry to utilize
Andrew Geissler23ec3322024-10-02 10:45:32 -040021# instead of our default (public.ecr.aws/ubuntu)
22# (ex. docker.io)
Patrick Williams02871c92021-02-01 20:57:19 -060023# http_proxy The HTTP address of the proxy server to connect to.
24# Default: "", proxy is not setup if this is not set
25
Patrick Williams276bd0e2024-10-02 10:34:32 -040026import json
Patrick Williams02871c92021-02-01 20:57:19 -060027import os
Andrew Geisslerf3d27e62024-04-09 15:24:49 -050028import re
Patrick Williams02871c92021-02-01 20:57:19 -060029import sys
Patrick Williamsb16f3e22021-02-06 08:16:47 -060030import threading
Patrick Williams276bd0e2024-10-02 10:34:32 -040031import urllib.request
Patrick Williamsa18d9c52021-02-05 09:52:26 -060032from datetime import date
33from hashlib import sha256
Patrick Williamse08ffba2022-12-05 10:33:46 -060034
35# typing.Dict is used for type-hints.
36from typing import Any, Callable, Dict, Iterable, Optional # noqa: F401
Patrick Williams02871c92021-02-01 20:57:19 -060037
Andrew Geissler8f7146f2024-12-11 14:20:47 -060038from sh import git, nproc # type: ignore
39
40try:
41 # System may have docker or it may have podman, try docker first
42 from sh import docker
43
44 container = docker
45except ImportError:
46 try:
47 from sh import podman
48
49 container = podman
50 except Exception:
51 print("No docker or podman found on system")
52 exit(1)
Patrick Williams41d86212022-11-25 18:28:43 -060053
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060054try:
55 # Python before 3.8 doesn't have TypedDict, so reroute to standard 'dict'.
56 from typing import TypedDict
Patrick Williams41d86212022-11-25 18:28:43 -060057except Exception:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060058
59 class TypedDict(dict): # type: ignore
60 # We need to do this to eat the 'total' argument.
Patrick Williams41d86212022-11-25 18:28:43 -060061 def __init_subclass__(cls, **kwargs: Any) -> None:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060062 super().__init_subclass__()
63
64
65# Declare some variables used in package definitions.
Patrick Williamsaae36d12021-02-04 16:30:04 -060066prefix = "/usr/local"
Patrick Williams02871c92021-02-01 20:57:19 -060067proc_count = nproc().strip()
Patrick Williams02871c92021-02-01 20:57:19 -060068
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060069
70class PackageDef(TypedDict, total=False):
Patrick Williams05fb2a02022-10-11 17:22:33 -050071 """Package Definition for packages dictionary."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060072
73 # rev [optional]: Revision of package to use.
74 rev: str
75 # url [optional]: lambda function to create URL: (package, rev) -> url.
76 url: Callable[[str, str], str]
77 # depends [optional]: List of package dependencies.
78 depends: Iterable[str]
79 # build_type [required]: Build type used for package.
80 # Currently supported: autoconf, cmake, custom, make, meson
81 build_type: str
82 # build_steps [optional]: Steps to run for 'custom' build_type.
83 build_steps: Iterable[str]
84 # config_flags [optional]: List of options to pass configuration tool.
85 config_flags: Iterable[str]
86 # config_env [optional]: List of environment variables to set for config.
87 config_env: Iterable[str]
88 # custom_post_dl [optional]: List of steps to run after download, but
89 # before config / build / install.
90 custom_post_dl: Iterable[str]
Patrick Williams6bce2ca2021-02-12 21:13:37 -060091 # custom_post_install [optional]: List of steps to run after install.
92 custom_post_install: Iterable[str]
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060093
94 # __tag [private]: Generated Docker tag name for package stage.
95 __tag: str
96 # __package [private]: Package object associated with this package.
97 __package: Any # Type is Package, but not defined yet.
98
Patrick Williams02871c92021-02-01 20:57:19 -060099
Patrick Williams72043242021-02-02 10:31:45 -0600100# Packages to include in image.
101packages = {
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600102 "boost": PackageDef(
Patrick Williamse4b761f2025-07-23 11:34:07 -0400103 rev="1.88.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600104 url=(
Jayanth Othayoth96982152024-12-12 05:13:54 -0600105 lambda pkg, rev: f"https://github.com/boostorg/{pkg}/releases/download/{pkg}-{rev}/{pkg}-{rev}-cmake.tar.gz"
Patrick Williams2abc4a42021-02-03 06:11:40 -0600106 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600107 build_type="custom",
108 build_steps=[
Patrick Williamse08ffba2022-12-05 10:33:46 -0600109 (
Andrew Geissler38b46872024-01-07 07:20:27 -0600110 "./bootstrap.sh"
Jayanth Othayoth96982152024-12-12 05:13:54 -0600111 f" --prefix={prefix} --with-libraries=atomic,context,coroutine,filesystem,process,url"
Patrick Williamse08ffba2022-12-05 10:33:46 -0600112 ),
Patrick Williamsaae36d12021-02-04 16:30:04 -0600113 "./b2",
Michal Orzel04770cc2024-06-18 10:38:22 +0200114 f"./b2 install --prefix={prefix} valgrind=on",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600115 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600116 ),
117 "USCiLab/cereal": PackageDef(
Patrick Williamsc1977832022-09-27 16:54:34 -0500118 rev="v1.3.2",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600119 build_type="custom",
120 build_steps=[f"cp -a include/cereal/ {prefix}/include/"],
121 ),
Ed Tanousc7198552022-07-01 08:15:50 -0700122 "danmar/cppcheck": PackageDef(
Patrick Williams51021782023-12-05 19:10:44 -0600123 rev="2.12.1",
Ed Tanousc7198552022-07-01 08:15:50 -0700124 build_type="cmake",
125 ),
Ratan Gupta3dc37e62025-07-17 16:47:13 +0530126 "DMTF/libspdm": PackageDef(
127 rev="3.7.0",
128 url=lambda pkg, rev: f"https://github.com/DMTF/libspdm/archive/{rev}.tar.gz",
129 build_type="cmake",
130 config_flags=(
131 lambda: (
132 lambda arch_mapping={
133 "x86_64": "x64",
134 "i586": "ia32",
135 "i686": "ia32",
136 "arm": "arm",
137 "aarch64": "aarch64",
138 "arm64": "aarch64",
139 "riscv32": "riscv32",
140 "riscv64": "riscv64",
141 "ppc64le": "ppc64le",
142 }: [
143 f"-DARCH={arch_mapping.get(__import__('platform').machine(), 'x64')}",
144 "-DTOOLCHAIN=GCC",
145 "-DTARGET=Release",
146 "-DCRYPTO=openssl",
147 "-DBUILD_LINUX_SHARED_LIB=ON",
148 "-DENABLE_BINARY_BUILD=1",
149 "-DDISABLE_TESTS=1",
150 f"-DCOMPILED_LIBCRYPTO_PATH={prefix}/lib",
151 f"-DCOMPILED_LIBSSL_PATH={prefix}/lib",
152 ]
153 )()
154 )(),
155 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600156 "CLIUtils/CLI11": PackageDef(
Patrick Williamsfc397332023-07-17 11:35:43 -0500157 rev="v2.3.2",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600158 build_type="cmake",
159 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600160 "-DBUILD_TESTING=OFF",
161 "-DCLI11_BUILD_DOCS=OFF",
162 "-DCLI11_BUILD_EXAMPLES=OFF",
163 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600164 ),
165 "fmtlib/fmt": PackageDef(
Patrick Williamse4b761f2025-07-23 11:34:07 -0400166 rev="11.2.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600167 build_type="cmake",
168 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600169 "-DFMT_DOC=OFF",
170 "-DFMT_TEST=OFF",
171 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600172 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600173 "Naios/function2": PackageDef(
Patrick Williamscb099742023-12-05 19:12:09 -0600174 rev="4.2.4",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600175 build_type="custom",
176 build_steps=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600177 f"mkdir {prefix}/include/function2",
178 f"cp include/function2/function2.hpp {prefix}/include/function2/",
179 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600180 ),
181 "google/googletest": PackageDef(
Patrick Williamse4b761f2025-07-23 11:34:07 -0400182 rev="v1.16.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600183 build_type="cmake",
William A. Kennington III4dd32c02021-05-28 01:58:13 -0700184 config_env=["CXXFLAGS=-std=c++20"],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600185 config_flags=["-DTHREADS_PREFER_PTHREAD_FLAG=ON"],
186 ),
Ed Tanous178b4b22023-06-15 09:03:11 -0700187 "nghttp2/nghttp2": PackageDef(
Patrick Williamse4b761f2025-07-23 11:34:07 -0400188 rev="v1.65.0",
Ed Tanous178b4b22023-06-15 09:03:11 -0700189 build_type="cmake",
190 config_env=["CXXFLAGS=-std=c++20"],
191 config_flags=[
192 "-DENABLE_LIB_ONLY=ON",
193 "-DENABLE_STATIC_LIB=ON",
194 ],
195 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600196 "nlohmann/json": PackageDef(
Patrick Williamse4b761f2025-07-23 11:34:07 -0400197 rev="v3.12.0",
Patrick Williams6bce2ca2021-02-12 21:13:37 -0600198 build_type="cmake",
199 config_flags=["-DJSON_BuildTests=OFF"],
200 custom_post_install=[
Patrick Williamse08ffba2022-12-05 10:33:46 -0600201 (
202 f"ln -s {prefix}/include/nlohmann/json.hpp"
203 f" {prefix}/include/json.hpp"
204 ),
Patrick Williamsaae36d12021-02-04 16:30:04 -0600205 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600206 ),
Przemyslaw Czarnowski058e3a32022-12-21 14:13:23 +0100207 "json-c/json-c": PackageDef(
Patrick Williamse4b761f2025-07-23 11:34:07 -0400208 rev="json-c-0.18-20240915",
Przemyslaw Czarnowski058e3a32022-12-21 14:13:23 +0100209 build_type="cmake",
210 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600211 "LibVNC/libvncserver": PackageDef(
Patrick Williamsc0421322023-12-05 19:18:57 -0600212 rev="LibVNCServer-0.9.14",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600213 build_type="cmake",
214 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600215 "leethomason/tinyxml2": PackageDef(
Patrick Williamse4b761f2025-07-23 11:34:07 -0400216 rev="11.0.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600217 build_type="cmake",
218 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600219 "tristanpenman/valijson": PackageDef(
Patrick Williamse4b761f2025-07-23 11:34:07 -0400220 rev="v1.0.5",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600221 build_type="cmake",
222 config_flags=[
Patrick Williams0eedeed2021-02-06 19:06:09 -0600223 "-Dvalijson_BUILD_TESTS=0",
224 "-Dvalijson_INSTALL_HEADERS=1",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600225 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600226 ),
Patrick Williamsc7e719f2025-07-24 16:51:57 -0400227 "libgpiod": PackageDef(
228 rev="1.6.5",
229 url=(
230 lambda pkg, rev: f"https://git.kernel.org/pub/scm/libs/{pkg}/{pkg}.git/snapshot/{pkg}-{rev}.tar.gz"
231 ),
232 build_type="autogen",
233 config_flags=["--enable-bindings-cxx"],
234 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600235 "open-power/pdbg": PackageDef(build_type="autoconf"),
236 "openbmc/gpioplus": PackageDef(
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600237 build_type="meson",
238 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600239 "-Dexamples=false",
240 "-Dtests=disabled",
241 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600242 ),
243 "openbmc/phosphor-dbus-interfaces": PackageDef(
244 depends=["openbmc/sdbusplus"],
245 build_type="meson",
William A. Kennington III4fe87772022-02-11 15:44:29 -0800246 config_flags=["-Dgenerate_md=false"],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600247 ),
248 "openbmc/phosphor-logging": PackageDef(
249 depends=[
Patrick Williams83394612021-02-03 07:12:50 -0600250 "USCiLab/cereal",
Patrick Williams83394612021-02-03 07:12:50 -0600251 "openbmc/phosphor-dbus-interfaces",
252 "openbmc/sdbusplus",
253 "openbmc/sdeventplus",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600254 ],
Patrick Williamsf79ce4c2021-04-30 16:00:49 -0500255 build_type="meson",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600256 config_flags=[
William A. Kennington III6c98f282022-10-05 13:37:04 -0700257 "-Dlibonly=true",
258 "-Dtests=disabled",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600259 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600260 ),
261 "openbmc/phosphor-objmgr": PackageDef(
262 depends=[
Brad Bishop11e57622022-09-14 16:10:25 -0400263 "CLIUtils/CLI11",
Patrick Williams70af95c2022-09-27 16:55:41 -0500264 "boost",
Patrick Williams83394612021-02-03 07:12:50 -0600265 "leethomason/tinyxml2",
Patrick Williams70af95c2022-09-27 16:55:41 -0500266 "openbmc/phosphor-dbus-interfaces",
Patrick Williams83394612021-02-03 07:12:50 -0600267 "openbmc/phosphor-logging",
268 "openbmc/sdbusplus",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600269 ],
Brad Bishop1197e352021-08-03 19:25:46 -0400270 build_type="meson",
271 config_flags=[
272 "-Dtests=disabled",
273 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600274 ),
Jason M. Billsc02ff272023-08-02 10:55:22 -0700275 "openbmc/libpeci": PackageDef(
276 build_type="meson",
277 config_flags=[
278 "-Draw-peci=disabled",
279 ],
280 ),
Manojkiran Eda1c19e452022-10-03 11:01:59 +0530281 "openbmc/libpldm": PackageDef(
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600282 build_type="meson",
Andrew Jeffery29163972024-12-06 17:01:56 +1030283 config_flags=[
284 "-Dabi=deprecated,stable",
285 "-Dtests=false",
286 "-Dabi-compliance-check=false",
287 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600288 ),
289 "openbmc/sdbusplus": PackageDef(
Patrick Williams54d01da2024-09-25 06:40:25 -0400290 depends=[
291 "nlohmann/json",
292 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600293 build_type="meson",
294 custom_post_dl=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600295 "cd tools",
296 f"./setup.py install --root=/ --prefix={prefix}",
297 "cd ..",
298 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600299 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600300 "-Dexamples=disabled",
301 "-Dtests=disabled",
302 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600303 ),
304 "openbmc/sdeventplus": PackageDef(
Patrick Williams70af95c2022-09-27 16:55:41 -0500305 depends=[
Patrick Williams70af95c2022-09-27 16:55:41 -0500306 "openbmc/stdplus",
307 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600308 build_type="meson",
309 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600310 "-Dexamples=false",
311 "-Dtests=disabled",
312 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600313 ),
314 "openbmc/stdplus": PackageDef(
Patrick Williams70af95c2022-09-27 16:55:41 -0500315 depends=[
Patrick Williams70af95c2022-09-27 16:55:41 -0500316 "fmtlib/fmt",
William A. Kennington IIIca1bf0c2022-10-05 02:23:30 -0700317 "google/googletest",
318 "Naios/function2",
Patrick Williams70af95c2022-09-27 16:55:41 -0500319 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600320 build_type="meson",
321 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600322 "-Dexamples=false",
323 "-Dtests=disabled",
William A. Kennington IIIca1bf0c2022-10-05 02:23:30 -0700324 "-Dgtest=enabled",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600325 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600326 ),
327} # type: Dict[str, PackageDef]
Patrick Williams02871c92021-02-01 20:57:19 -0600328
329# Define common flags used for builds
Patrick Williams02871c92021-02-01 20:57:19 -0600330configure_flags = " ".join(
331 [
332 f"--prefix={prefix}",
333 ]
334)
335cmake_flags = " ".join(
336 [
Patrick Williams02871c92021-02-01 20:57:19 -0600337 "-DBUILD_SHARED_LIBS=ON",
Patrick Williams0f2086b2021-02-05 06:49:49 -0600338 "-DCMAKE_BUILD_TYPE=RelWithDebInfo",
Patrick Williams02871c92021-02-01 20:57:19 -0600339 f"-DCMAKE_INSTALL_PREFIX:PATH={prefix}",
Patrick Williams0f2086b2021-02-05 06:49:49 -0600340 "-GNinja",
341 "-DCMAKE_MAKE_PROGRAM=ninja",
Patrick Williams02871c92021-02-01 20:57:19 -0600342 ]
343)
344meson_flags = " ".join(
345 [
346 "--wrap-mode=nodownload",
347 f"-Dprefix={prefix}",
348 ]
349)
350
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600351
352class Package(threading.Thread):
353 """Class used to build the Docker stages for each package.
354
355 Generally, this class should not be instantiated directly but through
356 Package.generate_all().
357 """
358
359 # Copy the packages dictionary.
360 packages = packages.copy()
361
362 # Lock used for thread-safety.
363 lock = threading.Lock()
364
365 def __init__(self, pkg: str):
Patrick Williams05fb2a02022-10-11 17:22:33 -0500366 """pkg - The name of this package (ex. foo/bar )"""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600367 super(Package, self).__init__()
368
369 self.package = pkg
370 self.exception = None # type: Optional[Exception]
371
372 # Reference to this package's
373 self.pkg_def = Package.packages[pkg]
374 self.pkg_def["__package"] = self
375
376 def run(self) -> None:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500377 """Thread 'run' function. Builds the Docker stage."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600378
379 # In case this package has no rev, fetch it from Github.
380 self._update_rev()
381
382 # Find all the Package objects that this package depends on.
383 # This section is locked because we are looking into another
384 # package's PackageDef dict, which could be being modified.
385 Package.lock.acquire()
386 deps: Iterable[Package] = [
387 Package.packages[deppkg]["__package"]
388 for deppkg in self.pkg_def.get("depends", [])
389 ]
390 Package.lock.release()
391
392 # Wait until all the depends finish building. We need them complete
393 # for the "COPY" commands.
394 for deppkg in deps:
395 deppkg.join()
396
397 # Generate this package's Dockerfile.
398 dockerfile = f"""
399FROM {docker_base_img_name}
400{self._df_copycmds()}
401{self._df_build()}
402"""
403
404 # Generate the resulting tag name and save it to the PackageDef.
405 # This section is locked because we are modifying the PackageDef,
406 # which can be accessed by other threads.
407 Package.lock.acquire()
408 tag = Docker.tagname(self._stagename(), dockerfile)
409 self.pkg_def["__tag"] = tag
410 Package.lock.release()
411
412 # Do the build / save any exceptions.
413 try:
414 Docker.build(self.package, tag, dockerfile)
415 except Exception as e:
416 self.exception = e
417
418 @classmethod
419 def generate_all(cls) -> None:
420 """Ensure a Docker stage is created for all defined packages.
421
422 These are done in parallel but with appropriate blocking per
423 package 'depends' specifications.
424 """
425
426 # Create a Package for each defined package.
427 pkg_threads = [Package(p) for p in cls.packages.keys()]
428
429 # Start building them all.
Patrick Williams6dbd7802021-02-20 08:34:10 -0600430 # This section is locked because threads depend on each other,
431 # based on the packages, and they cannot 'join' on a thread
432 # which is not yet started. Adding a lock here allows all the
433 # threads to start before they 'join' their dependencies.
434 Package.lock.acquire()
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600435 for t in pkg_threads:
436 t.start()
Patrick Williams6dbd7802021-02-20 08:34:10 -0600437 Package.lock.release()
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600438
439 # Wait for completion.
440 for t in pkg_threads:
441 t.join()
442 # Check if the thread saved off its own exception.
443 if t.exception:
444 print(f"Package {t.package} failed!", file=sys.stderr)
445 raise t.exception
446
447 @staticmethod
448 def df_all_copycmds() -> str:
449 """Formulate the Dockerfile snippet necessary to copy all packages
450 into the final image.
451 """
452 return Package.df_copycmds_set(Package.packages.keys())
453
454 @classmethod
455 def depcache(cls) -> str:
456 """Create the contents of the '/tmp/depcache'.
457 This file is a comma-separated list of "<pkg>:<rev>".
458 """
459
460 # This needs to be sorted for consistency.
461 depcache = ""
462 for pkg in sorted(cls.packages.keys()):
463 depcache += "%s:%s," % (pkg, cls.packages[pkg]["rev"])
464 return depcache
465
Patrick Williams276bd0e2024-10-02 10:34:32 -0400466 def _check_gerrit_topic(self) -> bool:
467 if not gerrit_topic:
468 return False
469 if not self.package.startswith("openbmc/"):
470 return False
471 if gerrit_project == self.package and gerrit_rev:
472 return False
473
474 try:
475 commits = json.loads(
476 urllib.request.urlopen(
477 f"https://gerrit.openbmc.org/changes/?q=status:open+project:{self.package}+topic:{gerrit_topic}"
478 )
479 .read()
480 .splitlines()[-1]
481 )
482
483 if len(commits) == 0:
484 return False
485 if len(commits) > 1:
486 print(
487 f"{self.package} has more than 1 commit under {gerrit_topic}; using lastest upstream: {len(commits)}",
488 file=sys.stderr,
489 )
490 return False
491
492 change_id = commits[0]["id"]
493
494 commit = json.loads(
495 urllib.request.urlopen(
496 f"https://gerrit.openbmc.org/changes/{change_id}/revisions/current/commit"
497 )
498 .read()
499 .splitlines()[-1]
500 )["commit"]
501
502 print(
503 f"Using {commit} from {gerrit_topic} for {self.package}",
504 file=sys.stderr,
505 )
506 self.pkg_def["rev"] = commit
507 return True
508
509 except urllib.error.HTTPError as e:
510 print(
511 f"Error loading topic {gerrit_topic} for {self.package}: ",
512 e,
513 file=sys.stderr,
514 )
515 return False
516
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600517 def _update_rev(self) -> None:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500518 """Look up the HEAD for missing a static rev."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600519
520 if "rev" in self.pkg_def:
521 return
522
Patrick Williams276bd0e2024-10-02 10:34:32 -0400523 if self._check_gerrit_topic():
524 return
525
Patrick Williams65b21fb2021-02-12 21:21:14 -0600526 # Check if Jenkins/Gerrit gave us a revision and use it.
527 if gerrit_project == self.package and gerrit_rev:
528 print(
529 f"Found Gerrit revision for {self.package}: {gerrit_rev}",
530 file=sys.stderr,
531 )
532 self.pkg_def["rev"] = gerrit_rev
533 return
534
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600535 # Ask Github for all the branches.
Patrick Williams05fb2a02022-10-11 17:22:33 -0500536 lookup = git(
537 "ls-remote", "--heads", f"https://github.com/{self.package}"
538 )
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600539
540 # Find the branch matching {branch} (or fallback to master).
541 # This section is locked because we are modifying the PackageDef.
542 Package.lock.acquire()
543 for line in lookup.split("\n"):
Andrew Geisslerf3d27e62024-04-09 15:24:49 -0500544 if re.fullmatch(f".*{branch}$", line.strip()):
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600545 self.pkg_def["rev"] = line.split()[0]
Andrew Geisslerf3d27e62024-04-09 15:24:49 -0500546 break
Patrick Williamsc7d73642022-10-11 17:22:06 -0500547 elif (
548 "refs/heads/master" in line or "refs/heads/main" in line
549 ) and "rev" not in self.pkg_def:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600550 self.pkg_def["rev"] = line.split()[0]
551 Package.lock.release()
552
553 def _stagename(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500554 """Create a name for the Docker stage associated with this pkg."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600555 return self.package.replace("/", "-").lower()
556
557 def _url(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500558 """Get the URL for this package."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600559 rev = self.pkg_def["rev"]
560
561 # If the lambda exists, call it.
562 if "url" in self.pkg_def:
563 return self.pkg_def["url"](self.package, rev)
564
565 # Default to the github archive URL.
566 return f"https://github.com/{self.package}/archive/{rev}.tar.gz"
567
568 def _cmd_download(self) -> str:
569 """Formulate the command necessary to download and unpack to source."""
570
571 url = self._url()
572 if ".tar." not in url:
573 raise NotImplementedError(
574 f"Unhandled download type for {self.package}: {url}"
575 )
576
577 cmd = f"curl -L {url} | tar -x"
578
579 if url.endswith(".bz2"):
580 cmd += "j"
581 elif url.endswith(".gz"):
582 cmd += "z"
583 else:
584 raise NotImplementedError(
585 f"Unknown tar flags needed for {self.package}: {url}"
586 )
587
588 return cmd
589
590 def _cmd_cd_srcdir(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500591 """Formulate the command necessary to 'cd' into the source dir."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600592 return f"cd {self.package.split('/')[-1]}*"
593
594 def _df_copycmds(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500595 """Formulate the dockerfile snippet necessary to COPY all depends."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600596
597 if "depends" not in self.pkg_def:
598 return ""
599 return Package.df_copycmds_set(self.pkg_def["depends"])
600
601 @staticmethod
602 def df_copycmds_set(pkgs: Iterable[str]) -> str:
603 """Formulate the Dockerfile snippet necessary to COPY a set of
604 packages into a Docker stage.
605 """
606
607 copy_cmds = ""
608
609 # Sort the packages for consistency.
610 for p in sorted(pkgs):
611 tag = Package.packages[p]["__tag"]
612 copy_cmds += f"COPY --from={tag} {prefix} {prefix}\n"
613 # Workaround for upstream docker bug and multiple COPY cmds
614 # https://github.com/moby/moby/issues/37965
615 copy_cmds += "RUN true\n"
616
617 return copy_cmds
618
619 def _df_build(self) -> str:
620 """Formulate the Dockerfile snippet necessary to download, build, and
621 install a package into a Docker stage.
622 """
623
624 # Download and extract source.
625 result = f"RUN {self._cmd_download()} && {self._cmd_cd_srcdir()} && "
626
627 # Handle 'custom_post_dl' commands.
628 custom_post_dl = self.pkg_def.get("custom_post_dl")
629 if custom_post_dl:
630 result += " && ".join(custom_post_dl) + " && "
631
632 # Build and install package based on 'build_type'.
633 build_type = self.pkg_def["build_type"]
634 if build_type == "autoconf":
635 result += self._cmd_build_autoconf()
Patrick Williamsc7e719f2025-07-24 16:51:57 -0400636 elif build_type == "autogen":
637 result += self._cmd_build_autogen()
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600638 elif build_type == "cmake":
639 result += self._cmd_build_cmake()
640 elif build_type == "custom":
641 result += self._cmd_build_custom()
642 elif build_type == "make":
643 result += self._cmd_build_make()
644 elif build_type == "meson":
645 result += self._cmd_build_meson()
646 else:
647 raise NotImplementedError(
648 f"Unhandled build type for {self.package}: {build_type}"
649 )
650
Patrick Williams6bce2ca2021-02-12 21:13:37 -0600651 # Handle 'custom_post_install' commands.
652 custom_post_install = self.pkg_def.get("custom_post_install")
653 if custom_post_install:
654 result += " && " + " && ".join(custom_post_install)
655
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600656 return result
657
658 def _cmd_build_autoconf(self) -> str:
659 options = " ".join(self.pkg_def.get("config_flags", []))
660 env = " ".join(self.pkg_def.get("config_env", []))
661 result = "./bootstrap.sh && "
662 result += f"{env} ./configure {configure_flags} {options} && "
663 result += f"make -j{proc_count} && make install"
664 return result
665
Patrick Williamsc7e719f2025-07-24 16:51:57 -0400666 def _cmd_build_autogen(self) -> str:
667 options = " ".join(self.pkg_def.get("config_flags", []))
668 env = " ".join(self.pkg_def.get("config_env", []))
669 result = f"{env} ./autogen.sh {configure_flags} {options} && "
670 result += "make && make install"
671 return result
672
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600673 def _cmd_build_cmake(self) -> str:
674 options = " ".join(self.pkg_def.get("config_flags", []))
675 env = " ".join(self.pkg_def.get("config_env", []))
676 result = "mkdir builddir && cd builddir && "
677 result += f"{env} cmake {cmake_flags} {options} .. && "
678 result += "cmake --build . --target all && "
679 result += "cmake --build . --target install && "
680 result += "cd .."
681 return result
682
683 def _cmd_build_custom(self) -> str:
684 return " && ".join(self.pkg_def.get("build_steps", []))
685
686 def _cmd_build_make(self) -> str:
687 return f"make -j{proc_count} && make install"
688
689 def _cmd_build_meson(self) -> str:
690 options = " ".join(self.pkg_def.get("config_flags", []))
691 env = " ".join(self.pkg_def.get("config_env", []))
Andrew Jefferye2da11a2023-06-15 10:16:37 +0930692 result = f"{env} meson setup builddir {meson_flags} {options} && "
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600693 result += "ninja -C builddir && ninja -C builddir install"
694 return result
695
696
697class Docker:
698 """Class to assist with Docker interactions. All methods are static."""
699
700 @staticmethod
701 def timestamp() -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500702 """Generate a timestamp for today using the ISO week."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600703 today = date.today().isocalendar()
704 return f"{today[0]}-W{today[1]:02}"
705
706 @staticmethod
Patrick Williams41d86212022-11-25 18:28:43 -0600707 def tagname(pkgname: Optional[str], dockerfile: str) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500708 """Generate a tag name for a package using a hash of the Dockerfile."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600709 result = docker_image_name
710 if pkgname:
711 result += "-" + pkgname
712
713 result += ":" + Docker.timestamp()
714 result += "-" + sha256(dockerfile.encode()).hexdigest()[0:16]
715
716 return result
717
718 @staticmethod
719 def build(pkg: str, tag: str, dockerfile: str) -> None:
Andrew Geissler22e61102023-02-14 14:44:00 -0600720 """Build a docker image using the Dockerfile and tagging it with 'tag'."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600721
722 # If we're not forcing builds, check if it already exists and skip.
723 if not force_build:
Andrew Geissler8f7146f2024-12-11 14:20:47 -0600724 if container.image.ls(
725 tag, "--format", '"{{.Repository}}:{{.Tag}}"'
726 ):
Patrick Williams05fb2a02022-10-11 17:22:33 -0500727 print(
728 f"Image {tag} already exists. Skipping.", file=sys.stderr
729 )
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600730 return
731
732 # Build it.
733 # Capture the output of the 'docker build' command and send it to
734 # stderr (prefixed with the package name). This allows us to see
Manojkiran Edaa6ebc6e2024-06-17 11:51:45 +0530735 # progress but not pollute stdout. Later on we output the final
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600736 # docker tag to stdout and we want to keep that pristine.
737 #
738 # Other unusual flags:
739 # --no-cache: Bypass the Docker cache if 'force_build'.
740 # --force-rm: Clean up Docker processes if they fail.
Andrew Geissler8f7146f2024-12-11 14:20:47 -0600741 container.build(
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600742 proxy_args,
743 "--network=host",
744 "--force-rm",
745 "--no-cache=true" if force_build else "--no-cache=false",
746 "-t",
747 tag,
748 "-",
749 _in=dockerfile,
750 _out=(
751 lambda line: print(
752 pkg + ":", line, end="", file=sys.stderr, flush=True
753 )
754 ),
Jonathan Doman88dd7922024-05-02 10:34:21 -0700755 _err_to_out=True,
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600756 )
757
758
759# Read a bunch of environment variables.
Patrick Williams05fb2a02022-10-11 17:22:33 -0500760docker_image_name = os.environ.get(
761 "DOCKER_IMAGE_NAME", "openbmc/ubuntu-unit-test"
762)
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600763force_build = os.environ.get("FORCE_DOCKER_BUILD")
764is_automated_ci_build = os.environ.get("BUILD_URL", False)
Patrick Williams6b141902025-07-23 11:24:11 -0400765distro = os.environ.get("DISTRO", "ubuntu:plucky")
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600766branch = os.environ.get("BRANCH", "master")
767ubuntu_mirror = os.environ.get("UBUNTU_MIRROR")
Andrew Geissler23ec3322024-10-02 10:45:32 -0400768docker_reg = os.environ.get("DOCKER_REG", "public.ecr.aws/ubuntu")
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600769http_proxy = os.environ.get("http_proxy")
770
Patrick Williams65b21fb2021-02-12 21:21:14 -0600771gerrit_project = os.environ.get("GERRIT_PROJECT")
772gerrit_rev = os.environ.get("GERRIT_PATCHSET_REVISION")
Patrick Williams276bd0e2024-10-02 10:34:32 -0400773gerrit_topic = os.environ.get("GERRIT_TOPIC")
Patrick Williams65b21fb2021-02-12 21:21:14 -0600774
Andrew Geisslerd0dabc32023-04-04 08:09:21 -0600775# Ensure appropriate docker build output to see progress and identify
776# any issues
777os.environ["BUILDKIT_PROGRESS"] = "plain"
778
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600779# Set up some common variables.
780username = os.environ.get("USER", "root")
781homedir = os.environ.get("HOME", "/root")
782gid = os.getgid()
783uid = os.getuid()
784
Josh Lehan6825a012022-03-17 18:31:39 -0700785# Use well-known constants if user is root
786if username == "root":
787 homedir = "/root"
788 gid = 0
789 uid = 0
790
Patrick Williams02871c92021-02-01 20:57:19 -0600791# Special flags if setting up a deb mirror.
792mirror = ""
793if "ubuntu" in distro and ubuntu_mirror:
794 mirror = f"""
Patrick Williamse08ffba2022-12-05 10:33:46 -0600795RUN echo "deb {ubuntu_mirror} \
796 $(. /etc/os-release && echo $VERSION_CODENAME) \
797 main restricted universe multiverse" > /etc/apt/sources.list && \\
798 echo "deb {ubuntu_mirror} \
799 $(. /etc/os-release && echo $VERSION_CODENAME)-updates \
800 main restricted universe multiverse" >> /etc/apt/sources.list && \\
801 echo "deb {ubuntu_mirror} \
802 $(. /etc/os-release && echo $VERSION_CODENAME)-security \
803 main restricted universe multiverse" >> /etc/apt/sources.list && \\
804 echo "deb {ubuntu_mirror} \
805 $(. /etc/os-release && echo $VERSION_CODENAME)-proposed \
806 main restricted universe multiverse" >> /etc/apt/sources.list && \\
807 echo "deb {ubuntu_mirror} \
808 $(. /etc/os-release && echo $VERSION_CODENAME)-backports \
809 main restricted universe multiverse" >> /etc/apt/sources.list
Patrick Williams02871c92021-02-01 20:57:19 -0600810"""
811
812# Special flags for proxying.
813proxy_cmd = ""
Adrian Ambrożewicz34ec77e2021-06-02 10:23:38 +0200814proxy_keyserver = ""
Patrick Williams02871c92021-02-01 20:57:19 -0600815proxy_args = []
816if http_proxy:
817 proxy_cmd = f"""
818RUN echo "[http]" >> {homedir}/.gitconfig && \
819 echo "proxy = {http_proxy}" >> {homedir}/.gitconfig
Tan Siewert3aa71c82025-01-24 15:26:55 +0100820COPY <<EOF_WGETRC {homedir}/.wgetrc
821https_proxy = {http_proxy}
822http_proxy = {http_proxy}
823use_proxy = on
Lei YUf7e52612025-01-08 11:06:59 +0000824EOF_WGETRC
Patrick Williams02871c92021-02-01 20:57:19 -0600825"""
Adrian Ambrożewicz34ec77e2021-06-02 10:23:38 +0200826 proxy_keyserver = f"--keyserver-options http-proxy={http_proxy}"
827
Patrick Williams02871c92021-02-01 20:57:19 -0600828 proxy_args.extend(
829 [
830 "--build-arg",
831 f"http_proxy={http_proxy}",
832 "--build-arg",
Lei YUd461cd62021-02-18 14:25:49 +0800833 f"https_proxy={http_proxy}",
Patrick Williams02871c92021-02-01 20:57:19 -0600834 ]
835 )
836
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600837# Create base Dockerfile.
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600838dockerfile_base = f"""
Andrew Geisslerfe2768c2024-10-02 10:10:29 -0400839FROM {docker_reg}/{distro}
Patrick Williams02871c92021-02-01 20:57:19 -0600840
841{mirror}
842
843ENV DEBIAN_FRONTEND noninteractive
844
Patrick Williams8949d3c2022-04-27 16:41:27 -0500845ENV PYTHONPATH "/usr/local/lib/python3.10/site-packages/"
Patrick Williams02871c92021-02-01 20:57:19 -0600846
Patrick Williamsbb16ac12021-04-12 12:23:51 -0500847# Sometimes the ubuntu key expires and we need a way to force an execution
848# of the apt-get commands for the dbgsym-keyring. When this happens we see
849# an error like: "Release: The following signatures were invalid:"
850# Insert a bogus echo that we can change here when we get this error to force
851# the update.
Patrick Williamsa1cbd402025-06-25 10:23:50 -0400852RUN echo "ubuntu keyserver rev as of 2025-06-25"
Patrick Williamsbb16ac12021-04-12 12:23:51 -0500853
Patrick Williams02871c92021-02-01 20:57:19 -0600854# We need the keys to be imported for dbgsym repos
855# New releases have a package, older ones fall back to manual fetching
856# https://wiki.ubuntu.com/Debug%20Symbol%20Packages
Jagpal Singh Gill575b5e42023-04-14 15:52:10 -0700857# Known issue with gpg to get keys via proxy -
858# https://bugs.launchpad.net/ubuntu/+source/gnupg2/+bug/1788190, hence using
859# curl to get keys.
Patrick Williams50837432021-02-06 12:24:05 -0600860RUN apt-get update && apt-get dist-upgrade -yy && \
Jian Zhang938d3032023-07-05 13:35:35 +0800861 ( apt-get install -yy gpgv ubuntu-dbgsym-keyring || \
Jagpal Singh Gill575b5e42023-04-14 15:52:10 -0700862 ( apt-get install -yy dirmngr curl && \
863 curl -sSL \
864 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xF2EDC64DC5AEE1F6B9C621F0C8CAB6595FDFF622' \
865 | apt-key add - ))
Patrick Williams02871c92021-02-01 20:57:19 -0600866
867# Parse the current repo list into a debug repo list
Patrick Williamse08ffba2022-12-05 10:33:46 -0600868RUN sed -n '/^deb /s,^deb [^ ]* ,deb http://ddebs.ubuntu.com ,p' \
869 /etc/apt/sources.list >/etc/apt/sources.list.d/debug.list
Patrick Williams02871c92021-02-01 20:57:19 -0600870
871# Remove non-existent debug repos
Patrick Williams41d86212022-11-25 18:28:43 -0600872RUN sed -i '/-\\(backports\\|security\\) /d' /etc/apt/sources.list.d/debug.list
Patrick Williams02871c92021-02-01 20:57:19 -0600873
874RUN cat /etc/apt/sources.list.d/debug.list
875
876RUN apt-get update && apt-get dist-upgrade -yy && apt-get install -yy \
Andrew Jeffery58f19152023-05-22 16:41:32 +0930877 abi-compliance-checker \
Andrew Jeffery8b112062023-05-22 20:49:11 +0930878 abi-dumper \
Patrick Williams02871c92021-02-01 20:57:19 -0600879 autoconf \
880 autoconf-archive \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600881 bison \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600882 cmake \
883 curl \
884 dbus \
885 device-tree-compiler \
Andrew Jeffery1c28d962025-05-09 14:17:29 +0930886 doxygen \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600887 flex \
Patrick Williamsea1bfb22025-07-23 10:46:34 -0400888 g++-15 \
889 gcc-15 \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600890 git \
Patrick Williamsb4eec872024-10-04 10:49:50 -0400891 glib-2.0 \
Patrick Williams6968e832024-08-16 17:43:24 -0400892 gnupg \
Patrick Williams02871c92021-02-01 20:57:19 -0600893 iproute2 \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600894 iputils-ping \
Manojkiran Eda524a3312023-04-05 15:37:47 +0530895 libaudit-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600896 libc6-dbg \
897 libc6-dev \
Patrick Williamsc7bc4d12024-10-04 11:22:02 -0400898 libcjson-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600899 libconfig++-dev \
900 libcryptsetup-dev \
Anirban Banerjeea7a30552024-12-20 19:12:42 -0800901 libcurl4-openssl-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600902 libdbus-1-dev \
903 libevdev-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600904 libi2c-dev \
905 libjpeg-dev \
906 libjson-perl \
907 libldap2-dev \
908 libmimetic-dev \
Ewelina Walkusz3ee62fb2025-02-25 16:04:52 +0100909 libmpfr-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600910 libnl-3-dev \
911 libnl-genl-3-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600912 libpam0g-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600913 libpciaccess-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600914 libperlio-gzip-perl \
915 libpng-dev \
916 libprotobuf-dev \
917 libsnmp-dev \
918 libssl-dev \
919 libsystemd-dev \
920 libtool \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600921 liburing-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600922 libxml2-utils \
Patrick Williams0eedeed2021-02-06 19:06:09 -0600923 libxml-simple-perl \
Patrick Williams6968e832024-08-16 17:43:24 -0400924 lsb-release \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600925 ninja-build \
926 npm \
927 pkg-config \
928 protobuf-compiler \
929 python3 \
930 python3-dev\
931 python3-git \
932 python3-mako \
933 python3-pip \
William A. Kennington III25ba1e22024-03-24 15:47:51 -0700934 python3-protobuf \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600935 python3-setuptools \
936 python3-socks \
937 python3-yaml \
John Wedig9adf68d2021-11-16 14:00:39 -0800938 rsync \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600939 shellcheck \
Ewelina Walkusz8dd1bfe2024-05-27 09:34:50 +0200940 socat \
Patrick Williams6968e832024-08-16 17:43:24 -0400941 software-properties-common \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600942 sudo \
943 systemd \
Patrick Williams917b1772024-12-11 15:15:44 -0500944 systemd-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600945 valgrind \
Andrew Geisslerb565f822022-12-14 11:43:25 -0600946 vim \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600947 wget \
948 xxd
Patrick Williams02871c92021-02-01 20:57:19 -0600949
Patrick Williamsea1bfb22025-07-23 10:46:34 -0400950RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-15 15 \
951 --slave /usr/bin/g++ g++ /usr/bin/g++-15 \
952 --slave /usr/bin/gcov gcov /usr/bin/gcov-15 \
953 --slave /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-15 \
954 --slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-15
Patrick Williams961f1482023-05-30 09:24:16 -0500955RUN update-alternatives --remove cpp /usr/bin/cpp && \
Patrick Williamsea1bfb22025-07-23 10:46:34 -0400956 update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-15 15
Patrick Williams02871c92021-02-01 20:57:19 -0600957
Patrick Williams6968e832024-08-16 17:43:24 -0400958# Set up LLVM apt repository.
Patrick Williams412b2812025-05-08 15:19:46 -0400959RUN bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" -- 20
Patrick Williams6968e832024-08-16 17:43:24 -0400960
961# Install extra clang tools
Patrick Williamsed8aeca2024-12-18 11:08:29 -0500962RUN apt-get install -y \
Patrick Williams412b2812025-05-08 15:19:46 -0400963 clang-20 \
964 clang-format-20 \
Ed Tanousd7133492025-07-24 12:20:11 -0700965 clang-tidy-20 \
966 lld-20
Patrick Williams6968e832024-08-16 17:43:24 -0400967
Patrick Williams412b2812025-05-08 15:19:46 -0400968RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-20 1000 \
969 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-20 \
970 --slave /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-20 \
971 --slave /usr/bin/clang-format clang-format /usr/bin/clang-format-20 \
Patrick Williamse08ffba2022-12-05 10:33:46 -0600972 --slave /usr/bin/run-clang-tidy run-clang-tidy.py \
Patrick Williams412b2812025-05-08 15:19:46 -0400973 /usr/bin/run-clang-tidy-20 \
Ed Tanousd7133492025-07-24 12:20:11 -0700974 --slave /usr/bin/scan-build scan-build /usr/bin/scan-build-20 \
975 --slave /usr/bin/lld lld /usr/bin/lld-20
Patrick Williams02871c92021-02-01 20:57:19 -0600976
Patrick Williams50837432021-02-06 12:24:05 -0600977"""
978
979if is_automated_ci_build:
980 dockerfile_base += f"""
Manojkiran Edaa6ebc6e2024-06-17 11:51:45 +0530981# Run an arbitrary command to pollute the docker cache regularly force us
Patrick Williams50837432021-02-06 12:24:05 -0600982# to re-run `apt-get update` daily.
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600983RUN echo {Docker.timestamp()}
Patrick Williams50837432021-02-06 12:24:05 -0600984RUN apt-get update && apt-get dist-upgrade -yy
985
986"""
987
Patrick Williams41d86212022-11-25 18:28:43 -0600988dockerfile_base += """
Patrick Williams5e4d8402023-04-11 22:19:30 -0500989RUN pip3 install --break-system-packages \
Patrick Williams818023d2023-04-10 13:07:15 -0500990 beautysh \
991 black \
992 codespell \
993 flake8 \
Ewelina Walkusz2d8c5512024-07-02 10:49:38 +0200994 gcovr \
Patrick Williams818023d2023-04-10 13:07:15 -0500995 gitlint \
996 inflection \
Arya K Padmanf7381ad2024-10-14 02:29:53 -0500997 isoduration \
Patrick Williams818023d2023-04-10 13:07:15 -0500998 isort \
999 jsonschema \
Patrick Williamsbcc78d32025-07-23 11:29:11 -04001000 meson==1.8.2 \
Patrick Williams9fdba2d2025-05-14 12:36:55 -04001001 referencing \
Patrick Williams818023d2023-04-10 13:07:15 -05001002 requests
Patrick Williamsb08ddf72022-12-06 08:56:31 -06001003
1004RUN npm install -g \
Xinnan Xied0757de2024-05-27 14:22:58 +08001005 eslint@v8.56.0 eslint-plugin-json@v3.1.0 \
Patrick Williams7d41f6d2022-12-06 10:19:43 -06001006 markdownlint-cli@latest \
Patrick Williamsb08ddf72022-12-06 08:56:31 -06001007 prettier@latest
Ed Tanousfb9948a2022-06-21 09:10:24 -07001008"""
1009
Patrick Williamsee3c9ee2021-02-12 20:56:01 -06001010# Build the base and stage docker images.
1011docker_base_img_name = Docker.tagname("base", dockerfile_base)
1012Docker.build("base", docker_base_img_name, dockerfile_base)
1013Package.generate_all()
Patrick Williams02871c92021-02-01 20:57:19 -06001014
Patrick Williamsee3c9ee2021-02-12 20:56:01 -06001015# Create the final Dockerfile.
Patrick Williamsa18d9c52021-02-05 09:52:26 -06001016dockerfile = f"""
Patrick Williams02871c92021-02-01 20:57:19 -06001017# Build the final output image
Patrick Williamsa18d9c52021-02-05 09:52:26 -06001018FROM {docker_base_img_name}
Patrick Williamsee3c9ee2021-02-12 20:56:01 -06001019{Package.df_all_copycmds()}
Patrick Williams02871c92021-02-01 20:57:19 -06001020
1021# Some of our infrastructure still relies on the presence of this file
1022# even though it is no longer needed to rebuild the docker environment
1023# NOTE: The file is sorted to ensure the ordering is stable.
Patrick Williamsee3c9ee2021-02-12 20:56:01 -06001024RUN echo '{Package.depcache()}' > /tmp/depcache
Patrick Williams02871c92021-02-01 20:57:19 -06001025
Patrick Williams67cc0612023-04-11 22:16:46 -05001026# Ensure the group, user, and home directory are created (or rename them if
1027# they already exist).
1028RUN if grep -q ":{gid}:" /etc/group ; then \
1029 groupmod -n {username} $(awk -F : '{{ if ($3 == {gid}) {{ print $1 }} }}' /etc/group) ; \
1030 else \
1031 groupadd -f -g {gid} {username} ; \
1032 fi
Patrick Williams02871c92021-02-01 20:57:19 -06001033RUN mkdir -p "{os.path.dirname(homedir)}"
Patrick Williams67cc0612023-04-11 22:16:46 -05001034RUN if grep -q ":{uid}:" /etc/passwd ; then \
Patrick Williams73b3ee92023-04-24 10:11:01 -05001035 usermod -l {username} -d {homedir} -m $(awk -F : '{{ if ($3 == {uid}) {{ print $1 }} }}' /etc/passwd) ; \
Patrick Williams67cc0612023-04-11 22:16:46 -05001036 else \
1037 useradd -d {homedir} -m -u {uid} -g {gid} {username} ; \
1038 fi
Patrick Williams02871c92021-02-01 20:57:19 -06001039RUN sed -i '1iDefaults umask=000' /etc/sudoers
1040RUN echo "{username} ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
1041
Andrew Geissler305a9a52021-04-07 11:08:40 -05001042# Ensure user has ability to write to /usr/local for different tool
1043# and data installs
Andrew Geissler7bb00b12021-05-10 15:12:08 -05001044RUN chown -R {username}:{username} /usr/local/share
Andrew Geissler305a9a52021-04-07 11:08:40 -05001045
Jonathan Domanab4fee82024-01-31 15:39:20 -08001046# Update library cache
1047RUN ldconfig
1048
Patrick Williams02871c92021-02-01 20:57:19 -06001049{proxy_cmd}
1050
1051RUN /bin/bash
1052"""
1053
Patrick Williamsa18d9c52021-02-05 09:52:26 -06001054# Do the final docker build
Patrick Williamsee3c9ee2021-02-12 20:56:01 -06001055docker_final_img_name = Docker.tagname(None, dockerfile)
1056Docker.build("final", docker_final_img_name, dockerfile)
1057
Patrick Williams00536fb2021-02-11 14:28:49 -06001058# Print the tag of the final image.
1059print(docker_final_img_name)