CMAKE_COMMAND script policy

CMAKE_COMMAND can be used to directly invoke CMake scripts with options. This is particularly useful to chain CMake scripts during CTest runs with fixtures. It’s important to set the minimum CMake version in the CMake script file as otherwise CMake defaults to CMake < 2.6 policies. That makes current CMake syntax fail in puzzling ways.

Example: Suppose a test script that reads a file using regex. This will fail without cmake_minimum_required() or cmake_policy(). I prefer cmake_minimum_required to make the script fail if someone uses the script directly with too-old CMake.


mytest.cmake contains:

cmake_minimum_required(VERSION 3.5)

file(STRINGS my.txt m REGEX "([\.A-Za-z0-9_]+)")

The failure will be over escaped \. if cmake_minimum_version or cmake_policy isn’t used in mytest.cmake.