Ninja Generators: Homogenize configuration with Makefiles

* Use same configuration variables to configure dependencies
* Abstract Ninja deps format from compiler one
This commit is contained in:
Marc Chevrier
2020-12-01 14:05:06 +01:00
parent a22a8f1e36
commit f8d8faff8d
6 changed files with 78 additions and 47 deletions

View File

@@ -1,11 +1,27 @@
include(Platform/Windows-Intel)
__windows_compiler_intel(C)
set(CMAKE_NINJA_DEPTYPE_C intel) # special value handled by CMake
set(CMAKE_DEPFILE_FLAGS_C "-QMMD -QMT <DEP_TARGET> -QMF <DEP_FILE>")
set(CMAKE_C_DEPFILE_FORMAT gcc)
if(CMAKE_GENERATOR MATCHES "^Ninja")
if(_CMAKE_NINJA_VERSION VERSION_LESS 1.9)
# This ninja version is too old to support the Intel depfile format.
# Fall back to msvc depfile format.
set(CMAKE_DEPFILE_FLAGS_C "/showIncludes")
set(CMAKE_C_DEPFILE_FORMAT msvc)
endif()
endif()
if((NOT DEFINED CMAKE_DEPENDS_USE_COMPILER OR CMAKE_DEPENDS_USE_COMPILER)
AND CMAKE_GENERATOR MATCHES "Makefiles|WMake")
# dependencies are computed by the compiler itself
set(CMAKE_C_DEPFILE_FORMAT gcc)
set(CMAKE_C_DEPENDS_USE_COMPILER TRUE)
endif()
if("${CMAKE_SOURCE_DIR}${CMAKE_BINARY_DIR}" MATCHES " ")
# The Intel compiler does not properly escape spaces in a depfile.
# Fall back to msvc depfile format.
set(CMAKE_DEPFILE_FLAGS_C "/showIncludes")
set(CMAKE_C_DEPFILE_FORMAT msvc)
endif()

View File

@@ -1,12 +1,28 @@
include(Platform/Windows-Intel)
set(_COMPILE_CXX " /TP")
__windows_compiler_intel(CXX)
set(CMAKE_NINJA_DEPTYPE_CXX intel) # special value handled by CMake
set(CMAKE_DEPFILE_FLAGS_CXX "-QMMD -QMT <DEP_TARGET> -QMF <DEP_FILE>")
set(CMAKE_CXX_DEPFILE_FORMAT gcc)
if(CMAKE_GENERATOR MATCHES "^Ninja")
if(_CMAKE_NINJA_VERSION VERSION_LESS 1.9)
# This ninja version is too old to support the Intel depfile format.
# Fall back to msvc depfile format.
set(CMAKE_DEPFILE_FLAGS_CXX "/showIncludes")
set(CMAKE_CXX_DEPFILE_FORMAT msvc)
endif()
endif()
if((NOT DEFINED CMAKE_DEPENDS_USE_COMPILER OR CMAKE_DEPENDS_USE_COMPILER)
AND CMAKE_GENERATOR MATCHES "Makefiles|WMake")
# dependencies are computed by the compiler itself
set(CMAKE_CXX_DEPFILE_FORMAT gcc)
set(CMAKE_CXX_DEPENDS_USE_COMPILER TRUE)
set(CMAKE_CXX_DEPENDS_USE_COMPILER TRUE)
endif()
if("${CMAKE_SOURCE_DIR}${CMAKE_BINARY_DIR}" MATCHES " ")
# The Intel compiler does not properly escape spaces in a depfile.
# Fall back to msvc depfile format.
set(CMAKE_DEPFILE_FLAGS_CXX "/showIncludes")
set(CMAKE_CXX_DEPFILE_FORMAT msvc)
endif()

View File

@@ -8,6 +8,20 @@ if(__WINDOWS_INTEL)
endif()
set(__WINDOWS_INTEL 1)
if (CMAKE_GENERATOR MATCHES "^Ninja")
# retrieve ninja version to enable dependencies configuration
# against Ninja capabilities
execute_process(COMMAND "${CMAKE_MAKE_PROGRAM}" --version
RESULT_VARIABLE _CMAKE_NINJA_RESULT
OUTPUT_VARIABLE _CMAKE_NINJA_VERSION
ERROR_VARIABLE _CMAKE_NINJA_VERSION)
if (NOT _CMAKE_NINJA_RESULT AND _CMAKE_NINJA_VERSION MATCHES "[0-9]+(\\.[0-9]+)*")
set (_CMAKE_NINJA_VERSION "${CMAKE_MATCH_0}")
endif()
unset(_CMAKE_NINJA_RESULT)
endif()
include(Platform/Windows-MSVC)
macro(__windows_compiler_intel lang)
__windows_compiler_msvc(${lang})

View File

