cmGlobalGenerator: Refactor global target construction

Avoid using partially-constructed cmTarget instances.  Collect the
information about how to construct each target in a separate structure
and then actually create each cmTarget with full construction.
This commit is contained in:
Brad King 2016-09-15 15:56:49 -04:00
parent 916d84450d
commit fa3897b24e
2 changed files with 119 additions and 120 deletions

View File

@ -1060,16 +1060,16 @@ void cmGlobalGenerator::Configure()
this->ConfigureDoneCMP0026AndCMP0024 = true; this->ConfigureDoneCMP0026AndCMP0024 = true;
// Put a copy of each global target in every directory. // Put a copy of each global target in every directory.
cmTargets globalTargets; std::vector<GlobalTargetInfo> globalTargets;
this->CreateDefaultGlobalTargets(&globalTargets); this->CreateDefaultGlobalTargets(globalTargets);
for (unsigned int i = 0; i < this->Makefiles.size(); ++i) { for (unsigned int i = 0; i < this->Makefiles.size(); ++i) {
cmMakefile* mf = this->Makefiles[i]; cmMakefile* mf = this->Makefiles[i];
cmTargets* targets = &(mf->GetTargets()); cmTargets* targets = &(mf->GetTargets());
cmTargets::iterator tit; for (std::vector<GlobalTargetInfo>::iterator gti = globalTargets.begin();
for (tit = globalTargets.begin(); tit != globalTargets.end(); ++tit) { gti != globalTargets.end(); ++gti) {
targets->insert( targets->insert(
cmTargets::value_type(tit->first, tit->second.CopyForDirectory(mf))); cmTargets::value_type(gti->Name, this->CreateGlobalTarget(*gti, mf)));
} }
} }
@ -2069,7 +2069,8 @@ inline std::string removeQuotes(const std::string& s)
return s; return s;
} }
void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets) void cmGlobalGenerator::CreateDefaultGlobalTargets(
std::vector<GlobalTargetInfo>& targets)
{ {
this->AddGlobalTarget_Package(targets); this->AddGlobalTarget_Package(targets);
this->AddGlobalTarget_PackageSource(targets); this->AddGlobalTarget_PackageSource(targets);
@ -2079,13 +2080,16 @@ void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
this->AddGlobalTarget_Install(targets); this->AddGlobalTarget_Install(targets);
} }
void cmGlobalGenerator::AddGlobalTarget_Package(cmTargets* targets) void cmGlobalGenerator::AddGlobalTarget_Package(
std::vector<GlobalTargetInfo>& targets)
{ {
cmMakefile* mf = this->Makefiles[0]; cmMakefile* mf = this->Makefiles[0];
const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir(); const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir();
std::string workingDir = mf->GetCurrentBinaryDirectory(); GlobalTargetInfo gti;
cmCustomCommandLines cpackCommandLines; gti.Name = this->GetPackageTargetName();
std::vector<std::string> depends; gti.Message = "Run CPack packaging tool...";
gti.UsesTerminal = true;
gti.WorkingDir = mf->GetCurrentBinaryDirectory();
cmCustomCommandLine singleLine; cmCustomCommandLine singleLine;
singleLine.push_back(cmSystemTools::GetCPackCommand()); singleLine.push_back(cmSystemTools::GetCPackCommand());
if (cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.') { if (cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.') {
@ -2097,34 +2101,32 @@ void cmGlobalGenerator::AddGlobalTarget_Package(cmTargets* targets)
configFile += "/CPackConfig.cmake"; configFile += "/CPackConfig.cmake";
std::string relConfigFile = "./CPackConfig.cmake"; std::string relConfigFile = "./CPackConfig.cmake";
singleLine.push_back(relConfigFile); singleLine.push_back(relConfigFile);
cpackCommandLines.push_back(singleLine); gti.CommandLines.push_back(singleLine);
if (this->GetPreinstallTargetName()) { if (this->GetPreinstallTargetName()) {
depends.push_back(this->GetPreinstallTargetName()); gti.Depends.push_back(this->GetPreinstallTargetName());
} else { } else {
const char* noPackageAll = const char* noPackageAll =
mf->GetDefinition("CMAKE_SKIP_PACKAGE_ALL_DEPENDENCY"); mf->GetDefinition("CMAKE_SKIP_PACKAGE_ALL_DEPENDENCY");
if (!noPackageAll || cmSystemTools::IsOff(noPackageAll)) { if (!noPackageAll || cmSystemTools::IsOff(noPackageAll)) {
depends.push_back(this->GetAllTargetName()); gti.Depends.push_back(this->GetAllTargetName());
} }
} }
if (cmSystemTools::FileExists(configFile.c_str())) { if (cmSystemTools::FileExists(configFile.c_str())) {
targets->insert(cmTargets::value_type( targets.push_back(gti);
this->GetPackageTargetName(),
this->CreateGlobalTarget(this->GetPackageTargetName(),
"Run CPack packaging tool...",
&cpackCommandLines, depends, workingDir.c_str(),
/*uses_terminal*/ true)));
} }
} }
void cmGlobalGenerator::AddGlobalTarget_PackageSource(cmTargets* targets) void cmGlobalGenerator::AddGlobalTarget_PackageSource(
std::vector<GlobalTargetInfo>& targets)
{ {
cmMakefile* mf = this->Makefiles[0]; cmMakefile* mf = this->Makefiles[0];
const char* packageSourceTargetName = this->GetPackageSourceTargetName(); const char* packageSourceTargetName = this->GetPackageSourceTargetName();
if (packageSourceTargetName) { if (packageSourceTargetName) {
std::string workingDir = mf->GetCurrentBinaryDirectory(); GlobalTargetInfo gti;
cmCustomCommandLines cpackCommandLines; gti.Name = packageSourceTargetName;
std::vector<std::string> depends; gti.Message = "Run CPack packaging tool for source...";
gti.WorkingDir = mf->GetCurrentBinaryDirectory();
gti.UsesTerminal = true;
cmCustomCommandLine singleLine; cmCustomCommandLine singleLine;
singleLine.push_back(cmSystemTools::GetCPackCommand()); singleLine.push_back(cmSystemTools::GetCPackCommand());
singleLine.push_back("--config"); singleLine.push_back("--config");
@ -2134,24 +2136,22 @@ void cmGlobalGenerator::AddGlobalTarget_PackageSource(cmTargets* targets)
singleLine.push_back(relConfigFile); singleLine.push_back(relConfigFile);
if (cmSystemTools::FileExists(configFile.c_str())) { if (cmSystemTools::FileExists(configFile.c_str())) {
singleLine.push_back(configFile); singleLine.push_back(configFile);
cpackCommandLines.push_back(singleLine); gti.CommandLines.push_back(singleLine);
targets->insert(cmTargets::value_type( targets.push_back(gti);
packageSourceTargetName,
this->CreateGlobalTarget(
packageSourceTargetName, "Run CPack packaging tool for source...",
&cpackCommandLines, depends, workingDir.c_str(),
/*uses_terminal*/ true)));
} }
} }
} }
void cmGlobalGenerator::AddGlobalTarget_Test(cmTargets* targets) void cmGlobalGenerator::AddGlobalTarget_Test(
std::vector<GlobalTargetInfo>& targets)
{ {
cmMakefile* mf = this->Makefiles[0]; cmMakefile* mf = this->Makefiles[0];
const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir(); const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir();
if (mf->IsOn("CMAKE_TESTING_ENABLED")) { if (mf->IsOn("CMAKE_TESTING_ENABLED")) {
cmCustomCommandLines cpackCommandLines; GlobalTargetInfo gti;
std::vector<std::string> depends; gti.Name = this->GetTestTargetName();
gti.Message = "Running tests...";
gti.UsesTerminal = true;
cmCustomCommandLine singleLine; cmCustomCommandLine singleLine;
singleLine.push_back(cmSystemTools::GetCTestCommand()); singleLine.push_back(cmSystemTools::GetCTestCommand());
singleLine.push_back("--force-new-ctest-process"); singleLine.push_back("--force-new-ctest-process");
@ -2163,21 +2163,18 @@ void cmGlobalGenerator::AddGlobalTarget_Test(cmTargets* targets)
{ {
singleLine.push_back("$(ARGS)"); singleLine.push_back("$(ARGS)");
} }
cpackCommandLines.push_back(singleLine); gti.CommandLines.push_back(singleLine);
targets->insert(cmTargets::value_type( targets.push_back(gti);
this->GetTestTargetName(),
this->CreateGlobalTarget(this->GetTestTargetName(), "Running tests...",
&cpackCommandLines, depends, CM_NULLPTR,
/*uses_terminal*/ true)));
} }
} }
void cmGlobalGenerator::AddGlobalTarget_EditCache(cmTargets* targets) void cmGlobalGenerator::AddGlobalTarget_EditCache(
std::vector<GlobalTargetInfo>& targets)
{ {
const char* editCacheTargetName = this->GetEditCacheTargetName(); const char* editCacheTargetName = this->GetEditCacheTargetName();
if (editCacheTargetName) { if (editCacheTargetName) {
cmCustomCommandLines cpackCommandLines; GlobalTargetInfo gti;
std::vector<std::string> depends; gti.Name = editCacheTargetName;
cmCustomCommandLine singleLine; cmCustomCommandLine singleLine;
// Use generator preference for the edit_cache rule if it is defined. // Use generator preference for the edit_cache rule if it is defined.
@ -2186,47 +2183,43 @@ void cmGlobalGenerator::AddGlobalTarget_EditCache(cmTargets* targets)
singleLine.push_back(edit_cmd); singleLine.push_back(edit_cmd);
singleLine.push_back("-H$(CMAKE_SOURCE_DIR)"); singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
singleLine.push_back("-B$(CMAKE_BINARY_DIR)"); singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
cpackCommandLines.push_back(singleLine); gti.Message = "Running CMake cache editor...";
targets->insert(cmTargets::value_type( gti.UsesTerminal = true;
editCacheTargetName, gti.CommandLines.push_back(singleLine);
this->CreateGlobalTarget(
editCacheTargetName, "Running CMake cache editor...",
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true)));
} else { } else {
singleLine.push_back(cmSystemTools::GetCMakeCommand()); singleLine.push_back(cmSystemTools::GetCMakeCommand());
singleLine.push_back("-E"); singleLine.push_back("-E");
singleLine.push_back("echo"); singleLine.push_back("echo");
singleLine.push_back("No interactive CMake dialog available."); singleLine.push_back("No interactive CMake dialog available.");
cpackCommandLines.push_back(singleLine); gti.Message = "No interactive CMake dialog available...";
targets->insert(cmTargets::value_type( gti.UsesTerminal = false;
editCacheTargetName, gti.CommandLines.push_back(singleLine);
this->CreateGlobalTarget(
editCacheTargetName, "No interactive CMake dialog available...",
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ false)));
} }
targets.push_back(gti);
} }
} }
void cmGlobalGenerator::AddGlobalTarget_RebuildCache(cmTargets* targets) void cmGlobalGenerator::AddGlobalTarget_RebuildCache(
std::vector<GlobalTargetInfo>& targets)
{ {
const char* rebuildCacheTargetName = this->GetRebuildCacheTargetName(); const char* rebuildCacheTargetName = this->GetRebuildCacheTargetName();
if (rebuildCacheTargetName) { if (rebuildCacheTargetName) {
cmCustomCommandLines cpackCommandLines; GlobalTargetInfo gti;
std::vector<std::string> depends; gti.Name = rebuildCacheTargetName;
gti.Message = "Running CMake to regenerate build system...";
gti.UsesTerminal = true;
cmCustomCommandLine singleLine; cmCustomCommandLine singleLine;
singleLine.push_back(cmSystemTools::GetCMakeCommand()); singleLine.push_back(cmSystemTools::GetCMakeCommand());
singleLine.push_back("-H$(CMAKE_SOURCE_DIR)"); singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
singleLine.push_back("-B$(CMAKE_BINARY_DIR)"); singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
cpackCommandLines.push_back(singleLine); gti.CommandLines.push_back(singleLine);
targets->insert(cmTargets::value_type( targets.push_back(gti);
rebuildCacheTargetName,
this->CreateGlobalTarget(
rebuildCacheTargetName, "Running CMake to regenerate build system...",
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true)));
} }
} }
void cmGlobalGenerator::AddGlobalTarget_Install(cmTargets* targets) void cmGlobalGenerator::AddGlobalTarget_Install(
std::vector<GlobalTargetInfo>& targets)
{ {
cmMakefile* mf = this->Makefiles[0]; cmMakefile* mf = this->Makefiles[0];
const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir(); const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir();
@ -2239,8 +2232,6 @@ void cmGlobalGenerator::AddGlobalTarget_Install(cmTargets* targets)
} else if (this->InstallTargetEnabled && !skipInstallRules) { } else if (this->InstallTargetEnabled && !skipInstallRules) {
if (!cmakeCfgIntDir || !*cmakeCfgIntDir || cmakeCfgIntDir[0] == '.') { if (!cmakeCfgIntDir || !*cmakeCfgIntDir || cmakeCfgIntDir[0] == '.') {
std::set<std::string>* componentsSet = &this->InstallComponents; std::set<std::string>* componentsSet = &this->InstallComponents;
cmCustomCommandLines cpackCommandLines;
std::vector<std::string> depends;
std::ostringstream ostr; std::ostringstream ostr;
if (!componentsSet->empty()) { if (!componentsSet->empty()) {
ostr << "Available install components are: "; ostr << "Available install components are: ";
@ -2248,23 +2239,25 @@ void cmGlobalGenerator::AddGlobalTarget_Install(cmTargets* targets)
} else { } else {
ostr << "Only default component available"; ostr << "Only default component available";
} }
targets->insert(cmTargets::value_type( GlobalTargetInfo gti;
"list_install_components", gti.Name = "list_install_components";
this->CreateGlobalTarget("list_install_components", ostr.str().c_str(), gti.Message = ostr.str();
&cpackCommandLines, depends, CM_NULLPTR, gti.UsesTerminal = false;
/*uses_terminal*/ false))); targets.push_back(gti);
} }
std::string cmd = cmSystemTools::GetCMakeCommand(); std::string cmd = cmSystemTools::GetCMakeCommand();
cmCustomCommandLines cpackCommandLines; GlobalTargetInfo gti;
std::vector<std::string> depends; gti.Name = this->GetInstallTargetName();
gti.Message = "Install the project...";
gti.UsesTerminal = true;
cmCustomCommandLine singleLine; cmCustomCommandLine singleLine;
if (this->GetPreinstallTargetName()) { if (this->GetPreinstallTargetName()) {
depends.push_back(this->GetPreinstallTargetName()); gti.Depends.push_back(this->GetPreinstallTargetName());
} else { } else {
const char* noall = const char* noall =
mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY"); mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
if (!noall || cmSystemTools::IsOff(noall)) { if (!noall || cmSystemTools::IsOff(noall)) {
depends.push_back(this->GetAllTargetName()); gti.Depends.push_back(this->GetAllTargetName());
} }
} }
if (mf->GetDefinition("CMake_BINARY_DIR") && if (mf->GetDefinition("CMake_BINARY_DIR") &&
@ -2289,46 +2282,39 @@ void cmGlobalGenerator::AddGlobalTarget_Install(cmTargets* targets)
} }
singleLine.push_back("-P"); singleLine.push_back("-P");
singleLine.push_back("cmake_install.cmake"); singleLine.push_back("cmake_install.cmake");
cpackCommandLines.push_back(singleLine); gti.CommandLines.push_back(singleLine);
targets->insert(cmTargets::value_type( targets.push_back(gti);
this->GetInstallTargetName(),
this->CreateGlobalTarget(this->GetInstallTargetName(),
"Install the project...", &cpackCommandLines,
depends, CM_NULLPTR, /*uses_terminal*/ true)));
// install_local // install_local
if (const char* install_local = this->GetInstallLocalTargetName()) { if (const char* install_local = this->GetInstallLocalTargetName()) {
gti.Name = install_local;
gti.Message = "Installing only the local directory...";
gti.UsesTerminal = true;
gti.CommandLines.clear();
cmCustomCommandLine localCmdLine = singleLine; cmCustomCommandLine localCmdLine = singleLine;
localCmdLine.insert(localCmdLine.begin() + 1, localCmdLine.insert(localCmdLine.begin() + 1,
"-DCMAKE_INSTALL_LOCAL_ONLY=1"); "-DCMAKE_INSTALL_LOCAL_ONLY=1");
cpackCommandLines.erase(cpackCommandLines.begin(),
cpackCommandLines.end());
cpackCommandLines.push_back(localCmdLine);
targets->insert(cmTargets::value_type( gti.CommandLines.push_back(localCmdLine);
install_local, targets.push_back(gti);
this->CreateGlobalTarget(
install_local, "Installing only the local directory...",
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true)));
} }
// install_strip // install_strip
const char* install_strip = this->GetInstallStripTargetName(); const char* install_strip = this->GetInstallStripTargetName();
if ((install_strip != CM_NULLPTR) && (mf->IsSet("CMAKE_STRIP"))) { if ((install_strip != CM_NULLPTR) && (mf->IsSet("CMAKE_STRIP"))) {
gti.Name = install_strip;
gti.Message = "Installing the project stripped...";
gti.UsesTerminal = true;
gti.CommandLines.clear();
cmCustomCommandLine stripCmdLine = singleLine; cmCustomCommandLine stripCmdLine = singleLine;
stripCmdLine.insert(stripCmdLine.begin() + 1, stripCmdLine.insert(stripCmdLine.begin() + 1,
"-DCMAKE_INSTALL_DO_STRIP=1"); "-DCMAKE_INSTALL_DO_STRIP=1");
cpackCommandLines.erase(cpackCommandLines.begin(), gti.CommandLines.push_back(stripCmdLine);
cpackCommandLines.end()); targets.push_back(gti);
cpackCommandLines.push_back(stripCmdLine);
targets->insert(cmTargets::value_type(
install_strip,
this->CreateGlobalTarget(
install_strip, "Installing the project stripped...",
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true)));
} }
} }
} }
@ -2362,14 +2348,12 @@ bool cmGlobalGenerator::UseFolderProperty()
return false; return false;
} }
cmTarget cmGlobalGenerator::CreateGlobalTarget( cmTarget cmGlobalGenerator::CreateGlobalTarget(GlobalTargetInfo const& gti,
const std::string& name, const char* message, cmMakefile* mf)
const cmCustomCommandLines* commandLines, std::vector<std::string> depends,
const char* workingDirectory, bool uses_terminal)
{ {
// Package // Package
cmTarget target(name, cmState::GLOBAL_TARGET, cmTarget::VisibilityNormal, cmTarget target(gti.Name, cmState::GLOBAL_TARGET, cmTarget::VisibilityNormal,
CM_NULLPTR); mf);
target.SetProperty("EXCLUDE_FROM_ALL", "TRUE"); target.SetProperty("EXCLUDE_FROM_ALL", "TRUE");
std::vector<std::string> no_outputs; std::vector<std::string> no_outputs;
@ -2377,12 +2361,14 @@ cmTarget cmGlobalGenerator::CreateGlobalTarget(
std::vector<std::string> no_depends; std::vector<std::string> no_depends;
// Store the custom command in the target. // Store the custom command in the target.
cmCustomCommand cc(CM_NULLPTR, no_outputs, no_byproducts, no_depends, cmCustomCommand cc(CM_NULLPTR, no_outputs, no_byproducts, no_depends,
*commandLines, CM_NULLPTR, workingDirectory); gti.CommandLines, CM_NULLPTR, gti.WorkingDir.c_str());
cc.SetUsesTerminal(uses_terminal); cc.SetUsesTerminal(gti.UsesTerminal);
target.AddPostBuildCommand(cc); target.AddPostBuildCommand(cc);
target.SetProperty("EchoString", message); if (!gti.Message.empty()) {
std::vector<std::string>::iterator dit; target.SetProperty("EchoString", gti.Message.c_str());
for (dit = depends.begin(); dit != depends.end(); ++dit) { }
for (std::vector<std::string>::const_iterator dit = gti.Depends.begin();
dit != gti.Depends.end(); ++dit) {
target.AddUtility(*dit); target.AddUtility(*dit);
} }

View File

@ -402,17 +402,30 @@ protected:
bool IsExcluded(cmLocalGenerator* root, cmLocalGenerator* gen) const; bool IsExcluded(cmLocalGenerator* root, cmLocalGenerator* gen) const;
bool IsExcluded(cmLocalGenerator* root, cmGeneratorTarget* target) const; bool IsExcluded(cmLocalGenerator* root, cmGeneratorTarget* target) const;
virtual void InitializeProgressMarks() {} virtual void InitializeProgressMarks() {}
void CreateDefaultGlobalTargets(cmTargets* targets);
void AddGlobalTarget_Package(cmTargets* targets); struct GlobalTargetInfo
void AddGlobalTarget_PackageSource(cmTargets* targets); {
void AddGlobalTarget_Test(cmTargets* targets); std::string Name;
void AddGlobalTarget_EditCache(cmTargets* targets); std::string Message;
void AddGlobalTarget_RebuildCache(cmTargets* targets); cmCustomCommandLines CommandLines;
void AddGlobalTarget_Install(cmTargets* targets); std::vector<std::string> Depends;
cmTarget CreateGlobalTarget(const std::string& name, const char* message, std::string WorkingDir;
const cmCustomCommandLines* commandLines, bool UsesTerminal;
std::vector<std::string> depends, GlobalTargetInfo()
const char* workingDir, bool uses_terminal); : UsesTerminal(false)
{
}
};
void CreateDefaultGlobalTargets(std::vector<GlobalTargetInfo>& targets);
void AddGlobalTarget_Package(std::vector<GlobalTargetInfo>& targets);
void AddGlobalTarget_PackageSource(std::vector<GlobalTargetInfo>& targets);
void AddGlobalTarget_Test(std::vector<GlobalTargetInfo>& targets);
void AddGlobalTarget_EditCache(std::vector<GlobalTargetInfo>& targets);
void AddGlobalTarget_RebuildCache(std::vector<GlobalTargetInfo>& targets);
void AddGlobalTarget_Install(std::vector<GlobalTargetInfo>& targets);
cmTarget CreateGlobalTarget(GlobalTargetInfo const& gti, cmMakefile* mf);
std::string FindMakeProgramFile; std::string FindMakeProgramFile;
std::string ConfiguredFilesPath; std::string ConfiguredFilesPath;