Home > Software engineering >  ctest doesn't seem to use the settings in CMAKE_TOOLCHAIN_FILE
ctest doesn't seem to use the settings in CMAKE_TOOLCHAIN_FILE

Time:01-22

After making my application work on Linux, I'm trying cross-compile it for Windows and MacOSX. I already use CMake.

I began by creating a toolchain file. This works. My linux program is compiled with mingw and I receive a .exe at the end.

# build linux
$ cmake -Bbuild-linux
$ cmake --build build-linux

# build windows
$ cmake -Bbuild-windows -DCMAKE_TOOLCHAIN_FILE=`pwd`/cmake/x86_64-w64-mingw32.cmake
$ cmake --build build-windows

What happens next, is I run ctest to execute the unit tests. In Linux, this works fine. But when I do this using the cross-compiled stuff, it can't find the .exe file.

# run tests linux
(cd build-linux/engine; ctest)

# run tests windows
(cd build-windows/engine; ctest)

No problem, I thought -- I would append the .exe suffix depending on the environment -- using if(WIN32)

# CMakeLists.txt

if(WIN32)
    SET(EXE_SUFFIX, ".exe")
endif()

ADD_TEST(NAME test_myapp
    COMMAND ${CMAKE_CURRENT_BINARY_DIR}/../../thirdparty/usr/bin/lua${EXE_SUFFIX} ${CMAKE_CURRENT_SOURCE_DIR}/test_myapp.lua
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    )

But WIN32 isn't available for some reason and it never appends the suffix. Why isn't WIN32 available? Is the problem that ctest doesn't know about the settings from the toolchain file? Does it not realize that WIN32 was declared?

I am able to use if(WIN32) elsewhere in my CMakeLists.txt file so long as cmake.exe is doing something with those lines, not ctest.exe.

In summary, WIN32 is not set when ctest runs.

Is there a way to execute unit tests without involving ctest? If it can't be trusted to do this right, maybe I don't use it anymore.

CodePudding user response:

This line is totally incorrect:

SET(EXE_SUFFIX, ".exe")

You have set the variable EXE_SUFFIX,, not EXE_SUFFIX. When you later expand ${EXE_SUFFIX}, it comes back empty, so that entire if (WIN32) block is a no-op from the perspective of the rest of the program.

  •  Tags:  
  • Related