mirror of
https://github.com/dhewm/dhewm3.git
synced 2025-03-20 17:51:02 +00:00
Fix D3_ARCH (work around CMake being useless)
CMAKE_SYSTEM_PROCESSOR used to be broken, CMake "fixed" it by redefining its meaning (from "Target CPU" to "Host CPU except when crosscompiling"). On Windows it always prints the host CPU, on Linux it at least made trouble in chroots and when running 64bit kernels with 32bit userlands (this used to be not totally uncommon on x86 before distros completely switched to amd64, and apparently Raspbian/Raspberry Pi OS does this on RPi4, see #267) Thankfully gcc and clang support "-dumpmachine" to print their (default) target system, so use that instead (MSVC already had a special case). On the upside, this allows getting rid of the MinGW special case. I hope this also works with Apple Clang..
This commit is contained in:
parent
f6bfdd16f9
commit
443802b683
1 changed files with 35 additions and 20 deletions
|
@ -67,20 +67,22 @@ endif()
|
|||
|
||||
# target cpu
|
||||
set(cpu ${CMAKE_SYSTEM_PROCESSOR})
|
||||
if(cpu STREQUAL "powerpc")
|
||||
set(cpu "ppc")
|
||||
elseif(cpu MATCHES "i.86")
|
||||
set(cpu "x86")
|
||||
elseif(cpu MATCHES "[aA][mM][dD]64" OR cpu MATCHES "[xX]64")
|
||||
set(cpu "x86_64")
|
||||
endif()
|
||||
|
||||
# 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)
|
||||
# Originally, ${CMAKE_SYSTEM_PROCESSOR} was supposed to contain the *target* CPU, according to CMake's documentation.
|
||||
# As far as I can tell this has always been broken (always returns host CPU) at least on Windows
|
||||
# (see e.g. https://cmake.org/pipermail/cmake-developers/2014-September/011405.html) and wasn't reliable on
|
||||
# other systems either, for example on Linux with 32bit userland but 64bit kernel it returned the kernel CPU type
|
||||
# (e.g. x86_64 instead of i686). Instead of fixing this, CMake eventually updated their documentation in 3.20,
|
||||
# now it's officially the same as CMAKE_HOST_SYSTEM_PROCESSOR except when cross-compiling (where it's explicitly set)
|
||||
# So we gotta figure out the actual target CPU type ourselves.. (why am I sticking to this garbage buildsystem?)
|
||||
if(NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL CMAKE_HOST_SYSTEM_PROCESSOR))
|
||||
# special case: cross-compiling, here CMAKE_SYSTEM_PROCESSOR should be correct, hopefully
|
||||
# (just leave cpu at ${CMAKE_SYSTEM_PROCESSOR})
|
||||
elseif(MSVC)
|
||||
# TODO: use CMAKE_GENERATOR_PLATFORM ? not sure if that works when building with MSVC compiler + ninja
|
||||
# or only when generating VS solutions (currently CMAKE_GENERATOR_PLATFORM can be "Win32" "x64" "ARM" or "ARM64")
|
||||
if(cpu MATCHES ".*[aA][rR][mM].*")
|
||||
# no idea how to detect windows for ARM(64), I don't own such hardware
|
||||
message(FATAL_ERROR "please fix this code to work for Windows on ARM and send a pull request")
|
||||
endif()
|
||||
if(CMAKE_CL_64)
|
||||
|
@ -88,18 +90,31 @@ if(MSVC)
|
|||
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() # not MSVC and not cross-compiling, assume GCC or clang (-compatible), seems to work for MinGW as well
|
||||
execute_process(COMMAND ${CMAKE_C_COMPILER} "-dumpmachine"
|
||||
RESULT_VARIABLE cc_dumpmachine_res
|
||||
OUTPUT_VARIABLE cc_dumpmachine_out)
|
||||
if(cc_dumpmachine_res EQUAL 0)
|
||||
message(STATUS "${CMAKE_C_COMPILER} -dumpmachine says ${cc_dumpmachine_out}")
|
||||
# gcc -dumpmachine and clang -dumpmachine seem to print something like "x86_64-linux-gnu" (gcc)
|
||||
# or "x64_64-pc-linux-gnu" (clang) or "i686-w64-mingw32" (32bit mingw-w64) i.e. starting with the CPU,
|
||||
# then "-" and then OS or whatever - so use everything up to first "-"
|
||||
string(REGEX MATCH "^[^-]+" cpu ${cc_dumpmachine_out})
|
||||
message(STATUS " => CPU extracted from that: ${cpu}")
|
||||
else()
|
||||
message(FATAL_ERROR "please fix this code to work for MINGW_CHOST = $ENV{MINGW_CHOST} and send a pull request!")
|
||||
message(WARNING "${CMAKE_C_COMPILER} -dumpmachine failed with error (code) ${cc_dumpmachine_res}")
|
||||
message(WARNING "will use the (sometimes incorrect) CMAKE_SYSTEM_PROCESSOR (${cpu}) to determine D3_ARCH")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(cpu STREQUAL "powerpc")
|
||||
set(cpu "ppc")
|
||||
elseif(cpu MATCHES "i.86")
|
||||
set(cpu "x86")
|
||||
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})
|
||||
|
||||
# target os
|
||||
|
|
Loading…
Reference in a new issue