Calling CMake from Powershell does not process files correctly

When I am invoking CMake from the Powershell, it behaves differently then when calling from cmd.
Mainly, it doesn’t parse any files specified by -DCMAKE_TOOLCHAIN_FILE=....

When e.g. I run

cmake .. -DCMAKE_TOOLCHAIN_FILE=..\my_toolchain.cmake

from cmd, the toolchain file is read, but when I am running from powershell, the toolchain file is not processed at all.

I am well aware that this might be a powershell thing, how powershell passes arguments and such. But it has already cost me various hours of my life, so my question is: is this a bug, or am I doing things wrong? any other options i pass with -DSOME_OPTION=ON are processed just fine from both cmd and powershell.

Any help is highly appreciated!

# CMakeLists.txt

cmake_minimum_required(VERSION 3.20)

project(toolchain_test)
option(TEST_VAR "my option" OFF)
message(STATUS "Test variable ${TEST_VAR}")

and

#my_toolchain.cmake
set(TEST_VAR ON)

My bet is on the “\” character. It’s nottoriously broken on Windows. I tend to do one of two things:

  • use “/” as path separtor always and everywhere
  • add type to variables that are supposed to be paths like so -DCMAKE_TOOLCHAIN_FILE:FILEPATH=..\my_toolchain.cmake

Justing using the ../ did not do the trick.
explicitly specifying it’s a path via :FILEPATH did, though. It even doesn’t matter if it’s \ or /.

Thanks for the hint (and the quick reply), I will start using :FILEPATH whenever I specify a toolchain file, to avoid the problem altogether.

I enclose the options in quotes, even for non-paths, to ensure they’re processed correctly on Windows.

cmake "-Dversion=1.2.3"

etc