Browse Source

Ninja Multi-Config: Deduplicate compile_commands.json for cross configs

compile_commands.json was being written for every permutation of
cross configurations. Deduplicate so only one command is output
for each configuration.

Fixes: #23733
Kyle Edwards 3 years ago
parent
commit
a186c1aff6

+ 5 - 3
Source/cmNinjaTargetGenerator.cxx

@@ -1343,9 +1343,11 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement(
     }
   }
 
-  this->ExportObjectCompileCommand(
-    language, sourceFilePath, objectDir, objectFileName, objectFileDir,
-    vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"], config);
+  if (firstForConfig) {
+    this->ExportObjectCompileCommand(
+      language, sourceFilePath, objectDir, objectFileName, objectFileDir,
+      vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"], config);
+  }
 
   objBuild.Outputs.push_back(objectFileName);
   if (firstForConfig) {

+ 28 - 0
Tests/RunCMake/NinjaMultiConfig/CompileCommands-check.cmake

@@ -0,0 +1,28 @@
+set(expected_compile_commands
+[==[^\[
+{
+  "directory": "[^
+]*(/Tests/RunCMake/NinjaMultiConfig/CompileCommands-build|\\\\Tests\\\\RunCMake\\\\NinjaMultiConfig\\\\CompileCommands-build)",
+  "command": "[^
+]*Debug[^
+]*",
+  "file": "[^
+]*(/Tests/RunCMake/NinjaMultiConfig/main\.c|\\\\Tests\\\\RunCMake\\\\NinjaMultiConfig\\\\main\.c)"
+},
+{
+  "directory": "[^
+]*(/Tests/RunCMake/NinjaMultiConfig/CompileCommands-build|\\\\Tests\\\\RunCMake\\\\NinjaMultiConfig\\\\CompileCommands-build)",
+  "command": "[^
+]*Release[^
+]*",
+  "file": "[^
+]*(/Tests/RunCMake/NinjaMultiConfig/main\.c|\\\\Tests\\\\RunCMake\\\\NinjaMultiConfig\\\\main\.c)"
+}
+]$]==])
+
+file(READ "${RunCMake_TEST_BINARY_DIR}/compile_commands.json" actual_compile_commands)
+if(NOT actual_compile_commands MATCHES "${expected_compile_commands}")
+  string(REPLACE "\n" "\n  " expected_compile_commands_formatted "${expected_compile_commands}")
+  string(REPLACE "\n" "\n  " actual_compile_commands_formatted "${actual_compile_commands}")
+  string(APPEND RunCMake_TEST_FAILED "Expected compile_commands.json to match:\n  ${expected_compile_commands_formatted}\nActual compile_commands.json:\n  ${actual_compile_commands_formatted}\n")
+endif()

+ 3 - 0
Tests/RunCMake/NinjaMultiConfig/CompileCommands.cmake

@@ -0,0 +1,3 @@
+enable_language(C)
+
+add_executable(exe main.c)

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

@@ -453,6 +453,11 @@ run_cmake_command(NoUnusedVariables ${CMAKE_COMMAND} ${CMAKE_CURRENT_LIST_DIR}
   "-DCMAKE_DEFAULT_BUILD_TYPE=Debug"
   "-DCMAKE_DEFAULT_CONFIGS=all"
   )
+unset(RunCMake_TEST_BINARY_DIR)
+
+set(RunCMake_TEST_OPTIONS "-DCMAKE_CONFIGURATION_TYPES=Debug\\;Release;-DCMAKE_CROSS_CONFIGS=all;-DCMAKE_EXPORT_COMPILE_COMMANDS=ON")
+run_cmake(CompileCommands)
+unset(RunCMake_TEST_OPTIONS)
 
 # CudaSimple uses separable compilation, which is currently only supported on NVCC.
 if(CMake_TEST_CUDA)