Browse Source

CMakePresets.json: Fix expansion issue with empty binaryDir

When resolving binaryDir into a full path from a relative path, we
forgot to check if binaryDir is altogether empty, causing empty
binaryDir's to resolve to the source directory. Fix this.

Fixes: #22434
Kyle Edwards 4 years ago
parent
commit
aa874dc609

+ 6 - 4
Source/cmCMakePresetsFile.cxx

@@ -197,11 +197,13 @@ bool ExpandMacros(const cmCMakePresetsFile& file,
   std::string binaryDir = preset.BinaryDir;
   CHECK_EXPAND(out, binaryDir, macroExpanders, file.GetVersion(preset))
 
-  if (!cmSystemTools::FileIsFullPath(binaryDir)) {
-    binaryDir = cmStrCat(file.SourceDir, '/', binaryDir);
+  if (!binaryDir.empty()) {
+    if (!cmSystemTools::FileIsFullPath(binaryDir)) {
+      binaryDir = cmStrCat(file.SourceDir, '/', binaryDir);
+    }
+    out->BinaryDir = cmSystemTools::CollapseFullPath(binaryDir);
+    cmSystemTools::ConvertToUnixSlashes(out->BinaryDir);
   }
-  out->BinaryDir = cmSystemTools::CollapseFullPath(binaryDir);
-  cmSystemTools::ConvertToUnixSlashes(out->BinaryDir);
 
   if (!preset.InstallDir.empty()) {
     std::string installDir = preset.InstallDir;

+ 4 - 0
Tests/RunCMake/CMakePresets/OptionalBinaryDirFieldNoS.cmake

@@ -0,0 +1,4 @@
+include(${CMAKE_CURRENT_LIST_DIR}/TestVariable.cmake)
+
+get_filename_component(_parent_dir "${CMAKE_SOURCE_DIR}" DIRECTORY)
+test_variable(CMAKE_BINARY_DIR "" "${_parent_dir}/OptionalBinaryDirFieldNoS-build")

+ 9 - 0
Tests/RunCMake/CMakePresets/OptionalBinaryDirFieldNoS.json.in

@@ -0,0 +1,9 @@
+{
+  "version": 3,
+  "configurePresets": [
+    {
+      "name": "OptionalBinaryDirFieldNoS",
+      "generator": "@RunCMake_GENERATOR@"
+    }
+  ]
+}

+ 5 - 0
Tests/RunCMake/CMakePresets/RunCMakeTest.cmake

@@ -312,6 +312,11 @@ unset(CMakePresets_FILE)
 # Test optional generator and buildDir fields
 run_cmake_presets(OptionalBinaryDirField -B "${RunCMake_BINARY_DIR}/OptionalBinaryDirField/build")
 run_cmake_presets(OptionalGeneratorField -G "${RunCMake_GENERATOR}")
+set(CMakePresets_NO_S_ARG TRUE)
+set(CMakePresets_SOURCE_ARG "../OptionalBinaryDirFieldNoS")
+run_cmake_presets(OptionalBinaryDirFieldNoS)
+unset(CMakePresets_SOURCE_ARG)
+unset(CMakePresets_NO_S_ARG)
 
 # Test the example from the documentation
 file(READ "${RunCMake_SOURCE_DIR}/../../../Help/manual/presets/example.json" _example)