-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCMakeLists.txt
255 lines (217 loc) · 6.13 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#[=============================================================================[
# The iconv extension
Configure the `iconv` extension.
This extension provides support for interface to iconv character set conversion
facility.
## PHP_EXT_ICONV
* Default: `ON`
* Values: `ON|OFF`
Enable the extension.
## PHP_EXT_ICONV_SHARED
* Default: `OFF`
* Values: `ON|OFF`
Build extension as shared.
#]=============================================================================]
cmake_minimum_required(VERSION 3.25...3.31)
project(
PhpExtensionIconv
LANGUAGES C
)
include(CheckLibraryExists)
include(CheckSourceRuns)
include(CheckSymbolExists)
include(CMakeDependentOption)
include(CMakePushCheckState)
include(FeatureSummary)
option(PHP_EXT_ICONV "Enable the iconv extension" ON)
add_feature_info(
"ext/iconv"
PHP_EXT_ICONV
"internationalization conversion interface"
)
cmake_dependent_option(
PHP_EXT_ICONV_SHARED
"Build the iconv extension as a shared library"
OFF
"PHP_EXT_ICONV;NOT BUILD_SHARED_LIBS"
OFF
)
if(NOT PHP_EXT_ICONV)
return()
endif()
if(PHP_EXT_ICONV_SHARED)
add_library(php_ext_iconv SHARED)
else()
add_library(php_ext_iconv)
endif()
target_sources(
php_ext_iconv
PRIVATE
iconv.c
iconv.stub.php
php_iconv.def
PUBLIC
FILE_SET HEADERS
FILES
php_iconv.h
)
target_compile_definitions(
php_ext_iconv
PRIVATE
ZEND_ENABLE_STATIC_TSRMLS_CACHE
$<$<PLATFORM_ID:Windows>:PHP_ICONV_EXPORTS>
)
find_package(Iconv)
set_package_properties(
Iconv
PROPERTIES
TYPE REQUIRED
PURPOSE "Necessary to enable the iconv extension."
)
target_link_libraries(php_ext_iconv PRIVATE Iconv::Iconv)
if(TARGET Iconv::Iconv)
# Sanity and library type checks.
if(Iconv_IS_BUILT_IN)
# When iconv is built into C library, first check if iconv function exists.
check_symbol_exists(iconv "iconv.h" _HAVE_ICONV)
# And if not, then check for libiconv function.
if(NOT _HAVE_ICONV)
check_symbol_exists(libiconv "iconv.h" HAVE_LIBICONV)
endif()
else()
check_library_exists(Iconv::Iconv libiconv "" HAVE_LIBICONV)
if(HAVE_LIBICONV)
set(ICONV_ALIASED_LIBICONV TRUE)
else()
check_library_exists(Iconv::Iconv iconv "" _HAVE_ICONV)
endif()
endif()
if(NOT _HAVE_ICONV AND NOT HAVE_LIBICONV)
message(
FATAL_ERROR
"Iconv sanity check failed: neither 'iconv()' nor 'libiconv()' function "
"could be found."
)
endif()
message(CHECK_START "Checking the iconv implementation")
# Check for GNU libiconv implementation.
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES Iconv::Iconv)
set(CMAKE_REQUIRED_QUIET TRUE)
check_symbol_exists(_libiconv_version "iconv.h" _have_gnu_libiconv)
cmake_pop_check_state()
if(_have_gnu_libiconv)
message(CHECK_PASS "GNU libiconv")
set(HAVE_LIBICONV TRUE)
set(PHP_ICONV_IMPL "libiconv")
endif()
# Check for glibc implementation.
if(NOT _have_gnu_libiconv)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES Iconv::Iconv)
set(CMAKE_REQUIRED_QUIET TRUE)
check_symbol_exists(gnu_get_libc_version gnu/libc-version.h HAVE_GLIBC_ICONV)
cmake_pop_check_state()
endif()
if(HAVE_GLIBC_ICONV)
message(CHECK_PASS "GNU C library")
set(PHP_ICONV_IMPL "glibc")
endif()
# Check for Konstantin Chuguev's iconv implementation.
if(NOT _have_gnu_libiconv AND NOT HAVE_GLIBC_ICONV)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES Iconv::Iconv)
set(CMAKE_REQUIRED_QUIET TRUE)
check_symbol_exists(iconv_ccs_init iconv.h _have_bsd_iconv)
cmake_pop_check_state()
endif()
if(_have_bsd_iconv)
message(CHECK_PASS "BSD iconv")
set(PHP_ICONV_IMPL "BSD iconv")
endif()
# Check for IBM iconv implementation.
if(NOT _have_gnu_libiconv AND NOT HAVE_GLIBC_ICONV AND NOT _have_bsd_iconv)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES Iconv::Iconv)
set(CMAKE_REQUIRED_QUIET TRUE)
check_symbol_exists(cstoccsid iconv.h HAVE_IBM_ICONV)
cmake_pop_check_state()
endif()
if(HAVE_IBM_ICONV)
message(CHECK_PASS "IBM iconv")
set(PHP_ICONV_IMPL "IBM iconv")
endif()
if(NOT PHP_ICONV_IMPL AND Iconv_IS_BUILT_IN)
message(CHECK_PASS "built in C library ${PHP_C_STANDARD_LIBRARY}")
elseif(NOT PHP_ICONV_IMPL)
message(CHECK_FAIL "not found, unknown")
endif()
# Sanity check. Check if iconv supports errno.
message(CHECK_START "Checking if iconv supports errno")
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES Iconv::Iconv)
set(CMAKE_REQUIRED_QUIET TRUE)
check_source_runs(C [[
#include <iconv.h>
#include <errno.h>
int main(void)
{
iconv_t cd;
cd = iconv_open("*to*", "*from*");
if (cd == (iconv_t)(-1)) {
if (errno == EINVAL) {
return 0;
} else {
return 1;
}
}
iconv_close(cd);
return 2;
}
]] PHP_ICONV_ERRNO_WORKS)
cmake_pop_check_state()
if(PHP_ICONV_ERRNO_WORKS)
message(CHECK_PASS "yes")
else()
message(CHECK_PASS "no")
message(FATAL_ERROR "The iconv 'errno' sanity check failed.")
endif()
message(CHECK_START "Checking if iconv supports //IGNORE")
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES Iconv::Iconv)
set(CMAKE_REQUIRED_QUIET TRUE)
check_source_runs(C [[
#include <iconv.h>
#include <stdlib.h>
int main(void)
{
iconv_t cd = iconv_open("UTF-8//IGNORE", "UTF-8");
if(cd == (iconv_t)-1) {
return 0;
}
char *in_p = "\xC3\xC3\xC3\xB8";
size_t in_left = 4, out_left = 4096;
char *out = malloc(out_left);
char *out_p = out;
size_t result = iconv(
cd,
(char **) &in_p,
&in_left,
(char **) &out_p,
&out_left
);
if(result == (size_t)-1) {
return 0;
}
return 1;
}
]] ICONV_BROKEN_IGNORE)
cmake_pop_check_state()
if(ICONV_BROKEN_IGNORE)
message(CHECK_FAIL "no")
else()
message(CHECK_PASS "yes")
endif()
endif()
set(HAVE_ICONV TRUE)
configure_file(cmake/config.h.in config.h)