Skip to content

Commit e142629

Browse files
committed
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.)
1 parent 0c1a893 commit e142629

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CMakeLists.txt

+10-2
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,23 @@ option(DEBUG_ARC_COMPAT
141141
option(ENABLE_OBJCXX "Enable support for Objective-C++" ON)
142142
option(TESTS "Enable building the tests")
143143
option(EMBEDDED_BLOCKS_RUNTIME "Include an embedded blocks runtime, rather than relying on libBlocksRuntime to supply it" ON)
144-
option(STRICT_APPLE_COMPATIBILITY "Use strict Apple compatibility, always defining BOOL as signed char" OFF)
144+
option(STRICT_APPLE_COMPATIBILITY "Use strict Apple compatibility, defining non-C99-bool BOOL as signed char" OFF)
145+
set(OBJC_BOOL_IS_BOOL_MODE auto CACHE STRING "Control whether BOOL should be defined as C99 bool. One of 'auto', 'always', 'never'.")
146+
147+
if ("${OBJC_BOOL_IS_BOOL_MODE}" STREQUAL "always")
148+
set(OBJC_BOOL_IS_BOOL_MODE_CONFIG OBJC_BOOL_IS_BOOL_MODE_ALWAYS)
149+
elseif ("${OBJC_BOOL_IS_BOOL_MODE}" STREQUAL "never")
150+
set(OBJC_BOOL_IS_BOOL_MODE_CONFIG OBJC_BOOL_IS_BOOL_MODE_NEVER)
151+
else()
152+
set(OBJC_BOOL_IS_BOOL_MODE_CONFIG OBJC_BOOL_IS_BOOL_MODE_AUTO)
153+
endif()
145154

146155
# For release builds, we disable spamming the terminal with warnings about
147156
# selector type mismatches
148157
add_compile_definitions($<$<CONFIG:Release>:NO_SELECTOR_MISMATCH_WARNINGS>)
149158
add_compile_definitions($<$<BOOL:${TYPE_DEPENDENT_DISPATCH}>:TYPE_DEPENDENT_DISPATCH>)
150159
add_compile_definitions($<$<BOOL:${ENABLE_TRACING}>:WITH_TRACING=1>)
151160
add_compile_definitions($<$<BOOL:${DEBUG_ARC_COMPAT}>:DEBUG_ARC_COMPAT>)
152-
add_compile_definitions($<$<BOOL:${STRICT_APPLE_COMPATIBILITY}>:STRICT_APPLE_COMPATIBILITY>)
153161

154162
configure_file(objc/objc-config.h.in objc/objc-config.h @ONLY)
155163
include_directories("${PROJECT_BINARY_DIR}/objc/")

objc/objc-config.h.in

+17
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
11
#cmakedefine STRICT_APPLE_COMPATIBILITY @STRICT_APPLE_COMPATIBILITY@
2+
3+
#define OBJC_BOOL_IS_BOOL_MODE_AUTO 1
4+
#define OBJC_BOOL_IS_BOOL_MODE_ALWAYS 2
5+
#define OBJC_BOOL_IS_BOOL_MODE_NEVER 3
6+
#cmakedefine OBJC_BOOL_IS_BOOL_MODE @OBJC_BOOL_IS_BOOL_MODE_CONFIG@
7+
8+
#define OBJC_BOOL_TYPE_STDBOOL 1
9+
#define OBJC_BOOL_TYPE_TRADITIONAL 2
10+
#define OBJC_BOOL_TYPE_APPLE 3
11+
12+
#if OBJC_BOOL_IS_BOOL_MODE == OBJC_BOOL_IS_BOOL_MODE_ALWAYS || (defined(__OBJC_BOOL_IS_BOOL) && OBJC_BOOL_IS_BOOL_MODE == OBJC_BOOL_IS_BOOL_MODE_AUTO)
13+
#define OBJC_BOOL_TYPE OBJC_BOOL_TYPE_STDBOOL
14+
#elif STRICT_APPLE_COMPATIBILITY
15+
#define OBJC_BOOL_TYPE OBJC_BOOL_TYPE_APPLE
16+
#else
17+
#define OBJC_BOOL_TYPE OBJC_BOOL_TYPE_TRADITIONAL
18+
#endif

objc/runtime.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ extern "C" {
4444
#include <sys/types.h>
4545
#include "Availability.h"
4646

47+
#if OBJC_BOOL_TYPE == OBJC_BOOL_TYPE_STDBOOL
48+
#include <stdbool.h>
49+
#endif
50+
4751
// Undo GNUstep substitutions
4852
#ifdef class_setVersion
4953
# undef class_setVersion
@@ -129,7 +133,9 @@ typedef struct objc_method *Method;
129133
/**
130134
* Objective-C boolean type.
131135
*/
132-
# ifdef STRICT_APPLE_COMPATIBILITY
136+
# if OBJC_BOOL_TYPE == OBJC_BOOL_TYPE_STDBOOL
137+
typedef bool BOOL;
138+
# elif OBJC_BOOL_TYPE == OBJC_BOOL_TYPE_APPLE
133139
typedef signed char BOOL;
134140
# else
135141
# if defined(__vxworks) || defined(_WIN32)

0 commit comments

Comments
 (0)