From bfb623ad96fa6f1dbc0c560403c4296e3c8e26c9 Mon Sep 17 00:00:00 2001 From: Jia Tan Date: Wed, 9 Aug 2023 20:54:15 +0800 Subject: [PATCH] CMake: Conditionally allow win95 threads and --enable-small. --- CMakeLists.txt | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b292ab27..d3a6ef5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -348,6 +348,11 @@ set(ENABLE_THREADS ON CACHE STRING set_property(CACHE ENABLE_THREADS PROPERTY STRINGS "${SUPPORTED_THREAD_METHODS}") +# This is a flag variable set when win95 threads are used. We must ensure +# the combination of enable_small and win95 threads is not used without a +# compiler supporting attribute __constructor__. +set(USE_WIN95_THREADS OFF) + if(NOT ENABLE_THREADS IN_LIST SUPPORTED_THREAD_METHODS) message(SEND_ERROR "'${ENABLE_THREADS}' is not a supported thread type") endif() @@ -368,14 +373,7 @@ if(ENABLE_THREADS) # Windows Vista. This is used for 32-bit x86 builds for # compatibility reasons since it makes no measurable difference # in performance compared to Vista threads. - # - # The Win95 threading lacks thread-safe one-time initialization - # function. - if (ENABLE_SMALL) - message(SEND_ERROR "Threading method win95 and ENABLE_SMALL " - "cannot be used at the same time") - endif() - + set(USE_WIN95_THREADS ON) add_compile_definitions(MYTHREAD_WIN95) else() add_compile_definitions(MYTHREAD_VISTA) @@ -763,6 +761,19 @@ check_c_source_compiles(" cmake_pop_check_state() tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR) +# The Win95 threading lacks a thread-safe one-time initialization function. +# The one-time initialization is needed for crc32_small.c and crc64_small.c +# create the CRC tables. So if small mode is enabled, the threading mode is +# win95, and the compiler does not support attribute constructor, then we +# would end up with a multithreaded build that is thread-unsafe. As a +# result this configuration is not allowed. +if(USE_WIN95_THREADS AND ENABLE_SMALL AND NOT HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR) + message(SEND_ERROR "Threading method win95 and ENABLE_SMALL " + "cannot be used at the same time with a compiler " + "that doesn't support " + "__attribute__((__constructor__))") +endif() + # cpuid.h check_include_file(cpuid.h HAVE_CPUID_H) tuklib_add_definition_if(liblzma HAVE_CPUID_H)