Merge branch 'compiler-features' into 'master'

Rework compiler features detection

See merge request cairo/cairo!627
This commit is contained in:
Tim-Philipp Müller 2025-07-01 12:32:05 +00:00
commit 0bf0265009
8 changed files with 60 additions and 28 deletions

View file

@ -73,7 +73,7 @@
#define CAIRO_BOILERPLATE_DEBUG(x)
#endif
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
#if defined (__GNUC__)
#ifdef __MINGW32__
#define CAIRO_BOILERPLATE_PRINTF_FORMAT(fmt_index, va_index) \
__attribute__((__format__(__MINGW_PRINTF_FORMAT, fmt_index, va_index)))

View file

@ -76,7 +76,7 @@ if cc.get_id() != 'msvc'
endif
supported_cflags = cc.get_supported_arguments(cflags)
add_project_arguments(supported_cflags, language: 'c')
add_project_arguments(supported_cflags, language: ['c', 'cpp'])
# We only wish to enable attribute(warn_unused_result) if we can prevent
# gcc from generating thousands of warnings about the misapplication of the
@ -95,10 +95,13 @@ if cc.get_id() == 'msvc'
add_project_arguments('/wd4244', '/wd4146',
# Don't warn about double -> float truncation
'/wd4305',
language : 'c')
# Don't warn about _cairo_status -> _cairo_int_status conversion
'/wd5286',
language : ['c', 'cpp'])
add_project_arguments('-D_CRT_SECURE_NO_WARNINGS', language : ['c', 'cpp'])
endif
add_project_arguments('-D_GNU_SOURCE', language: 'c')
add_project_arguments('-D_GNU_SOURCE', language: ['c', 'cpp'])
pkgmod = import('pkgconfig')
python3 = import('python').find_installation()
@ -733,7 +736,7 @@ endforeach
extra_link_args += pthread_link_args
# Atomics are an optional feature in C11. Also need to check that C11 atomics are lock free.
# Windows can't use C11 atomics as some files are compiled with C++.
# On Windows we use the Interlocked family of functions
if host_machine.system() != 'windows'
if cc.links(files('meson-cc-tests/atomic-ops-c11.c'), name: 'Atomic ops: c11')
conf.set('HAVE_C11_ATOMIC_PRIMITIVES', 1)

View file

@ -484,6 +484,36 @@ _cairo_atomic_ptr_cmpxchg_return_old_fallback(cairo_atomic_intptr_t *x, void *ol
(void) ret__; \
} while (0)
#if defined (_WIN32)
typedef INIT_ONCE cairo_atomic_once_t;
#define CAIRO_ATOMIC_ONCE_INIT INIT_ONCE_STATIC_INIT
static cairo_always_inline cairo_bool_t
_cairo_atomic_init_once_enter(cairo_atomic_once_t *once)
{
BOOL pending;
if (unlikely (!InitOnceBeginInitialize (once, 0, &pending, NULL))) {
assert (0 && "InitOnceBeginInitialize failed");
}
if (likely (!pending))
return 0;
return 1;
}
static cairo_always_inline void
_cairo_atomic_init_once_leave(cairo_atomic_once_t *once)
{
if (unlikely (InitOnceComplete (once, 0, NULL))) {
assert (0 && "InitOnceComplete failed");
}
}
#else
typedef cairo_atomic_int_t cairo_atomic_once_t;
#define CAIRO_ATOMIC_ONCE_UNINITIALIZED (0)
@ -515,6 +545,8 @@ _cairo_atomic_init_once_leave(cairo_atomic_once_t *once)
assert (0 && "incorrect use of _cairo_atomic_init_once API (once != CAIRO_ATOMIC_ONCE_INITIALIZING)");
}
#endif /* !_WIN32 */
CAIRO_END_DECLS
#endif

View file

