Skip to content

Commit 105584a

Browse files
committed
Add ENABLE_EXCEPTION option
This option adds the ability to compile libobjc2 without Objective-C exception support. ENABLE_EXCEPTION is enabled by default.
1 parent 53c293e commit 105584a

File tree

2 files changed

+85
-62
lines changed

2 files changed

+85
-62
lines changed

CMakeLists.txt

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ set(libobjc_HDRS
9494
objc/objc-arc.h
9595
objc/objc-auto.h
9696
objc/objc-class.h
97-
objc/objc-exception.h
9897
objc/objc-runtime.h
9998
objc/objc-visibility.h
10099
objc/objc.h
@@ -107,12 +106,6 @@ set(libobjc_CXX_SRCS
107106
selector_table.cc
108107
)
109108

110-
# Windows does not use DWARF EH, except when using the GNU ABI (MinGW)
111-
if (WIN32 AND NOT MINGW)
112-
list(APPEND libobjc_CXX_SRCS eh_win32_msvc.cc)
113-
elseif (NOT MINGW)
114-
list(APPEND libobjc_C_SRCS eh_personality.c)
115-
endif ()
116109

117110
find_package(tsl-robin-map)
118111

@@ -141,6 +134,7 @@ option(LEGACY_COMPAT "Enable legacy compatibility features" OFF)
141134
option(DEBUG_ARC_COMPAT
142135
"Log warnings for classes that don't hit ARC fast paths" OFF)
143136
option(ENABLE_OBJCXX "Enable support for Objective-C++" ON)
137+
option(ENABLE_EXCEPTIONS "Enable exceptions" ON)
144138
option(TESTS "Enable building the tests")
145139
option(EMBEDDED_BLOCKS_RUNTIME "Include an embedded blocks runtime, rather than relying on libBlocksRuntime to supply it" ON)
146140
option(STRICT_APPLE_COMPATIBILITY "Use strict Apple compatibility, always defining BOOL as signed char" OFF)
@@ -161,6 +155,17 @@ if (EMBEDDED_BLOCKS_RUNTIME)
161155
list(APPEND libobjc_C_SRCS block_to_imp.c)
162156
endif()
163157

158+
if (ENABLE_EXCEPTIONS)
159+
# Windows does not use DWARF EH, except when using the GNU ABI (MinGW)
160+
if (WIN32 AND NOT MINGW)
161+
list(APPEND libobjc_CXX_SRCS eh_win32_msvc.cc)
162+
elseif (NOT MINGW)
163+
list(APPEND libobjc_C_SRCS eh_personality.c)
164+
endif ()
165+
166+
list(APPEND libobjc_HDRS objc/objc-exception.h)
167+
endif()
168+
164169
if (OLDABI_COMPAT)
165170
list(APPEND libobjc_C_SRCS legacy.c abi_version.c statics_loader.c)
166171
add_definitions(-DOLDABI_COMPAT=1)
@@ -220,23 +225,25 @@ endif()
220225

221226

222227

223-
if (WIN32 AND NOT MINGW)
224-
message(STATUS "Using MSVC-compatible exception model")
225-
elseif (MINGW)
226-
message(STATUS "Using MinGW-compatible exception model")
227-
list(APPEND libobjc_CXX_SRCS objcxx_eh.cc objcxx_eh_mingw.cc)
228-
else ()
229-
set(EH_PERSONALITY_FLAGS "")
230-
if (CMAKE_CXX_COMPILER_TARGET)
231-
list(APPEND EH_PERSONALITY_FLAGS "${CMAKE_CXX_COMPILE_OPTIONS_TARGET}${CMAKE_CXX_COMPILER_TARGET}")
228+
if (ENABLE_EXCEPTIONS)
229+
if (WIN32 AND NOT MINGW)
230+
message(STATUS "Using MSVC-compatible exception model")
231+
elseif (MINGW)
232+
message(STATUS "Using MinGW-compatible exception model")
233+
list(APPEND libobjc_CXX_SRCS objcxx_eh.cc objcxx_eh_mingw.cc)
234+
else ()
235+
set(EH_PERSONALITY_FLAGS "")
236+
if (CMAKE_CXX_COMPILER_TARGET)
237+
list(APPEND EH_PERSONALITY_FLAGS "${CMAKE_CXX_COMPILE_OPTIONS_TARGET}${CMAKE_CXX_COMPILER_TARGET}")
238+
endif ()
239+
add_custom_command(OUTPUT eh_trampoline.S
240+
COMMAND ${CMAKE_CXX_COMPILER} ARGS ${EH_PERSONALITY_FLAGS} -fPIC -S "${CMAKE_SOURCE_DIR}/eh_trampoline.cc" -o - -fexceptions -fno-inline | sed "s/__gxx_personality_v0/test_eh_personality/g" > "${CMAKE_BINARY_DIR}/eh_trampoline.S"
241+
MAIN_DEPENDENCY eh_trampoline.cc)
242+
list(APPEND libobjc_ASM_SRCS eh_trampoline.S)
243+
list(APPEND libobjc_CXX_SRCS objcxx_eh.cc)
244+
# Find libm for linking, as some versions of libc++ don't link against it
245+
find_library(M_LIBRARY m)
232246
endif ()
233-
add_custom_command(OUTPUT eh_trampoline.S
234-
COMMAND ${CMAKE_CXX_COMPILER} ARGS ${EH_PERSONALITY_FLAGS} -fPIC -S "${CMAKE_SOURCE_DIR}/eh_trampoline.cc" -o - -fexceptions -fno-inline | sed "s/__gxx_personality_v0/test_eh_personality/g" > "${CMAKE_BINARY_DIR}/eh_trampoline.S"
235-
MAIN_DEPENDENCY eh_trampoline.cc)
236-
list(APPEND libobjc_ASM_SRCS eh_trampoline.S)
237-
list(APPEND libobjc_CXX_SRCS objcxx_eh.cc)
238-
# Find libm for linking, as some versions of libc++ don't link against it
239-
find_library(M_LIBRARY m)
240247
endif ()
241248

242249
if (EMBEDDED_BLOCKS_RUNTIME)
@@ -256,7 +263,9 @@ else ()
256263
endif ()
257264

258265
add_library(objc SHARED ${libobjc_C_SRCS} ${libobjc_ASM_SRCS} ${libobjc_OBJC_SRCS} ${libobjc_OBJCXX_SRCS} ${libobjc_ASM_OBJS})
259-
target_compile_options(objc PRIVATE "$<$<OR:$<COMPILE_LANGUAGE:OBJC>,$<COMPILE_LANGUAGE:OBJCXX>>:-Wno-gnu-folding-constant;-Wno-deprecated-objc-isa-usage;-Wno-objc-root-class;-fobjc-runtime=gnustep-2.0>$<$<COMPILE_LANGUAGE:C>:-Xclang;-fexceptions;-Wno-gnu-folding-constant>")
266+
267+
#target_compile_options(objc PRIVATE "$<$<OR:$<COMPILE_LANGUAGE:OBJC>,$<COMPILE_LANGUAGE:OBJCXX>>:-Wno-gnu-folding-constant;-Wno-deprecated-objc-isa-usage;-Wno-objc-root-class;-fobjc-runtime=gnustep-2.0>$<$<COMPILE_LANGUAGE:C>:-Xclang;-fexceptions;-Wno-gnu-folding-constant>")
268+
target_compile_options(objc PRIVATE "$<$<OR:$<COMPILE_LANGUAGE:OBJC>,$<COMPILE_LANGUAGE:OBJCXX>>:-Wno-gnu-folding-constant;-fno-exceptions;-fno-objc-exceptions;-Wno-deprecated-objc-isa-usage;-Wno-objc-root-class;-fobjc-runtime=gnustep-2.0>$<$<COMPILE_LANGUAGE:C>:-Xclang;-Wno-gnu-folding-constant>")
260269

261270
list(APPEND libobjc_CXX_SRCS ${libobjcxx_CXX_SRCS})
262271
target_sources(objc PRIVATE ${libobjc_CXX_SRCS})
@@ -466,13 +475,15 @@ if (TESTS)
466475
add_subdirectory(Test)
467476
endif (TESTS)
468477

469-
CHECK_CXX_SOURCE_COMPILES("
470-
#include <stdlib.h>
471-
extern \"C\" {
472-
__attribute__((weak))
473-
void *__cxa_allocate_exception(size_t thrown_size) noexcept;
474-
}
475-
#include <exception>
476-
int main() { return 0; }" CXA_ALLOCATE_EXCEPTION_NOEXCEPT_COMPILES)
477-
478-
add_compile_definitions($<IF:$<BOOL:${CXA_ALLOCATE_EXCEPTION_NOEXCEPT_COMPILES}>,CXA_ALLOCATE_EXCEPTION_SPECIFIER=noexcept,CXA_ALLOCATE_EXCEPTION_SPECIFIER>)
478+
if (ENABLE_EXCEPTIONS)
479+
CHECK_CXX_SOURCE_COMPILES("
480+
#include <stdlib.h>
481+
extern \"C\" {
482+
__attribute__((weak))
483+
void *__cxa_allocate_exception(size_t thrown_size) noexcept;
484+
}
485+
#include <exception>
486+
int main() { return 0; }" CXA_ALLOCATE_EXCEPTION_NOEXCEPT_COMPILES)
487+
488+
add_compile_definitions($<IF:$<BOOL:${CXA_ALLOCATE_EXCEPTION_NOEXCEPT_COMPILES}>,CXA_ALLOCATE_EXCEPTION_SPECIFIER=noexcept,CXA_ALLOCATE_EXCEPTION_SPECIFIER>)
489+
endif()

Test/CMakeLists.txt

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Clear the LD_LIBRARY_PATH if GNUstep set it so that we don't accidentally use
32
# the installed version
43

@@ -19,13 +18,11 @@ set(TESTS
1918
BlockTest_arc.m
2019
ConstantString.m
2120
Category.m
22-
ExceptionTest.m
2321
FastARC.m
2422
FastARCPool.m
2523
FastRefCount.m
2624
Forward.m
2725
ManyManySelectors.m
28-
NestedExceptions.m
2926
PropertyAttributeTest.m
3027
ProtocolExtendedProperties.m
3128
PropertyIntrospectionTest.m
@@ -43,16 +40,36 @@ set(TESTS
4340
IVarSuperclassOverlap.m
4441
objc_msgSend.m
4542
msgInterpose.m
46-
NilException.m
4743
MethodArguments.m
4844
zeroSizedIVar.m
4945
exchange.m
5046
hash_table_delete.c
5147
hash_test.c
5248
setSuperclass.m
53-
UnexpectedException.m
5449
)
5550

51+
if (ENABLE_EXCEPTIONS)
52+
set(ADDITIONAL_CFLAGS "-fobjc-exceptions")
53+
54+
list(APPEND TESTS
55+
ExceptionTest.m
56+
NestedExceptions.m
57+
NilException.m
58+
UnexpectedException.m
59+
)
60+
61+
# Don't run the tests that are specific to Itanium-style exceptions on
62+
# Windows.
63+
if (NOT WIN32)
64+
list(APPEND TESTS
65+
BoxedForeignException.m
66+
ForeignException.m
67+
)
68+
endif()
69+
else()
70+
set(ADDITIONAL_CFLAGS "-fno-objc-exceptions")
71+
endif()
72+
5673
if (EMBEDDED_BLOCKS_RUNTIME)
5774
list(APPEND TESTS BlockImpTest.m)
5875
endif()
@@ -73,13 +90,6 @@ if (WIN32)
7390
objc_msgSend_WoA64.mm
7491
)
7592
endif()
76-
else ()
77-
# Don't run the tests that are specific to Itanium-style exceptions on
78-
# Windows.
79-
list(APPEND TESTS
80-
BoxedForeignException.m
81-
ForeignException.m
82-
)
8393
endif ()
8494

8595
if (ENABLE_ALL_OBJC_ARC_TESTS)
@@ -102,14 +112,14 @@ remove_definitions(-D__OBJC_RUNTIME_INTERNAL__=1)
102112
add_library(test_runtime_legacy OBJECT Test.m)
103113
set_target_properties(test_runtime_legacy PROPERTIES
104114
INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR};${PROJECT_BINARY_DIR}/objc/"
105-
COMPILE_FLAGS "-Xclang -fblocks -fobjc-runtime=gnustep-1.7"
115+
COMPILE_FLAGS "-Xclang -fblocks -fobjc-runtime=gnustep-1.7 ${ADDITIONAL_CFLAGS}"
106116
LINKER_LANGUAGE C
107117
)
108118

