resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmAddExecutableCommand.cxx
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmAddExecutableCommand.h"
4
5 #include "cmExecutionStatus.h"
6 #include "cmGeneratorExpression.h"
7 #include "cmGlobalGenerator.h"
8 #include "cmMakefile.h"
9 #include "cmStateTypes.h"
10 #include "cmStringAlgorithms.h"
11 #include "cmTarget.h"
12
13 bool cmAddExecutableCommand(std::vector<std::string> const& args,
14                             cmExecutionStatus& status)
15 {
16   if (args.empty()) {
17     status.SetError("called with incorrect number of arguments");
18     return false;
19   }
20
21   cmMakefile& mf = status.GetMakefile();
22   auto s = args.begin();
23
24   std::string const& exename = *s;
25
26   ++s;
27   bool use_win32 = false;
28   bool use_macbundle = false;
29   bool excludeFromAll = false;
30   bool importTarget = false;
31   bool importGlobal = false;
32   bool isAlias = false;
33   while (s != args.end()) {
34     if (*s == "WIN32") {
35       ++s;
36       use_win32 = true;
37     } else if (*s == "MACOSX_BUNDLE") {
38       ++s;
39       use_macbundle = true;
40     } else if (*s == "EXCLUDE_FROM_ALL") {
41       ++s;
42       excludeFromAll = true;
43     } else if (*s == "IMPORTED") {
44       ++s;
45       importTarget = true;
46     } else if (importTarget && *s == "GLOBAL") {
47       ++s;
48       importGlobal = true;
49     } else if (*s == "ALIAS") {
50       ++s;
51       isAlias = true;
52     } else {
53       break;
54     }
55   }
56
57   if (importTarget && !importGlobal) {
58     importGlobal = mf.IsImportedTargetGlobalScope();
59   }
60
61   bool nameOk = cmGeneratorExpression::IsValidTargetName(exename) &&
62     !cmGlobalGenerator::IsReservedTarget(exename);
63
64   if (nameOk && !importTarget && !isAlias) {
65     nameOk = exename.find(':') == std::string::npos;
66   }
67   if (!nameOk && !mf.CheckCMP0037(exename, cmStateEnums::EXECUTABLE)) {
68     return false;
69   }
70
71   // Special modifiers are not allowed with IMPORTED signature.
72   if (importTarget && (use_win32 || use_macbundle || excludeFromAll)) {
73     if (use_win32) {
74       status.SetError("may not be given WIN32 for an IMPORTED target.");
75     } else if (use_macbundle) {
76       status.SetError(
77         "may not be given MACOSX_BUNDLE for an IMPORTED target.");
78     } else // if(excludeFromAll)
79     {
80       status.SetError(
81         "may not be given EXCLUDE_FROM_ALL for an IMPORTED target.");
82     }
83     return false;
84   }
85   if (isAlias) {
86     if (!cmGeneratorExpression::IsValidTargetName(exename)) {
87       status.SetError("Invalid name for ALIAS: " + exename);
88       return false;
89     }
90     if (excludeFromAll) {
91       status.SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
92       return false;
93     }
94     if (importTarget || importGlobal) {
95       status.SetError("IMPORTED with ALIAS is not allowed.");
96       return false;
97     }
98     if (args.size() != 3) {
99       status.SetError("ALIAS requires exactly one target argument.");
100       return false;
101     }
102
103     std::string const& aliasedName = *s;
104     if (mf.IsAlias(aliasedName)) {
105       status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
106                                "\" because target \"", aliasedName,
107                                "\" is itself an ALIAS."));
108       return false;
109     }
110     cmTarget* aliasedTarget = mf.FindTargetToUse(aliasedName, true);
111     if (!aliasedTarget) {
112       status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
113                                "\" because target \"", aliasedName,
114                                "\" does not already exist."));
115       return false;
116     }
117     cmStateEnums::TargetType type = aliasedTarget->GetType();
118     if (type != cmStateEnums::EXECUTABLE) {
119       status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
120                                "\" because target \"", aliasedName,
121                                "\" is not an executable."));
122       return false;
123     }
124     mf.AddAlias(exename, aliasedName,
125                 !aliasedTarget->IsImported() ||
126                   aliasedTarget->IsImportedGloballyVisible());
127     return true;
128   }
129
130   // Handle imported target creation.
131   if (importTarget) {
132     // Make sure the target does not already exist.
133     if (mf.FindTargetToUse(exename)) {
134       status.SetError(cmStrCat(
135         "cannot create imported target \"", exename,
136         "\" because another target with the same name already exists."));
137       return false;
138     }
139
140     // Create the imported target.
141     mf.AddImportedTarget(exename, cmStateEnums::EXECUTABLE, importGlobal);
142     return true;
143   }
144
145   // Enforce name uniqueness.
146   {
147     std::string msg;
148     if (!mf.EnforceUniqueName(exename, msg)) {
149       status.SetError(msg);
150       return false;
151     }
152   }
153
154   std::vector<std::string> srclists(s, args.end());
155   cmTarget* tgt = mf.AddExecutable(exename, srclists, excludeFromAll);
156   if (use_win32) {
157     tgt->SetProperty("WIN32_EXECUTABLE", "ON");
158   }
159   if (use_macbundle) {
160     tgt->SetProperty("MACOSX_BUNDLE", "ON");
161   }
162
163   return true;
164 }