"cmake.exe: Bad address" while building in MinGW-w64

258 Views Asked by At

I am trying to build a library (FMIL 2.0.3) from source with MSYS2 and mingw-w64-x86_64-cmake, and I am stumped by the error cmake.exe: Bad address when calling make

The exact error, when executing make in the msys2 mingw64 shell, is:

   $ make
   [  1%] Built target c99snprintf
   [  7%] Built target jmutils
   [  8%] Performing dependent_reconfigure step for 'expatex'
=> /bin/sh: line 1: /C/msys64/mingw64/bin/cmake.exe: Bad address  <==============================================
   make[2]: *** [CMakeFiles/expatex.dir/build.make:101: ExpatEx/stamp/expatex-dependent_reconfigure] Error 126
   make[1]: *** [CMakeFiles/Makefile2:257: CMakeFiles/expatex.dir/all] Error 2
   make: *** [Makefile:146: all] Error 2

The previous step, executing cmake, finished without error:

$ cmake -G "MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DFMILIB_BUILD_STATIC_LIB=OFF -DFMILIB_BUILD_SHARED_LIB=ON ..

I am using a fresh installation (msys2-x86_64-20230318), and have attempted updating and re-installing cmake, make, and gcc without success:

$ pacman -S mingw-w64-x86_64-cmake    mingw-w64-x86_64-gcc    make

$ cmake --version
cmake version 3.26.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ which cmake
/mingw64/bin/cmake

$ make --version
GNU Make 4.4.1
Built for x86_64-pc-msys
$ which make
/usr/bin/make

$ gcc --version
gcc.exe (Rev10, Built by MSYS2 project) 12.2.0
$ which gcc
/mingw64/bin/gcc

$ pacman -Syuu
:: Synchronizing package databases...
 clangarm64 is up to date
 mingw32 is up to date
 mingw64 is up to date
 ucrt64 is up to date
 clang32 is up to date
 clang64 is up to date
 msys is up to date
:: Starting core system upgrade...
 there is nothing to do
:: Starting full system upgrade...
 there is nothing to do

The problem can apparently be circumvented by using Ninja instead of make.

What can I do to resolve or further analyze this error? It seems to be the cmake command itself which is crashing.

Things I tried or excluded , based on my research so far:

  • I have no spaces in any paths,
  • I am not using visual studio,
  • my gcc is the current version supplied by mingw,
  • starting the shell with msys2_shell.cmd -mingw64 -use-full-path made no difference.
  • there are no windows event logs or ms defender logs correlating to the problem
1

There are 1 best solutions below

1
HugoRune On

The problem was caused by a long and possibly malformed line in one of the generated make files.

I hope this can be useful to others having a similar error. Apparently this is some weird interaction between mingw, cmake and make under Windows.

Here, one of the cmake files contained this part:

  ExternalProject_Add_Step(
    expatex dependent_reconfigure
    DEPENDEES configure
    DEPENDERS build
>>  COMMAND ${CMAKE_COMMAND} -E echo "Running:  ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"  ${EXPAT_SETTINGS} ${FMIXML_EXPAT_DIR}"
    COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" ${EXPAT_SETTINGS} "${FMIXML_EXPAT_DIR}"
    DEPENDS ${CMAKE_BINARY_DIR}/CMakeCache.txt
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/ExpatEx
  )

The marked line contains some weird nested quotation marks, but since this is just an echo statement, that should not matter.

The generated line looks like this, with some very questionable quotation marks, but no obvious syntax error, since it is all just after -E echo

/C/msys64/mingw64/bin/cmake.exe -E echo "Running:  C:/msys64/mingw64/bin/cmake.exe -G " "MSYS Makefiles\"  -DBUILD_tools:BOOLEAN=OFF" -DBUILD_examples:BOOLEAN=OFF -DBUILD_tests:BOOLEAN=OFF ...

Nevertheless, removing this one line from the generated make file resolved the error.

Apparently, when the make process is executing the cmake process in the mingw shell with this line, it causes an error in cmake. I could not reproduce the error by executing this line manually, it happens only when executed from within make. Maybe make is using a different shell to execute cmake, and that shell is having trouble with the nested quotes.