RunCMakeTest.cmake 9.76 KB
Newer Older
1 2
cmake_minimum_required(VERSION 3.19) # CMP0053

3 4 5 6 7 8 9
include(RunCMake)

# Fix Visual Studio generator name
if(RunCMake_GENERATOR MATCHES "^(Visual Studio [0-9]+ [0-9]+) ")
  set(RunCMake_GENERATOR "${CMAKE_MATCH_1}")
endif()

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
set(RunCMake-check-file check.cmake)

function(validate_schema file expected_result)
  execute_process(
    COMMAND "${PYTHON_EXECUTABLE}" "${RunCMake_SOURCE_DIR}/validate_schema.py" "${file}"
    RESULT_VARIABLE _result
    OUTPUT_VARIABLE _output
    ERROR_VARIABLE _error
    )
  if(NOT _result STREQUAL expected_result)
    string(REPLACE "\n" "\n" _output_p "${_output}")
    string(REPLACE "\n" "\n" _error_p "${_error}")
    string(APPEND RunCMake_TEST_FAILED "Expected result of validating ${file}: ${expected_result}\nActual result: ${_result}\nOutput:\n${_output_p}\nError:\n${_error_p}")
  endif()

  set(RunCMake_TEST_FAILED "${RunCMake_TEST_FAILED}" PARENT_SCOPE)
endfunction()

28 29 30
function(run_cmake_presets name)
  set(RunCMake_TEST_SOURCE_DIR "${RunCMake_BINARY_DIR}/${name}")
  set(_source_arg "${RunCMake_TEST_SOURCE_DIR}")
31 32
  if(CMakePresets_SOURCE_ARG)
    set(_source_arg "${CMakePresets_SOURCE_ARG}")
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  endif()
  file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}")
  file(MAKE_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}")
  configure_file("${RunCMake_SOURCE_DIR}/CMakeLists.txt.in" "${RunCMake_TEST_SOURCE_DIR}/CMakeLists.txt" @ONLY)

  if(NOT CMakePresets_FILE)
    set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/${name}.json.in")
  endif()
  if(EXISTS "${CMakePresets_FILE}")
    configure_file("${CMakePresets_FILE}" "${RunCMake_TEST_SOURCE_DIR}/CMakePresets.json" @ONLY)
  endif()

  if(NOT CMakeUserPresets_FILE)
    set(CMakeUserPresets_FILE "${RunCMake_SOURCE_DIR}/${name}User.json.in")
  endif()
  if(EXISTS "${CMakeUserPresets_FILE}")
    configure_file("${CMakeUserPresets_FILE}" "${RunCMake_TEST_SOURCE_DIR}/CMakeUserPresets.json" @ONLY)
  endif()

  set(_s_arg -S)
  if(CMakePresets_NO_S_ARG)
    set(_s_arg)
  endif()
  set(_source_args ${_s_arg} ${_source_arg})
  if(CMakePresets_NO_SOURCE_ARGS)
    set(_source_args)
  endif()
  set(_unused_cli --no-warn-unused-cli)
  if(CMakePresets_WARN_UNUSED_CLI)
    set(_unused_cli)
  endif()

  set(RunCMake_TEST_COMMAND ${CMAKE_COMMAND}
    ${_source_args}
    -DRunCMake_TEST=${name}
    -DRunCMake_GENERATOR=${RunCMake_GENERATOR}
    -DCMAKE_MAKE_PROGRAM=${RunCMake_MAKE_PROGRAM}
    ${_unused_cli}
    --preset=${name}
    ${ARGN}
    )
  run_cmake(${name})
endfunction()

