]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
meson: generate better arch defines for clang bpf compilation
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 3 Jan 2022 17:38:23 +0000 (18:38 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Tue, 4 Jan 2022 00:18:47 +0000 (00:18 +0000)
The code assume that meson's cpu_family can be mapped directly to
'-D__<cpu_family>__'. This works in a surprising number of cases, but not for a
few architectures. PPC uses "powerpc", and RISC-V omits the trailing underscores.
ARM and RISC-V require a second define too.

Fixes #21900.

(I don't think this matters too much: we need *something* so that gnu/stubs.h
can be successfully included. But we don't actually call syscalls or depend too
much on the host environment, so things should be fine as long as we don't get
a compilation error.)

src/core/bpf/meson.build

index cd0cd3230b4c8f89d2b793cf93c398fd841cd035..c2465a845f44c7a64ab183c9a95cecb667545f04 100644 (file)
@@ -13,7 +13,25 @@ clang_flags = [
         '-c',
 ]
 
-clang_arch_flag = '-D__@0@__'.format(host_machine.cpu_family())
+# Generate defines that are appropriate to tell the compiler what architecture
+# we're compiling for. By default we just map meson's cpu_family to __<cpu_family>__.
+# This dictionary contains the exceptions where this doesn't work.
+#
+# C.f. https://mesonbuild.com/Reference-tables.html#cpu-families
+# and src/basic/missing_syscall_def.h.
+cpu_arch_defines = {
+        'ppc'     : ['-D__powerpc__'],
+        'ppc64'   : ['-D__powerpc64__', '-D_CALL_ELF=2'],
+        'riscv32' : ['-D__riscv', '-D__riscv_xlen=32'],
+        'riscv64' : ['-D__riscv', '-D__riscv_xlen=64'],
+        'x86'     : ['-D__i386__'],
+
+        # For arm, assume hardware fp is available.
+        'arm'     : ['-D__arm__', '-D__ARM_PCS_VFP'],
+}
+
+clang_arch_flags = cpu_arch_defines.get(host_machine.cpu_family(),
+                                        ['-D__@0@__'.format(host_machine.cpu_family())])
 
 if meson.version().version_compare('>= 0.58')
         libbpf_include_dir = libbpf.get_variable('includedir')
@@ -24,7 +42,7 @@ endif
 bpf_o_unstripped_cmd = [
         clang,
         clang_flags,
-        clang_arch_flag,
+        clang_arch_flags,
         '-I.'
 ]