diff --git a/src/cmake/Modules/CheckMSize.cc b/src/cmake/Modules/CheckMSize.cc index 6429709a3..b4ea27a8a 100644 --- a/src/cmake/Modules/CheckMSize.cc +++ b/src/cmake/Modules/CheckMSize.cc @@ -15,11 +15,11 @@ int main() { for (unsigned i = 1; i < MAX_SZ; i++) { void * p = malloc(i); size_t r = _msize(p); - if (r < i || r > 2*(i + 8)) { - fprintf(stderr, "Unexpected _msize behavior"); - return 0; + if (r < i || ((i > 128) && (r > 2 * i))) { + fprintf(stderr, "Unexpected _msize behavior: i = %d, r = %d\n", i, int(r)); + return 1; } free(p); } - return 1; + return 0; } diff --git a/src/cmake/Modules/CheckMallocUsableSize.cc b/src/cmake/Modules/CheckMallocUsableSize.cc index 9a0e28a61..04e28040c 100644 --- a/src/cmake/Modules/CheckMallocUsableSize.cc +++ b/src/cmake/Modules/CheckMallocUsableSize.cc @@ -15,11 +15,11 @@ int main() { for (unsigned i = 1; i < MAX_SZ; i++) { void * p = malloc(i); size_t r = malloc_usable_size(p); - if (r < i || r > 2*(i + 8)) { - fprintf(stderr, "Unexpected malloc_usable_size behavior"); - return 0; + if (r < i || ((i > 128) && (r > 2 * i))) { + fprintf(stderr, "Unexpected malloc_usable_size behavior: i = %d, r = %d\n", i, int(r)); + return 1; } free(p); } - return 1; + return 0; } diff --git a/src/cmake/Modules/FindMSize.cmake b/src/cmake/Modules/FindMSize.cmake index 903210e45..3fa412f0c 100644 --- a/src/cmake/Modules/FindMSize.cmake +++ b/src/cmake/Modules/FindMSize.cmake @@ -1,17 +1,46 @@ find_path(MALLOC_DIR NAMES malloc.h ) -try_run(MSIZE_CHECK MSIZE_CHECK_BUILD - ${LEAN_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp - ${LEAN_SOURCE_DIR}/cmake/Modules/CheckMSize.cc - CMAKE_FLAGS -DINCLUDE_DIRECTORIES=${MALLOC_DIR} - RUN_OUTPUT_VARIABLE MSIZE_TRY_OUT) - -if("${MSIZE_CHECK_BUILD}" MATCHES "TRUE" AND "${MSIZE_CHECK}" MATCHES "0") - message(STATUS "Found malloc_usable_size") - set(MSIZE_FOUND TRUE) +if(CMAKE_CROSSCOMPILING) + if("${CMAKE_SYSTEM_NAME}" MATCHES "Windows") + # If it's cross-compilation and target is windows, first compile MSIZE_CHECK.exe + execute_process(COMMAND ${CMAKE_CXX_COMPILER} + "-o" ${LEAN_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/MSIZE_CHECK.exe + ${LEAN_SOURCE_DIR}/cmake/Modules/CheckMSize.cc + RESULT_VARIABLE MSIZE_CHECK_BUILD_RESULT) + if("${MSIZE_CHECK_BUILD_RESULT}" MATCHES "0") + set(MSIZE_CHECK_BUILD "TRUE") + # Check whether "wine" exists to run "MSIZE_CHECK.exe" + execute_process(COMMAND "bash" "-c" "wine --version > /dev/null 2&>1" + RESULT_VARIABLE WINE_CHECK) + if("${WINE_CHECK}" MATCHES "0") + execute_process(COMMAND "wine" + ${LEAN_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/MSIZE_CHECK.exe + RESULT_VARIABLE MSIZE_CHECK) + else() + # NOTE: We can compile a progrm with _msize, but we can't test + # it due to the lack of wine. We assume that it is usable + set(MSIZE_CHECK "0") + set(MSIZE_UNTESTED "(untested)") + endif() + else() + set(MSIZE_CHECK_BUILD "FALSE") + endif() + else() + # It's cross-compilation but the target is not Windows + set(MSIZE_FOUND FALSE) + endif() else() - message(STATUS "Usable malloc_usable_size was not detected") - set(MSIZE_FOUND FALSE) + try_run(MSIZE_CHECK MSIZE_CHECK_BUILD + ${LEAN_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp + ${LEAN_SOURCE_DIR}/cmake/Modules/CheckMSize.cc + CMAKE_FLAGS -DINCLUDE_DIRECTORIES=${MALLOC_DIR} + RUN_OUTPUT_VARIABLE MSIZE_TRY_OUT) endif() - +if("${MSIZE_CHECK_BUILD}" MATCHES "TRUE" AND "${MSIZE_CHECK}" MATCHES "0") + message(STATUS "Found _msize " "${MSIZE_UNTESTED}") + set(MSIZE_FOUND TRUE) +else() + message(STATUS "Usable _msize was not detected") + set(MSIZE_FOUND FALSE) +endif()