Skip to content

Commit

Permalink
Support defining C99 bool as the BOOL type
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
2xsaiko committed Dec 21, 2024
1 parent 38933c1 commit 7a13e68
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,23 @@ 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
add_compile_definitions($<$<CONFIG:Release>:NO_SELECTOR_MISMATCH_WARNINGS>)
add_compile_definitions($<$<BOOL:${TYPE_DEPENDENT_DISPATCH}>:TYPE_DEPENDENT_DISPATCH>)
add_compile_definitions($<$<BOOL:${ENABLE_TRACING}>:WITH_TRACING=1>)
add_compile_definitions($<$<BOOL:${DEBUG_ARC_COMPAT}>:DEBUG_ARC_COMPAT>)
add_compile_definitions($<$<BOOL:${STRICT_APPLE_COMPATIBILITY}>:STRICT_APPLE_COMPATIBILITY>)

configure_file(objc/objc-config.h.in objc/objc-config.h @ONLY)
include_directories("${PROJECT_BINARY_DIR}/objc/")
Expand Down
17 changes: 17 additions & 0 deletions objc/objc-config.h.in
Original file line number Diff line number Diff line change
@@ -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 || (defined(__OBJC_BOOL_IS_BOOL) && OBJC_BOOL_IS_BOOL_MODE == OBJC_BOOL_IS_BOOL_MODE_AUTO)
#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
8 changes: 7 additions & 1 deletion objc/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ extern "C" {
#include <sys/types.h>
#include "Availability.h"

#if OBJC_BOOL_TYPE == OBJC_BOOL_TYPE_STDBOOL
#include <stdbool.h>
#endif

// Undo GNUstep substitutions
#ifdef class_setVersion
# undef class_setVersion
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7a13e68

Please sign in to comment.