Merge topic 'refactor-target-construction'

7a2e114d cmTarget: Inline SetType method at only remaining call site
00e78c19 cmTarget: Construct with basic information up front
9d11bd50 Avoid requiring default cmTarget constructor for map indexing
d97513d8 cmTarget: Add method to get a copy adapted for a directory
This commit is contained in:
Brad King 2016-09-15 08:46:40 -04:00 committed by CMake Topic Stage
commit e0e047fe4b
6 changed files with 114 additions and 78 deletions

View File

@ -77,9 +77,8 @@ std::string cmExportTryCompileFileGenerator::FindTargets(
CM_AUTO_PTR<cmCompiledGeneratorExpression> cge = ge.Parse(prop); CM_AUTO_PTR<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
cmTarget dummyHead; cmTarget dummyHead("try_compile_dummy_exe", cmState::EXECUTABLE,
dummyHead.SetType(cmState::EXECUTABLE, "try_compile_dummy_exe"); cmTarget::VisibilityNormal, tgt->Target->GetMakefile());
dummyHead.SetMakefile(tgt->Target->GetMakefile());
cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator()); cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator());

View File

@ -1068,8 +1068,8 @@ void cmGlobalGenerator::Configure()
cmTargets* targets = &(mf->GetTargets()); cmTargets* targets = &(mf->GetTargets());
cmTargets::iterator tit; cmTargets::iterator tit;
for (tit = globalTargets.begin(); tit != globalTargets.end(); ++tit) { for (tit = globalTargets.begin(); tit != globalTargets.end(); ++tit) {
(*targets)[tit->first] = tit->second; targets->insert(
(*targets)[tit->first].SetMakefile(mf); cmTargets::value_type(tit->first, tit->second.CopyForDirectory(mf)));
} }
} }
@ -2101,9 +2101,12 @@ void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
} }
} }
if (cmSystemTools::FileExists(configFile.c_str())) { if (cmSystemTools::FileExists(configFile.c_str())) {
(*targets)[this->GetPackageTargetName()] = this->CreateGlobalTarget( targets->insert(cmTargets::value_type(
this->GetPackageTargetName(), "Run CPack packaging tool...", this->GetPackageTargetName(),
&cpackCommandLines, depends, workingDir.c_str(), /*uses_terminal*/ true); this->CreateGlobalTarget(this->GetPackageTargetName(),
"Run CPack packaging tool...",
&cpackCommandLines, depends, workingDir.c_str(),
/*uses_terminal*/ true)));
} }
// CPack source // CPack source
const char* packageSourceTargetName = this->GetPackageSourceTargetName(); const char* packageSourceTargetName = this->GetPackageSourceTargetName();
@ -2122,10 +2125,12 @@ void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
if (cmSystemTools::FileExists(configFile.c_str())) { if (cmSystemTools::FileExists(configFile.c_str())) {
singleLine.push_back(configFile); singleLine.push_back(configFile);
cpackCommandLines.push_back(singleLine); cpackCommandLines.push_back(singleLine);
(*targets)[packageSourceTargetName] = this->CreateGlobalTarget( targets->insert(cmTargets::value_type(
packageSourceTargetName, "Run CPack packaging tool for source...", packageSourceTargetName,
&cpackCommandLines, depends, workingDir.c_str(), this->CreateGlobalTarget(
/*uses_terminal*/ true); packageSourceTargetName, "Run CPack packaging tool for source...",
&cpackCommandLines, depends, workingDir.c_str(),
/*uses_terminal*/ true)));
} }
} }
@ -2146,10 +2151,11 @@ void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
singleLine.push_back("$(ARGS)"); singleLine.push_back("$(ARGS)");
} }
cpackCommandLines.push_back(singleLine); cpackCommandLines.push_back(singleLine);
(*targets)[this->GetTestTargetName()] = targets->insert(cmTargets::value_type(
this->GetTestTargetName(),
this->CreateGlobalTarget(this->GetTestTargetName(), "Running tests...", this->CreateGlobalTarget(this->GetTestTargetName(), "Running tests...",
&cpackCommandLines, depends, CM_NULLPTR, &cpackCommandLines, depends, CM_NULLPTR,
/*uses_terminal*/ true); /*uses_terminal*/ true)));
} }
// Edit Cache // Edit Cache
@ -2167,18 +2173,22 @@ void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
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); cpackCommandLines.push_back(singleLine);
(*targets)[editCacheTargetName] = this->CreateGlobalTarget( targets->insert(cmTargets::value_type(
editCacheTargetName, "Running CMake cache editor...", editCacheTargetName,
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true); 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); cpackCommandLines.push_back(singleLine);
(*targets)[editCacheTargetName] = this->CreateGlobalTarget( targets->insert(cmTargets::value_type(
editCacheTargetName, "No interactive CMake dialog available...", editCacheTargetName,
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ false); this->CreateGlobalTarget(
editCacheTargetName, "No interactive CMake dialog available...",
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ false)));
} }
} }
@ -2193,9 +2203,11 @@ void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
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); cpackCommandLines.push_back(singleLine);
(*targets)[rebuildCacheTargetName] = this->CreateGlobalTarget( targets->insert(cmTargets::value_type(
rebuildCacheTargetName, "Running CMake to regenerate build system...", rebuildCacheTargetName,
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true); this->CreateGlobalTarget(
rebuildCacheTargetName, "Running CMake to regenerate build system...",
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true)));
} }
// Install // Install
@ -2219,9 +2231,11 @@ void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
ostr << "Only default component available"; ostr << "Only default component available";
} }
singleLine.push_back(ostr.str()); singleLine.push_back(ostr.str());
(*targets)["list_install_components"] = this->CreateGlobalTarget( targets->insert(cmTargets::value_type(
"list_install_components", ostr.str().c_str(), &cpackCommandLines, "list_install_components",
depends, CM_NULLPTR, /*uses_terminal*/ false); this->CreateGlobalTarget("list_install_components", ostr.str().c_str(),
&cpackCommandLines, depends, CM_NULLPTR,
/*uses_terminal*/ false)));
} }
std::string cmd = cmSystemTools::GetCMakeCommand(); std::string cmd = cmSystemTools::GetCMakeCommand();
cpackCommandLines.erase(cpackCommandLines.begin(), cpackCommandLines.erase(cpackCommandLines.begin(),
@ -2260,9 +2274,11 @@ void cmGlobalGenerator::CreateDefaultGlobalTargets(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); cpackCommandLines.push_back(singleLine);
(*targets)[this->GetInstallTargetName()] = this->CreateGlobalTarget( targets->insert(cmTargets::value_type(
this->GetInstallTargetName(), "Install the project...", this->GetInstallTargetName(),
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true); 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()) {
@ -2274,9 +2290,11 @@ void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
cpackCommandLines.end()); cpackCommandLines.end());
cpackCommandLines.push_back(localCmdLine); cpackCommandLines.push_back(localCmdLine);
(*targets)[install_local] = this->CreateGlobalTarget( targets->insert(cmTargets::value_type(
install_local, "Installing only the local directory...", install_local,
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true); this->CreateGlobalTarget(
install_local, "Installing only the local directory...",
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true)));
} }
// install_strip // install_strip
@ -2290,9 +2308,11 @@ void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
cpackCommandLines.end()); cpackCommandLines.end());
cpackCommandLines.push_back(stripCmdLine); cpackCommandLines.push_back(stripCmdLine);
(*targets)[install_strip] = this->CreateGlobalTarget( targets->insert(cmTargets::value_type(
install_strip, "Installing the project stripped...", install_strip,
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true); this->CreateGlobalTarget(
install_strip, "Installing the project stripped...",
&cpackCommandLines, depends, CM_NULLPTR, /*uses_terminal*/ true)));
} }
} }
} }
@ -2332,8 +2352,8 @@ cmTarget cmGlobalGenerator::CreateGlobalTarget(
const char* workingDirectory, bool uses_terminal) const char* workingDirectory, bool uses_terminal)
{ {
// Package // Package
cmTarget target; cmTarget target(name, cmState::GLOBAL_TARGET, cmTarget::VisibilityNormal,
target.SetType(cmState::GLOBAL_TARGET, name); CM_NULLPTR);
target.SetProperty("EXCLUDE_FROM_ALL", "TRUE"); target.SetProperty("EXCLUDE_FROM_ALL", "TRUE");
std::vector<std::string> no_outputs; std::vector<std::string> no_outputs;

View File

@ -37,14 +37,17 @@ bool cmInstallTargetsCommand::InitialPass(std::vector<std::string> const& args,
} }
runtime_dir = *s; runtime_dir = *s;
} else if (tgts.find(*s) != tgts.end()) {
tgts[*s].SetInstallPath(args[0].c_str());
tgts[*s].SetRuntimeInstallPath(runtime_dir.c_str());
tgts[*s].SetHaveInstallRule(true);
} else { } else {
std::string str = "Cannot find target: \"" + *s + "\" to install."; cmTargets::iterator ti = tgts.find(*s);
this->SetError(str); if (ti != tgts.end()) {
return false; ti->second.SetInstallPath(args[0].c_str());
ti->second.SetRuntimeInstallPath(runtime_dir.c_str());
ti->second.SetHaveInstallRule(true);
} else {
std::string str = "Cannot find target: \"" + *s + "\" to install.";
this->SetError(str);
return false;
}
} }
} }