109119
add_library(test_runtime OBJECT Test.m)
110120
set_target_properties(test_runtime PROPERTIES
111121
INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR};${PROJECT_BINARY_DIR}/objc/"
112-
COMPILE_FLAGS "-Xclang -fblocks -fobjc-runtime=gnustep-2.0"
122+
COMPILE_FLAGS "-Xclang -fblocks -fobjc-runtime=gnustep-2.0 ${ADDITIONAL_CFLAGS}"
113123
LINKER_LANGUAGE C
114124
)
115125

@@ -142,16 +152,16 @@ function(addtest_flags TEST_NAME FLAGS TEST_SOURCE)
142152
endfunction(addtest_flags)
143153

144154
function(addtest_variants TEST TEST_SOURCE LEGACY)
145-
addtest_flags(${TEST} "-O0 -fobjc-runtime=gnustep-2.2 -UNDEBUG -DGS_RUNTIME_V2" "${TEST_SOURCE}")
155+
addtest_flags(${TEST} "-O0 -fobjc-runtime=gnustep-2.2 ${ADDITIONAL_CFLAGS} -UNDEBUG -DGS_RUNTIME_V2" "${TEST_SOURCE}")
146156
target_sources(${TEST} PRIVATE $<TARGET_OBJECTS:test_runtime>)
147-
addtest_flags("${TEST}_optimised" "-O3 -fobjc-runtime=gnustep-2.2 -UNDEBUG -DGS_RUNTIME_V2" "${TEST_SOURCE}")
157+
addtest_flags("${TEST}_optimised" "-O3 -fobjc-runtime=gnustep-2.2 ${ADDITIONAL_CFLAGS} -UNDEBUG -DGS_RUNTIME_V2" "${TEST_SOURCE}")
148158
target_sources("${TEST}_optimised" PRIVATE $<TARGET_OBJECTS:test_runtime>)
149159

