cmake/Source/cmAddExecutableCommand.cxx

165 lines
4.8 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmAddExecutableCommand.h"
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmGeneratorExpression.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmTarget.h"
2020-02-01 23:06:01 +01:00
bool cmAddExecutableCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2018-04-23 21:13:27 +02:00
if (args.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
auto s = args.begin();
2017-07-20 19:35:53 +02:00
std::string const& exename = *s;
++s;
bool use_win32 = false;
bool use_macbundle = false;
bool excludeFromAll = false;
bool importTarget = false;
2012-04-19 19:04:21 +03:00
bool importGlobal = false;
2013-11-03 12:27:13 +02:00
bool isAlias = false;
2016-07-09 11:21:54 +02:00
while (s != args.end()) {
if (*s == "WIN32") {
++s;
use_win32 = true;
2016-07-09 11:21:54 +02:00
} else if (*s == "MACOSX_BUNDLE") {
++s;
use_macbundle = true;
2016-07-09 11:21:54 +02:00
} else if (*s == "EXCLUDE_FROM_ALL") {
++s;
excludeFromAll = true;
2016-07-09 11:21:54 +02:00
} else if (*s == "IMPORTED") {
++s;
importTarget = true;
} else if (importTarget && *s == "GLOBAL") {
2012-04-19 19:04:21 +03:00
++s;
importGlobal = true;
2016-07-09 11:21:54 +02:00
} else if (*s == "ALIAS") {
2013-11-03 12:27:13 +02:00
++s;
isAlias = true;
2016-07-09 11:21:54 +02:00
} else {
break;
}
2016-07-09 11:21:54 +02:00
}
2022-08-04 22:12:04 +02:00
if (importTarget && !importGlobal) {
importGlobal = mf.IsImportedTargetGlobalScope();
}
2014-08-03 19:52:23 +02:00
bool nameOk = cmGeneratorExpression::IsValidTargetName(exename) &&
!cmGlobalGenerator::IsReservedTarget(exename);
2016-07-09 11:21:54 +02:00
if (nameOk && !importTarget && !isAlias) {
2017-04-14 19:02:05 +02:00
nameOk = exename.find(':') == std::string::npos;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
if (!nameOk && !mf.CheckCMP0037(exename, cmStateEnums::EXECUTABLE)) {
2018-04-23 21:13:27 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Special modifiers are not allowed with IMPORTED signature.
2016-07-09 11:21:54 +02:00
if (importTarget && (use_win32 || use_macbundle || excludeFromAll)) {
if (use_win32) {
2020-02-01 23:06:01 +01:00
status.SetError("may not be given WIN32 for an IMPORTED target.");
2016-07-09 11:21:54 +02:00
} else if (use_macbundle) {
2020-02-01 23:06:01 +01:00
status.SetError(
"may not be given MACOSX_BUNDLE for an IMPORTED target.");
2016-07-09 11:21:54 +02:00
} else // if(excludeFromAll)
{
2020-02-01 23:06:01 +01:00
status.SetError(
"may not be given EXCLUDE_FROM_ALL for an IMPORTED target.");
}
2016-07-09 11:21:54 +02:00
return false;
}
if (isAlias) {
if (!cmGeneratorExpression::IsValidTargetName(exename)) {
2020-02-01 23:06:01 +01:00
status.SetError("Invalid name for ALIAS: " + exename);
2013-11-03 12:27:13 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
if (excludeFromAll) {
2020-02-01 23:06:01 +01:00
status.SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
2013-11-03 12:27:13 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
if (importTarget || importGlobal) {
2020-02-01 23:06:01 +01:00
status.SetError("IMPORTED with ALIAS is not allowed.");
2013-11-03 12:27:13 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
if (args.size() != 3) {
2020-02-01 23:06:01 +01:00
status.SetError("ALIAS requires exactly one target argument.");
2013-11-03 12:27:13 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
2018-04-23 21:13:27 +02:00
std::string const& aliasedName = *s;
2020-02-01 23:06:01 +01:00
if (mf.IsAlias(aliasedName)) {
status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
"\" because target \"", aliasedName,
"\" is itself an ALIAS."));
2013-11-03 12:27:13 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
cmTarget* aliasedTarget = mf.FindTargetToUse(aliasedName, true);
2016-07-09 11:21:54 +02:00
if (!aliasedTarget) {
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
"\" because target \"", aliasedName,
"\" does not already exist."));
2013-11-03 12:27:13 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2017-04-14 19:02:05 +02:00
cmStateEnums::TargetType type = aliasedTarget->GetType();
if (type != cmStateEnums::EXECUTABLE) {
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
"\" because target \"", aliasedName,
"\" is not an executable."));
2013-11-03 12:27:13 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
mf.AddAlias(exename, aliasedName,
!aliasedTarget->IsImported() ||
aliasedTarget->IsImportedGloballyVisible());
2013-11-03 12:27:13 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
// Handle imported target creation.
2016-07-09 11:21:54 +02:00
if (importTarget) {
// Make sure the target does not already exist.
2020-02-01 23:06:01 +01:00
if (mf.FindTargetToUse(exename)) {
status.SetError(cmStrCat(
"cannot create imported target \"", exename,
"\" because another target with the same name already exists."));
return false;
2016-07-09 11:21:54 +02:00
}
// Create the imported target.
2020-02-01 23:06:01 +01:00
mf.AddImportedTarget(exename, cmStateEnums::EXECUTABLE, importGlobal);
return true;
2016-07-09 11:21:54 +02:00
}
// Enforce name uniqueness.
{
2016-07-09 11:21:54 +02:00
std::string msg;
2020-02-01 23:06:01 +01:00
if (!mf.EnforceUniqueName(exename, msg)) {
status.SetError(msg);
2016-07-09 11:21:54 +02:00
return false;
}
}
std::vector<std::string> srclists(s, args.end());
2020-02-01 23:06:01 +01:00
cmTarget* tgt = mf.AddExecutable(exename, srclists, excludeFromAll);
2016-07-09 11:21:54 +02:00
if (use_win32) {
tgt->SetProperty("WIN32_EXECUTABLE", "ON");
2016-07-09 11:21:54 +02:00
}
if (use_macbundle) {
tgt->SetProperty("MACOSX_BUNDLE", "ON");
2016-07-09 11:21:54 +02:00
}
return true;
}