@@ -479,6 +479,16 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
479479 set (TEST_CLEANUP ${SNMALLOC_CLEANUP} )
480480 endif ()
481481
482+ # Tests that only include snmalloc_testlib.h (no snmalloc.h or snmalloc_core.h).
483+ # These are mitigation-independent and can be compiled once, then linked
484+ # against both fast and check testlib variants.
485+ set (TESTLIB_ONLY_TESTS
486+ bits first_operation memory_usage multi_atexit multi_threadatexit
487+ redblack statistics teardown
488+ contention external_pointer large_alloc lotsofthreads post_teardown
489+ singlethread startup
490+ )
491+
482492 function (make_tests TAG DEFINES )
483493 foreach (TEST_CATEGORY ${TEST_CATEGORIES} )
484494 message (VERBOSE "Adding ${TAG} /${TEST_CATEGORY} tests" )
@@ -488,7 +498,32 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
488498 aux_source_directory (${TESTDIR} /${TEST_CATEGORY}/${TEST} SRC )
489499 set (TESTNAME "${TEST_CATEGORY} -${TEST} -${TAG} " )
490500
491- add_executable (${TESTNAME} ${SRC} )
501+ # Check if this test is testlib-only (mitigation-independent).
502+ # If so, compile the source once into an object library and reuse
503+ # across flavours.
504+ list (FIND TESTLIB_ONLY_TESTS ${TEST} _testlib_only_idx)
505+ set (OBJLIBNAME "${TEST_CATEGORY} -${TEST} -obj" )
506+ if (NOT ${_testlib_only_idx} EQUAL -1 AND NOT TARGET ${OBJLIBNAME} )
507+ # First flavour to see this test creates the object library.
508+ add_library (${OBJLIBNAME} OBJECT ${SRC} )
509+ target_link_libraries (${OBJLIBNAME} snmalloc )
510+ target_compile_definitions (${OBJLIBNAME} PRIVATE "SNMALLOC_USE_${TEST_CLEANUP} " )
511+ add_warning_flags (${OBJLIBNAME} )
512+ if (SNMALLOC_SANITIZER)
513+ target_compile_options (${OBJLIBNAME} PRIVATE -g -fsanitize=${SNMALLOC_SANITIZER} -fno-omit-frame-pointer )
514+ if (${SNMALLOC_SANITIZER} MATCHES "thread" )
515+ target_compile_definitions (${OBJLIBNAME} PRIVATE SNMALLOC_THREAD_SANITIZER_ENABLED )
516+ endif ()
517+ endif ()
518+ endif ()
519+
520+ if (NOT ${_testlib_only_idx} EQUAL -1)
521+ # Testlib-only: link the shared object library against this
522+ # flavour's testlib.
523+ add_executable (${TESTNAME} $<TARGET_OBJECTS :${OBJLIBNAME} >)
524+ else ()
525+ add_executable (${TESTNAME} ${SRC} )
526+ endif ()
492527
493528 if (SNMALLOC_SANITIZER)
494529 target_compile_options (${TESTNAME} PRIVATE -g -fsanitize=${SNMALLOC_SANITIZER} -fno-omit-frame-pointer )
@@ -551,6 +586,12 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
551586 if (NOT DEFINES STREQUAL " " )
552587 target_compile_definitions (${LIBNAME} PRIVATE ${DEFINES} )
553588 endif ()
589+ if (SNMALLOC_SANITIZER)
590+ target_compile_options (${LIBNAME} PRIVATE -g -fsanitize=${SNMALLOC_SANITIZER} -fno-omit-frame-pointer )
591+ if (${SNMALLOC_SANITIZER} MATCHES "thread" )
592+ target_compile_definitions (${LIBNAME} PRIVATE SNMALLOC_THREAD_SANITIZER_ENABLED )
593+ endif ()
594+ endif ()
554595 add_warning_flags (${LIBNAME} )
555596 endfunction ()
556597
0 commit comments