Fix Win32/VS build on Win64 hosts

Turns out that ${CMAKE_SYSTEM_PROCESSOR} is broken on Windows
(both for Visual Studio and for 32bit MinGW/msys shells which for some
 reason seem to output x86_64 in uname -a)

Also suppressed -Wclass-memaccess warnings from GCC >= 8
This commit is contained in:
Daniel Gibson 2021-02-22 02:48:39 +01:00
parent 4bd46b3e28
commit 891f8720fc

View file

@ -71,20 +71,37 @@ if(cpu STREQUAL "powerpc")
set(cpu "ppc")
elseif(cpu MATCHES "i.86")
set(cpu "x86")
endif()
if(MSVC AND CMAKE_CL_64)
elseif(cpu MATCHES "[aA][mM][dD]64" OR cpu MATCHES "[xX]64")
set(cpu "x86_64")
endif()
add_definitions(-DD3_ARCH="${cpu}" -DD3_SIZEOFPTR=${CMAKE_SIZEOF_VOID_P})
if(cpu STREQUAL "x86_64" AND NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
# TODO: same for arm64? PPC64?
message(SEND_ERROR "CMake thinks sizeof(void*) == 4, but that the target CPU is x86_64!")
message(FATAL_ERROR "If you're building in a 32bit chroot on a 64bit host, switch to it with 'linux32 chroot' or at least call cmake with linux32 (or your OSs equivalent)!")
# On Windows ${CMAKE_SYSTEM_PROCESSOR} is broken (always returns host CPU)
# which has only been reported >7 years ago (https://cmake.org/pipermail/cmake-developers/2014-September/011405.html)
# so obviously a fix is too much to ask. Here's the special case to make that wonderful platform work;
# except if it's Windows for ARM(64) I guess, no idea how to detect that properly (I don't own such hardware).
if(MSVC)
if(cpu MATCHES ".*[aA][rR][mM].*")
message(FATAL_ERROR "please fix this code to work for Windows on ARM and send a pull request")
endif()
if(CMAKE_CL_64)
set(cpu "x86_64")
else()
set(cpu "x86")
endif()
elseif(DEFINED ENV{MINGW_CHOST})
# looks like it's broken in MinGW32 shells (or 32bit mingw-w64 shells) as well, this should help:
message(STATUS "MINGW_CHOST = $ENV{MINGW_CHOST}")
if($ENV{MINGW_CHOST} MATCHES "^i.86.*")
set(cpu "x86")
elseif($ENV{MINGW_CHOST} MATCHES "^x86_64.*")
set(cpu "x86_64")
else()
message(FATAL_ERROR "please fix this code to work for MINGW_CHOST = $ENV{MINGW_CHOST} and send a pull request!")
endif()
endif()
add_definitions(-DD3_ARCH="${cpu}" -DD3_SIZEOFPTR=${CMAKE_SIZEOF_VOID_P})
# target os
if(APPLE)
set(os "macosx")
@ -94,6 +111,14 @@ endif()
add_definitions(-DD3_OSTYPE="${os}")
message(STATUS "Setting -DD3_ARCH=\"${cpu}\" -DD3_SIZEOFPTR=${CMAKE_SIZEOF_VOID_P} -DD3_OSTYPE=\"${os}\" ")
if(cpu MATCHES ".*64.*" AND NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
# tough luck if some CPU architecture has "64" in its name but uses 32bit pointers
message(SEND_ERROR "CMake thinks sizeof(void*) == 4, but the target CPU looks like a 64bit CPU!")
message(FATAL_ERROR "If you're building in a 32bit chroot on a 64bit host, switch to it with 'linux32 chroot' or at least call cmake with linux32 (or your OSs equivalent)!")
endif()
# build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo")
@ -230,6 +255,12 @@ if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
if(cxx_has_Woverload_virtual)
add_compile_options(-Woverloaded-virtual)
endif()
# shut up about using memcpy() on classes, in the cases doom3 uses it it seems to be fine
CHECK_CXX_COMPILER_FLAG("-Wno-class-memaccess" cxx_has_Wno-class-memaccess)
if(cxx_has_Wno-class-memaccess)
add_compile_options(-Wno-class-memaccess)
endif()
if(AROS)
set(CMAKE_SHARED_LIBRARY_SUFFIX ".aros-${cpu}")