150160
# -fobjc-arc is not supported on platforms using the legacy runtime
151161
if (${LEGACY} AND ${OLDABI_COMPAT} AND NOT ${TEST} MATCHES ".*_arc")
152-
addtest_flags("${TEST}_legacy" "-O0 -fobjc-runtime=gnustep-1.7 -UNDEBUG" "${TEST_SOURCE}")
162+
addtest_flags("${TEST}_legacy" "-O0 -fobjc-runtime=gnustep-1.7 ${ADDITIONAL_CFLAGS} -UNDEBUG" "${TEST_SOURCE}")
153163
target_sources("${TEST}_legacy" PRIVATE $<TARGET_OBJECTS:test_runtime_legacy>)
154-
addtest_flags("${TEST}_legacy_optimised" "-O3 -fobjc-runtime=gnustep-1.7 -UNDEBUG" "${TEST_SOURCE}")
164+
addtest_flags("${TEST}_legacy_optimised" "-O3 -fobjc-runtime=gnustep-1.7 ${ADDITIONAL_CFLAGS} -UNDEBUG" "${TEST_SOURCE}")
155165
target_sources("${TEST}_legacy_optimised" PRIVATE $<TARGET_OBJECTS:test_runtime_legacy>)
156166
endif()
157167
endfunction(addtest_variants)
@@ -166,18 +176,20 @@ foreach(TEST_SOURCE ${NEW_TESTS})
166176
addtest_variants(${TEST} ${TEST_SOURCE} false)
167177
endforeach()
168178

