From 63af72c9e91e40e773903a119d91a3742b38cf68 Mon Sep 17 00:00:00 2001 From: Katalin Rebhan Date: Wed, 18 Dec 2024 14:26:37 +0100 Subject: [PATCH 1/2] Remove leftover define moved to config header --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 887f0a9e..1d2a7212 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,7 +151,6 @@ add_compile_definitions($<$:NO_SELECTOR_MISMATCH_WARNINGS>) add_compile_definitions($<$:TYPE_DEPENDENT_DISPATCH>) add_compile_definitions($<$:WITH_TRACING=1>) add_compile_definitions($<$:DEBUG_ARC_COMPAT>) -add_compile_definitions($<$:STRICT_APPLE_COMPATIBILITY>) configure_file(objc/objc-config.h.in objc/objc-config.h @ONLY) include_directories("${PROJECT_BINARY_DIR}/objc/") From 10cdcd42b4f3ca28a4b2f6813780f571a2561a7a Mon Sep 17 00:00:00 2001 From: Katalin Rebhan Date: Wed, 18 Dec 2024 14:26:37 +0100 Subject: [PATCH 2/2] Support defining C99 bool as the BOOL type Whether to use C99 bool can be set at build time using -DOBJC_BOOL_IS_BOOL_MODE=auto/always/never. Default is to use C99 bool if __OBJC_BOOL_IS_BOOL is defined and set to 1, which is currently the case for Clang on newer Apple platforms. This matches the behavior of Apple's libobjc (especially when combined with STRICT_APPLE_COMPATIBILITY). (See https://www.jviotti.com/2024/01/05/is-objective-c-bool-a-boolean-type-it-depends.html for more information about the __OBJC_BOOL_IS_BOOL definition.) --- CMakeLists.txt | 11 ++++++++++- objc/objc-config.h.in | 17 +++++++++++++++++ objc/runtime.h | 8 +++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d2a7212..76cce2a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,7 +143,16 @@ option(DEBUG_ARC_COMPAT option(ENABLE_OBJCXX "Enable support for Objective-C++" ON) option(TESTS "Enable building the tests") option(EMBEDDED_BLOCKS_RUNTIME "Include an embedded blocks runtime, rather than relying on libBlocksRuntime to supply it" ON) -option(STRICT_APPLE_COMPATIBILITY "Use strict Apple compatibility, always defining BOOL as signed char" OFF) +option(STRICT_APPLE_COMPATIBILITY "Use strict Apple compatibility, defining non-C99-bool BOOL as signed char" OFF) +set(OBJC_BOOL_IS_BOOL_MODE auto CACHE STRING "Control whether BOOL should be defined as C99 bool. One of 'auto', 'always', 'never'.") + +if ("${OBJC_BOOL_IS_BOOL_MODE}" STREQUAL "always") + set(OBJC_BOOL_IS_BOOL_MODE_CONFIG OBJC_BOOL_IS_BOOL_MODE_ALWAYS) +elseif ("${OBJC_BOOL_IS_BOOL_MODE}" STREQUAL "never") + set(OBJC_BOOL_IS_BOOL_MODE_CONFIG OBJC_BOOL_IS_BOOL_MODE_NEVER) +else() + set(OBJC_BOOL_IS_BOOL_MODE_CONFIG OBJC_BOOL_IS_BOOL_MODE_AUTO) +endif() # For release builds, we disable spamming the terminal with warnings about # selector type mismatches diff --git a/objc/objc-config.h.in b/objc/objc-config.h.in index 907563c4..c6163fa5 100644 --- a/objc/objc-config.h.in +++ b/objc/objc-config.h.in @@ -1 +1,18 @@ #cmakedefine STRICT_APPLE_COMPATIBILITY @STRICT_APPLE_COMPATIBILITY@ + +#define OBJC_BOOL_IS_BOOL_MODE_AUTO 1 +#define OBJC_BOOL_IS_BOOL_MODE_ALWAYS 2 +#define OBJC_BOOL_IS_BOOL_MODE_NEVER 3 +#cmakedefine OBJC_BOOL_IS_BOOL_MODE @OBJC_BOOL_IS_BOOL_MODE_CONFIG@ + +#define OBJC_BOOL_TYPE_STDBOOL 1 +#define OBJC_BOOL_TYPE_TRADITIONAL 2 +#define OBJC_BOOL_TYPE_APPLE 3 + +#if OBJC_BOOL_IS_BOOL_MODE == OBJC_BOOL_IS_BOOL_MODE_ALWAYS || (OBJC_BOOL_IS_BOOL_MODE == OBJC_BOOL_IS_BOOL_MODE_AUTO && defined(__OBJC_BOOL_IS_BOOL) && __OBJC_BOOL_IS_BOOL) +#define OBJC_BOOL_TYPE OBJC_BOOL_TYPE_STDBOOL +#elif STRICT_APPLE_COMPATIBILITY +#define OBJC_BOOL_TYPE OBJC_BOOL_TYPE_APPLE +#else +#define OBJC_BOOL_TYPE OBJC_BOOL_TYPE_TRADITIONAL +#endif diff --git a/objc/runtime.h b/objc/runtime.h index c9a0ba63..ba4ac6ed 100644 --- a/objc/runtime.h +++ b/objc/runtime.h @@ -44,6 +44,10 @@ extern "C" { #include #include "Availability.h" +#if OBJC_BOOL_TYPE == OBJC_BOOL_TYPE_STDBOOL +#include +#endif + // Undo GNUstep substitutions #ifdef class_setVersion # undef class_setVersion @@ -129,7 +133,9 @@ typedef struct objc_method *Method; /** * Objective-C boolean type. */ -# ifdef STRICT_APPLE_COMPATIBILITY +# if OBJC_BOOL_TYPE == OBJC_BOOL_TYPE_STDBOOL +typedef bool BOOL; +# elif OBJC_BOOL_TYPE == OBJC_BOOL_TYPE_APPLE typedef signed char BOOL; # else # if defined(__vxworks) || defined(_WIN32)