# Test CMakePresets.json errors
78
set(CMakePresets_SCHEMA_EXPECTED_RESULT 1)
79
run_cmake_presets(NoCMakePresets)
80
run_cmake_presets(Comment)
81 82 83 84 85 86 87
run_cmake_presets(JSONParseError)
run_cmake_presets(InvalidRoot)
run_cmake_presets(NoVersion)
run_cmake_presets(InvalidVersion)
run_cmake_presets(LowVersion)
run_cmake_presets(HighVersion)
run_cmake_presets(InvalidVendor)
88
set(CMakePresets_SCHEMA_EXPECTED_RESULT 0)
89
run_cmake_presets(NoPresets)
90
set(CMakePresets_SCHEMA_EXPECTED_RESULT 1)
91 92 93 94 95
run_cmake_presets(InvalidPresets)
run_cmake_presets(PresetNotObject)
run_cmake_presets(NoPresetName)
run_cmake_presets(InvalidPresetName)
run_cmake_presets(EmptyPresetName)
96
set(CMakePresets_SCHEMA_EXPECTED_RESULT 0)
97
run_cmake_presets(NoPresetGenerator)
98
set(CMakePresets_SCHEMA_EXPECTED_RESULT 1)
99
run_cmake_presets(InvalidPresetGenerator)
100
set(CMakePresets_SCHEMA_EXPECTED_RESULT 0)
101
run_cmake_presets(NoPresetBinaryDir)
102
set(CMakePresets_SCHEMA_EXPECTED_RESULT 1)
103 104 105 106 107 108 109 110 111
run_cmake_presets(InvalidPresetBinaryDir)
run_cmake_presets(InvalidVariables)
run_cmake_presets(VariableNotObject)
run_cmake_presets(NoVariableValue)
run_cmake_presets(InvalidVariableValue)
run_cmake_presets(ExtraRootField)
run_cmake_presets(ExtraPresetField)
run_cmake_presets(ExtraVariableField)
run_cmake_presets(InvalidPresetVendor)
112
set(CMakePresets_SCHEMA_EXPECTED_RESULT 0)
113 114 115 116 117 118 119
run_cmake_presets(DuplicatePresets)
run_cmake_presets(CyclicInheritance0)
run_cmake_presets(CyclicInheritance1)
run_cmake_presets(CyclicInheritance2)
run_cmake_presets(InvalidInheritance)
run_cmake_presets(ErrorNoWarningDev)
run_cmake_presets(ErrorNoWarningDeprecated)
120
set(CMakePresets_SCHEMA_EXPECTED_RESULT 1)
121 122 123 124
run_cmake_presets(InvalidArchitectureStrategy)
run_cmake_presets(UnknownArchitectureStrategy)
run_cmake_presets(InvalidToolsetStrategy)
run_cmake_presets(UnknownToolsetStrategy)
125 126
run_cmake_presets(EmptyCacheKey)
run_cmake_presets(EmptyEnvKey)
127 128 129 130 131 132 133
set(CMakePresets_SCHEMA_EXPECTED_RESULT 0)
run_cmake_presets(UnclosedMacro)
run_cmake_presets(NoSuchMacro)
run_cmake_presets(EnvCycle)
run_cmake_presets(EmptyEnv)
run_cmake_presets(EmptyPenv)
set(CMakePresets_SCHEMA_EXPECTED_RESULT 1)
134 135 136

# Test cmakeMinimumRequired field
run_cmake_presets(MinimumRequiredInvalid)
137
set(CMakePresets_SCHEMA_EXPECTED_RESULT 0)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
run_cmake_presets(MinimumRequiredEmpty)
run_cmake_presets(MinimumRequiredMajor)
run_cmake_presets(MinimumRequiredMinor)
run_cmake_presets(MinimumRequiredPatch)

# Test properly working CMakePresets.json
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/CMakePresets.json.in")
unset(ENV{TEST_ENV})
unset(ENV{TEST_ENV_REF})
unset(ENV{TEST_D_ENV_REF})
set(ENV{TEST_ENV_OVERRIDE} "This environment variable will be overridden")
set(ENV{TEST_PENV} "Process environment variable")
set(ENV{TEST_ENV_REF_PENV} "suffix")
run_cmake_presets(Good "-DTEST_OVERRIDE_1=Overridden value" "-DTEST_OVERRIDE_2:STRING=Overridden value" -C "${RunCMake_SOURCE_DIR}/CacheOverride.cmake" "-UTEST_UNDEF")
unset(ENV{TEST_ENV_OVERRIDE})
unset(ENV{TEST_PENV})
unset(ENV{TEST_ENV_REF_PENV})
run_cmake_presets(GoodNoArgs)
file(REMOVE_RECURSE ${RunCMake_BINARY_DIR}/GoodBinaryUp-build)
run_cmake_presets(GoodBinaryUp)
158
set(CMakePresets_SOURCE_ARG "../GoodBinaryRelative")
159
run_cmake_presets(GoodBinaryRelative)
160
unset(CMakePresets_SOURCE_ARG)
161 162 163 164 165 166 167 168 169 170 171 172 173
run_cmake_presets(GoodSpaces "--preset=Good Spaces")
if(WIN32)
  run_cmake_presets(GoodWindowsBackslash)
