@@ -20,6 +20,13 @@ Check for thread safety, a.k.a. ZTS (Zend thread safety) build.
2020 during the generation phase to determine thread safety enabled from the
2121 configuration phase. For example, the `PHP_EXTENSION_DIR` configuration
2222 variable needs to be set depending on the thread safety.
23+
24+ ## Basic usage
25+
26+ ```cmake
27+ # CMakeLists.txt
28+ include(PHP/ThreadSafety)
29+ ```
2330#]=============================================================================]
2431
2532include_guard (GLOBAL )
@@ -32,68 +39,55 @@ define_property(
3239 BRIEF_DOCS "Whether the PHP has thread safety enabled"
3340)
3441
35- function (_php_thread_safety)
36- message (CHECK_START "Checking whether to enable thread safety (ZTS)" )
37-
38- add_feature_info(
39- "Thread safety (ZTS)"
40- PHP_THREAD_SAFETY
41- "safe execution in multi-threaded environments"
42- )
43-
44- if (NOT PHP_THREAD_SAFETY)
45- message (CHECK_FAIL "no" )
46- return ()
47- endif ()
48-
49- set (THREADS_PREFER_PTHREAD_FLAG TRUE )
50- set (THREADS_ENABLE_SYSTEM_EXTENSIONS TRUE )
51- find_package (Threads)
52- set_package_properties(
53- Threads
54- PROPERTIES
55- TYPE REQUIRED
56- PURPOSE "Necessary to enable PHP thread safety."
57- )
58-
59- if (Threads_FOUND)
60- message (CHECK_PASS "yes" )
61- else ()
62- message (CHECK_FAIL "failed" )
63- endif ()
64-
65- target_link_libraries (php_configuration INTERFACE Threads::Threads)
66-
67- set (ZTS TRUE PARENT_SCOPE)
68-
69- # Add ZTS compile definition. Some PHP headers might not have php_config.h
70- # directly available. For example, some Zend headers.
71- target_compile_definitions (php_configuration INTERFACE ZTS)
72-
73- # Set custom target property on the PHP configuration target.
74- set_target_properties (php_configuration PROPERTIES PHP_THREAD_SAFETY ON )
75-
76- # Add compile definitions for POSIX threads conformance.
77- # TODO: Recheck these definitions since many of them are deprecated or
78- # obsolete in favor of the compiler automatic definitions when using threading
79- # flag on such system.
80- target_compile_definitions (
81- php_configuration
82- INTERFACE
83- $<$<AND :$<PLATFORM_ID:SunOS>,$<COMPILE_LANGUAGE:ASM,C,CXX>>:_POSIX_PTHREAD_SEMANTICS;_REENTRANT>
84- $<$<AND :$<PLATFORM_ID:FreeBSD>,$<COMPILE_LANGUAGE:ASM,C,CXX>>:_REENTRANT;_THREAD_SAFE>
85- $<$<AND :$<PLATFORM_ID:AIX>,$<COMPILE_LANGUAGE:ASM,C,CXX>>:_THREAD_SAFE>
86- $<$<AND :$<PLATFORM_ID:Linux,HP-UX,SCO_SV,UNIX_SV,UnixWare>,$<COMPILE_LANGUAGE:ASM,C,CXX>>:_REENTRANT>
87- )
88- endfunction ()
89-
90- # Run at the end of the configuration so that apache2handler SAPI can
91- # automatically enable thread safety by setting PHP_THREAD_SAFETY to 'ON'
92- # during the configuration. Elsewhere, thread safety should be opt-in and
93- # automatic enabling in the configuration phase shouldn't be encouraged.
94- cmake_language(
95- DEFER
96- DIRECTORY ${PHP_SOURCE_DIR}
97- ID 1 # Run before other calls so ZTS variable is added in main/php_config.h.
98- CALL _php_thread_safety
42+ message (CHECK_START "Checking whether to enable thread safety (ZTS)" )
43+
44+ add_feature_info(
45+ "Thread safety (ZTS)"
46+ PHP_THREAD_SAFETY
47+ "safe execution in multi-threaded environments"
48+ )
49+
50+ if (NOT PHP_THREAD_SAFETY)
51+ message (CHECK_FAIL "no" )
52+ return ()
53+ endif ()
54+
55+ set (THREADS_PREFER_PTHREAD_FLAG TRUE )
56+ set (THREADS_ENABLE_SYSTEM_EXTENSIONS TRUE )
57+ find_package (Threads)
58+ set_package_properties(
59+ Threads
60+ PROPERTIES
61+ TYPE REQUIRED
62+ PURPOSE "Necessary to enable PHP thread safety."
63+ )
64+
65+ if (Threads_FOUND)
66+ message (CHECK_PASS "yes" )
67+ else ()
68+ message (CHECK_FAIL "failed" )
69+ endif ()
70+
71+ target_link_libraries (php_configuration INTERFACE Threads::Threads)
72+
73+ set (ZTS TRUE )
74+
75+ # Add ZTS compile definition. Some PHP headers might not have php_config.h
76+ # directly available. For example, some Zend headers.
77+ target_compile_definitions (php_configuration INTERFACE ZTS)
78+
79+ # Set custom target property on the PHP configuration target.
80+ set_target_properties (php_configuration PROPERTIES PHP_THREAD_SAFETY ON )
81+
82+ # Add compile definitions for POSIX threads conformance.
83+ # TODO: Recheck these definitions since many of them are deprecated or
84+ # obsolete in favor of the compiler automatic definitions when using threading
85+ # flag on such system.
86+ target_compile_definitions (
87+ php_configuration
88+ INTERFACE
89+ $<$<AND :$<PLATFORM_ID:SunOS>,$<COMPILE_LANGUAGE:ASM,C,CXX>>:_POSIX_PTHREAD_SEMANTICS;_REENTRANT>
90+ $<$<AND :$<PLATFORM_ID:FreeBSD>,$<COMPILE_LANGUAGE:ASM,C,CXX>>:_REENTRANT;_THREAD_SAFE>
91+ $<$<AND :$<PLATFORM_ID:AIX>,$<COMPILE_LANGUAGE:ASM,C,CXX>>:_THREAD_SAFE>
92+ $<$<AND :$<PLATFORM_ID:Linux,HP-UX,SCO_SV,UNIX_SV,UnixWare>,$<COMPILE_LANGUAGE:ASM,C,CXX>>:_REENTRANT>
9993)
0 commit comments