Skip to content

Commit 623fe37

Browse files
authored
Fix architecture checks (#1969)
* Use standard modules to test endianness CMake prior to v3.20 provides a module TestBigEndian which we can use to set the WABT_BIG_ENDIAN define. As of 3.20, the CMAKE_<LANG>_BYTE_ORDER variable should be preferred. Leaving this as a note for the future. * Fix x87 math detection TARGET_ARCH was only used to determine whether to add gcc-specific SSE math flags (-msse2 -mfpmath=sse). The new approach simply assumes gcc compatibility with its __i386__ and __SSE2_MATH__ symbols, which are defined precisely when we are targeting x86-32 with SSE2 math enabled. If those macros are defined, then we conclude all is well. Otherwise, we add the flags if we know the compiler is gcc or clang (and will thus accept them) and issue a warning if the compiler is unknown. Fixes #1709 Fixes #1688
1 parent eb2a07e commit 623fe37

File tree

3 files changed

+24
-41
lines changed

3 files changed

+24
-41
lines changed

CMakeLists.txt

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ include(CheckTypeSize)
108108
check_type_size(ssize_t SSIZE_T)
109109
check_type_size(size_t SIZEOF_SIZE_T)
110110

111+
include(TestBigEndian) # Note: deprecated in CMake 3.20
112+
test_big_endian(WABT_BIG_ENDIAN)
113+
111114
configure_file(src/config.h.in include/wabt/config.h @ONLY)
112115

113116

@@ -167,49 +170,23 @@ else ()
167170
add_definitions(-Wno-clobbered)
168171
endif ()
169172

173+
# wasm doesn't allow for x87 floating point math
170174
if (NOT EMSCRIPTEN)
171-
# try to get the target architecture by compiling a dummy.c file and
172-
# checking the architecture using the file command.
173-
file(WRITE ${WABT_BINARY_DIR}/dummy.c "main(){}")
174-
try_compile(
175-
COMPILE_OK
176-
${WABT_BINARY_DIR}
177-
${WABT_BINARY_DIR}/dummy.c
178-
COPY_FILE ${WABT_BINARY_DIR}/dummy
179-
)
180-
if (COMPILE_OK)
181-
execute_process(
182-
COMMAND file ${WABT_BINARY_DIR}/dummy
183-
RESULT_VARIABLE FILE_RESULT
184-
OUTPUT_VARIABLE FILE_OUTPUT
185-
ERROR_QUIET
186-
)
187-
188-
if (FILE_RESULT EQUAL 0)
189-
if (${FILE_OUTPUT} MATCHES "x86[-_]64")
190-
set(TARGET_ARCH "x86-64")
191-
elseif (${FILE_OUTPUT} MATCHES "Intel 80386")
192-
set(TARGET_ARCH "i386")
193-
elseif (${FILE_OUTPUT} MATCHES "ARM")
194-
set(TARGET_ARCH "ARM")
195-
elseif (${FILE_OUTPUT} MATCHES "IBM S/390")
196-
set(TARGET_ARCH "s390x")
197-
else ()
198-
message(WARNING "Unknown target architecture!")
199-
endif ()
175+
check_symbol_exists(__i386__ "" TARGET_IS_X86_32)
176+
check_symbol_exists(__SSE2_MATH__ "" HAVE_SSE2_MATH)
177+
178+
if (TARGET_IS_X86_32 AND NOT HAVE_SSE2_MATH)
179+
if (COMPILER_IS_GNU OR COMPILER_IS_CLANG)
180+
add_compile_options(-msse2 -mfpmath=sse)
200181
else ()
201-
message(WARNING "Error running `file` command on dummy executable")
182+
message(
183+
WARNING
184+
"Unknown compiler ${CMAKE_CXX_COMPILER_ID} appears to target x86-32 with x87 "
185+
"math. If you want wasm2c to work in a spec-compliant way, please add flags to "
186+
"use SSE2 math and set TARGET_IS_X86_32 and HAVE_SSE2_MATH appropriately at the "
187+
"CMake command line."
188+
)
202189
endif ()
203-
else ()
204-
message(WARNING "Error compiling dummy.c file")
205-
endif ()
206-
207-
if (TARGET_ARCH STREQUAL "i386")
208-
# wasm doesn't allow for x87 floating point math
209-
add_definitions(-msse2 -mfpmath=sse)
210-
endif ()
211-
if (TARGET_ARCH STREQUAL "s390x")
212-
add_definitions(-DWABT_BIG_ENDIAN=1)
213190
endif ()
214191
endif ()
215192
endif ()
@@ -384,6 +361,9 @@ endif ()
384361
IF (NOT WIN32)
385362
add_library(wasm-rt-impl STATIC wasm2c/wasm-rt-impl.c wasm2c/wasm-rt-impl.h)
386363
add_library(wabt::wasm-rt-impl ALIAS wasm-rt-impl)
364+
if (WABT_BIG_ENDIAN)
365+
target_compile_definitions(wasm-rt-impl PUBLIC WABT_BIG_ENDIAN=1)
366+
endif ()
387367

388368
if (WABT_INSTALL_RULES)
389369
install(

src/config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
/* Whether ENABLE_VIRTUAL_TERMINAL_PROCESSING is defined by windows.h */
4343
#cmakedefine01 HAVE_WIN32_VT100
4444

45+
/* Whether the target architecture is big endian */
46+
#cmakedefine01 WABT_BIG_ENDIAN
47+
4548
#cmakedefine01 COMPILER_IS_CLANG
4649
#cmakedefine01 COMPILER_IS_GNU
4750
#cmakedefine01 COMPILER_IS_MSVC

test/run-tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
SLOW_TIMEOUT_MULTIPLIER = 3
4040

4141
if sys.byteorder == 'big':
42-
wasm2c_args = ['--cflags=-DWABT_BIG_ENDIAN']
42+
wasm2c_args = ['--cflags=-DWABT_BIG_ENDIAN=1']
4343
else:
4444
wasm2c_args = []
4545

0 commit comments

Comments
 (0)