FortranCInterface/Detect.cmake fails in Debug configuration
CMake-3.21.4
Generator: Ninja (1.10.2)
Configuration: Debug
platform: Windows
Compiler: ifort-19.1.3.311
Right when FortranCInterface_HEADER()
is called, CMake displays the message Failed to load sample executable
and crashes later on, having produced an empty mangling header. This message is coming from ${CMAKE_MODULE_PATH}/FortranCInterface/Detect.cmake
.
By examining the content of ${CMAKE_BINARY_DIR}/CMakeFiles/FortranCInterface
(almost) everything looks right:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2021-11-01 09:25 CMakeFiles
d----- 2021-11-01 09:25 symbols
-a---- 2021-11-01 09:25 2068 .ninja_deps
-a---- 2021-11-01 09:25 4772 .ninja_log
-a---- 2021-11-01 09:25 36521 build.ninja
-a---- 2021-11-01 09:25 13438 CMakeCache.txt
-a---- 2021-11-01 09:25 1584 cmake_install.cmake
-a---- 2021-11-01 09:25 192 exe-Debug.cmake
-a---- 2021-11-01 09:25 66560 FortranCInterface.exe
-a---- 2021-11-01 09:25 526656 FortranCInterface.ilk
-a---- 2021-11-01 09:25 634880 FortranCInterface.pdb
-a---- 2021-11-01 09:25 124 Input.cmake
-a---- 2021-11-01 09:25 9656 myfort.lib
-a---- 2021-11-01 09:25 479 mymodule.mod
-a---- 2021-11-01 09:25 484 my_module.mod
-a---- 2021-11-01 09:25 1392 Output.cmake
-a---- 2021-11-01 09:25 67932 symbols.lib
Except one thing: Detect.cmake
attempts (line 60) to optionally include()
a exe-Release.cmake
which assigns FortranCInterface_EXE
a value:
# Locate the sample project executable.
set(FortranCInterface_EXE)
if(FortranCInterface_COMPILED)
include(${FortranCInterface_BINARY_DIR}/exe-Release.cmake OPTIONAL)
else()
But exe-Debug.cmake
is present instead. Unsurprisingly, it never gets included, in turn FortranCInterface_EXE
never gets set and a few lines later, the execution enters the elseif(NOT _result)
branch (line 78) of if(FortranCInterface_EXE)
.
The workaround was straightforward, actually adding an extra include()
, like so:
# Locate the sample project executable.
set(FortranCInterface_EXE)
if(FortranCInterface_COMPILED)
include(${FortranCInterface_BINARY_DIR}/exe-Release.cmake OPTIONAL)
include(${FortranCInterface_BINARY_DIR}/exe-Debug.cmake OPTIONAL)
else()