@ -54,7 +54,7 @@
#define CAIRO_STACK_ARRAY_LENGTH(T) (CAIRO_STACK_BUFFER_SIZE / sizeof(T))
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
#if defined (__GNUC__)
#ifdef __MINGW32__
#define CAIRO_PRINTF_FORMAT(fmt_index, va_index) \
__attribute__((__format__(__MINGW_PRINTF_FORMAT, fmt_index, va_index)))
@ -67,8 +67,8 @@
#endif
#define CAIRO_HAS_HIDDEN_SYMBOLS 1
#if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && \
(defined(__ELF__) || defined(__APPLE__)) && \
#if defined(__GNUC__) && \
(defined(__ELF__) || defined(__APPLE__)) && \
!defined(__sun)
#define cairo_private_no_warn __attribute__((__visibility__("hidden")))
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
@ -97,7 +97,7 @@
indicating that the old function has been deprecated by the new
function.
*/
#if __GNUC__ >= 2 && defined(__ELF__)
#if defined (__GNUC__) && defined(__ELF__)
# define CAIRO_FUNCTION_ALIAS(old, new) \
extern __typeof (new) old \
__asm__ ("" #old) \
@ -126,17 +126,21 @@
* constant-folding, with 'cairo_const 'also guaranteeing that pointer contents
* do not change across the function call.
*/
#if __GNUC__ >= 3
#if defined (__GNUC__)
#define cairo_pure __attribute__((pure))
#define cairo_const __attribute__((const))
#define cairo_always_inline inline __attribute__((always_inline))
#elif defined (_MSC_VER)
#define cairo_pure
#define cairo_const
#define cairo_always_inline __forceinline
#else
#define cairo_pure
#define cairo_const
#define cairo_always_inline inline
#endif
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
#if defined(__GNUC__) && defined(__OPTIMIZE__)
#define likely(expr) (__builtin_expect (!!(expr), 1))
#define unlikely(expr) (__builtin_expect (!!(expr), 0))
#else
@ -144,7 +148,7 @@
#define unlikely(expr) (expr)
#endif
#ifndef __GNUC__
#if !defined(__GNUC__)
#undef __attribute__
#define __attribute__(x)
#endif
@ -161,18 +165,11 @@
#define strdup _strdup
#define unlink _unlink
#if _MSC_VER < 1900
#define vsnprintf _vsnprintf
#define vsnprintf _vsnprintf // TODO
#define snprintf _snprintf
#endif
#endif
#ifdef _MSC_VER
#ifndef __cplusplus
#undef inline
#define inline __inline
#endif
#endif
#if defined(_MSC_VER) && defined(_M_IX86)
/* When compiling with /Gy and /OPT:ICF identical functions will be folded in together.
The CAIRO_ENSURE_UNIQUE macro ensures that a function is always unique and

View file

@ -53,7 +53,7 @@
#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(CAIRO_WIN32_STATIC_BUILD)
# define _cairo_export __declspec(dllexport)
# define _cairo_import __declspec(dllimport)
#elif defined(__GNUC__) && (__GNUC__ >= 4)
#elif defined(__GNUC__)
# define _cairo_export __attribute__((__visibility__("default")))
# define _cairo_import
#else

View file

@ -175,7 +175,7 @@ do { \
static inline int cairo_const
_cairo_popcount (uint32_t mask)
{
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
#if defined (__GNUC__)
return __builtin_popcount (mask);
#else
register int y;

View file

@ -98,7 +98,7 @@
#endif
#if __GNUC__ >= 3
#if defined (__GNUC__)
#define csi_pure __attribute__((pure))
#define csi_const __attribute__((const))
#else
@ -106,7 +106,7 @@
#define csi_const
#endif
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
#if defined(__GNUC__) && defined(__OPTIMIZE__)
#define _CSI_BOOLEAN_EXPR(expr) \
__extension__ ({ \
int _csi_boolean_var_; \
@ -133,7 +133,7 @@
(type *)((char *) (ptr) - (char *) &((type *)0)->member)
#endif
#if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(__ELF__) && !defined(__sun)
#if defined(__GNUC__) && defined(__ELF__) && !defined(__sun)
#define csi_private_no_warn __attribute__((__visibility__("hidden")))
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
#define csi_private_no_warn __hidden

View file

@ -100,7 +100,7 @@
*/
#define CAIRO_BITSWAP8(c) ((((c) * 0x0802LU & 0x22110LU) | ((c) * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16)
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
#if defined(__GNUC__)
#ifdef __MINGW32__
#define CAIRO_PRINTF_FORMAT(fmt_index, va_index) \
__attribute__((__format__(__MINGW_PRINTF_FORMAT, fmt_index, va_index)))
@ -143,7 +143,7 @@ static void *_dlhandle = RTLD_NEXT;
#endif
#define BUCKET(b, ptr) (((uintptr_t) (ptr) >> PTR_SHIFT) % ARRAY_LENGTH (b))
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
#if defined(__GNUC__) && defined(__OPTIMIZE__)
#define _BOOLEAN_EXPR(expr) \
__extension__ ({ \
int _boolean_var_; \
@ -220,7 +220,7 @@ static void _init_trace (void);
#define INIT_TRACE_ONCE() pthread_once (&once_control, _init_trace)
#if __GNUC__ >= 3 && defined(__ELF__) && !defined(__sun)
#if defined(__GNUC__) && defined(__ELF__) && !defined(__sun)
# define _enter_trace() INIT_TRACE_ONCE ()
# define _exit_trace() do { } while (0)
# define _should_trace() 1