diff options
author | Stephen Kelly <steveire@gmail.com> | 2012-05-09 21:47:37 (GMT) |
---|---|---|
committer | Stephen Kelly <steveire@gmail.com> | 2012-06-04 21:42:37 (GMT) |
commit | db839bec7d076b54c5e9ad0d19386a26557a509e (patch) | |
tree | a9ad4797209348cc105d19e01eacf5c25ddff8bf | |
parent | 8778357898327c1e4893d8dc140fc0e63331e36b (diff) | |
download | CMake-db839bec7d076b54c5e9ad0d19386a26557a509e.zip CMake-db839bec7d076b54c5e9ad0d19386a26557a509e.tar.gz CMake-db839bec7d076b54c5e9ad0d19386a26557a509e.tar.bz2 |
Make the CMAKE_EXPORT_COMPILE_COMMANDS option work with Ninja.
-rw-r--r-- | Modules/CMakeGenericSystem.cmake | 6 | ||||
-rw-r--r-- | Source/cmGlobalNinjaGenerator.cxx | 42 | ||||
-rw-r--r-- | Source/cmGlobalNinjaGenerator.h | 6 | ||||
-rw-r--r-- | Source/cmNinjaTargetGenerator.cxx | 30 | ||||
-rw-r--r-- | Tests/CMakeLists.txt | 2 |
5 files changed, 85 insertions, 1 deletions
diff --git a/Modules/CMakeGenericSystem.cmake b/Modules/CMakeGenericSystem.cmake index c733969..0ccbfac 100644 --- a/Modules/CMakeGenericSystem.cmake +++ b/Modules/CMakeGenericSystem.cmake @@ -60,6 +60,12 @@ IF(CMAKE_GENERATOR MATCHES "Makefiles") ENDIF(CMAKE_GENERATOR MATCHES "Unix Makefiles") ENDIF(CMAKE_GENERATOR MATCHES "Makefiles") +IF(CMAKE_GENERATOR MATCHES "Ninja") + SET(CMAKE_EXPORT_COMPILE_COMMANDS OFF CACHE BOOL + "Enable/Disable output of compile commands during generation." + ) + MARK_AS_ADVANCED(CMAKE_EXPORT_COMPILE_COMMANDS) +ENDIF(CMAKE_GENERATOR MATCHES "Ninja") # GetDefaultWindowsPrefixBase # diff --git a/Source/cmGlobalNinjaGenerator.cxx b/Source/cmGlobalNinjaGenerator.cxx index 328706c..0e89fab 100644 --- a/Source/cmGlobalNinjaGenerator.cxx +++ b/Source/cmGlobalNinjaGenerator.cxx @@ -341,6 +341,7 @@ cmGlobalNinjaGenerator::cmGlobalNinjaGenerator() : cmGlobalGenerator() , BuildFileStream(0) , RulesFileStream(0) + , CompileCommandsStream(0) , Rules() , AllDependencies() { @@ -390,6 +391,7 @@ void cmGlobalNinjaGenerator::Generate() this->BuildFileStream->setstate(std::ios_base::failbit); } + this->CloseCompileCommandsStream(); this->CloseRulesFileStream(); this->CloseBuildFileStream(); } @@ -623,6 +625,46 @@ void cmGlobalNinjaGenerator::CloseRulesFileStream() } } +void cmGlobalNinjaGenerator::AddCXXCompileCommand( + const std::string &commandLine, + const std::string &sourceFile) +{ + // Compute Ninja's build file path. + std::string buildFileDir = + this->GetCMakeInstance()->GetHomeOutputDirectory(); + if (!this->CompileCommandsStream) + { + std::string buildFilePath = buildFileDir + "/compile_commands.json"; + + // Get a stream where to generate things. + this->CompileCommandsStream = + new cmGeneratedFileStream(buildFilePath.c_str()); + *this->CompileCommandsStream << "["; + } else { + *this->CompileCommandsStream << "," << std::endl; + } + + *this->CompileCommandsStream << "\n{\n" + << " \"directory\": \"" + << cmGlobalGenerator::EscapeJSON(buildFileDir) << "\",\n" + << " \"command\": \"" + << cmGlobalGenerator::EscapeJSON(commandLine) << "\",\n" + << " \"file\": \"" + << cmGlobalGenerator::EscapeJSON(sourceFile) << "\"\n" + << "}"; +} + +void cmGlobalNinjaGenerator::CloseCompileCommandsStream() +{ + if (this->CompileCommandsStream) + { + *this->CompileCommandsStream << "\n]"; + delete this->CompileCommandsStream; + this->CompileCommandsStream = 0; + } + +} + void cmGlobalNinjaGenerator::WriteDisclaimer(std::ostream& os) { os diff --git a/Source/cmGlobalNinjaGenerator.h b/Source/cmGlobalNinjaGenerator.h index e652972..7b6b9b7 100644 --- a/Source/cmGlobalNinjaGenerator.h +++ b/Source/cmGlobalNinjaGenerator.h @@ -213,6 +213,9 @@ public: cmGeneratedFileStream* GetRulesFileStream() const { return this->RulesFileStream; } + void AddCXXCompileCommand(const std::string &commandLine, + const std::string &sourceFile); + /** * Add a rule to the generated build system. * Call WriteRule() behind the scene but perform some check before like: @@ -254,6 +257,8 @@ private: void OpenBuildFileStream(); void CloseBuildFileStream(); + void CloseCompileCommandsStream(); + void OpenRulesFileStream(); void CloseRulesFileStream(); @@ -311,6 +316,7 @@ private: /// The file containing the rule statements. (The action attached to each /// edge of the compilation DAG). cmGeneratedFileStream* RulesFileStream; + cmGeneratedFileStream* CompileCommandsStream; /// The type used to store the set of rules added to the generated build /// system. diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx index 80007f1..7c58154 100644 --- a/Source/cmNinjaTargetGenerator.cxx +++ b/Source/cmNinjaTargetGenerator.cxx @@ -487,6 +487,36 @@ cmNinjaTargetGenerator vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat( this->GetTargetPDB().c_str(), cmLocalGenerator::SHELL); + if(this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) + { + cmLocalGenerator::RuleVariables compileObjectVars; + std::string lang = language; + compileObjectVars.Language = lang.c_str(); + compileObjectVars.Source = sourceFileName.c_str(); + compileObjectVars.Object = objectFileName.c_str(); + compileObjectVars.Flags = vars["FLAGS"].c_str(); + compileObjectVars.Defines = vars["DEFINES"].c_str(); + + // Rule for compiling object file. + std::string compileCmdVar = "CMAKE_"; + compileCmdVar += language; + compileCmdVar += "_COMPILE_OBJECT"; + std::string compileCmd = + this->GetMakefile()->GetRequiredDefinition(compileCmdVar.c_str()); + std::vector<std::string> compileCmds; + cmSystemTools::ExpandListArgument(compileCmd, compileCmds); + + for (std::vector<std::string>::iterator i = compileCmds.begin(); + i != compileCmds.end(); ++i) + this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars); + + std::string cmdLine = + this->GetLocalGenerator()->BuildCommandLine(compileCmds); + + this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, + sourceFileName); + } + cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(), comment, rule, diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 5972c19..ffa5bcb 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -47,7 +47,7 @@ CONFIGURE_FILE(${CMake_SOURCE_DIR}/Tests/EnforceConfig.cmake.in # Testing IF(BUILD_TESTING) - IF("${CMAKE_TEST_GENERATOR}" MATCHES "Unix Makefiles") + IF("${CMAKE_TEST_GENERATOR}" MATCHES "Unix Makefiles" OR "${CMAKE_TEST_GENERATOR}" MATCHES Ninja) SET(TEST_CompileCommandOutput 1) ENDIF() |