]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
meson: detect availability of attributes using cc.has_function_attribute()
authorMike Yuan <me@yhndnzj.com>
Wed, 25 Mar 2026 17:13:11 +0000 (18:13 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 25 Mar 2026 20:54:21 +0000 (21:54 +0100)
Alternative to fabc22f5998e610eb7ba70a963cab9f94dca5c0a

As suggested in https://github.com/systemd/systemd/pull/41174#discussion_r2966411375

meson.build
src/boot/meson.build
src/fundamental/macro-fundamental.h

index c52f8e17c2bbe6e5e4a96cf2a5093865a5ad3d4c..925b1b13ed0e0422bc558468700a13bdbae70d47 100644 (file)
@@ -524,12 +524,6 @@ if cc.compiles('''
         add_project_arguments('-Werror=shadow', language : 'c')
 endif
 
-have = cc.compiles(
-        '__attribute__((__retain__)) int x;',
-        args : '-Werror=attributes',
-        name : '__attribute__((__retain__))')
-conf.set10('HAVE_ATTRIBUTE_RETAIN', have)
-
 if cxx_cmd != ''
         add_project_arguments(cxx.get_supported_arguments(basic_disabled_warnings), language : 'cpp')
 endif
@@ -544,6 +538,17 @@ conf.set10('HAVE_WARNING_ZERO_LENGTH_BOUNDS', have)
 have = cc.has_argument('-Wzero-as-null-pointer-constant')
 conf.set10('HAVE_WARNING_ZERO_AS_NULL_POINTER_CONSTANT', have)
 
+possible_c_attributes = [
+        'alloc_size',
+        'fallthrough',
+        'retain',
+]
+
+foreach attr : possible_c_attributes
+        have = cc.has_function_attribute(attr)
+        conf.set10('HAVE_ATTRIBUTE_' + attr.to_upper(), have)
+endforeach
+
 #####################################################################
 # compilation result tests
 
index 058d9276bd1fea7456914b0954283f44ff94f3fe..dfac98f034a6d072bbbe942a3ccfcf9215590a14 100644 (file)
@@ -80,10 +80,13 @@ endif
 efi_conf = configuration_data()
 
 # import several configs from userspace
-foreach name : ['HAVE_ATTRIBUTE_RETAIN',
-                'HAVE_WARNING_ZERO_AS_NULL_POINTER_CONSTANT',
-                'HAVE_WARNING_ZERO_LENGTH_BOUNDS',
-               ]
+foreach name : ['HAVE_WARNING_ZERO_LENGTH_BOUNDS',
+                'HAVE_WARNING_ZERO_AS_NULL_POINTER_CONSTANT']
+        efi_conf.set(name, conf.get(name))
+endforeach
+
+foreach attr : possible_c_attributes
+        name = 'HAVE_ATTRIBUTE_' + attr.to_upper()
         efi_conf.set(name, conf.get(name))
 endforeach
 
index 1941e88d3760e5782efecffcbcf45827f03f9336..d99a00c9bf936161293b9ec0b3146c2f35523564 100644 (file)
 #define _weak_ __attribute__((__weak__))
 #define _weakref_(x) __attribute__((__weakref__(#x)))
 
-#if HAVE_ATTRIBUTE_RETAIN
-#  define _retain_ __attribute__((__retain__))
+#if HAVE_ATTRIBUTE_ALLOC_SIZE
+#  define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__)))
 #else
-#  define _retain_
+#  define _alloc_(...)
 #endif
 
-#ifdef __clang__
-#  define _alloc_(...)
+#if HAVE_ATTRIBUTE_FALLTHROUGH
+#  define _fallthrough_ __attribute__((__fallthrough__))
 #else
-#  define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__)))
+#  define _fallthrough_
 #endif
 
-#if defined(__clang__) && __clang_major__ < 10
-#  define _fallthrough_
+#if HAVE_ATTRIBUTE_RETAIN
+#  define _retain_ __attribute__((__retain__))
 #else
-#  define _fallthrough_ __attribute__((__fallthrough__))
+#  define _retain_
 #endif
 
 #if __GNUC__ >= 15