endif()
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/GoodBOM.json.in")
run_cmake_presets(GoodBOM)
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/CMakePresets.json.in")
file(REMOVE_RECURSE ${RunCMake_BINARY_DIR}/GoodBinaryCmdLine-build)
run_cmake_presets(GoodBinaryCmdLine -B ${RunCMake_BINARY_DIR}/GoodBinaryCmdLine-build)
run_cmake_presets(GoodGeneratorCmdLine -G ${RunCMake_GENERATOR})
run_cmake_presets(InvalidGeneratorCmdLine -G "Invalid Generator")
set(CMakePresets_NO_S_ARG TRUE)
run_cmake_presets(GoodNoS)
174 175 176 177 178 179 180 181
set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/GoodNoSCachePrep-build")
run_cmake_presets(GoodNoSCachePrep)
set(CMakePresets_SOURCE_ARG ".")
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_presets(GoodNoSCache)
unset(RunCMake_TEST_NO_CLEAN)
unset(CMakePresets_SOURCE_ARG)
unset(RunCMake_TEST_BINARY_DIR)
182
unset(CMakePresets_NO_S_ARG)
183 184 185 186 187 188 189
set(CMakePresets_NO_SOURCE_ARGS 1)
set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/GoodNoSourceArg")
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_presets(GoodNoSourceArg)
unset(RunCMake_TEST_NO_CLEAN)
unset(RunCMake_TEST_BINARY_DIR)
unset(CMakePresets_NO_SOURCE_ARGS)
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
run_cmake_presets(GoodInheritanceParent)
run_cmake_presets(GoodInheritanceChild)
run_cmake_presets(GoodInheritanceOverride)
run_cmake_presets(GoodInheritanceMulti)
run_cmake_presets(GoodInheritanceMultiSecond)
run_cmake_presets(GoodInheritanceMacro)

# Test bad preset arguments
run_cmake_presets(VendorMacro)
run_cmake_presets(InvalidGenerator)

# Test Visual Studio-specific stuff
if(RunCMake_GENERATOR MATCHES "^Visual Studio ")
  run_cmake_presets(VisualStudioGeneratorArch)
  run_cmake_presets(VisualStudioWin32)
  run_cmake_presets(VisualStudioWin64)
  run_cmake_presets(VisualStudioWin32Override -A x64)
  if(NOT RunCMake_GENERATOR STREQUAL "Visual Studio 9 2008")
    run_cmake_presets(VisualStudioToolset)
    run_cmake_presets(VisualStudioToolsetOverride -T "Test Toolset")
    run_cmake_presets(VisualStudioInheritanceParent)
    run_cmake_presets(VisualStudioInheritanceChild)
    run_cmake_presets(VisualStudioInheritanceOverride)
    run_cmake_presets(VisualStudioInheritanceMulti)
    run_cmake_presets(VisualStudioInheritanceMultiSecond)
  endif()
else()
217 218 219
  run_cmake_presets(ArchToolsetStrategyNone)
  run_cmake_presets(ArchToolsetStrategyDefault)
  run_cmake_presets(ArchToolsetStrategyIgnore)
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
endif()

# Test bad command line arguments
run_cmake_presets(NoSuchPreset)
run_cmake_presets(NoPresetArgument --preset=)
run_cmake_presets(UseHiddenPreset)

# Test CMakeUserPresets.json
unset(CMakePresets_FILE)
run_cmake_presets(GoodUserOnly)
run_cmake_presets(GoodUserFromMain)
run_cmake_presets(GoodUserFromUser)

# Test CMakeUserPresets.json errors
run_cmake_presets(UserDuplicateInUser)
run_cmake_presets(UserDuplicateCross)
run_cmake_presets(UserInheritance)

# Test listing presets
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/ListPresets.json.in")
run_cmake_presets(ListPresets --list-presets)

set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/ListPresetsWorkingDir")
set(RunCMake_TEST_NO_CLEAN 1)
set(CMakePresets_NO_SOURCE_ARGS 1)
run_cmake_presets(ListPresetsWorkingDir --list-presets)
unset(CMakePresets_NO_SOURCE_ARGS)
unset(RunCMake_TEST_NO_CLEAN)
unset(RunCMake_TEST_BINARY_DIR)

run_cmake_presets(ListPresetsNoSuchPreset)
run_cmake_presets(ListPresetsHidden)

# Test warning and error flags
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/Warnings.json.in")
set(CMakePresets_WARN_UNUSED_CLI 1)
run_cmake_presets(NoWarningFlags)
run_cmake_presets(WarningFlags)
run_cmake_presets(DisableWarningFlags)
run_cmake_presets(ErrorDev)
run_cmake_presets(ErrorDeprecated)
unset(CMakePresets_WARN_UNUSED_CLI)

# Test debug
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/Debug.json.in")
run_cmake_presets(NoDebug)
run_cmake_presets(Debug)
267 268 269 270 271 272 273

# Test the example from the documentation
file(READ "${RunCMake_SOURCE_DIR}/../../../Help/manual/presets/example.json" _example)
string(REPLACE "\"generator\": \"Ninja\"" "\"generator\": \"@RunCMake_GENERATOR@\"" _example "${_example}")
file(WRITE "${RunCMake_BINARY_DIR}/example.json.in" "${_example}")
set(CMakePresets_FILE "${RunCMake_BINARY_DIR}/example.json.in")
run_cmake_presets(DocumentationExample --preset=default)