@@ -435,11 +435,10 @@ macro(__windows_compiler_msvc lang)
set(CMAKE_${lang}_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL -MDd)
endif()
set(CMAKE_${lang}_LINKER_SUPPORTS_PDB ON)
set(CMAKE_NINJA_DEPTYPE_${lang} msvc)
__windows_compiler_msvc_enable_rc("${_PLATFORM_DEFINES} ${_PLATFORM_DEFINES_${lang}}")
# define generic information about compiler dependencies
# activation is done on per language platform configuration basis
if (MSVC_VERSION GREATER 1300)
set(CMAKE_DEPFILE_FLAGS_${lang} "/showIncludes")
set(CMAKE_${lang}_DEPFILE_FORMAT msvc)

View File

@@ -12,7 +12,9 @@
#include <utility>
#include <cm/memory>
#include <cm/string_view>
#include <cmext/algorithm>
#include <cmext/string_view>
#include <cm3p/json/value.h>
#include <cm3p/json/writer.h>
@@ -245,32 +247,6 @@ void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
}
bool cmNinjaTargetGenerator::NeedDepTypeMSVC(const std::string& lang) const
{
std::string const& deptype = this->GetMakefile()->GetSafeDefinition(
cmStrCat("CMAKE_NINJA_DEPTYPE_", lang));
if (deptype == "msvc") {
return true;
}
if (deptype == "intel") {
// Ninja does not really define "intel", but we use it to switch based
// on whether this environment supports "gcc" or "msvc" deptype.
if (!this->GetGlobalGenerator()->SupportsMultilineDepfile()) {
// This ninja version is too old to support the Intel depfile format.
// Fall back to msvc deptype.
return true;
}
if ((this->Makefile->GetHomeDirectory().find(' ') != std::string::npos) ||
(this->Makefile->GetHomeOutputDirectory().find(' ') !=
std::string::npos)) {
// The Intel compiler does not properly escape spaces in a depfile.
// Fall back to msvc deptype.
return true;
}
}
return false;
}
// TODO: Refactor with
// void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
@@ -727,10 +703,6 @@ void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang,
std::string cldeps;
if (!compilationPreprocesses) {
// The compiler will not do preprocessing, so it has no such dependencies.
} else if (this->NeedDepTypeMSVC(lang)) {
rule.DepType = "msvc";
rule.DepFile.clear();
flags += " /showIncludes";
} else if (mf->IsOn(cmStrCat("CMAKE_NINJA_CMCLDEPS_", lang))) {
// For the MS resource compiler we need cmcldeps, but skip dependencies
// for source-file try_compile cases because they are always fresh.
@@ -746,16 +718,23 @@ void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang,
"\" \"", cl, "\" ");
}
} else {
rule.DepType = "gcc";
rule.DepFile = "$DEP_FILE";
const auto& depType = this->GetMakefile()->GetSafeDefinition(
cmStrCat("CMAKE_", lang, "_DEPFILE_FORMAT"));
if (depType == "msvc"_s) {
rule.DepType = "msvc";
rule.DepFile.clear();
} else {
rule.DepType = "gcc";
rule.DepFile = "$DEP_FILE";
}
vars.DependencyFile = rule.DepFile.c_str();
vars.DependencyTarget = "$out";
const std::string flagsName = cmStrCat("CMAKE_DEPFILE_FLAGS_", lang);
std::string depfileFlags = mf->GetSafeDefinition(flagsName);
if (!depfileFlags.empty()) {
cmSystemTools::ReplaceString(depfileFlags, "<DEP_FILE>", "$DEP_FILE");
cmSystemTools::ReplaceString(depfileFlags, "<DEP_TARGET>", "$out");
cmSystemTools::ReplaceString(
depfileFlags, "<CMAKE_C_COMPILER>",
cmToCStr(mf->GetDefinition("CMAKE_C_COMPILER")));
rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
depfileFlags, vars);
flags += cmStrCat(' ', depfileFlags);
}
}
@@ -875,6 +854,14 @@ void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang,
compileCmds.front().insert(0, cldeps);
}
const auto& extraCommands = this->GetMakefile()->GetSafeDefinition(
cmStrCat("CMAKE_", lang, "_DEPENDS_EXTRA_COMMANDS"));
if (!extraCommands.empty()) {
auto commandList = cmExpandedList(extraCommands);
compileCmds.insert(compileCmds.end(), commandList.cbegin(),
commandList.cend());
}
for (std::string& i : compileCmds) {
i = cmStrCat(launcher, i);
rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
@@ -1161,7 +1148,8 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement(
vars["DEFINES"] = this->ComputeDefines(source, language, config);
vars["INCLUDES"] = this->ComputeIncludes(source, language, config);
if (!this->NeedDepTypeMSVC(language)) {
if (this->GetMakefile()->GetSafeDefinition(
cmStrCat("CMAKE_", language, "_DEPFILE_FORMAT")) != "msvc"_s) {
bool replaceExt(false);
if (!language.empty()) {
std::string repVar =

View File

@@ -42,8 +42,6 @@ public:
std::string GetTargetName() const;
bool NeedDepTypeMSVC(const std::string& lang) const;
protected:
bool SetMsvcTargetPdbVariable(cmNinjaVars&, const std::string& config) const;