CheckTypeSize fails with mingw cross compiler

I am running into an issue, trying to compile libcurl in GitHub CI with mingw-w64. It turned out that the issue is not specific to curl, but happens directly with mingw:

I am directly in a ubuntu:latest docker container. I install g++-mingw-w64-x86-64 using apt and try to build a simple program:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
project(test LANGUAGES C CXX)

include(CheckTypeSize)
check_type_size(size_t sizet)

And configure it:

cmake . -B build -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++

It fails with the following output:

-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc - works
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++ - works
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of size_t
CMake Error at /usr/local/lib/python3.8/dist-packages/cmake/data/share/cmake-3.29/Modules/CheckTypeSize.cmake:146 (try_compile):
  Cannot copy output executable

    ''

  to destination specified by COPY_FILE:

    '/test/build/CMakeFiles/CheckTypeSize/sizet.bin'

  because:

    No such file or directory (input)

  Recorded try_compile output location doesn't exist:

    /test/build/CMakeFiles/CMakeScratch/TryCompile-lXSj87/cmTC_f2ef9

Call Stack (most recent call first):
  /usr/local/lib/python3.8/dist-packages/cmake/data/share/cmake-3.29/Modules/CheckTypeSize.cmake:271 (__check_type_size_impl)
  CMakeLists.txt:5 (check_type_size)


-- Configuring incomplete, errors occurred!
root@1e03e2b8deb5:/test#

Apparently others have had similar issues:

However, none of these offer a solution. It seems all of them just say it’s a bug that is fixed in CMake 3.25.

I have tried the same build process listed above using apt version CMake 3.16 and pip-installed versions 3.24, 3.25, 3.28, 3.29 but all of them show the same error.

Is this a bug and if so, is there a workaround? I need both the CheckTypeSize module and MinGW to build libcurl from source, as said…

Thank you for any help!

Thoughts @brad.king?

Add -DCMAKE_SYSTEM_NAME=Windows to tell CMake that you’re cross-compiling to Windows.