169-
# Tests that are more than a single file.
170-
addtest_variants("CXXExceptions" "CXXException.m;CXXException.cc" true)
171-
addtest_variants("ForwardDeclareProtocolAccess" "ForwardDeclareProtocolAccess.m;ForwardDeclareProtocol.m" true)
172-
if (ENABLE_OBJCXX)
173-
addtest_variants(ObjCXXEHInterop "ObjCXXEHInterop.mm;ObjCXXEHInterop.m" true)
174-
addtest_variants(ObjCXXEHInteropTwice "ObjCXXEHInteropTwice.mm" true)
175-
if (WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL Clang)
176-
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16.0.0)
179+
if (ENABLE_EXCEPTIONS)
180+
# Tests that are more than a single file.
181+
addtest_variants("CXXExceptions" "CXXException.m;CXXException.cc" true)
182+
addtest_variants("ForwardDeclareProtocolAccess" "ForwardDeclareProtocolAccess.m;ForwardDeclareProtocol.m" true)
183+
if (ENABLE_OBJCXX)
184+
addtest_variants(ObjCXXEHInterop "ObjCXXEHInterop.mm;ObjCXXEHInterop.m" true)
185+
addtest_variants(ObjCXXEHInteropTwice "ObjCXXEHInteropTwice.mm" true)
186+
if (WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL Clang)
187+
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16.0.0)
188+
addtest_variants(ObjCXXEHInterop_arc "ObjCXXEHInterop_arc.mm;ObjCXXEHInterop_arc.m" true)
189+
endif()
190+
else()
177191
addtest_variants(ObjCXXEHInterop_arc "ObjCXXEHInterop_arc.mm;ObjCXXEHInterop_arc.m" true)
178192
endif()
179-
else()
180-
addtest_variants(ObjCXXEHInterop_arc "ObjCXXEHInterop_arc.mm;ObjCXXEHInterop_arc.m" true)
181193
endif()
182194
endif()
183195

0 commit comments

Comments
 (0)