Browse Source

EXPORT_COMPILE_COMMANDS: add `output` field

Also write for all configurations from multi-config generators.

This field was added in the Clang 5 documentation and not present in the
Clang 4 documentation (sometime between Dec 2016 and Mar 2017 according
to `web.archive.org`).
Ben Boeckel 3 years ago
parent
commit
887a8874c3

+ 7 - 0
Help/release/dev/compile-commands-output-field.rst

@@ -0,0 +1,7 @@
+compile-commands-output-field
+-----------------------------
+
+* The :prop_tgt:`EXPORT_COMPILE_COMMANDS` target property will now have the
+  ``output`` field in the compile commands objects. This allows multi-config
+  generators (namely :generator:`Ninja Multi-Config` generator) to contain the
+  compile commands for all configurations.

+ 5 - 2
Source/cmGlobalNinjaGenerator.cxx

@@ -1171,7 +1171,8 @@ void cmGlobalNinjaGenerator::AddAdditionalCleanFile(std::string fileName,
 }
 
 void cmGlobalNinjaGenerator::AddCXXCompileCommand(
-  const std::string& commandLine, const std::string& sourceFile)
+  const std::string& commandLine, const std::string& sourceFile,
+  const std::string& objPath)
 {
   // Compute Ninja's build file path.
   std::string buildFileDir =
@@ -1205,7 +1206,9 @@ void cmGlobalNinjaGenerator::AddCXXCompileCommand(
      << R"(  "command": ")"
      << cmGlobalGenerator::EscapeJSON(commandLine) << "\",\n"
      << R"(  "file": ")"
-     << cmGlobalGenerator::EscapeJSON(sourceFileName) << "\"\n"
+     << cmGlobalGenerator::EscapeJSON(sourceFileName) << "\",\n"
+     << R"(  "output": ")"
+     << cmGlobalGenerator::EscapeJSON(objPath) << "\"\n"
      << "}";
   /* clang-format on */
 }

+ 2 - 1
Source/cmGlobalNinjaGenerator.h

@@ -292,7 +292,8 @@ public:
   }
 
   void AddCXXCompileCommand(const std::string& commandLine,
-                            const std::string& sourceFile);
+                            const std::string& sourceFile,
+                            const std::string& objPath);
 
   /**
    * Add a rule to the generated build system.

+ 4 - 2
Source/cmGlobalUnixMakefileGenerator3.cxx

@@ -143,7 +143,7 @@ void cmGlobalUnixMakefileGenerator3::Generate()
 
 void cmGlobalUnixMakefileGenerator3::AddCXXCompileCommand(
   const std::string& sourceFile, const std::string& workingDirectory,
-  const std::string& compileCommand)
+  const std::string& compileCommand, const std::string& objPath)
 {
   if (!this->CommandDatabase) {
     std::string commandDatabaseName =
@@ -164,7 +164,9 @@ void cmGlobalUnixMakefileGenerator3::AddCXXCompileCommand(
                          << "\",\n"
                          << R"(  "file": ")"
                          << cmGlobalGenerator::EscapeJSON(sourceFile)
-                         << "\"\n}";
+                         << "\",\n"
+                         << R"(  "output": ")"
+                         << cmGlobalGenerator::EscapeJSON(objPath) << "\"\n}";
 }
 
 void cmGlobalUnixMakefileGenerator3::WriteMainMakefile2()

+ 2 - 1
Source/cmGlobalUnixMakefileGenerator3.h

@@ -174,7 +174,8 @@ public:
 
   void AddCXXCompileCommand(const std::string& sourceFile,
                             const std::string& workingDirectory,
-                            const std::string& compileCommand);
+                            const std::string& compileCommand,
+                            const std::string& objPath);
 
   /** Does the make tool tolerate .NOTPARALLEL? */
   virtual bool AllowNotParallel() const { return true; }

+ 1 - 1
Source/cmMakefileTargetGenerator.cxx

@@ -1031,7 +1031,7 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles(
       }
 
       this->GlobalGenerator->AddCXXCompileCommand(
-        source.GetFullPath(), workingDirectory, compileCommand);
+        source.GetFullPath(), workingDirectory, compileCommand, relativeObj);
     }
 
     // See if we need to use a compiler launcher like ccache or distcc

+ 2 - 1
Source/cmNinjaTargetGenerator.cxx

@@ -1998,7 +1998,8 @@ void cmNinjaTargetGenerator::ExportObjectCompileCommand(
   std::string cmdLine = this->GetLocalGenerator()->BuildCommandLine(
     compileCmds, outputConfig, outputConfig);
 
-  this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName);
+  this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName,
+                                                   objectFileName);
 }
 
 void cmNinjaTargetGenerator::AdditionalCleanFiles(const std::string& config)

+ 4 - 2
Tests/RunCMake/NinjaMultiConfig/CompileCommands-check.cmake

@@ -7,7 +7,8 @@ set(expected_compile_commands
 ]*Debug[^
 ]*",
   "file": "[^
-]*(/Tests/RunCMake/NinjaMultiConfig/main\.c|\\\\Tests\\\\RunCMake\\\\NinjaMultiConfig\\\\main\.c)"
+]*(/Tests/RunCMake/NinjaMultiConfig/main\.c|\\\\Tests\\\\RunCMake\\\\NinjaMultiConfig\\\\main\.c)",
+  "output": "(CMakeFiles/exe\.dir/Debug/main\.c\.o|CMakeFiles\\\\exe\.dir\\\\Debug\\\\main\.c\.obj)"
 },
 {
   "directory": "[^
@@ -16,7 +17,8 @@ set(expected_compile_commands
 ]*Release[^
 ]*",
   "file": "[^
-]*(/Tests/RunCMake/NinjaMultiConfig/main\.c|\\\\Tests\\\\RunCMake\\\\NinjaMultiConfig\\\\main\.c)"
+]*(/Tests/RunCMake/NinjaMultiConfig/main\.c|\\\\Tests\\\\RunCMake\\\\NinjaMultiConfig\\\\main\.c)",
+  "output": "(CMakeFiles/exe\.dir/Release/main\.c\.o|CMakeFiles\\\\exe\.dir\\\\Release\\\\main\.c\.obj)"
 }
 ]$]==])