cmGeneratorExpressionNode.cxx 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  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 "cmGeneratorExpressionNode.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmGeneratorExpression.h"
  6. #include "cmGeneratorExpressionContext.h"
  7. #include "cmGeneratorExpressionDAGChecker.h"
  8. #include "cmGeneratorExpressionEvaluator.h"
  9. #include "cmGeneratorTarget.h"
  10. #include "cmGlobalGenerator.h"
  11. #include "cmLinkItem.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmMessageType.h"
  15. #include "cmOutputConverter.h"
  16. #include "cmPolicies.h"
  17. #include "cmRange.h"
  18. #include "cmState.h"
  19. #include "cmStateSnapshot.h"
  20. #include "cmStateTypes.h"
  21. #include "cmSystemTools.h"
  22. #include "cmTarget.h"
  23. #include "cmake.h"
  24. #include "cmsys/RegularExpression.hxx"
  25. #include "cmsys/String.h"
  26. #include <algorithm>
  27. #include <assert.h>
  28. #include <errno.h>
  29. #include <iterator>
  30. #include <map>
  31. #include <memory> // IWYU pragma: keep
  32. #include <set>
  33. #include <sstream>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <utility>
  37. std::string cmGeneratorExpressionNode::EvaluateDependentExpression(
  38. std::string const& prop, cmLocalGenerator* lg,
  39. cmGeneratorExpressionContext* context, cmGeneratorTarget const* headTarget,
  40. cmGeneratorTarget const* currentTarget,
  41. cmGeneratorExpressionDAGChecker* dagChecker)
  42. {
  43. cmGeneratorExpression ge(context->Backtrace);
  44. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  45. cge->SetEvaluateForBuildsystem(context->EvaluateForBuildsystem);
  46. std::string result =
  47. cge->Evaluate(lg, context->Config, context->Quiet, headTarget,
  48. currentTarget, dagChecker, context->Language);
  49. if (cge->GetHadContextSensitiveCondition()) {
  50. context->HadContextSensitiveCondition = true;
  51. }
  52. if (cge->GetHadHeadSensitiveCondition()) {
  53. context->HadHeadSensitiveCondition = true;
  54. }
  55. return result;
  56. }
  57. static const struct ZeroNode : public cmGeneratorExpressionNode
  58. {
  59. ZeroNode() {} // NOLINT(modernize-use-equals-default)
  60. bool GeneratesContent() const override { return false; }
  61. bool AcceptsArbitraryContentParameter() const override { return true; }
  62. std::string Evaluate(
  63. const std::vector<std::string>& /*parameters*/,
  64. cmGeneratorExpressionContext* /*context*/,
  65. const GeneratorExpressionContent* /*content*/,
  66. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  67. {
  68. return std::string();
  69. }
  70. } zeroNode;
  71. static const struct OneNode : public cmGeneratorExpressionNode
  72. {
  73. OneNode() {} // NOLINT(modernize-use-equals-default)
  74. bool AcceptsArbitraryContentParameter() const override { return true; }
  75. std::string Evaluate(
  76. const std::vector<std::string>& parameters,
  77. cmGeneratorExpressionContext* /*context*/,
  78. const GeneratorExpressionContent* /*content*/,
  79. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  80. {
  81. return parameters.front();
  82. }
  83. } oneNode;
  84. static const struct OneNode buildInterfaceNode;
  85. static const struct ZeroNode installInterfaceNode;
  86. #define BOOLEAN_OP_NODE(OPNAME, OP, SUCCESS_VALUE, FAILURE_VALUE) \
  87. static const struct OP##Node : public cmGeneratorExpressionNode \
  88. { \
  89. OP##Node() {} /* NOLINT(modernize-use-equals-default) */ \
  90. virtual int NumExpectedParameters() const { return OneOrMoreParameters; } \
  91. \
  92. std::string Evaluate(const std::vector<std::string>& parameters, \
  93. cmGeneratorExpressionContext* context, \
  94. const GeneratorExpressionContent* content, \
  95. cmGeneratorExpressionDAGChecker*) const \
  96. { \
  97. for (std::string const& param : parameters) { \
  98. if (param == #FAILURE_VALUE) { \
  99. return #FAILURE_VALUE; \
  100. } \
  101. if (param != #SUCCESS_VALUE) { \
  102. reportError(context, content->GetOriginalExpression(), \
  103. "Parameters to $<" #OP \
  104. "> must resolve to either '0' or '1'."); \
  105. return std::string(); \
  106. } \
  107. } \
  108. return #SUCCESS_VALUE; \
  109. } \
  110. } OPNAME;
  111. BOOLEAN_OP_NODE(andNode, AND, 1, 0)
  112. BOOLEAN_OP_NODE(orNode, OR, 0, 1)
  113. #undef BOOLEAN_OP_NODE
  114. static const struct NotNode : public cmGeneratorExpressionNode
  115. {
  116. NotNode() {} // NOLINT(modernize-use-equals-default)
  117. std::string Evaluate(
  118. const std::vector<std::string>& parameters,
  119. cmGeneratorExpressionContext* context,
  120. const GeneratorExpressionContent* content,
  121. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  122. {
  123. if (parameters.front() != "0" && parameters.front() != "1") {
  124. reportError(
  125. context, content->GetOriginalExpression(),
  126. "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
  127. return std::string();
  128. }
  129. return parameters.front() == "0" ? "1" : "0";
  130. }
  131. } notNode;
  132. static const struct BoolNode : public cmGeneratorExpressionNode
  133. {
  134. BoolNode() {} // NOLINT(modernize-use-equals-default)
  135. int NumExpectedParameters() const override { return 1; }
  136. std::string Evaluate(
  137. const std::vector<std::string>& parameters,
  138. cmGeneratorExpressionContext* /*context*/,
  139. const GeneratorExpressionContent* /*content*/,
  140. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  141. {
  142. return !cmSystemTools::IsOff(parameters.front()) ? "1" : "0";
  143. }
  144. } boolNode;
  145. static const struct IfNode : public cmGeneratorExpressionNode
  146. {
  147. IfNode() {} // NOLINT(modernize-use-equals-default)
  148. int NumExpectedParameters() const override { return 3; }
  149. std::string Evaluate(const std::vector<std::string>& parameters,
  150. cmGeneratorExpressionContext* context,
  151. const GeneratorExpressionContent* content,
  152. cmGeneratorExpressionDAGChecker*) const override
  153. {
  154. if (parameters[0] != "1" && parameters[0] != "0") {
  155. reportError(context, content->GetOriginalExpression(),
  156. "First parameter to $<IF> must resolve to exactly one '0' "
  157. "or '1' value.");
  158. return std::string();
  159. }
  160. return parameters[0] == "1" ? parameters[1] : parameters[2];
  161. }
  162. } ifNode;
  163. static const struct StrEqualNode : public cmGeneratorExpressionNode
  164. {
  165. StrEqualNode() {} // NOLINT(modernize-use-equals-default)
  166. int NumExpectedParameters() const override { return 2; }
  167. std::string Evaluate(
  168. const std::vector<std::string>& parameters,
  169. cmGeneratorExpressionContext* /*context*/,
  170. const GeneratorExpressionContent* /*content*/,
  171. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  172. {
  173. return parameters.front() == parameters[1] ? "1" : "0";
  174. }
  175. } strEqualNode;
  176. static const struct EqualNode : public cmGeneratorExpressionNode
  177. {
  178. EqualNode() {} // NOLINT(modernize-use-equals-default)
  179. int NumExpectedParameters() const override { return 2; }
  180. std::string Evaluate(
  181. const std::vector<std::string>& parameters,
  182. cmGeneratorExpressionContext* context,
  183. const GeneratorExpressionContent* content,
  184. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  185. {
  186. char* pEnd;
  187. int base = 0;
  188. bool flipSign = false;
  189. const char* lhs = parameters[0].c_str();
  190. if (cmHasLiteralPrefix(lhs, "0b") || cmHasLiteralPrefix(lhs, "0B")) {
  191. base = 2;
  192. lhs += 2;
  193. }
  194. if (cmHasLiteralPrefix(lhs, "-0b") || cmHasLiteralPrefix(lhs, "-0B")) {
  195. base = 2;
  196. lhs += 3;
  197. flipSign = true;
  198. }
  199. if (cmHasLiteralPrefix(lhs, "+0b") || cmHasLiteralPrefix(lhs, "+0B")) {
  200. base = 2;
  201. lhs += 3;
  202. }
  203. long lnum = strtol(lhs, &pEnd, base);
  204. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  205. reportError(context, content->GetOriginalExpression(),
  206. "$<EQUAL> parameter " + parameters[0] +
  207. " is not a valid integer.");
  208. return std::string();
  209. }
  210. if (flipSign) {
  211. lnum = -lnum;
  212. }
  213. base = 0;
  214. flipSign = false;
  215. const char* rhs = parameters[1].c_str();
  216. if (cmHasLiteralPrefix(rhs, "0b") || cmHasLiteralPrefix(rhs, "0B")) {
  217. base = 2;
  218. rhs += 2;
  219. }
  220. if (cmHasLiteralPrefix(rhs, "-0b") || cmHasLiteralPrefix(rhs, "-0B")) {
  221. base = 2;
  222. rhs += 3;
  223. flipSign = true;
  224. }
  225. if (cmHasLiteralPrefix(rhs, "+0b") || cmHasLiteralPrefix(rhs, "+0B")) {
  226. base = 2;
  227. rhs += 3;
  228. }
  229. long rnum = strtol(rhs, &pEnd, base);
  230. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  231. reportError(context, content->GetOriginalExpression(),
  232. "$<EQUAL> parameter " + parameters[1] +
  233. " is not a valid integer.");
  234. return std::string();
  235. }
  236. if (flipSign) {
  237. rnum = -rnum;
  238. }
  239. return lnum == rnum ? "1" : "0";
  240. }
  241. } equalNode;
  242. static const struct InListNode : public cmGeneratorExpressionNode
  243. {
  244. InListNode() {} // NOLINT(modernize-use-equals-default)
  245. int NumExpectedParameters() const override { return 2; }
  246. std::string Evaluate(
  247. const std::vector<std::string>& parameters,
  248. cmGeneratorExpressionContext* context,
  249. const GeneratorExpressionContent* /*content*/,
  250. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  251. {
  252. std::vector<std::string> values, checkValues;
  253. bool check = false;
  254. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0085)) {
  255. case cmPolicies::WARN:
  256. if (parameters.front().empty()) {
  257. check = true;
  258. cmSystemTools::ExpandListArgument(parameters[1], checkValues, true);
  259. }
  260. CM_FALLTHROUGH;
  261. case cmPolicies::OLD:
  262. cmSystemTools::ExpandListArgument(parameters[1], values);
  263. if (check && values != checkValues) {
  264. std::ostringstream e;
  265. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0085)
  266. << "\nSearch Item:\n \"" << parameters.front()
  267. << "\"\nList:\n \"" << parameters[1] << "\"\n";
  268. context->LG->GetCMakeInstance()->IssueMessage(
  269. MessageType ::AUTHOR_WARNING, e.str(), context->Backtrace);
  270. return "0";
  271. }
  272. if (values.empty()) {
  273. return "0";
  274. }
  275. break;
  276. case cmPolicies::REQUIRED_IF_USED:
  277. case cmPolicies::REQUIRED_ALWAYS:
  278. case cmPolicies::NEW:
  279. cmSystemTools::ExpandListArgument(parameters[1], values, true);
  280. break;
  281. }
  282. return std::find(values.cbegin(), values.cend(), parameters.front()) ==
  283. values.cend()
  284. ? "0"
  285. : "1";
  286. }
  287. } inListNode;
  288. static const struct FilterNode : public cmGeneratorExpressionNode
  289. {
  290. FilterNode() {} // NOLINT(modernize-use-equals-default)
  291. int NumExpectedParameters() const override { return 3; }
  292. std::string Evaluate(
  293. const std::vector<std::string>& parameters,
  294. cmGeneratorExpressionContext* context,
  295. const GeneratorExpressionContent* content,
  296. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  297. {
  298. if (parameters.size() != 3) {
  299. reportError(context, content->GetOriginalExpression(),
  300. "$<FILTER:...> expression requires three parameters");
  301. return {};
  302. }
  303. if (parameters[1] != "INCLUDE" && parameters[1] != "EXCLUDE") {
  304. reportError(
  305. context, content->GetOriginalExpression(),
  306. "$<FILTER:...> second parameter must be either INCLUDE or EXCLUDE");
  307. return {};
  308. }
  309. const bool exclude = parameters[1] == "EXCLUDE";
  310. cmsys::RegularExpression re;
  311. if (!re.compile(parameters[2])) {
  312. reportError(context, content->GetOriginalExpression(),
  313. "$<FILTER:...> failed to compile regex");
  314. return {};
  315. }
  316. std::vector<std::string> values, result;
  317. cmSystemTools::ExpandListArgument(parameters.front(), values, true);
  318. std::copy_if(values.cbegin(), values.cend(), std::back_inserter(result),
  319. [&re, exclude](std::string const& input) {
  320. return exclude ^ re.find(input);
  321. });
  322. return cmJoin(cmMakeRange(result.cbegin(), result.cend()), ";");
  323. }
  324. } filterNode;
  325. static const struct RemoveDuplicatesNode : public cmGeneratorExpressionNode
  326. {
  327. RemoveDuplicatesNode() {} // NOLINT(modernize-use-equals-default)
  328. int NumExpectedParameters() const override { return 1; }
  329. std::string Evaluate(
  330. const std::vector<std::string>& parameters,
  331. cmGeneratorExpressionContext* context,
  332. const GeneratorExpressionContent* content,
  333. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  334. {
  335. if (parameters.size() != 1) {
  336. reportError(
  337. context, content->GetOriginalExpression(),
  338. "$<REMOVE_DUPLICATES:...> expression requires one parameter");
  339. }
  340. std::vector<std::string> values;
  341. cmSystemTools::ExpandListArgument(parameters.front(), values, true);
  342. auto valuesEnd = cmRemoveDuplicates(values);
  343. auto valuesBegin = values.cbegin();
  344. return cmJoin(cmMakeRange(valuesBegin, valuesEnd), ";");
  345. }
  346. } removeDuplicatesNode;
  347. static const struct TargetExistsNode : public cmGeneratorExpressionNode
  348. {
  349. TargetExistsNode() {} // NOLINT(modernize-use-equals-default)
  350. int NumExpectedParameters() const override { return 1; }
  351. std::string Evaluate(
  352. const std::vector<std::string>& parameters,
  353. cmGeneratorExpressionContext* context,
  354. const GeneratorExpressionContent* content,
  355. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  356. {
  357. if (parameters.size() != 1) {
  358. reportError(context, content->GetOriginalExpression(),
  359. "$<TARGET_EXISTS:...> expression requires one parameter");
  360. return std::string();
  361. }
  362. std::string targetName = parameters.front();
  363. if (targetName.empty() ||
  364. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  365. reportError(context, content->GetOriginalExpression(),
  366. "$<TARGET_EXISTS:tgt> expression requires a non-empty "
  367. "valid target name.");
  368. return std::string();
  369. }
  370. return context->LG->GetMakefile()->FindTargetToUse(targetName) ? "1" : "0";
  371. }
  372. } targetExistsNode;
  373. static const struct TargetNameIfExistsNode : public cmGeneratorExpressionNode
  374. {
  375. TargetNameIfExistsNode() {} // NOLINT(modernize-use-equals-default)
  376. int NumExpectedParameters() const override { return 1; }
  377. std::string Evaluate(
  378. const std::vector<std::string>& parameters,
  379. cmGeneratorExpressionContext* context,
  380. const GeneratorExpressionContent* content,
  381. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  382. {
  383. if (parameters.size() != 1) {
  384. reportError(context, content->GetOriginalExpression(),
  385. "$<TARGET_NAME_IF_EXISTS:...> expression requires one "
  386. "parameter");
  387. return std::string();
  388. }
  389. std::string targetName = parameters.front();
  390. if (targetName.empty() ||
  391. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  392. reportError(context, content->GetOriginalExpression(),
  393. "$<TARGET_NAME_IF_EXISTS:tgt> expression requires a "
  394. "non-empty valid target name.");
  395. return std::string();
  396. }
  397. return context->LG->GetMakefile()->FindTargetToUse(targetName)
  398. ? targetName
  399. : std::string();
  400. }
  401. } targetNameIfExistsNode;
  402. struct GenexEvaluator : public cmGeneratorExpressionNode
  403. {
  404. GenexEvaluator() {} // NOLINT(modernize-use-equals-default)
  405. protected:
  406. std::string EvaluateExpression(
  407. const std::string& genexOperator, const std::string& expression,
  408. cmGeneratorExpressionContext* context,
  409. const GeneratorExpressionContent* content,
  410. cmGeneratorExpressionDAGChecker* dagCheckerParent) const
  411. {
  412. if (context->HeadTarget) {
  413. cmGeneratorExpressionDAGChecker dagChecker(
  414. context->Backtrace, context->HeadTarget,
  415. genexOperator + ":" + expression, content, dagCheckerParent);
  416. switch (dagChecker.Check()) {
  417. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  418. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE: {
  419. dagChecker.ReportError(context, content->GetOriginalExpression());
  420. return std::string();
  421. }
  422. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  423. case cmGeneratorExpressionDAGChecker::DAG:
  424. break;
  425. }
  426. return this->EvaluateDependentExpression(
  427. expression, context->LG, context, context->HeadTarget,
  428. context->CurrentTarget, &dagChecker);
  429. }
  430. return this->EvaluateDependentExpression(
  431. expression, context->LG, context, context->HeadTarget,
  432. context->CurrentTarget, dagCheckerParent);
  433. }
  434. };
  435. static const struct TargetGenexEvalNode : public GenexEvaluator
  436. {
  437. TargetGenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  438. int NumExpectedParameters() const override { return 2; }
  439. bool AcceptsArbitraryContentParameter() const override { return true; }
  440. std::string Evaluate(
  441. const std::vector<std::string>& parameters,
  442. cmGeneratorExpressionContext* context,
  443. const GeneratorExpressionContent* content,
  444. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  445. {
  446. const std::string& targetName = parameters.front();
  447. if (targetName.empty() ||
  448. !cmGeneratorExpression::IsValidTargetName(targetName)) {
  449. reportError(context, content->GetOriginalExpression(),
  450. "$<TARGET_GENEX_EVAL:tgt, ...> expression requires a "
  451. "non-empty valid target name.");
  452. return std::string();
  453. }
  454. const auto* target = context->LG->FindGeneratorTargetToUse(targetName);
  455. if (!target) {
  456. std::ostringstream e;
  457. e << "$<TARGET_GENEX_EVAL:tgt, ...> target \"" << targetName
  458. << "\" not found.";
  459. reportError(context, content->GetOriginalExpression(), e.str());
  460. return std::string();
  461. }
  462. const std::string& expression = parameters[1];
  463. if (expression.empty()) {
  464. return expression;
  465. }
  466. cmGeneratorExpressionContext targetContext(
  467. context->LG, context->Config, context->Quiet, target, target,
  468. context->EvaluateForBuildsystem, context->Backtrace, context->Language);
  469. return this->EvaluateExpression("TARGET_GENEX_EVAL", expression,
  470. &targetContext, content, dagCheckerParent);
  471. }
  472. } targetGenexEvalNode;
  473. static const struct GenexEvalNode : public GenexEvaluator
  474. {
  475. GenexEvalNode() {} // NOLINT(modernize-use-equals-default)
  476. int NumExpectedParameters() const override { return 1; }
  477. bool AcceptsArbitraryContentParameter() const override { return true; }
  478. std::string Evaluate(
  479. const std::vector<std::string>& parameters,
  480. cmGeneratorExpressionContext* context,
  481. const GeneratorExpressionContent* content,
  482. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  483. {
  484. const std::string& expression = parameters[0];
  485. if (expression.empty()) {
  486. return expression;
  487. }
  488. return this->EvaluateExpression("GENEX_EVAL", expression, context, content,
  489. dagCheckerParent);
  490. }
  491. } genexEvalNode;
  492. static const struct LowerCaseNode : public cmGeneratorExpressionNode
  493. {
  494. LowerCaseNode() {} // NOLINT(modernize-use-equals-default)
  495. bool AcceptsArbitraryContentParameter() const override { return true; }
  496. std::string Evaluate(
  497. const std::vector<std::string>& parameters,
  498. cmGeneratorExpressionContext* /*context*/,
  499. const GeneratorExpressionContent* /*content*/,
  500. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  501. {
  502. return cmSystemTools::LowerCase(parameters.front());
  503. }
  504. } lowerCaseNode;
  505. static const struct UpperCaseNode : public cmGeneratorExpressionNode
  506. {
  507. UpperCaseNode() {} // NOLINT(modernize-use-equals-default)
  508. bool AcceptsArbitraryContentParameter() const override { return true; }
  509. std::string Evaluate(
  510. const std::vector<std::string>& parameters,
  511. cmGeneratorExpressionContext* /*context*/,
  512. const GeneratorExpressionContent* /*content*/,
  513. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  514. {
  515. return cmSystemTools::UpperCase(parameters.front());
  516. }
  517. } upperCaseNode;
  518. static const struct MakeCIdentifierNode : public cmGeneratorExpressionNode
  519. {
  520. MakeCIdentifierNode() {} // NOLINT(modernize-use-equals-default)
  521. bool AcceptsArbitraryContentParameter() const override { return true; }
  522. std::string Evaluate(
  523. const std::vector<std::string>& parameters,
  524. cmGeneratorExpressionContext* /*context*/,
  525. const GeneratorExpressionContent* /*content*/,
  526. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  527. {
  528. return cmSystemTools::MakeCidentifier(parameters.front());
  529. }
  530. } makeCIdentifierNode;
  531. static const struct Angle_RNode : public cmGeneratorExpressionNode
  532. {
  533. Angle_RNode() {} // NOLINT(modernize-use-equals-default)
  534. int NumExpectedParameters() const override { return 0; }
  535. std::string Evaluate(
  536. const std::vector<std::string>& /*parameters*/,
  537. cmGeneratorExpressionContext* /*context*/,
  538. const GeneratorExpressionContent* /*content*/,
  539. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  540. {
  541. return ">";
  542. }
  543. } angle_rNode;
  544. static const struct CommaNode : public cmGeneratorExpressionNode
  545. {
  546. CommaNode() {} // NOLINT(modernize-use-equals-default)
  547. int NumExpectedParameters() const override { return 0; }
  548. std::string Evaluate(
  549. const std::vector<std::string>& /*parameters*/,
  550. cmGeneratorExpressionContext* /*context*/,
  551. const GeneratorExpressionContent* /*content*/,
  552. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  553. {
  554. return ",";
  555. }
  556. } commaNode;
  557. static const struct SemicolonNode : public cmGeneratorExpressionNode
  558. {
  559. SemicolonNode() {} // NOLINT(modernize-use-equals-default)
  560. int NumExpectedParameters() const override { return 0; }
  561. std::string Evaluate(
  562. const std::vector<std::string>& /*parameters*/,
  563. cmGeneratorExpressionContext* /*context*/,
  564. const GeneratorExpressionContent* /*content*/,
  565. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  566. {
  567. return ";";
  568. }
  569. } semicolonNode;
  570. struct CompilerIdNode : public cmGeneratorExpressionNode
  571. {
  572. CompilerIdNode() {} // NOLINT(modernize-use-equals-default)
  573. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  574. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  575. cmGeneratorExpressionContext* context,
  576. const GeneratorExpressionContent* content,
  577. cmGeneratorExpressionDAGChecker* /*unused*/,
  578. const std::string& lang) const
  579. {
  580. std::string const& compilerId =
  581. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  582. "_COMPILER_ID");
  583. if (parameters.empty()) {
  584. return compilerId;
  585. }
  586. static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
  587. if (!compilerIdValidator.find(parameters.front())) {
  588. reportError(context, content->GetOriginalExpression(),
  589. "Expression syntax not recognized.");
  590. return std::string();
  591. }
  592. if (compilerId.empty()) {
  593. return parameters.front().empty() ? "1" : "0";
  594. }
  595. if (strcmp(parameters.front().c_str(), compilerId.c_str()) == 0) {
  596. return "1";
  597. }
  598. if (cmsysString_strcasecmp(parameters.front().c_str(),
  599. compilerId.c_str()) == 0) {
  600. switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
  601. case cmPolicies::WARN: {
  602. std::ostringstream e;
  603. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0044);
  604. context->LG->GetCMakeInstance()->IssueMessage(
  605. MessageType::AUTHOR_WARNING, e.str(), context->Backtrace);
  606. CM_FALLTHROUGH;
  607. }
  608. case cmPolicies::OLD:
  609. return "1";
  610. case cmPolicies::NEW:
  611. case cmPolicies::REQUIRED_ALWAYS:
  612. case cmPolicies::REQUIRED_IF_USED:
  613. break;
  614. }
  615. }
  616. return "0";
  617. }
  618. };
  619. static const struct CCompilerIdNode : public CompilerIdNode
  620. {
  621. CCompilerIdNode() {} // NOLINT(modernize-use-equals-default)
  622. std::string Evaluate(
  623. const std::vector<std::string>& parameters,
  624. cmGeneratorExpressionContext* context,
  625. const GeneratorExpressionContent* content,
  626. cmGeneratorExpressionDAGChecker* dagChecker) const override
  627. {
  628. if (!context->HeadTarget) {
  629. reportError(
  630. context, content->GetOriginalExpression(),
  631. "$<C_COMPILER_ID> may only be used with binary targets. It may "
  632. "not be used with add_custom_command or add_custom_target.");
  633. return std::string();
  634. }
  635. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  636. "C");
  637. }
  638. } cCompilerIdNode;
  639. static const struct CXXCompilerIdNode : public CompilerIdNode
  640. {
  641. CXXCompilerIdNode() {} // NOLINT(modernize-use-equals-default)
  642. std::string Evaluate(
  643. const std::vector<std::string>& parameters,
  644. cmGeneratorExpressionContext* context,
  645. const GeneratorExpressionContent* content,
  646. cmGeneratorExpressionDAGChecker* dagChecker) const override
  647. {
  648. if (!context->HeadTarget) {
  649. reportError(
  650. context, content->GetOriginalExpression(),
  651. "$<CXX_COMPILER_ID> may only be used with binary targets. It may "
  652. "not be used with add_custom_command or add_custom_target.");
  653. return std::string();
  654. }
  655. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  656. "CXX");
  657. }
  658. } cxxCompilerIdNode;
  659. static const struct CUDACompilerIdNode : public CompilerIdNode
  660. {
  661. CUDACompilerIdNode() {} // NOLINT(modernize-use-equals-default)
  662. std::string Evaluate(
  663. const std::vector<std::string>& parameters,
  664. cmGeneratorExpressionContext* context,
  665. const GeneratorExpressionContent* content,
  666. cmGeneratorExpressionDAGChecker* dagChecker) const override
  667. {
  668. if (!context->HeadTarget) {
  669. reportError(
  670. context, content->GetOriginalExpression(),
  671. "$<CUDA_COMPILER_ID> may only be used with binary targets. It may "
  672. "not be used with add_custom_command or add_custom_target.");
  673. return std::string();
  674. }
  675. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  676. "CUDA");
  677. }
  678. } cudaCompilerIdNode;
  679. static const struct FortranCompilerIdNode : public CompilerIdNode
  680. {
  681. FortranCompilerIdNode() {} // NOLINT(modernize-use-equals-default)
  682. std::string Evaluate(
  683. const std::vector<std::string>& parameters,
  684. cmGeneratorExpressionContext* context,
  685. const GeneratorExpressionContent* content,
  686. cmGeneratorExpressionDAGChecker* dagChecker) const override
  687. {
  688. if (!context->HeadTarget) {
  689. reportError(
  690. context, content->GetOriginalExpression(),
  691. "$<Fortran_COMPILER_ID> may only be used with binary targets. It may "
  692. "not be used with add_custom_command or add_custom_target.");
  693. return std::string();
  694. }
  695. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  696. "Fortran");
  697. }
  698. } fortranCompilerIdNode;
  699. struct CompilerVersionNode : public cmGeneratorExpressionNode
  700. {
  701. CompilerVersionNode() {} // NOLINT(modernize-use-equals-default)
  702. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  703. std::string EvaluateWithLanguage(const std::vector<std::string>& parameters,
  704. cmGeneratorExpressionContext* context,
  705. const GeneratorExpressionContent* content,
  706. cmGeneratorExpressionDAGChecker* /*unused*/,
  707. const std::string& lang) const
  708. {
  709. std::string const& compilerVersion =
  710. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_" + lang +
  711. "_COMPILER_VERSION");
  712. if (parameters.empty()) {
  713. return compilerVersion;
  714. }
  715. static cmsys::RegularExpression compilerIdValidator("^[0-9\\.]*$");
  716. if (!compilerIdValidator.find(parameters.front())) {
  717. reportError(context, content->GetOriginalExpression(),
  718. "Expression syntax not recognized.");
  719. return std::string();
  720. }
  721. if (compilerVersion.empty()) {
  722. return parameters.front().empty() ? "1" : "0";
  723. }
  724. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  725. parameters.front().c_str(),
  726. compilerVersion.c_str())
  727. ? "1"
  728. : "0";
  729. }
  730. };
  731. static const struct CCompilerVersionNode : public CompilerVersionNode
  732. {
  733. CCompilerVersionNode() {} // NOLINT(modernize-use-equals-default)
  734. std::string Evaluate(
  735. const std::vector<std::string>& parameters,
  736. cmGeneratorExpressionContext* context,
  737. const GeneratorExpressionContent* content,
  738. cmGeneratorExpressionDAGChecker* dagChecker) const override
  739. {
  740. if (!context->HeadTarget) {
  741. reportError(
  742. context, content->GetOriginalExpression(),
  743. "$<C_COMPILER_VERSION> may only be used with binary targets. It "
  744. "may not be used with add_custom_command or add_custom_target.");
  745. return std::string();
  746. }
  747. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  748. "C");
  749. }
  750. } cCompilerVersionNode;
  751. static const struct CXXCompilerVersionNode : public CompilerVersionNode
  752. {
  753. CXXCompilerVersionNode() {} // NOLINT(modernize-use-equals-default)
  754. std::string Evaluate(
  755. const std::vector<std::string>& parameters,
  756. cmGeneratorExpressionContext* context,
  757. const GeneratorExpressionContent* content,
  758. cmGeneratorExpressionDAGChecker* dagChecker) const override
  759. {
  760. if (!context->HeadTarget) {
  761. reportError(
  762. context, content->GetOriginalExpression(),
  763. "$<CXX_COMPILER_VERSION> may only be used with binary targets. It "
  764. "may not be used with add_custom_command or add_custom_target.");
  765. return std::string();
  766. }
  767. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  768. "CXX");
  769. }
  770. } cxxCompilerVersionNode;
  771. static const struct CUDACompilerVersionNode : public CompilerVersionNode
  772. {
  773. CUDACompilerVersionNode() {} // NOLINT(modernize-use-equals-default)
  774. std::string Evaluate(
  775. const std::vector<std::string>& parameters,
  776. cmGeneratorExpressionContext* context,
  777. const GeneratorExpressionContent* content,
  778. cmGeneratorExpressionDAGChecker* dagChecker) const override
  779. {
  780. if (!context->HeadTarget) {
  781. reportError(
  782. context, content->GetOriginalExpression(),
  783. "$<CUDA_COMPILER_VERSION> may only be used with binary targets. It "
  784. "may not be used with add_custom_command or add_custom_target.");
  785. return std::string();
  786. }
  787. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  788. "CUDA");
  789. }
  790. } cudaCompilerVersionNode;
  791. static const struct FortranCompilerVersionNode : public CompilerVersionNode
  792. {
  793. FortranCompilerVersionNode() {} // NOLINT(modernize-use-equals-default)
  794. std::string Evaluate(
  795. const std::vector<std::string>& parameters,
  796. cmGeneratorExpressionContext* context,
  797. const GeneratorExpressionContent* content,
  798. cmGeneratorExpressionDAGChecker* dagChecker) const override
  799. {
  800. if (!context->HeadTarget) {
  801. reportError(
  802. context, content->GetOriginalExpression(),
  803. "$<Fortran_COMPILER_VERSION> may only be used with binary targets. "
  804. "It may not be used with add_custom_command or add_custom_target.");
  805. return std::string();
  806. }
  807. return this->EvaluateWithLanguage(parameters, context, content, dagChecker,
  808. "Fortran");
  809. }
  810. } fortranCompilerVersionNode;
  811. struct PlatformIdNode : public cmGeneratorExpressionNode
  812. {
  813. PlatformIdNode() {} // NOLINT(modernize-use-equals-default)
  814. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  815. std::string Evaluate(
  816. const std::vector<std::string>& parameters,
  817. cmGeneratorExpressionContext* context,
  818. const GeneratorExpressionContent* /*content*/,
  819. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  820. {
  821. std::string const& platformId =
  822. context->LG->GetMakefile()->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  823. if (parameters.empty()) {
  824. return platformId;
  825. }
  826. if (platformId.empty()) {
  827. return parameters.front().empty() ? "1" : "0";
  828. }
  829. if (parameters.front() == platformId) {
  830. return "1";
  831. }
  832. return "0";
  833. }
  834. } platformIdNode;
  835. static const struct VersionGreaterNode : public cmGeneratorExpressionNode
  836. {
  837. VersionGreaterNode() {} // NOLINT(modernize-use-equals-default)
  838. int NumExpectedParameters() const override { return 2; }
  839. std::string Evaluate(
  840. const std::vector<std::string>& parameters,
  841. cmGeneratorExpressionContext* /*context*/,
  842. const GeneratorExpressionContent* /*content*/,
  843. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  844. {
  845. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER,
  846. parameters.front().c_str(),
  847. parameters[1].c_str())
  848. ? "1"
  849. : "0";
  850. }
  851. } versionGreaterNode;
  852. static const struct VersionGreaterEqNode : public cmGeneratorExpressionNode
  853. {
  854. VersionGreaterEqNode() {} // NOLINT(modernize-use-equals-default)
  855. int NumExpectedParameters() const override { return 2; }
  856. std::string Evaluate(
  857. const std::vector<std::string>& parameters,
  858. cmGeneratorExpressionContext* /*context*/,
  859. const GeneratorExpressionContent* /*content*/,
  860. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  861. {
  862. return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  863. parameters.front().c_str(),
  864. parameters[1].c_str())
  865. ? "1"
  866. : "0";
  867. }
  868. } versionGreaterEqNode;
  869. static const struct VersionLessNode : public cmGeneratorExpressionNode
  870. {
  871. VersionLessNode() {} // NOLINT(modernize-use-equals-default)
  872. int NumExpectedParameters() const override { return 2; }
  873. std::string Evaluate(
  874. const std::vector<std::string>& parameters,
  875. cmGeneratorExpressionContext* /*context*/,
  876. const GeneratorExpressionContent* /*content*/,
  877. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  878. {
  879. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  880. parameters.front().c_str(),
  881. parameters[1].c_str())
  882. ? "1"
  883. : "0";
  884. }
  885. } versionLessNode;
  886. static const struct VersionLessEqNode : public cmGeneratorExpressionNode
  887. {
  888. VersionLessEqNode() {} // NOLINT(modernize-use-equals-default)
  889. int NumExpectedParameters() const override { return 2; }
  890. std::string Evaluate(
  891. const std::vector<std::string>& parameters,
  892. cmGeneratorExpressionContext* /*context*/,
  893. const GeneratorExpressionContent* /*content*/,
  894. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  895. {
  896. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS_EQUAL,
  897. parameters.front().c_str(),
  898. parameters[1].c_str())
  899. ? "1"
  900. : "0";
  901. }
  902. } versionLessEqNode;
  903. static const struct VersionEqualNode : public cmGeneratorExpressionNode
  904. {
  905. VersionEqualNode() {} // NOLINT(modernize-use-equals-default)
  906. int NumExpectedParameters() const override { return 2; }
  907. std::string Evaluate(
  908. const std::vector<std::string>& parameters,
  909. cmGeneratorExpressionContext* /*context*/,
  910. const GeneratorExpressionContent* /*content*/,
  911. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  912. {
  913. return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  914. parameters.front().c_str(),
  915. parameters[1].c_str())
  916. ? "1"
  917. : "0";
  918. }
  919. } versionEqualNode;
  920. static const struct LinkOnlyNode : public cmGeneratorExpressionNode
  921. {
  922. LinkOnlyNode() {} // NOLINT(modernize-use-equals-default)
  923. std::string Evaluate(
  924. const std::vector<std::string>& parameters,
  925. cmGeneratorExpressionContext* context,
  926. const GeneratorExpressionContent* content,
  927. cmGeneratorExpressionDAGChecker* dagChecker) const override
  928. {
  929. if (!dagChecker) {
  930. reportError(context, content->GetOriginalExpression(),
  931. "$<LINK_ONLY:...> may only be used for linking");
  932. return std::string();
  933. }
  934. if (!dagChecker->GetTransitivePropertiesOnly()) {
  935. return parameters.front();
  936. }
  937. return std::string();
  938. }
  939. } linkOnlyNode;
  940. static const struct ConfigurationNode : public cmGeneratorExpressionNode
  941. {
  942. ConfigurationNode() {} // NOLINT(modernize-use-equals-default)
  943. int NumExpectedParameters() const override { return 0; }
  944. std::string Evaluate(
  945. const std::vector<std::string>& /*parameters*/,
  946. cmGeneratorExpressionContext* context,
  947. const GeneratorExpressionContent* /*content*/,
  948. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  949. {
  950. context->HadContextSensitiveCondition = true;
  951. return context->Config;
  952. }
  953. } configurationNode;
  954. static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
  955. {
  956. ConfigurationTestNode() {} // NOLINT(modernize-use-equals-default)
  957. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  958. std::string Evaluate(
  959. const std::vector<std::string>& parameters,
  960. cmGeneratorExpressionContext* context,
  961. const GeneratorExpressionContent* content,
  962. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  963. {
  964. if (parameters.empty()) {
  965. return configurationNode.Evaluate(parameters, context, content, nullptr);
  966. }
  967. static cmsys::RegularExpression configValidator("^[A-Za-z0-9_]*$");
  968. if (!configValidator.find(parameters.front())) {
  969. reportError(context, content->GetOriginalExpression(),
  970. "Expression syntax not recognized.");
  971. return std::string();
  972. }
  973. context->HadContextSensitiveCondition = true;
  974. if (context->Config.empty()) {
  975. return parameters.front().empty() ? "1" : "0";
  976. }
  977. if (cmsysString_strcasecmp(parameters.front().c_str(),
  978. context->Config.c_str()) == 0) {
  979. return "1";
  980. }
  981. if (context->CurrentTarget && context->CurrentTarget->IsImported()) {
  982. const char* loc = nullptr;
  983. const char* imp = nullptr;
  984. std::string suffix;
  985. if (context->CurrentTarget->Target->GetMappedConfig(
  986. context->Config, &loc, &imp, suffix)) {
  987. // This imported target has an appropriate location
  988. // for this (possibly mapped) config.
  989. // Check if there is a proper config mapping for the tested config.
  990. std::vector<std::string> mappedConfigs;
  991. std::string mapProp = "MAP_IMPORTED_CONFIG_";
  992. mapProp += cmSystemTools::UpperCase(context->Config);
  993. if (const char* mapValue =
  994. context->CurrentTarget->GetProperty(mapProp)) {
  995. cmSystemTools::ExpandListArgument(cmSystemTools::UpperCase(mapValue),
  996. mappedConfigs);
  997. return std::find(mappedConfigs.begin(), mappedConfigs.end(),
  998. cmSystemTools::UpperCase(parameters.front())) !=
  999. mappedConfigs.end()
  1000. ? "1"
  1001. : "0";
  1002. }
  1003. }
  1004. }
  1005. return "0";
  1006. }
  1007. } configurationTestNode;
  1008. static const struct JoinNode : public cmGeneratorExpressionNode
  1009. {
  1010. JoinNode() {} // NOLINT(modernize-use-equals-default)
  1011. int NumExpectedParameters() const override { return 2; }
  1012. bool AcceptsArbitraryContentParameter() const override { return true; }
  1013. std::string Evaluate(
  1014. const std::vector<std::string>& parameters,
  1015. cmGeneratorExpressionContext* /*context*/,
  1016. const GeneratorExpressionContent* /*content*/,
  1017. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1018. {
  1019. std::vector<std::string> list;
  1020. cmSystemTools::ExpandListArgument(parameters.front(), list);
  1021. return cmJoin(list, parameters[1]);
  1022. }
  1023. } joinNode;
  1024. static const struct CompileLanguageNode : public cmGeneratorExpressionNode
  1025. {
  1026. CompileLanguageNode() {} // NOLINT(modernize-use-equals-default)
  1027. int NumExpectedParameters() const override { return OneOrZeroParameters; }
  1028. std::string Evaluate(
  1029. const std::vector<std::string>& parameters,
  1030. cmGeneratorExpressionContext* context,
  1031. const GeneratorExpressionContent* content,
  1032. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1033. {
  1034. if (context->Language.empty()) {
  1035. reportError(
  1036. context, content->GetOriginalExpression(),
  1037. "$<COMPILE_LANGUAGE:...> may only be used to specify include "
  1038. "directories, compile definitions, compile options, and to evaluate "
  1039. "components of the file(GENERATE) command.");
  1040. return std::string();
  1041. }
  1042. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1043. std::string genName = gg->GetName();
  1044. if (genName.find("Makefiles") == std::string::npos &&
  1045. genName.find("Ninja") == std::string::npos &&
  1046. genName.find("Visual Studio") == std::string::npos &&
  1047. genName.find("Xcode") == std::string::npos &&
  1048. genName.find("Watcom WMake") == std::string::npos) {
  1049. reportError(context, content->GetOriginalExpression(),
  1050. "$<COMPILE_LANGUAGE:...> not supported for this generator.");
  1051. return std::string();
  1052. }
  1053. if (parameters.empty()) {
  1054. return context->Language;
  1055. }
  1056. return context->Language == parameters.front() ? "1" : "0";
  1057. }
  1058. } languageNode;
  1059. #define TRANSITIVE_PROPERTY_NAME(PROPERTY) , "INTERFACE_" #PROPERTY
  1060. static const char* targetPropertyTransitiveWhitelist[] = {
  1061. nullptr CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_NAME)
  1062. };
  1063. #undef TRANSITIVE_PROPERTY_NAME
  1064. template <typename T>
  1065. std::string getLinkedTargetsContent(
  1066. std::vector<T> const& libraries, cmGeneratorTarget const* target,
  1067. cmGeneratorTarget const* headTarget, cmGeneratorExpressionContext* context,
  1068. cmGeneratorExpressionDAGChecker* dagChecker,
  1069. const std::string& interfacePropertyName)
  1070. {
  1071. std::string linkedTargetsContent;
  1072. std::string sep;
  1073. std::string depString;
  1074. for (T const& l : libraries) {
  1075. // Broken code can have a target in its own link interface.
  1076. // Don't follow such link interface entries so as not to create a
  1077. // self-referencing loop.
  1078. if (l.Target && l.Target != target) {
  1079. std::string uniqueName =
  1080. target->GetGlobalGenerator()->IndexGeneratorTargetUniquely(l.Target);
  1081. depString += sep + "$<TARGET_PROPERTY:" + std::move(uniqueName) + "," +
  1082. interfacePropertyName + ">";
  1083. sep = ";";
  1084. }
  1085. }
  1086. if (!depString.empty()) {
  1087. linkedTargetsContent =
  1088. cmGeneratorExpressionNode::EvaluateDependentExpression(
  1089. depString, target->GetLocalGenerator(), context, headTarget, target,
  1090. dagChecker);
  1091. }
  1092. linkedTargetsContent =
  1093. cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);
  1094. return linkedTargetsContent;
  1095. }
  1096. static const struct TargetPropertyNode : public cmGeneratorExpressionNode
  1097. {
  1098. TargetPropertyNode() {} // NOLINT(modernize-use-equals-default)
  1099. // This node handles errors on parameter count itself.
  1100. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1101. std::string Evaluate(
  1102. const std::vector<std::string>& parameters,
  1103. cmGeneratorExpressionContext* context,
  1104. const GeneratorExpressionContent* content,
  1105. cmGeneratorExpressionDAGChecker* dagCheckerParent) const override
  1106. {
  1107. if (parameters.size() != 1 && parameters.size() != 2) {
  1108. reportError(
  1109. context, content->GetOriginalExpression(),
  1110. "$<TARGET_PROPERTY:...> expression requires one or two parameters");
  1111. return std::string();
  1112. }
  1113. static cmsys::RegularExpression propertyNameValidator("^[A-Za-z0-9_]+$");
  1114. cmGeneratorTarget const* target = context->HeadTarget;
  1115. std::string propertyName = parameters.front();
  1116. if (parameters.size() == 1) {
  1117. context->HadHeadSensitiveCondition = true;
  1118. }
  1119. if (!target && parameters.size() == 1) {
  1120. reportError(
  1121. context, content->GetOriginalExpression(),
  1122. "$<TARGET_PROPERTY:prop> may only be used with binary targets. "
  1123. "It may not be used with add_custom_command or add_custom_target. "
  1124. "Specify the target to read a property from using the "
  1125. "$<TARGET_PROPERTY:tgt,prop> signature instead.");
  1126. return std::string();
  1127. }
  1128. if (parameters.size() == 2) {
  1129. if (parameters.front().empty() && parameters[1].empty()) {
  1130. reportError(
  1131. context, content->GetOriginalExpression(),
  1132. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1133. "target name and property name.");
  1134. return std::string();
  1135. }
  1136. if (parameters.front().empty()) {
  1137. reportError(
  1138. context, content->GetOriginalExpression(),
  1139. "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
  1140. "target name.");
  1141. return std::string();
  1142. }
  1143. std::string targetName = parameters.front();
  1144. propertyName = parameters[1];
  1145. if (!cmGeneratorExpression::IsValidTargetName(targetName)) {
  1146. if (!propertyNameValidator.find(propertyName)) {
  1147. ::reportError(context, content->GetOriginalExpression(),
  1148. "Target name and property name not supported.");
  1149. return std::string();
  1150. }
  1151. ::reportError(context, content->GetOriginalExpression(),
  1152. "Target name not supported.");
  1153. return std::string();
  1154. }
  1155. static const std::string propALIASED_TARGET = "ALIASED_TARGET";
  1156. if (propertyName == propALIASED_TARGET) {
  1157. if (context->LG->GetMakefile()->IsAlias(targetName)) {
  1158. if (cmGeneratorTarget* tgt =
  1159. context->LG->FindGeneratorTargetToUse(targetName)) {
  1160. return tgt->GetName();
  1161. }
  1162. }
  1163. return "";
  1164. }
  1165. target = context->LG->FindGeneratorTargetToUse(targetName);
  1166. if (!target) {
  1167. std::ostringstream e;
  1168. e << "Target \"" << targetName << "\" not found.";
  1169. reportError(context, content->GetOriginalExpression(), e.str());
  1170. return std::string();
  1171. }
  1172. context->AllTargets.insert(target);
  1173. }
  1174. if (target == context->HeadTarget) {
  1175. // Keep track of the properties seen while processing.
  1176. // The evaluation of the LINK_LIBRARIES generator expressions
  1177. // will check this to ensure that properties have one consistent
  1178. // value for all evaluations.
  1179. context->SeenTargetProperties.insert(propertyName);
  1180. }
  1181. if (propertyName == "SOURCES") {
  1182. context->SourceSensitiveTargets.insert(target);
  1183. }
  1184. if (propertyName.empty()) {
  1185. reportError(
  1186. context, content->GetOriginalExpression(),
  1187. "$<TARGET_PROPERTY:...> expression requires a non-empty property "
  1188. "name.");
  1189. return std::string();
  1190. }
  1191. if (!propertyNameValidator.find(propertyName)) {
  1192. ::reportError(context, content->GetOriginalExpression(),
  1193. "Property name not supported.");
  1194. return std::string();
  1195. }
  1196. assert(target);
  1197. if (propertyName == "LINKER_LANGUAGE") {
  1198. if (target->LinkLanguagePropagatesToDependents() && dagCheckerParent &&
  1199. (dagCheckerParent->EvaluatingLinkLibraries() ||
  1200. dagCheckerParent->EvaluatingSources())) {
  1201. reportError(
  1202. context, content->GetOriginalExpression(),
  1203. "LINKER_LANGUAGE target property can not be used while evaluating "
  1204. "link libraries for a static library");
  1205. return std::string();
  1206. }
  1207. return target->GetLinkerLanguage(context->Config);
  1208. }
  1209. cmGeneratorExpressionDAGChecker dagChecker(
  1210. context->Backtrace, target, propertyName, content, dagCheckerParent);
  1211. switch (dagChecker.Check()) {
  1212. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  1213. dagChecker.ReportError(context, content->GetOriginalExpression());
  1214. return std::string();
  1215. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  1216. // No error. We just skip cyclic references.
  1217. return std::string();
  1218. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  1219. for (size_t i = 1; i < cm::size(targetPropertyTransitiveWhitelist);
  1220. ++i) {
  1221. if (targetPropertyTransitiveWhitelist[i] == propertyName) {
  1222. // No error. We're not going to find anything new here.
  1223. return std::string();
  1224. }
  1225. }
  1226. case cmGeneratorExpressionDAGChecker::DAG:
  1227. break;
  1228. }
  1229. const char* prop = target->GetProperty(propertyName);
  1230. if (dagCheckerParent) {
  1231. if (dagCheckerParent->EvaluatingGenexExpression() ||
  1232. dagCheckerParent->EvaluatingPICExpression()) {
  1233. // No check required.
  1234. } else if (dagCheckerParent->EvaluatingLinkLibraries()) {
  1235. #define TRANSITIVE_PROPERTY_COMPARE(PROPERTY) \
  1236. (#PROPERTY == propertyName || "INTERFACE_" #PROPERTY == propertyName) ||
  1237. if (CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(
  1238. TRANSITIVE_PROPERTY_COMPARE) false) { // NOLINT(*)
  1239. reportError(
  1240. context, content->GetOriginalExpression(),
  1241. "$<TARGET_PROPERTY:...> expression in link libraries "
  1242. "evaluation depends on target property which is transitive "
  1243. "over the link libraries, creating a recursion.");
  1244. return std::string();
  1245. }
  1246. #undef TRANSITIVE_PROPERTY_COMPARE
  1247. if (!prop) {
  1248. return std::string();
  1249. }
  1250. } else {
  1251. #define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) dagCheckerParent->METHOD() ||
  1252. assert(CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
  1253. ASSERT_TRANSITIVE_PROPERTY_METHOD) false); // NOLINT(clang-tidy)
  1254. #undef ASSERT_TRANSITIVE_PROPERTY_METHOD
  1255. }
  1256. }
  1257. std::string linkedTargetsContent;
  1258. std::string interfacePropertyName;
  1259. bool isInterfaceProperty = false;
  1260. #define POPULATE_INTERFACE_PROPERTY_NAME(prop) \
  1261. if (propertyName == #prop) { \
  1262. interfacePropertyName = "INTERFACE_" #prop; \
  1263. } else if (propertyName == "INTERFACE_" #prop) { \
  1264. interfacePropertyName = "INTERFACE_" #prop; \
  1265. isInterfaceProperty = true; \
  1266. } else
  1267. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(POPULATE_INTERFACE_PROPERTY_NAME)
  1268. // Note that the above macro terminates with an else
  1269. /* else */ if (cmHasLiteralPrefix(propertyName, "COMPILE_DEFINITIONS_")) {
  1270. cmPolicies::PolicyStatus polSt =
  1271. context->LG->GetPolicyStatus(cmPolicies::CMP0043);
  1272. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1273. interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
  1274. }
  1275. }
  1276. #undef POPULATE_INTERFACE_PROPERTY_NAME
  1277. cmGeneratorTarget const* headTarget =
  1278. context->HeadTarget && isInterfaceProperty ? context->HeadTarget
  1279. : target;
  1280. if (isInterfaceProperty) {
  1281. if (cmLinkInterfaceLibraries const* iface =
  1282. target->GetLinkInterfaceLibraries(context->Config, headTarget,
  1283. true)) {
  1284. linkedTargetsContent =
  1285. getLinkedTargetsContent(iface->Libraries, target, headTarget,
  1286. context, &dagChecker, interfacePropertyName);
  1287. }
  1288. } else if (!interfacePropertyName.empty()) {
  1289. if (cmLinkImplementationLibraries const* impl =
  1290. target->GetLinkImplementationLibraries(context->Config)) {
  1291. linkedTargetsContent =
  1292. getLinkedTargetsContent(impl->Libraries, target, target, context,
  1293. &dagChecker, interfacePropertyName);
  1294. }
  1295. }
  1296. if (!prop) {
  1297. if (target->IsImported() ||
  1298. target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  1299. return linkedTargetsContent;
  1300. }
  1301. if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
  1302. context->Config)) {
  1303. context->HadContextSensitiveCondition = true;
  1304. return target->GetLinkInterfaceDependentBoolProperty(propertyName,
  1305. context->Config)
  1306. ? "1"
  1307. : "0";
  1308. }
  1309. if (target->IsLinkInterfaceDependentStringProperty(propertyName,
  1310. context->Config)) {
  1311. context->HadContextSensitiveCondition = true;
  1312. const char* propContent =
  1313. target->GetLinkInterfaceDependentStringProperty(propertyName,
  1314. context->Config);
  1315. return propContent ? propContent : "";
  1316. }
  1317. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1318. context->Config)) {
  1319. context->HadContextSensitiveCondition = true;
  1320. const char* propContent =
  1321. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1322. context->Config);
  1323. return propContent ? propContent : "";
  1324. }
  1325. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1326. context->Config)) {
  1327. context->HadContextSensitiveCondition = true;
  1328. const char* propContent =
  1329. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1330. context->Config);
  1331. return propContent ? propContent : "";
  1332. }
  1333. return linkedTargetsContent;
  1334. }
  1335. if (!target->IsImported() && dagCheckerParent &&
  1336. !dagCheckerParent->EvaluatingLinkLibraries()) {
  1337. if (target->IsLinkInterfaceDependentNumberMinProperty(propertyName,
  1338. context->Config)) {
  1339. context->HadContextSensitiveCondition = true;
  1340. const char* propContent =
  1341. target->GetLinkInterfaceDependentNumberMinProperty(propertyName,
  1342. context->Config);
  1343. return propContent ? propContent : "";
  1344. }
  1345. if (target->IsLinkInterfaceDependentNumberMaxProperty(propertyName,
  1346. context->Config)) {
  1347. context->HadContextSensitiveCondition = true;
  1348. const char* propContent =
  1349. target->GetLinkInterfaceDependentNumberMaxProperty(propertyName,
  1350. context->Config);
  1351. return propContent ? propContent : "";
  1352. }
  1353. }
  1354. if (!interfacePropertyName.empty()) {
  1355. std::string result = this->EvaluateDependentExpression(
  1356. prop, context->LG, context, headTarget, target, &dagChecker);
  1357. if (!linkedTargetsContent.empty()) {
  1358. result += (result.empty() ? "" : ";") + linkedTargetsContent;
  1359. }
  1360. return result;
  1361. }
  1362. return prop;
  1363. }
  1364. } targetPropertyNode;
  1365. static const struct TargetNameNode : public cmGeneratorExpressionNode
  1366. {
  1367. TargetNameNode() {} // NOLINT(modernize-use-equals-default)
  1368. bool GeneratesContent() const override { return true; }
  1369. bool AcceptsArbitraryContentParameter() const override { return true; }
  1370. bool RequiresLiteralInput() const override { return true; }
  1371. std::string Evaluate(
  1372. const std::vector<std::string>& parameters,
  1373. cmGeneratorExpressionContext* /*context*/,
  1374. const GeneratorExpressionContent* /*content*/,
  1375. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1376. {
  1377. return parameters.front();
  1378. }
  1379. int NumExpectedParameters() const override { return 1; }
  1380. } targetNameNode;
  1381. static const struct TargetObjectsNode : public cmGeneratorExpressionNode
  1382. {
  1383. TargetObjectsNode() {} // NOLINT(modernize-use-equals-default)
  1384. std::string Evaluate(
  1385. const std::vector<std::string>& parameters,
  1386. cmGeneratorExpressionContext* context,
  1387. const GeneratorExpressionContent* content,
  1388. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1389. {
  1390. std::string tgtName = parameters.front();
  1391. cmGeneratorTarget* gt = context->LG->FindGeneratorTargetToUse(tgtName);
  1392. if (!gt) {
  1393. std::ostringstream e;
  1394. e << "Objects of target \"" << tgtName
  1395. << "\" referenced but no such target exists.";
  1396. reportError(context, content->GetOriginalExpression(), e.str());
  1397. return std::string();
  1398. }
  1399. cmStateEnums::TargetType type = gt->GetType();
  1400. if (type != cmStateEnums::EXECUTABLE &&
  1401. type != cmStateEnums::STATIC_LIBRARY &&
  1402. type != cmStateEnums::SHARED_LIBRARY &&
  1403. type != cmStateEnums::MODULE_LIBRARY &&
  1404. type != cmStateEnums::OBJECT_LIBRARY) {
  1405. std::ostringstream e;
  1406. e << "Objects of target \"" << tgtName
  1407. << "\" referenced but is not an allowed library types (EXECUTABLE, "
  1408. << "STATIC, SHARED, MODULE, OBJECT).";
  1409. reportError(context, content->GetOriginalExpression(), e.str());
  1410. return std::string();
  1411. }
  1412. if (!context->EvaluateForBuildsystem) {
  1413. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1414. std::string reason;
  1415. if (!gg->HasKnownObjectFileLocation(&reason)) {
  1416. std::ostringstream e;
  1417. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1418. "is only suitable for consumption by CMake (limited"
  1419. << reason
  1420. << "). "
  1421. "It is not suitable for writing out elsewhere.";
  1422. reportError(context, content->GetOriginalExpression(), e.str());
  1423. return std::string();
  1424. }
  1425. }
  1426. std::vector<std::string> objects;
  1427. if (gt->IsImported()) {
  1428. const char* loc = nullptr;
  1429. const char* imp = nullptr;
  1430. std::string suffix;
  1431. if (gt->Target->GetMappedConfig(context->Config, &loc, &imp, suffix)) {
  1432. cmSystemTools::ExpandListArgument(loc, objects);
  1433. }
  1434. context->HadContextSensitiveCondition = true;
  1435. } else {
  1436. gt->GetTargetObjectNames(context->Config, objects);
  1437. std::string obj_dir;
  1438. if (context->EvaluateForBuildsystem) {
  1439. // Use object file directory with buildsystem placeholder.
  1440. obj_dir = gt->ObjectDirectory;
  1441. // Here we assume that the set of object files produced
  1442. // by an object library does not vary with configuration
  1443. // and do not set HadContextSensitiveCondition to true.
  1444. } else {
  1445. // Use object file directory with per-config location.
  1446. obj_dir = gt->GetObjectDirectory(context->Config);
  1447. context->HadContextSensitiveCondition = true;
  1448. }
  1449. for (std::string& o : objects) {
  1450. o = obj_dir + o;
  1451. }
  1452. }
  1453. // Create the cmSourceFile instances in the referencing directory.
  1454. cmMakefile* mf = context->LG->GetMakefile();
  1455. for (std::string& o : objects) {
  1456. mf->AddTargetObject(tgtName, o);
  1457. }
  1458. return cmJoin(objects, ";");
  1459. }
  1460. } targetObjectsNode;
  1461. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1462. {
  1463. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  1464. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1465. std::string Evaluate(
  1466. const std::vector<std::string>& parameters,
  1467. cmGeneratorExpressionContext* context,
  1468. const GeneratorExpressionContent* content,
  1469. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1470. {
  1471. cmGeneratorTarget const* target = context->HeadTarget;
  1472. if (!target) {
  1473. reportError(
  1474. context, content->GetOriginalExpression(),
  1475. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1476. "not be used with add_custom_command or add_custom_target.");
  1477. return std::string();
  1478. }
  1479. context->HadHeadSensitiveCondition = true;
  1480. typedef std::map<std::string, std::vector<std::string>> LangMap;
  1481. static LangMap availableFeatures;
  1482. LangMap testedFeatures;
  1483. for (std::string const& p : parameters) {
  1484. std::string error;
  1485. std::string lang;
  1486. if (!context->LG->GetMakefile()->CompileFeatureKnown(
  1487. context->HeadTarget->Target, p, lang, &error)) {
  1488. reportError(context, content->GetOriginalExpression(), error);
  1489. return std::string();
  1490. }
  1491. testedFeatures[lang].push_back(p);
  1492. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1493. const char* featuresKnown =
  1494. context->LG->GetMakefile()->CompileFeaturesAvailable(lang, &error);
  1495. if (!featuresKnown) {
  1496. reportError(context, content->GetOriginalExpression(), error);
  1497. return std::string();
  1498. }
  1499. cmSystemTools::ExpandListArgument(featuresKnown,
  1500. availableFeatures[lang]);
  1501. }
  1502. }
  1503. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1504. for (auto const& lit : testedFeatures) {
  1505. std::vector<std::string> const& langAvailable =
  1506. availableFeatures[lit.first];
  1507. const char* standardDefault = context->LG->GetMakefile()->GetDefinition(
  1508. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  1509. for (std::string const& it : lit.second) {
  1510. if (std::find(langAvailable.begin(), langAvailable.end(), it) ==
  1511. langAvailable.end()) {
  1512. return "0";
  1513. }
  1514. if (standardDefault && !*standardDefault) {
  1515. // This compiler has no notion of language standard levels.
  1516. // All features known for the language are always available.
  1517. continue;
  1518. }
  1519. if (!context->LG->GetMakefile()->HaveStandardAvailable(
  1520. target->Target, lit.first, it)) {
  1521. if (evalLL) {
  1522. const char* l = target->GetProperty(lit.first + "_STANDARD");
  1523. if (!l) {
  1524. l = standardDefault;
  1525. }
  1526. assert(l);
  1527. context->MaxLanguageStandard[target][lit.first] = l;
  1528. } else {
  1529. return "0";
  1530. }
  1531. }
  1532. }
  1533. }
  1534. return "1";
  1535. }
  1536. } compileFeaturesNode;
  1537. static const char* targetPolicyWhitelist[] = {
  1538. nullptr
  1539. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1540. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1541. #undef TARGET_POLICY_STRING
  1542. };
  1543. cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1544. const char* policy)
  1545. {
  1546. #define RETURN_POLICY(POLICY) \
  1547. if (strcmp(policy, #POLICY) == 0) { \
  1548. return tgt->GetPolicyStatus##POLICY(); \
  1549. }
  1550. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1551. #undef RETURN_POLICY
  1552. assert(false && "Unreachable code. Not a valid policy");
  1553. return cmPolicies::WARN;
  1554. }
  1555. cmPolicies::PolicyID policyForString(const char* policy_id)
  1556. {
  1557. #define RETURN_POLICY_ID(POLICY_ID) \
  1558. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1559. return cmPolicies::POLICY_ID; \
  1560. }
  1561. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1562. #undef RETURN_POLICY_ID
  1563. assert(false && "Unreachable code. Not a valid policy");
  1564. return cmPolicies::CMP0002;
  1565. }
  1566. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1567. {
  1568. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  1569. int NumExpectedParameters() const override { return 1; }
  1570. std::string Evaluate(
  1571. const std::vector<std::string>& parameters,
  1572. cmGeneratorExpressionContext* context,
  1573. const GeneratorExpressionContent* content,
  1574. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1575. {
  1576. if (!context->HeadTarget) {
  1577. reportError(
  1578. context, content->GetOriginalExpression(),
  1579. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1580. "may not be used with add_custom_command or add_custom_target.");
  1581. return std::string();
  1582. }
  1583. context->HadContextSensitiveCondition = true;
  1584. context->HadHeadSensitiveCondition = true;
  1585. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  1586. const char* policy = targetPolicyWhitelist[i];
  1587. if (parameters.front() == policy) {
  1588. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1589. switch (statusForTarget(context->HeadTarget, policy)) {
  1590. case cmPolicies::WARN:
  1591. lg->IssueMessage(
  1592. MessageType::AUTHOR_WARNING,
  1593. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1594. CM_FALLTHROUGH;
  1595. case cmPolicies::REQUIRED_IF_USED:
  1596. case cmPolicies::REQUIRED_ALWAYS:
  1597. case cmPolicies::OLD:
  1598. return "0";
  1599. case cmPolicies::NEW:
  1600. return "1";
  1601. }
  1602. }
  1603. }
  1604. reportError(
  1605. context, content->GetOriginalExpression(),
  1606. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1607. "policies. Currently it may be used with the following policies:\n"
  1608. #define STRINGIFY_HELPER(X) #X
  1609. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1610. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1611. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1612. #undef TARGET_POLICY_LIST_ITEM
  1613. );
  1614. return std::string();
  1615. }
  1616. } targetPolicyNode;
  1617. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1618. {
  1619. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  1620. bool GeneratesContent() const override { return true; }
  1621. int NumExpectedParameters() const override { return 0; }
  1622. std::string Evaluate(
  1623. const std::vector<std::string>& /*parameters*/,
  1624. cmGeneratorExpressionContext* context,
  1625. const GeneratorExpressionContent* content,
  1626. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1627. {
  1628. reportError(context, content->GetOriginalExpression(),
  1629. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1630. "should never be evaluated.");
  1631. return std::string();
  1632. }
  1633. } installPrefixNode;
  1634. class ArtifactDirTag;
  1635. class ArtifactLinkerTag;
  1636. class ArtifactNameTag;
  1637. class ArtifactPathTag;
  1638. class ArtifactPdbTag;
  1639. class ArtifactSonameTag;
  1640. class ArtifactBundleDirTag;
  1641. class ArtifactBundleContentDirTag;
  1642. template <typename ArtifactT>
  1643. struct TargetFilesystemArtifactResultCreator
  1644. {
  1645. static std::string Create(cmGeneratorTarget* target,
  1646. cmGeneratorExpressionContext* context,
  1647. const GeneratorExpressionContent* content);
  1648. };
  1649. template <>
  1650. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1651. {
  1652. static std::string Create(cmGeneratorTarget* target,
  1653. cmGeneratorExpressionContext* context,
  1654. const GeneratorExpressionContent* content)
  1655. {
  1656. // The target soname file (.so.1).
  1657. if (target->IsDLLPlatform()) {
  1658. ::reportError(context, content->GetOriginalExpression(),
  1659. "TARGET_SONAME_FILE is not allowed "
  1660. "for DLL target platforms.");
  1661. return std::string();
  1662. }
  1663. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1664. ::reportError(context, content->GetOriginalExpression(),
  1665. "TARGET_SONAME_FILE is allowed only for "
  1666. "SHARED libraries.");
  1667. return std::string();
  1668. }
  1669. std::string result = target->GetDirectory(context->Config);
  1670. result += "/";
  1671. result += target->GetSOName(context->Config);
  1672. return result;
  1673. }
  1674. };
  1675. template <>
  1676. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1677. {
  1678. static std::string Create(cmGeneratorTarget* target,
  1679. cmGeneratorExpressionContext* context,
  1680. const GeneratorExpressionContent* content)
  1681. {
  1682. if (target->IsImported()) {
  1683. ::reportError(context, content->GetOriginalExpression(),
  1684. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1685. return std::string();
  1686. }
  1687. std::string language = target->GetLinkerLanguage(context->Config);
  1688. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1689. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1690. ::reportError(context, content->GetOriginalExpression(),
  1691. "TARGET_PDB_FILE is not supported by the target linker.");
  1692. return std::string();
  1693. }
  1694. cmStateEnums::TargetType targetType = target->GetType();
  1695. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1696. targetType != cmStateEnums::MODULE_LIBRARY &&
  1697. targetType != cmStateEnums::EXECUTABLE) {
  1698. ::reportError(context, content->GetOriginalExpression(),
  1699. "TARGET_PDB_FILE is allowed only for "
  1700. "targets with linker created artifacts.");
  1701. return std::string();
  1702. }
  1703. std::string result = target->GetPDBDirectory(context->Config);
  1704. result += "/";
  1705. result += target->GetPDBName(context->Config);
  1706. return result;
  1707. }
  1708. };
  1709. template <>
  1710. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1711. {
  1712. static std::string Create(cmGeneratorTarget* target,
  1713. cmGeneratorExpressionContext* context,
  1714. const GeneratorExpressionContent* content)
  1715. {
  1716. // The file used to link to the target (.so, .lib, .a).
  1717. if (!target->IsLinkable()) {
  1718. ::reportError(context, content->GetOriginalExpression(),
  1719. "TARGET_LINKER_FILE is allowed only for libraries and "
  1720. "executables with ENABLE_EXPORTS.");
  1721. return std::string();
  1722. }
  1723. cmStateEnums::ArtifactType artifact =
  1724. target->HasImportLibrary(context->Config)
  1725. ? cmStateEnums::ImportLibraryArtifact
  1726. : cmStateEnums::RuntimeBinaryArtifact;
  1727. return target->GetFullPath(context->Config, artifact);
  1728. }
  1729. };
  1730. template <>
  1731. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  1732. {
  1733. static std::string Create(cmGeneratorTarget* target,
  1734. cmGeneratorExpressionContext* context,
  1735. const GeneratorExpressionContent* content)
  1736. {
  1737. if (target->IsImported()) {
  1738. ::reportError(context, content->GetOriginalExpression(),
  1739. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  1740. return std::string();
  1741. }
  1742. if (!target->IsBundleOnApple()) {
  1743. ::reportError(context, content->GetOriginalExpression(),
  1744. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1745. return std::string();
  1746. }
  1747. std::string outpath = target->GetDirectory(context->Config) + '/';
  1748. return target->BuildBundleDirectory(outpath, context->Config,
  1749. cmGeneratorTarget::BundleDirLevel);
  1750. }
  1751. };
  1752. template <>
  1753. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1754. {
  1755. static std::string Create(cmGeneratorTarget* target,
  1756. cmGeneratorExpressionContext* context,
  1757. const GeneratorExpressionContent* content)
  1758. {
  1759. if (target->IsImported()) {
  1760. ::reportError(
  1761. context, content->GetOriginalExpression(),
  1762. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1763. return std::string();
  1764. }
  1765. if (!target->IsBundleOnApple()) {
  1766. ::reportError(
  1767. context, content->GetOriginalExpression(),
  1768. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1769. return std::string();
  1770. }
  1771. std::string outpath = target->GetDirectory(context->Config) + '/';
  1772. return target->BuildBundleDirectory(outpath, context->Config,
  1773. cmGeneratorTarget::ContentLevel);
  1774. }
  1775. };
  1776. template <>
  1777. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1778. {
  1779. static std::string Create(cmGeneratorTarget* target,
  1780. cmGeneratorExpressionContext* context,
  1781. const GeneratorExpressionContent* /*unused*/)
  1782. {
  1783. return target->GetFullPath(context->Config,
  1784. cmStateEnums::RuntimeBinaryArtifact, true);
  1785. }
  1786. };
  1787. template <typename ArtifactT>
  1788. struct TargetFilesystemArtifactResultGetter
  1789. {
  1790. static std::string Get(const std::string& result);
  1791. };
  1792. template <>
  1793. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1794. {
  1795. static std::string Get(const std::string& result)
  1796. {
  1797. return cmSystemTools::GetFilenameName(result);
  1798. }
  1799. };
  1800. template <>
  1801. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1802. {
  1803. static std::string Get(const std::string& result)
  1804. {
  1805. return cmSystemTools::GetFilenamePath(result);
  1806. }
  1807. };
  1808. template <>
  1809. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1810. {
  1811. static std::string Get(const std::string& result) { return result; }
  1812. };
  1813. struct TargetArtifactBase : public cmGeneratorExpressionNode
  1814. {
  1815. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  1816. protected:
  1817. cmGeneratorTarget* GetTarget(
  1818. const std::vector<std::string>& parameters,
  1819. cmGeneratorExpressionContext* context,
  1820. const GeneratorExpressionContent* content,
  1821. cmGeneratorExpressionDAGChecker* dagChecker) const
  1822. {
  1823. // Lookup the referenced target.
  1824. std::string name = parameters.front();
  1825. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1826. ::reportError(context, content->GetOriginalExpression(),
  1827. "Expression syntax not recognized.");
  1828. return nullptr;
  1829. }
  1830. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1831. if (!target) {
  1832. ::reportError(context, content->GetOriginalExpression(),
  1833. "No target \"" + name + "\"");
  1834. return nullptr;
  1835. }
  1836. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  1837. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  1838. ::reportError(context, content->GetOriginalExpression(),
  1839. "Target \"" + name +
  1840. "\" is not an executable or library.");
  1841. return nullptr;
  1842. }
  1843. if (dagChecker &&
  1844. (dagChecker->EvaluatingLinkLibraries(target) ||
  1845. (dagChecker->EvaluatingSources() &&
  1846. target == dagChecker->TopTarget()))) {
  1847. ::reportError(context, content->GetOriginalExpression(),
  1848. "Expressions which require the linker language may not "
  1849. "be used while evaluating link libraries");
  1850. return nullptr;
  1851. }
  1852. return target;
  1853. }
  1854. };
  1855. template <typename ArtifactT, typename ComponentT>
  1856. struct TargetFilesystemArtifact : public TargetArtifactBase
  1857. {
  1858. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  1859. int NumExpectedParameters() const override { return 1; }
  1860. std::string Evaluate(
  1861. const std::vector<std::string>& parameters,
  1862. cmGeneratorExpressionContext* context,
  1863. const GeneratorExpressionContent* content,
  1864. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1865. {
  1866. cmGeneratorTarget* target =
  1867. this->GetTarget(parameters, context, content, dagChecker);
  1868. if (!target) {
  1869. return std::string();
  1870. }
  1871. context->DependTargets.insert(target);
  1872. context->AllTargets.insert(target);
  1873. std::string result =
  1874. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1875. content);
  1876. if (context->HadError) {
  1877. return std::string();
  1878. }
  1879. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1880. }
  1881. };
  1882. template <typename ArtifactT>
  1883. struct TargetFilesystemArtifactNodeGroup
  1884. {
  1885. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  1886. {
  1887. }
  1888. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  1889. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  1890. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  1891. };
  1892. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  1893. targetNodeGroup;
  1894. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  1895. targetLinkerNodeGroup;
  1896. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  1897. targetSoNameNodeGroup;
  1898. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  1899. targetPdbNodeGroup;
  1900. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  1901. targetBundleDirNode;
  1902. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  1903. ArtifactPathTag>
  1904. targetBundleContentDirNode;
  1905. //
  1906. // To retrieve base name for various artifacts
  1907. //
  1908. template <typename ArtifactT>
  1909. struct TargetOutputNameArtifactResultGetter
  1910. {
  1911. static std::string Get(cmGeneratorTarget* target,
  1912. cmGeneratorExpressionContext* context,
  1913. const GeneratorExpressionContent* content);
  1914. };
  1915. template <>
  1916. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  1917. {
  1918. static std::string Get(cmGeneratorTarget* target,
  1919. cmGeneratorExpressionContext* context,
  1920. const GeneratorExpressionContent* /*unused*/)
  1921. {
  1922. return target->GetOutputName(context->Config,
  1923. cmStateEnums::RuntimeBinaryArtifact) +
  1924. target->GetFilePostfix(context->Config);
  1925. }
  1926. };
  1927. template <>
  1928. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  1929. {
  1930. static std::string Get(cmGeneratorTarget* target,
  1931. cmGeneratorExpressionContext* context,
  1932. const GeneratorExpressionContent* content)
  1933. {
  1934. // The file used to link to the target (.so, .lib, .a).
  1935. if (!target->IsLinkable()) {
  1936. ::reportError(context, content->GetOriginalExpression(),
  1937. "TARGET_LINKER_FILE_BASE_NAME is allowed only for "
  1938. "libraries and executables with ENABLE_EXPORTS.");
  1939. return std::string();
  1940. }
  1941. cmStateEnums::ArtifactType artifact =
  1942. target->HasImportLibrary(context->Config)
  1943. ? cmStateEnums::ImportLibraryArtifact
  1944. : cmStateEnums::RuntimeBinaryArtifact;
  1945. return target->GetOutputName(context->Config, artifact) +
  1946. target->GetFilePostfix(context->Config);
  1947. }
  1948. };
  1949. template <>
  1950. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  1951. {
  1952. static std::string Get(cmGeneratorTarget* target,
  1953. cmGeneratorExpressionContext* context,
  1954. const GeneratorExpressionContent* content)
  1955. {
  1956. if (target->IsImported()) {
  1957. ::reportError(
  1958. context, content->GetOriginalExpression(),
  1959. "TARGET_PDB_FILE_BASE_NAME not allowed for IMPORTED targets.");
  1960. return std::string();
  1961. }
  1962. std::string language = target->GetLinkerLanguage(context->Config);
  1963. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1964. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1965. ::reportError(
  1966. context, content->GetOriginalExpression(),
  1967. "TARGET_PDB_FILE_BASE_NAME is not supported by the target linker.");
  1968. return std::string();
  1969. }
  1970. cmStateEnums::TargetType targetType = target->GetType();
  1971. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1972. targetType != cmStateEnums::MODULE_LIBRARY &&
  1973. targetType != cmStateEnums::EXECUTABLE) {
  1974. ::reportError(context, content->GetOriginalExpression(),
  1975. "TARGET_PDB_FILE_BASE_NAME is allowed only for "
  1976. "targets with linker created artifacts.");
  1977. return std::string();
  1978. }
  1979. return target->GetPDBOutputName(context->Config) +
  1980. target->GetFilePostfix(context->Config);
  1981. }
  1982. };
  1983. template <typename ArtifactT>
  1984. struct TargetFileBaseNameArtifact : public TargetArtifactBase
  1985. {
  1986. TargetFileBaseNameArtifact() {} // NOLINT(modernize-use-equals-default)
  1987. int NumExpectedParameters() const override { return 1; }
  1988. std::string Evaluate(
  1989. const std::vector<std::string>& parameters,
  1990. cmGeneratorExpressionContext* context,
  1991. const GeneratorExpressionContent* content,
  1992. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1993. {
  1994. cmGeneratorTarget* target =
  1995. this->GetTarget(parameters, context, content, dagChecker);
  1996. if (!target) {
  1997. return std::string();
  1998. }
  1999. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  2000. target, context, content);
  2001. if (context->HadError) {
  2002. return std::string();
  2003. }
  2004. return result;
  2005. }
  2006. };
  2007. static const TargetFileBaseNameArtifact<ArtifactNameTag>
  2008. targetFileBaseNameNode;
  2009. static const TargetFileBaseNameArtifact<ArtifactLinkerTag>
  2010. targetLinkerFileBaseNameNode;
  2011. static const TargetFileBaseNameArtifact<ArtifactPdbTag>
  2012. targetPdbFileBaseNameNode;
  2013. class ArtifactFilePrefixTag;
  2014. class ArtifactLinkerFilePrefixTag;
  2015. class ArtifactFileSuffixTag;
  2016. class ArtifactLinkerFileSuffixTag;
  2017. template <typename ArtifactT>
  2018. struct TargetFileArtifactResultGetter
  2019. {
  2020. static std::string Get(cmGeneratorTarget* target,
  2021. cmGeneratorExpressionContext* context,
  2022. const GeneratorExpressionContent* content);
  2023. };
  2024. template <>
  2025. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  2026. {
  2027. static std::string Get(cmGeneratorTarget* target,
  2028. cmGeneratorExpressionContext* context,
  2029. const GeneratorExpressionContent*)
  2030. {
  2031. return target->GetFilePrefix(context->Config);
  2032. }
  2033. };
  2034. template <>
  2035. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  2036. {
  2037. static std::string Get(cmGeneratorTarget* target,
  2038. cmGeneratorExpressionContext* context,
  2039. const GeneratorExpressionContent* content)
  2040. {
  2041. if (!target->IsLinkable()) {
  2042. ::reportError(context, content->GetOriginalExpression(),
  2043. "TARGET_LINKER_PREFIX is allowed only for libraries and "
  2044. "executables with ENABLE_EXPORTS.");
  2045. return std::string();
  2046. }
  2047. cmStateEnums::ArtifactType artifact =
  2048. target->HasImportLibrary(context->Config)
  2049. ? cmStateEnums::ImportLibraryArtifact
  2050. : cmStateEnums::RuntimeBinaryArtifact;
  2051. return target->GetFilePrefix(context->Config, artifact);
  2052. }
  2053. };
  2054. template <>
  2055. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  2056. {
  2057. static std::string Get(cmGeneratorTarget* target,
  2058. cmGeneratorExpressionContext* context,
  2059. const GeneratorExpressionContent*)
  2060. {
  2061. return target->GetFileSuffix(context->Config);
  2062. }
  2063. };
  2064. template <>
  2065. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  2066. {
  2067. static std::string Get(cmGeneratorTarget* target,
  2068. cmGeneratorExpressionContext* context,
  2069. const GeneratorExpressionContent* content)
  2070. {
  2071. if (!target->IsLinkable()) {
  2072. ::reportError(context, content->GetOriginalExpression(),
  2073. "TARGET_LINKER_SUFFIX is allowed only for libraries and "
  2074. "executables with ENABLE_EXPORTS.");
  2075. return std::string();
  2076. }
  2077. cmStateEnums::ArtifactType artifact =
  2078. target->HasImportLibrary(context->Config)
  2079. ? cmStateEnums::ImportLibraryArtifact
  2080. : cmStateEnums::RuntimeBinaryArtifact;
  2081. return target->GetFileSuffix(context->Config, artifact);
  2082. }
  2083. };
  2084. template <typename ArtifactT>
  2085. struct TargetFileArtifact : public TargetArtifactBase
  2086. {
  2087. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  2088. int NumExpectedParameters() const override { return 1; }
  2089. std::string Evaluate(
  2090. const std::vector<std::string>& parameters,
  2091. cmGeneratorExpressionContext* context,
  2092. const GeneratorExpressionContent* content,
  2093. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2094. {
  2095. cmGeneratorTarget* target =
  2096. this->GetTarget(parameters, context, content, dagChecker);
  2097. if (!target) {
  2098. return std::string();
  2099. }
  2100. std::string result =
  2101. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  2102. if (context->HadError) {
  2103. return std::string();
  2104. }
  2105. return result;
  2106. }
  2107. };
  2108. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  2109. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  2110. targetLinkerFilePrefixNode;
  2111. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  2112. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  2113. targetLinkerFileSuffixNode;
  2114. static const struct ShellPathNode : public cmGeneratorExpressionNode
  2115. {
  2116. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  2117. std::string Evaluate(
  2118. const std::vector<std::string>& parameters,
  2119. cmGeneratorExpressionContext* context,
  2120. const GeneratorExpressionContent* content,
  2121. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2122. {
  2123. std::vector<std::string> listIn;
  2124. cmSystemTools::ExpandListArgument(parameters.front(), listIn);
  2125. if (listIn.empty()) {
  2126. reportError(context, content->GetOriginalExpression(),
  2127. "\"\" is not an absolute path.");
  2128. return std::string();
  2129. }
  2130. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  2131. cmOutputConverter converter(snapshot);
  2132. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  2133. std::vector<std::string> listOut;
  2134. listOut.reserve(listIn.size());
  2135. for (auto const& in : listIn) {
  2136. if (!cmSystemTools::FileIsFullPath(in)) {
  2137. reportError(context, content->GetOriginalExpression(),
  2138. "\"" + in + "\" is not an absolute path.");
  2139. return std::string();
  2140. }
  2141. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  2142. }
  2143. return cmJoin(listOut, separator);
  2144. }
  2145. } shellPathNode;
  2146. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  2147. const std::string& identifier)
  2148. {
  2149. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  2150. { "0", &zeroNode },
  2151. { "1", &oneNode },
  2152. { "AND", &andNode },
  2153. { "OR", &orNode },
  2154. { "NOT", &notNode },
  2155. { "C_COMPILER_ID", &cCompilerIdNode },
  2156. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  2157. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  2158. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  2159. { "VERSION_GREATER", &versionGreaterNode },
  2160. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  2161. { "VERSION_LESS", &versionLessNode },
  2162. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  2163. { "VERSION_EQUAL", &versionEqualNode },
  2164. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  2165. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  2166. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  2167. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  2168. { "PLATFORM_ID", &platformIdNode },
  2169. { "COMPILE_FEATURES", &compileFeaturesNode },
  2170. { "CONFIGURATION", &configurationNode },
  2171. { "CONFIG", &configurationTestNode },
  2172. { "TARGET_FILE", &targetNodeGroup.File },
  2173. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  2174. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  2175. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  2176. { "TARGET_FILE_BASE_NAME", &targetFileBaseNameNode },
  2177. { "TARGET_LINKER_FILE_BASE_NAME", &targetLinkerFileBaseNameNode },
  2178. { "TARGET_PDB_FILE_BASE_NAME", &targetPdbFileBaseNameNode },
  2179. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  2180. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  2181. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  2182. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  2183. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  2184. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  2185. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  2186. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  2187. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  2188. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  2189. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  2190. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  2191. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  2192. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  2193. { "STREQUAL", &strEqualNode },
  2194. { "EQUAL", &equalNode },
  2195. { "IN_LIST", &inListNode },
  2196. { "FILTER", &filterNode },
  2197. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  2198. { "LOWER_CASE", &lowerCaseNode },
  2199. { "UPPER_CASE", &upperCaseNode },
  2200. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  2201. { "BOOL", &boolNode },
  2202. { "IF", &ifNode },
  2203. { "ANGLE-R", &angle_rNode },
  2204. { "COMMA", &commaNode },
  2205. { "SEMICOLON", &semicolonNode },
  2206. { "TARGET_PROPERTY", &targetPropertyNode },
  2207. { "TARGET_NAME", &targetNameNode },
  2208. { "TARGET_OBJECTS", &targetObjectsNode },
  2209. { "TARGET_POLICY", &targetPolicyNode },
  2210. { "TARGET_EXISTS", &targetExistsNode },
  2211. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  2212. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  2213. { "GENEX_EVAL", &genexEvalNode },
  2214. { "BUILD_INTERFACE", &buildInterfaceNode },
  2215. { "INSTALL_INTERFACE", &installInterfaceNode },
  2216. { "INSTALL_PREFIX", &installPrefixNode },
  2217. { "JOIN", &joinNode },
  2218. { "LINK_ONLY", &linkOnlyNode },
  2219. { "COMPILE_LANGUAGE", &languageNode },
  2220. { "SHELL_PATH", &shellPathNode }
  2221. };
  2222. {
  2223. auto itr = nodeMap.find(identifier);
  2224. if (itr != nodeMap.end()) {
  2225. return itr->second;
  2226. }
  2227. }
  2228. return nullptr;
  2229. }
  2230. void reportError(cmGeneratorExpressionContext* context,
  2231. const std::string& expr, const std::string& result)
  2232. {
  2233. context->HadError = true;
  2234. if (context->Quiet) {
  2235. return;
  2236. }
  2237. std::ostringstream e;
  2238. /* clang-format off */
  2239. e << "Error evaluating generator expression:\n"
  2240. << " " << expr << "\n"
  2241. << result;
  2242. /* clang-format on */
  2243. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2244. e.str(), context->Backtrace);
  2245. }