View File

@ -979,8 +979,9 @@ void cmMakefile::AddCustomCommandOldStyle(
// then add the source to the target to make sure the rule is // then add the source to the target to make sure the rule is
// included. // included.
if (sf && !sf->GetPropertyAsBool("__CMAKE_RULE")) { if (sf && !sf->GetPropertyAsBool("__CMAKE_RULE")) {
if (this->Targets.find(target) != this->Targets.end()) { cmTargets::iterator ti = this->Targets.find(target);
this->Targets[target].AddSource(sf->GetFullPath()); if (ti != this->Targets.end()) {
ti->second.AddSource(sf->GetFullPath());
} else { } else {
cmSystemTools::Error("Attempt to add a custom rule to a target " cmSystemTools::Error("Attempt to add a custom rule to a target "
"that does not exist yet for target ", "that does not exist yet for target ",
@ -1924,10 +1925,10 @@ cmTarget* cmMakefile::AddNewTarget(cmState::TargetType type,
const std::string& name) const std::string& name)
{ {
cmTargets::iterator it = cmTargets::iterator it =
this->Targets.insert(cmTargets::value_type(name, cmTarget())).first; this->Targets
cmTarget& target = it->second; .insert(cmTargets::value_type(
target.SetType(type, name); name, cmTarget(name, type, cmTarget::VisibilityNormal, this)))
target.SetMakefile(this); .first;
this->GetGlobalGenerator()->IndexTarget(&it->second); this->GetGlobalGenerator()->IndexTarget(&it->second);
return &it->second; return &it->second;
} }
@ -3709,10 +3710,10 @@ cmTarget* cmMakefile::AddImportedTarget(const std::string& name,
cmState::TargetType type, bool global) cmState::TargetType type, bool global)
{ {
// Create the target. // Create the target.
CM_AUTO_PTR<cmTarget> target(new cmTarget); CM_AUTO_PTR<cmTarget> target(
target->SetType(type, name); new cmTarget(name, type, global ? cmTarget::VisibilityImportedGlobally
target->MarkAsImported(global); : cmTarget::VisibilityImported,
target->SetMakefile(this); this));
// Add to the set of available imported targets. // Add to the set of available imported targets.
this->ImportedTargets[name] = target.get(); this->ImportedTargets[name] = target.get();

View File

@ -59,28 +59,41 @@ public:
std::vector<cmListFileBacktrace> LinkImplementationPropertyBacktraces; std::vector<cmListFileBacktrace> LinkImplementationPropertyBacktraces;
}; };
cmTarget::cmTarget() cmTarget::cmTarget(std::string const& name, cmState::TargetType type,
Visibility vis, cmMakefile* mf)
{ {
assert(mf || type == cmState::GLOBAL_TARGET);
this->Name = name;
this->TargetTypeValue = type;
this->Makefile = CM_NULLPTR; this->Makefile = CM_NULLPTR;
this->HaveInstallRule = false; this->HaveInstallRule = false;
this->DLLPlatform = false; this->DLLPlatform = false;
this->IsAndroid = false; this->IsAndroid = false;
this->IsImportedTarget = false; this->IsImportedTarget =
this->ImportedGloballyVisible = false; (vis == VisibilityImported || vis == VisibilityImportedGlobally);
this->ImportedGloballyVisible = vis == VisibilityImportedGlobally;
this->BuildInterfaceIncludesAppended = false; this->BuildInterfaceIncludesAppended = false;
}
void cmTarget::SetType(cmState::TargetType type, const std::string& name)
{
this->Name = name;
// only add dependency information for library targets // only add dependency information for library targets
this->TargetTypeValue = type;
if (this->TargetTypeValue >= cmState::STATIC_LIBRARY && if (this->TargetTypeValue >= cmState::STATIC_LIBRARY &&
this->TargetTypeValue <= cmState::MODULE_LIBRARY) { this->TargetTypeValue <= cmState::MODULE_LIBRARY) {
this->RecordDependencies = true; this->RecordDependencies = true;
} else { } else {
this->RecordDependencies = false; this->RecordDependencies = false;
} }
if (mf) {
this->SetMakefile(mf);
}
}
cmTarget cmTarget::CopyForDirectory(cmMakefile* mf) const
{
assert(this->GetType() == cmState::GLOBAL_TARGET);
assert(this->GetMakefile() == CM_NULLPTR);
cmTarget result(*this);
result.SetMakefile(mf);
return result;
} }
void cmTarget::SetMakefile(cmMakefile* mf) void cmTarget::SetMakefile(cmMakefile* mf)
@ -1062,12 +1075,6 @@ void cmTarget::CheckProperty(const std::string& prop,
} }
} }
void cmTarget::MarkAsImported(bool global)
{
this->IsImportedTarget = true;
this->ImportedGloballyVisible = global;
}
bool cmTarget::HandleLocationPropertyPolicy(cmMakefile* context) const bool cmTarget::HandleLocationPropertyPolicy(cmMakefile* context) const
{ {
if (this->IsImported()) { if (this->IsImported()) {

View File

@ -63,7 +63,16 @@ private:
class cmTarget class cmTarget
{ {
public: public:
cmTarget(); enum Visibility
{
VisibilityNormal,
VisibilityImported,
VisibilityImportedGlobally
};
cmTarget(std::string const& name, cmState::TargetType type, Visibility vis,
cmMakefile* mf);
enum CustomCommandType enum CustomCommandType
{ {
PRE_BUILD, PRE_BUILD,
@ -76,18 +85,13 @@ public:
*/ */
cmState::TargetType GetType() const { return this->TargetTypeValue; } cmState::TargetType GetType() const { return this->TargetTypeValue; }
/**
* Set the target type
*/
void SetType(cmState::TargetType f, const std::string& name);
void MarkAsImported(bool global = false);
///! Set/Get the name of the target ///! Set/Get the name of the target
const std::string& GetName() const { return this->Name; } const std::string& GetName() const { return this->Name; }
///! Set the cmMakefile that owns this target /** Get a copy of this target adapted for the given directory. */
void SetMakefile(cmMakefile* mf); cmTarget CopyForDirectory(cmMakefile* mf) const;
/** Get the cmMakefile that owns this target. */
cmMakefile* GetMakefile() const { return this->Makefile; } cmMakefile* GetMakefile() const { return this->Makefile; }
#define DECLARE_TARGET_POLICY(POLICY) \ #define DECLARE_TARGET_POLICY(POLICY) \
@ -279,6 +283,8 @@ public:
}; };
private: private:
void SetMakefile(cmMakefile* mf);
bool HandleLocationPropertyPolicy(cmMakefile* context) const; bool HandleLocationPropertyPolicy(cmMakefile* context) const;
const char* GetSuffixVariableInternal(bool implib) const; const char* GetSuffixVariableInternal(bool implib) const;