Skip to content

Commit 30c96f6

Browse files
authored
[wasm64] Fix pointer size detection (emscripten-core#20201)
Fixes: emscripten-core#20175
1 parent 7d7dbde commit 30c96f6

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

cmake/Modules/Platform/Emscripten.cmake

+7-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,16 @@ set(EMSCRIPTEN_SYSROOT "${_emcache_output}/sysroot")
217217
list(APPEND CMAKE_FIND_ROOT_PATH "${EMSCRIPTEN_SYSROOT}")
218218
list(APPEND CMAKE_SYSTEM_PREFIX_PATH /)
219219

220-
if ($ENV{CFLAGS} MATCHES "MEMORY64")
220+
if (${CMAKE_C_FLAGS} MATCHES "MEMORY64")
221221
set(CMAKE_LIBRARY_ARCHITECTURE "wasm64-emscripten")
222+
set(CMAKE_SIZEOF_VOID_P 8)
223+
set(CMAKE_C_SIZEOF_DATA_PTR 8)
224+
set(CMAKE_CXX_SIZEOF_DATA_PTR 8)
222225
else()
223226
set(CMAKE_LIBRARY_ARCHITECTURE "wasm32-emscripten")
227+
set(CMAKE_SIZEOF_VOID_P 4)
228+
set(CMAKE_C_SIZEOF_DATA_PTR 4)
229+
set(CMAKE_CXX_SIZEOF_DATA_PTR 4)
224230
endif()
225231

226232
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
@@ -290,11 +296,8 @@ set(CMAKE_SIZEOF_INT 4)
290296
set(CMAKE_SIZEOF_UNSIGNED_LONG 4)
291297
set(CMAKE_SIZEOF_UNSIGNED_INT 4)
292298
set(CMAKE_SIZEOF_LONG 4)
293-
set(CMAKE_SIZEOF_VOID_P 4)
294299
set(CMAKE_SIZEOF_FLOAT 4)
295300
set(CMAKE_SIZEOF_DOUBLE 8)
296-
set(CMAKE_C_SIZEOF_DATA_PTR 4)
297-
set(CMAKE_CXX_SIZEOF_DATA_PTR 4)
298301
set(CMAKE_HAVE_LIMITS_H 1)
299302
set(CMAKE_HAVE_UNISTD_H 1)
300303
set(CMAKE_HAVE_PTHREAD_H 1)

test/cmake/check_type_size/CMakeLists.txt

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ cmake_minimum_required(VERSION 3.0)
22

33
project(test_cmake)
44

5+
if (CMAKE_LIBRARY_ARCHITECTURE MATCHES "wasm64")
6+
set(PTR_SIZE 8)
7+
else()
8+
set(PTR_SIZE 4)
9+
endif()
10+
11+
if (NOT "${CMAKE_SIZEOF_VOID_P}" EQUAL "${PTR_SIZE}")
12+
message(FATAL_ERROR "CMAKE_SIZEOF_VOID_P is not ${PTR_SIZE}! (${CMAKE_SIZEOF_VOID_P})")
13+
endif()
14+
message(STATUS "CMAKE_SIZEOF_VOID_P -> ${CMAKE_SIZEOF_VOID_P}")
15+
516
include(CheckTypeSize)
617

718
check_type_size("int" int_size)
@@ -15,3 +26,9 @@ if (NOT "${big_size}" EQUAL "1028")
1526
message(FATAL_ERROR "CHECK_TYPE_SIZE with int[256+1] did not return 1028! (${big_size})")
1627
endif()
1728
message(STATUS "CHECK_TYPE_SIZE int -> ${big_size}")
29+
30+
check_type_size("void*" ptr_size)
31+
if (NOT "${ptr_size}" EQUAL "${PTR_SIZE}")
32+
message(FATAL_ERROR "CHECK_TYPE_SIZE with void* did not return ${PTR_SIZE}! (${ptr_size})")
33+
endif()
34+
message(STATUS "CHECK_TYPE_SIZE void* -> ${ptr_size}")

test/test_other.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -939,12 +939,21 @@ def test_pkg_config_packages(self):
939939
self.assertContained(version, out)
940940

941941
@parameterized({
942-
'': [[]],
943-
'pthreads': [['-DCMAKE_CXX_FLAGS=-pthread', '-DCMAKE_C_FLAGS=-pthread']],
942+
'': [None],
943+
'wasm64': ['-sMEMORY64'],
944+
'pthreads': ['-pthread'],
944945
})
945-
def test_cmake_check_type_size(self, flags):
946-
cmd = [EMCMAKE, 'cmake', test_file('cmake/check_type_size')] + flags
947-
self.run_process(cmd)
946+
def test_cmake_check_type_size(self, cflag):
947+
if cflag == '-sMEMORY64':
948+
self.require_wasm64()
949+
cmd = [EMCMAKE, 'cmake', test_file('cmake/check_type_size')]
950+
if cflag:
951+
cmd += [f'-DCMAKE_CXX_FLAGS={cflag}', f'-DCMAKE_C_FLAGS={cflag}']
952+
output = self.run_process(cmd, stdout=PIPE).stdout
953+
if cflag == '-sMEMORY64':
954+
self.assertContained('CMAKE_SIZEOF_VOID_P -> 8', output)
955+
else:
956+
self.assertContained('CMAKE_SIZEOF_VOID_P -> 4', output)
948957

949958
# Verify that this test works without needing to run node. We do this by breaking node
950959
# execution.

0 commit comments

Comments
 (0)