@@ -506,8 +506,8 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
506506 endif ()
507507
508508
509- function (compile_object name ${ARGN} )
510- add_library (${name} OBJECT ${ARGN} )
509+ function (compile name TYPE ${ARGN} )
510+ add_library (${name} ${TYPE} ${ARGN} )
511511 target_link_libraries (${name} snmalloc)
512512 set_target_properties (${name} PROPERTIES CXX_VISIBILITY_PRESET hidden INTERPROCEDURAL_OPTIMIZATION ${SNMALLOC_COMPILER_SUPPORT_IPO} )
513513 target_compile_definitions (${name} PRIVATE "SNMALLOC_USE_${SNMALLOC_CLEANUP} " )
@@ -553,12 +553,14 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
553553 install (TARGETS ${name} EXPORT snmallocConfig)
554554 endfunction ()
555555
556- function (compile_object_noexc name ${ARGN} )
557- compile_object (${name} ${ARGN} )
556+ function (compile_noexc name TYPE ${ARGN} )
557+ compile (${name} ${TYPE } ${ARGN} )
558558
559+ target_compile_definitionS (${name} PRIVATE -DSNMALLOC_NOEXC)
559560 if (MSVC )
560561 target_compile_definitions (${name} PRIVATE -D_HAS_EXCEPTIONS=0)
561562 else ()
563+ target_compile_options (${name} PRIVATE -fno-exceptions)
562564 # Ensure that we do not link against C++ stdlib when compiling shims.
563565 # If the compiler supports excluding the C++ stdlib implementation, use
564566 # it. Otherwise, fall back to linking the library as if it were C, which
@@ -573,53 +575,51 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
573575 endif ()
574576 endfunction ()
575577
576- function (add_shim name type )
577- add_library (${name} ${type} )
578- target_link_libraries (${name} ${ARGN} )
579- install (TARGETS ${name} EXPORT snmallocConfig)
580- endfunction ()
578+ # Various files for overriding libc/rust behaviours.
579+ set (MALLOC src/snmalloc/override/malloc.cc)
580+ set (NEW src/snmalloc/override/new.cc)
581+ set (MEMCPY src/snmalloc/override/memcpy.cc)
582+ set (RUST src/snmalloc/override/rust.cc)
583+
584+ set (ALLOC ${MALLOC} ${NEW} )
585+ set (ALL ${ALLOC} ${MEMCPY} )
586+
587+ # Exception handler code needs to be compiled with exceptions enabled.
588+ compile(snmalloc_exception_handler OBJECT src/snmalloc/override/failure.cc)
581589
582- compile_object(snmalloc_exception_handler src/snmalloc/override/failure.cc)
583- compile_object_noexc(snmalloc_core src/snmalloc/override/malloc.cc)
584- compile_object_noexc(snmalloc_new src/snmalloc/override/new.cc)
585- compile_object_noexc(snmalloc_memcpy src/snmalloc/override/memcpy.cc)
586- compile_object_noexc(snmalloc_rust src/snmalloc/override/rust.cc)
587-
588- compile_object(snmalloc_exception_handler_checks src/snmalloc/override/failure.cc)
589- target_compile_definitions (snmalloc_exception_handler_checks PRIVATE SNMALLOC_CHECK_CLIENT)
590- compile_object_noexc(snmalloc_core_checks src/snmalloc/override/malloc.cc)
591- target_compile_definitions (snmalloc_core_checks PRIVATE SNMALLOC_CHECK_CLIENT)
592- compile_object_noexc(snmalloc_new_checks src/snmalloc/override/new.cc)
593- target_compile_definitions (snmalloc_new_checks PRIVATE SNMALLOC_CHECK_CLIENT)
594- compile_object_noexc(snmalloc_memcpy_checks src/snmalloc/override/memcpy.cc)
595- target_compile_definitions (snmalloc_memcpy_checks PRIVATE SNMALLOC_CHECK_CLIENT)
596- compile_object_noexc(snmalloc_rust_checks src/snmalloc/override/rust.cc)
597- target_compile_definitions (snmalloc_rust_checks PRIVATE SNMALLOC_CHECK_CLIENT)
598-
599- add_shim(snmalloc-new-override STATIC snmalloc_new snmalloc_exception_handler)
590+ # Compile a versions without exceptions as that is what snmalloc previously did.
591+ compile_noexc(snmalloc_exception_handler_noexc OBJECT src/snmalloc/override/failure.cc)
592+
593+ compile_noexc(snmalloc-new-override STATIC ${NEW} )
594+ target_link_libraries (snmalloc-new-override snmalloc_exception_handler)
600595
601596 if (SNMALLOC_STATIC_LIBRARY)
602- add_shim (snmallocshim-static STATIC snmalloc_core snmalloc_exception_handler snmalloc_new snmalloc_memcpy )
597+ compile_noexc (snmallocshim-static STATIC src/snmalloc/override/malloc.cc src/snmalloc/override/new.cc src/snmalloc/override/memcpy.cc )
603598 target_compile_definitions (snmallocshim-static PRIVATE
604599 SNMALLOC_STATIC_LIBRARY_PREFIX=${SNMALLOC_STATIC_LIBRARY_PREFIX} )
605600 endif ()
606601
607- set (SNMALLOC_UNCHECK_OBJECTS snmalloc_core snmalloc_exception_handler snmalloc_new)
608- set (SNMALLOC_CHECK_OBJECTS snmalloc_core_checks snmalloc_exception_handler_checks snmalloc_new_checks)
609-
610602 if (NOT WIN32 )
611- add_shim(snmallocshim SHARED ${SNMALLOC_UNCHECK_OBJECTS} )
603+ compile_noexc(snmallocshim SHARED ${ALLOC} )
604+ target_link_libraries (snmallocshim snmalloc_exception_handler_noexc)
605+
606+ compile(snmallocnew SHARED ${ALLOC} )
607+ target_link_libraries (snmallocnew snmalloc_exception_handler)
608+
612609 if (SNMALLOC_MEMCPY_OVERRIDE)
613- add_shim(snmallocshim-checks-memcpy-only SHARED ${SNMALLOC_UNCHECK_OBJECTS} snmalloc_memcpy)
614- add_shim(snmallocshim-checks SHARED ${SNMALLOC_CHECK_OBJECTS} snmalloc_new_checks snmalloc_memcpy_checks)
610+ compile(snmallocshim-checks-memcpy-only SHARED ${ALL} )
611+ target_link_libraries (snmallocshim-checks-memcpy-only snmalloc_exception_handler)
612+ compile(snmallocshim-checks SHARED ${ALL} )
615613 else ()
616- add_shim (snmallocshim-checks SHARED ${SNMALLOC_CHECK_OBJECTS } )
614+ compile (snmallocshim-checks SHARED ${MALLOC } )
617615 endif ()
616+ target_compile_definitions (snmallocshim-checks PRIVATE SNMALLOC_CHECK_CLIENT)
617+ target_link_libraries (snmallocshim-checks snmalloc_exception_handler)
618618 endif ()
619619
620620 if (SNMALLOC_RUST_SUPPORT)
621- add_shim (snmallocshim-rust STATIC snmalloc_rust )
622- add_shim (snmallocshim-checks-rust STATIC snmalloc_rust_checks )
621+ compile_noexc (snmallocshim-rust STATIC ${RUST} )
622+ compile_noexc (snmallocshim-checks-rust STATIC ${RUST} )
623623 target_compile_definitions (snmallocshim-checks-rust PRIVATE SNMALLOC_CHECK_CLIENT)
624624 endif ()
625625
0 commit comments