Skip to content

Commit 5ef0daf

Browse files
committed
Improve cross-compiling for more run checks
1 parent fba3113 commit 5ef0daf

File tree

6 files changed

+368
-328
lines changed

6 files changed

+368
-328
lines changed

cmake/cmake/Requirements.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ message(CHECK_START "Checking system character set")
2828
if(NOT CMAKE_CROSSCOMPILING OR CMAKE_CROSSCOMPILING_EMULATOR)
2929
cmake_push_check_state(RESET)
3030
set(CMAKE_REQUIRED_QUIET TRUE)
31-
check_source_runs(C "
31+
check_source_runs(C [[
3232
int main(void) {
3333
return (unsigned char)'A' != (unsigned char)0xC1;
3434
}
35-
" _php_is_ebcdic)
35+
]] _php_is_ebcdic)
3636
cmake_pop_check_state()
3737

3838
if(_php_is_ebcdic)

cmake/cmake/modules/PHP/CheckPreadPwrite.cmake

Lines changed: 87 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -45,51 +45,59 @@ function(_php_check_pread)
4545
return()
4646
endif()
4747

48-
if(NOT CMAKE_CROSSCOMPILING)
49-
set(
50-
temporaryFile
51-
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/php_check_pread.tmp
52-
)
53-
54-
file(WRITE "${temporaryFile}" "test\n")
55-
56-
cmake_push_check_state(RESET)
57-
set(CMAKE_REQUIRED_QUIET TRUE)
58-
check_source_runs(C "
59-
#include <sys/types.h>
60-
#include <sys/stat.h>
61-
#include <fcntl.h>
62-
#include <unistd.h>
63-
#include <errno.h>
64-
#include <stdlib.h>
65-
66-
int main(void) {
67-
char buf[3];
68-
int fd = open(\"${temporaryFile}\", O_RDONLY);
48+
set(
49+
temporaryFile
50+
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/php_check_pread.tmp
51+
)
6952

70-
if (fd < 0) return 1;
71-
if (pread(fd, buf, 2, 0) != 2) return 1;
72-
/* Linux glibc breakage until 2.2.5 */
73-
if (pread(fd, buf, 2, -1) != -1 || errno != EINVAL) return 1;
53+
file(WRITE "${temporaryFile}" "test\n")
7454

75-
return 0;
76-
}
77-
" HAVE_PREAD)
78-
cmake_pop_check_state()
79-
endif()
55+
cmake_push_check_state(RESET)
56+
set(CMAKE_REQUIRED_QUIET TRUE)
57+
set(CMAKE_REQUIRED_DEFINITIONS -DTMP_FILE=${temporaryFile})
58+
59+
check_source_runs(C [[
60+
#define xstr(s) str(s)
61+
#define str(s) #s
62+
63+
#include <sys/types.h>
64+
#include <sys/stat.h>
65+
#include <fcntl.h>
66+
#include <unistd.h>
67+
#include <errno.h>
68+
#include <stdlib.h>
69+
70+
int main(void)
71+
{
72+
char buf[3];
73+
int fd = open(xstr(TMP_FILE), O_RDONLY);
74+
75+
if (fd < 0) return 1;
76+
if (pread(fd, buf, 2, 0) != 2) return 1;
77+
/* Linux glibc breakage until 2.2.5 */
78+
if (pread(fd, buf, 2, -1) != -1 || errno != EINVAL) return 1;
79+
80+
return 0;
81+
}
82+
]] HAVE_PREAD)
83+
cmake_pop_check_state()
8084

8185
# This check is obsolete. Some systems once had pread() available with 3rd
8286
# argument of type 'off64_t', but didn't provide declaration in the headers.
83-
if(NOT HAVE_PREAD AND NOT CMAKE_CROSSCOMPILING)
87+
if(NOT HAVE_PREAD)
8488
cmake_push_check_state(RESET)
8589
set(CMAKE_REQUIRED_QUIET TRUE)
90+
set(CMAKE_REQUIRED_DEFINITIONS -DTMP_FILE=${temporaryFile})
8691

8792
# Needs '_GNU_SOURCE' to enable '_LARGEFILE64_SOURCE' for using 'off64_t'.
8893
# Default way would be the '_FILE_OFFSET_BITS=64' but this is skipped to
8994
# match the current php-src code.
9095
set(CMAKE_REQUIRED_LIBRARIES PHP::SystemExtensions)
9196

92-
check_source_runs(C "
97+
check_source_runs(C [[
98+
#define xstr(s) str(s)
99+
#define str(s) #s
100+
93101
#include <sys/types.h>
94102
#include <sys/stat.h>
95103
#include <fcntl.h>
@@ -100,9 +108,10 @@ function(_php_check_pread)
100108
/* Provide a missing declaration. */
101109
ssize_t pread(int, void *, size_t, off64_t);
102110

103-
int main(void) {
111+
int main(void)
112+
{
104113
char buf[3];
105-
int fd = open(\"${temporaryFile}\", O_RDONLY);
114+
int fd = open(xstr(TMP_FILE), O_RDONLY);
106115

107116
if (fd < 0) return 1;
108117
if (pread(fd, buf, 2, 0) != 2) return 1;
@@ -111,7 +120,7 @@ function(_php_check_pread)
111120

112121
return 0;
113122
}
114-
" PHP_PREAD_64)
123+
]] PHP_PREAD_64)
115124
cmake_pop_check_state()
116125

117126
if(PHP_PREAD_64)
@@ -144,56 +153,59 @@ function(_php_check_pwrite)
144153
return()
145154
endif()
146155

147-
if(NOT CMAKE_CROSSCOMPILING)
156+
cmake_push_check_state(RESET)
157+
set(CMAKE_REQUIRED_QUIET TRUE)
148158
set(
149-
temporaryFile
150-
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/php_check_pwrite.tmp
159+
CMAKE_REQUIRED_DEFINITIONS
160+
-DTMP_FILE=${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/php_check_pwrite.tmp
151161
)
152162

153-
cmake_push_check_state(RESET)
154-
set(CMAKE_REQUIRED_QUIET TRUE)
155-
156-
check_source_runs(C "
157-
#include <sys/types.h>
158-
#include <sys/stat.h>
159-
#include <fcntl.h>
160-
#include <unistd.h>
161-
#include <errno.h>
162-
#include <stdlib.h>
163-
164-
int main(void) {
165-
int fd = open(\"${temporaryFile}\", O_WRONLY|O_CREAT, 0600);
166-
167-
if (fd < 0) return 1;
168-
if (pwrite(fd, \"text\", 4, 0) != 4) return 1;
169-
/* Linux glibc breakage until 2.2.5 */
170-
if (pwrite(fd, \"text\", 4, -1) != -1 || errno != EINVAL) return 1;
171-
172-
return 0;
173-
}
174-
" HAVE_PWRITE)
175-
cmake_pop_check_state()
176-
endif()
163+
check_source_runs(C [[
164+
#define xstr(s) str(s)
165+
#define str(s) #s
166+
167+
#include <sys/types.h>
168+
#include <sys/stat.h>
169+
#include <fcntl.h>
170+
#include <unistd.h>
171+
#include <errno.h>
172+
#include <stdlib.h>
173+
174+
int main(void)
175+
{
176+
int fd = open(xstr(TMP_FILE), O_WRONLY|O_CREAT, 0600);
177+
178+
if (fd < 0) return 1;
179+
if (pwrite(fd, "text", 4, 0) != 4) return 1;
180+
/* Linux glibc breakage until 2.2.5 */
181+
if (pwrite(fd, "text", 4, -1) != -1 || errno != EINVAL) return 1;
182+
183+
return 0;
184+
}
185+
]] HAVE_PWRITE)
186+
cmake_pop_check_state()
177187

178188
# This check is obsolete. Some systems once had pwrite() available with 3rd
179189
# argument of type 'off64_t', but didn't provide declaration in the headers.
180190
# On later systems the 2nd argument of pwrite() should also have the 'const'
181191
# keyword.
182-
if(NOT HAVE_PWRITE AND NOT CMAKE_CROSSCOMPILING)
183-
set(
184-
temporaryFile
185-
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/php_check_pwrite64.tmp
186-
)
187-
192+
if(NOT HAVE_PWRITE)
188193
cmake_push_check_state(RESET)
189194
set(CMAKE_REQUIRED_QUIET TRUE)
195+
set(
196+
CMAKE_REQUIRED_DEFINITIONS
197+
-DTMP_FILE=${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/php_check_pwrite64.tmp
198+
)
190199

191200
# Needs '_GNU_SOURCE' to enable '_LARGEFILE64_SOURCE' for using 'off64_t'.
192201
# Default way would be the '_FILE_OFFSET_BITS=64' but this is skipped to
193202
# match the current php-src code.
194203
set(CMAKE_REQUIRED_LIBRARIES PHP::SystemExtensions)
195204

196-
check_source_runs(C "
205+
check_source_runs(C [[
206+
#define xstr(s) str(s)
207+
#define str(s) #s
208+
197209
#include <sys/types.h>
198210
#include <sys/stat.h>
199211
#include <fcntl.h>
@@ -204,17 +216,18 @@ function(_php_check_pwrite)
204216
/* Provide a missing declaration. */
205217
ssize_t pwrite(int, void *, size_t, off64_t);
206218

207-
int main(void) {
208-
int fd = open(\"${temporaryFile}\", O_WRONLY|O_CREAT, 0600);
219+
int main(void)
220+
{
221+
int fd = open(xstr(TMP_FILE), O_WRONLY|O_CREAT, 0600);
209222

210223
if (fd < 0) return 1;
211-
if (pwrite(fd, \"text\", 4, 0) != 4) return 1;
224+
if (pwrite(fd, "text", 4, 0) != 4) return 1;
212225
/* Linux glibc breakage until 2.2.5 */
213-
if (pwrite(fd, \"text\", 4, -1) != -1 || errno != EINVAL) return 1;
226+
if (pwrite(fd, "text", 4, -1) != -1 || errno != EINVAL) return 1;
214227

215228
return 0;
216229
}
217-
" PHP_PWRITE_64)
230+
]] PHP_PWRITE_64)
218231
cmake_pop_check_state()
219232

220233
if(PHP_PWRITE_64)

cmake/cmake/modules/PHP/CheckPtrace.cmake

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -41,78 +41,74 @@ endif()
4141

4242
if(_have_ptrace)
4343
message(CHECK_START "Checking whether ptrace works")
44+
check_source_runs(C [[
45+
#include <unistd.h>
46+
#include <signal.h>
47+
#include <sys/wait.h>
48+
#include <sys/types.h>
49+
#include <sys/ptrace.h>
50+
#include <errno.h>
51+
52+
#if !defined(PTRACE_ATTACH) && defined(PT_ATTACH)
53+
#define PTRACE_ATTACH PT_ATTACH
54+
#endif
55+
56+
#if !defined(PTRACE_DETACH) && defined(PT_DETACH)
57+
#define PTRACE_DETACH PT_DETACH
58+
#endif
59+
60+
#if !defined(PTRACE_PEEKDATA) && defined(PT_READ_D)
61+
#define PTRACE_PEEKDATA PT_READ_D
62+
#endif
63+
64+
int main(void)
65+
{
66+
/* copy will fail if sizeof(long) == 8 and we've got "int ptrace()" */
67+
long v1 = (unsigned int) -1;
68+
long v2;
69+
pid_t child;
70+
int status;
71+
72+
if ( (child = fork()) ) { /* parent */
73+
int ret = 0;
74+
75+
if (0 > ptrace(PTRACE_ATTACH, child, 0, 0)) {
76+
return 2;
77+
}
4478

45-
if(NOT CMAKE_CROSSCOMPILING)
46-
check_source_runs(C [[
47-
#include <unistd.h>
48-
#include <signal.h>
49-
#include <sys/wait.h>
50-
#include <sys/types.h>
51-
#include <sys/ptrace.h>
52-
#include <errno.h>
53-
54-
#if !defined(PTRACE_ATTACH) && defined(PT_ATTACH)
55-
#define PTRACE_ATTACH PT_ATTACH
56-
#endif
57-
58-
#if !defined(PTRACE_DETACH) && defined(PT_DETACH)
59-
#define PTRACE_DETACH PT_DETACH
60-
#endif
61-
62-
#if !defined(PTRACE_PEEKDATA) && defined(PT_READ_D)
63-
#define PTRACE_PEEKDATA PT_READ_D
64-
#endif
65-
66-
int main(void) {
67-
/* copy will fail if sizeof(long) == 8 and we've got "int ptrace()" */
68-
long v1 = (unsigned int) -1;
69-
long v2;
70-
pid_t child;
71-
int status;
72-
73-
if ( (child = fork()) ) { /* parent */
74-
int ret = 0;
75-
76-
if (0 > ptrace(PTRACE_ATTACH, child, 0, 0)) {
77-
return 2;
78-
}
79-
80-
waitpid(child, &status, 0);
79+
waitpid(child, &status, 0);
8180

82-
#ifdef PT_IO
83-
struct ptrace_io_desc ptio = {
84-
.piod_op = PIOD_READ_D,
85-
.piod_offs = &v1,
86-
.piod_addr = &v2,
87-
.piod_len = sizeof(v1)
88-
};
81+
#ifdef PT_IO
82+
struct ptrace_io_desc ptio = {
83+
.piod_op = PIOD_READ_D,
84+
.piod_offs = &v1,
85+
.piod_addr = &v2,
86+
.piod_len = sizeof(v1)
87+
};
8988

90-
if (0 > ptrace(PT_IO, child, (void *) &ptio, 0)) {
91-
ret = 3;
92-
}
93-
#else
94-
errno = 0;
89+
if (0 > ptrace(PT_IO, child, (void *) &ptio, 0)) {
90+
ret = 3;
91+
}
92+
#else
93+
errno = 0;
9594

96-
v2 = ptrace(PTRACE_PEEKDATA, child, (void *) &v1, 0);
95+
v2 = ptrace(PTRACE_PEEKDATA, child, (void *) &v1, 0);
9796

98-
if (errno) {
99-
ret = 4;
100-
}
101-
#endif
102-
ptrace(PTRACE_DETACH, child, (void *) 1, 0);
97+
if (errno) {
98+
ret = 4;
99+
}
100+
#endif
101+
ptrace(PTRACE_DETACH, child, (void *) 1, 0);
103102

104-
kill(child, SIGKILL);
103+
kill(child, SIGKILL);
105104

106-
return ret ? ret : (v1 != v2);
107-
} else { /* child */
108-
sleep(10);
109-
return 0;
110-
}
105+
return ret ? ret : (v1 != v2);
106+
} else { /* child */
107+
sleep(10);
108+
return 0;
111109
}
112-
]] HAVE_PTRACE)
113-
else()
114-
set(HAVE_PTRACE 1 CACHE INTERNAL "Whether ptrace() is present and works")
115-
endif()
110+
}
111+
]] HAVE_PTRACE)
116112

117113
if(HAVE_PTRACE)
118114
message(CHECK_PASS "yes")

0 commit comments

Comments
 (0)