build: support MSVC arm64 cross build on x86

Adjust cmake code to support building a Windows arm64 binary on a
Windows x86/x64 host.

Add /EHsc to MSVC compiler flags for exception unwind support.

Check VCPKG_TARGET_TRIPLET in Architecture.cmake to set the appropriate
cmake variables for cross-compiling for arm64.

Add MSVC_x86_Host_Compile.cmake script, used from HostCompile.cmake, to
adjust the path of the cl.exe compiler and ENV{LIB} to build a C program
for x86 when cross-compiling, necessary for the bin2c utility.

Use wxrc from dependencies submodule when cross-compiling, because the
cross wxrc executable cannot be run on the host.

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
This commit is contained in:
Rafael Kitover 2022-12-30 02:30:08 +00:00 committed by Fabrice de Gans
parent 77f1235a95
commit 6307348cbc
5 changed files with 61 additions and 12 deletions

View File

@ -812,9 +812,9 @@ elseif(MSVC)
if(CMAKE_BUILD_TYPE STREQUAL Debug)
set(runtime "${runtime}d")
add_compiler_flags(${runtime} /ZI /W4 /Ob0 /Od /RTC1 /DDEBUG)
add_compiler_flags(${runtime} /ZI /W4 /Ob0 /Od /RTC1 /DDEBUG /EHsc)
else()
add_compiler_flags(/w /DNDEBUG)
add_compiler_flags(/w /DNDEBUG /EHsc)
if(CMAKE_BUILD_TYPE STREQUAL Release)
if(X86_32)

View File

@ -21,6 +21,12 @@ elseif(CMAKE_C_SIZEOF_DATA_PTR EQUAL 8)
set(BITS 64)
endif()
if(VCPKG_TARGET_TRIPLET MATCHES "^[aA][rR][mM]64")
set(CMAKE_SYSTEM_PROCESSOR ARM64)
elseif(VCPKG_TARGET_TRIPLET MATCHES "^[aA][rR][mM]-")
set(CMAKE_SYSTEM_PROCESSOR ARM)
endif()
# Turn asm on by default on 32bit x86 and set WINARCH for windows stuff.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "[xX]86|i[3-9]86|[aA][mM][dD]64")
if(CMAKE_C_SIZEOF_DATA_PTR EQUAL 4) # 32 bit
@ -52,4 +58,8 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "[aA][aA][rR][cC][hH]|[aA][rR][mM]")
set(ARCH_NAME arm64)
set(WINARCH arm64)
endif()
if(NOT CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "[aA][aA][rR][cC][hH]|[aA][rR][mM]")
set(CMAKE_CROSSCOMPILING TRUE)
endif()
endif()

View File

@ -1,23 +1,34 @@
function(host_compile src dst_cmd)
if(CMAKE_CROSSCOMPILING)
unset(link_flags)
set(dst "${dst_cmd}")
if(CMAKE_HOST_WIN32)
set(dst "${dst_cmd}.exe")
if(CMAKE_COMPILER_IS_GNUCXX)
set(link_flags -Wl,--subsystem,console)
endif()
else()
set(dst ${dst_cmd})
endif()
# assume cc foo.c -o foo # will work on most hosts
add_custom_command(
OUTPUT ${dst}
COMMAND cc ${src} -o ${dst} ${link_flags}
DEPENDS ${src}
)
if(MSVC)
set(msvc_compile_script ${CMAKE_SOURCE_DIR}/cmake/MSVC_x86_Host_Compile.cmake)
add_custom_command(
OUTPUT ${dst}
DEPENDS ${src} ${msvc_compile_script}
COMMAND ${CMAKE_COMMAND} -D "src=${src}" -D "dst=${dst}" -P ${msvc_compile_script}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
else()
set(dst ${dst_cmd})
# Assume: cc foo.c -o foo # will work on most hosts
add_custom_command(
OUTPUT ${dst}
DEPENDS ${src}
COMMAND cc ${src} -o ${dst} ${link_flags}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
endif()
else()
get_filename_component(dst ${dst_cmd} NAME)

View File

@ -0,0 +1,24 @@
# This is a hack to be run with cmake -P to compile a source C program passed
# in `src` to the `dst` executable using the host MSVC x86 toolchain.
find_program(cl_path NAME cl.exe HINTS ENV PATH)
string(REGEX REPLACE "[^/]+/cl\\.exe$" "x86/cl.exe" cl_path "${cl_path}")
set(orig_lib "$ENV{LIB}")
set(new_lib)
foreach(lib $ENV{LIB})
string(REGEX REPLACE "[^\\]+$" "x86" lib "${lib}")
list(APPEND new_lib "${lib}")
endforeach()
set(ENV{LIB} "${new_lib}")
execute_process(
COMMAND ${cl_path} /nologo ${src} /Fe:${dst}
OUTPUT_QUIET
)
set(ENV{LIB} "${orig_lib}")

View File

@ -649,6 +649,10 @@ endif()
include(HostCompile)
host_compile(${CMAKE_CURRENT_SOURCE_DIR}/bin2c.c ${BIN2C})
if(CMAKE_HOST_WIN32 AND CMAKE_CROSSCOMPILING)
set(WXRC ${CMAKE_SOURCE_DIR}/dependencies/wxrc/wxrc.exe)
endif()
if(WXRC)
separate_arguments(WXRC UNIX_COMMAND ${WXRC})
elseif(DEFINED ENV{WXRC})