cmGeneratorExpressionNode.cxx 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  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. if (gt->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1400. std::ostringstream e;
  1401. e << "Objects of target \"" << tgtName
  1402. << "\" referenced but is not an OBJECT library.";
  1403. reportError(context, content->GetOriginalExpression(), e.str());
  1404. return std::string();
  1405. }
  1406. if (!context->EvaluateForBuildsystem) {
  1407. cmGlobalGenerator* gg = context->LG->GetGlobalGenerator();
  1408. std::string reason;
  1409. if (!gg->HasKnownObjectFileLocation(&reason)) {
  1410. std::ostringstream e;
  1411. e << "The evaluation of the TARGET_OBJECTS generator expression "
  1412. "is only suitable for consumption by CMake (limited"
  1413. << reason
  1414. << "). "
  1415. "It is not suitable for writing out elsewhere.";
  1416. reportError(context, content->GetOriginalExpression(), e.str());
  1417. return std::string();
  1418. }
  1419. }
  1420. std::vector<std::string> objects;
  1421. if (gt->IsImported()) {
  1422. const char* loc = nullptr;
  1423. const char* imp = nullptr;
  1424. std::string suffix;
  1425. if (gt->Target->GetMappedConfig(context->Config, &loc, &imp, suffix)) {
  1426. cmSystemTools::ExpandListArgument(loc, objects);
  1427. }
  1428. context->HadContextSensitiveCondition = true;
  1429. } else {
  1430. gt->GetTargetObjectNames(context->Config, objects);
  1431. std::string obj_dir;
  1432. if (context->EvaluateForBuildsystem) {
  1433. // Use object file directory with buildsystem placeholder.
  1434. obj_dir = gt->ObjectDirectory;
  1435. // Here we assume that the set of object files produced
  1436. // by an object library does not vary with configuration
  1437. // and do not set HadContextSensitiveCondition to true.
  1438. } else {
  1439. // Use object file directory with per-config location.
  1440. obj_dir = gt->GetObjectDirectory(context->Config);
  1441. context->HadContextSensitiveCondition = true;
  1442. }
  1443. for (std::string& o : objects) {
  1444. o = obj_dir + o;
  1445. }
  1446. }
  1447. // Create the cmSourceFile instances in the referencing directory.
  1448. cmMakefile* mf = context->LG->GetMakefile();
  1449. for (std::string& o : objects) {
  1450. mf->AddTargetObject(tgtName, o);
  1451. }
  1452. return cmJoin(objects, ";");
  1453. }
  1454. } targetObjectsNode;
  1455. static const struct CompileFeaturesNode : public cmGeneratorExpressionNode
  1456. {
  1457. CompileFeaturesNode() {} // NOLINT(modernize-use-equals-default)
  1458. int NumExpectedParameters() const override { return OneOrMoreParameters; }
  1459. std::string Evaluate(
  1460. const std::vector<std::string>& parameters,
  1461. cmGeneratorExpressionContext* context,
  1462. const GeneratorExpressionContent* content,
  1463. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1464. {
  1465. cmGeneratorTarget const* target = context->HeadTarget;
  1466. if (!target) {
  1467. reportError(
  1468. context, content->GetOriginalExpression(),
  1469. "$<COMPILE_FEATURE> may only be used with binary targets. It may "
  1470. "not be used with add_custom_command or add_custom_target.");
  1471. return std::string();
  1472. }
  1473. context->HadHeadSensitiveCondition = true;
  1474. typedef std::map<std::string, std::vector<std::string>> LangMap;
  1475. static LangMap availableFeatures;
  1476. LangMap testedFeatures;
  1477. for (std::string const& p : parameters) {
  1478. std::string error;
  1479. std::string lang;
  1480. if (!context->LG->GetMakefile()->CompileFeatureKnown(
  1481. context->HeadTarget->Target, p, lang, &error)) {
  1482. reportError(context, content->GetOriginalExpression(), error);
  1483. return std::string();
  1484. }
  1485. testedFeatures[lang].push_back(p);
  1486. if (availableFeatures.find(lang) == availableFeatures.end()) {
  1487. const char* featuresKnown =
  1488. context->LG->GetMakefile()->CompileFeaturesAvailable(lang, &error);
  1489. if (!featuresKnown) {
  1490. reportError(context, content->GetOriginalExpression(), error);
  1491. return std::string();
  1492. }
  1493. cmSystemTools::ExpandListArgument(featuresKnown,
  1494. availableFeatures[lang]);
  1495. }
  1496. }
  1497. bool evalLL = dagChecker && dagChecker->EvaluatingLinkLibraries();
  1498. for (auto const& lit : testedFeatures) {
  1499. std::vector<std::string> const& langAvailable =
  1500. availableFeatures[lit.first];
  1501. const char* standardDefault = context->LG->GetMakefile()->GetDefinition(
  1502. "CMAKE_" + lit.first + "_STANDARD_DEFAULT");
  1503. for (std::string const& it : lit.second) {
  1504. if (std::find(langAvailable.begin(), langAvailable.end(), it) ==
  1505. langAvailable.end()) {
  1506. return "0";
  1507. }
  1508. if (standardDefault && !*standardDefault) {
  1509. // This compiler has no notion of language standard levels.
  1510. // All features known for the language are always available.
  1511. continue;
  1512. }
  1513. if (!context->LG->GetMakefile()->HaveStandardAvailable(
  1514. target->Target, lit.first, it)) {
  1515. if (evalLL) {
  1516. const char* l = target->GetProperty(lit.first + "_STANDARD");
  1517. if (!l) {
  1518. l = standardDefault;
  1519. }
  1520. assert(l);
  1521. context->MaxLanguageStandard[target][lit.first] = l;
  1522. } else {
  1523. return "0";
  1524. }
  1525. }
  1526. }
  1527. }
  1528. return "1";
  1529. }
  1530. } compileFeaturesNode;
  1531. static const char* targetPolicyWhitelist[] = {
  1532. nullptr
  1533. #define TARGET_POLICY_STRING(POLICY) , #POLICY
  1534. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)
  1535. #undef TARGET_POLICY_STRING
  1536. };
  1537. cmPolicies::PolicyStatus statusForTarget(cmGeneratorTarget const* tgt,
  1538. const char* policy)
  1539. {
  1540. #define RETURN_POLICY(POLICY) \
  1541. if (strcmp(policy, #POLICY) == 0) { \
  1542. return tgt->GetPolicyStatus##POLICY(); \
  1543. }
  1544. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
  1545. #undef RETURN_POLICY
  1546. assert(false && "Unreachable code. Not a valid policy");
  1547. return cmPolicies::WARN;
  1548. }
  1549. cmPolicies::PolicyID policyForString(const char* policy_id)
  1550. {
  1551. #define RETURN_POLICY_ID(POLICY_ID) \
  1552. if (strcmp(policy_id, #POLICY_ID) == 0) { \
  1553. return cmPolicies::POLICY_ID; \
  1554. }
  1555. CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
  1556. #undef RETURN_POLICY_ID
  1557. assert(false && "Unreachable code. Not a valid policy");
  1558. return cmPolicies::CMP0002;
  1559. }
  1560. static const struct TargetPolicyNode : public cmGeneratorExpressionNode
  1561. {
  1562. TargetPolicyNode() {} // NOLINT(modernize-use-equals-default)
  1563. int NumExpectedParameters() const override { return 1; }
  1564. std::string Evaluate(
  1565. const std::vector<std::string>& parameters,
  1566. cmGeneratorExpressionContext* context,
  1567. const GeneratorExpressionContent* content,
  1568. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1569. {
  1570. if (!context->HeadTarget) {
  1571. reportError(
  1572. context, content->GetOriginalExpression(),
  1573. "$<TARGET_POLICY:prop> may only be used with binary targets. It "
  1574. "may not be used with add_custom_command or add_custom_target.");
  1575. return std::string();
  1576. }
  1577. context->HadContextSensitiveCondition = true;
  1578. context->HadHeadSensitiveCondition = true;
  1579. for (size_t i = 1; i < cm::size(targetPolicyWhitelist); ++i) {
  1580. const char* policy = targetPolicyWhitelist[i];
  1581. if (parameters.front() == policy) {
  1582. cmLocalGenerator* lg = context->HeadTarget->GetLocalGenerator();
  1583. switch (statusForTarget(context->HeadTarget, policy)) {
  1584. case cmPolicies::WARN:
  1585. lg->IssueMessage(
  1586. MessageType::AUTHOR_WARNING,
  1587. cmPolicies::GetPolicyWarning(policyForString(policy)));
  1588. CM_FALLTHROUGH;
  1589. case cmPolicies::REQUIRED_IF_USED:
  1590. case cmPolicies::REQUIRED_ALWAYS:
  1591. case cmPolicies::OLD:
  1592. return "0";
  1593. case cmPolicies::NEW:
  1594. return "1";
  1595. }
  1596. }
  1597. }
  1598. reportError(
  1599. context, content->GetOriginalExpression(),
  1600. "$<TARGET_POLICY:prop> may only be used with a limited number of "
  1601. "policies. Currently it may be used with the following policies:\n"
  1602. #define STRINGIFY_HELPER(X) #X
  1603. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  1604. #define TARGET_POLICY_LIST_ITEM(POLICY) " * " STRINGIFY(POLICY) "\n"
  1605. CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)
  1606. #undef TARGET_POLICY_LIST_ITEM
  1607. );
  1608. return std::string();
  1609. }
  1610. } targetPolicyNode;
  1611. static const struct InstallPrefixNode : public cmGeneratorExpressionNode
  1612. {
  1613. InstallPrefixNode() {} // NOLINT(modernize-use-equals-default)
  1614. bool GeneratesContent() const override { return true; }
  1615. int NumExpectedParameters() const override { return 0; }
  1616. std::string Evaluate(
  1617. const std::vector<std::string>& /*parameters*/,
  1618. cmGeneratorExpressionContext* context,
  1619. const GeneratorExpressionContent* content,
  1620. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  1621. {
  1622. reportError(context, content->GetOriginalExpression(),
  1623. "INSTALL_PREFIX is a marker for install(EXPORT) only. It "
  1624. "should never be evaluated.");
  1625. return std::string();
  1626. }
  1627. } installPrefixNode;
  1628. class ArtifactDirTag;
  1629. class ArtifactLinkerTag;
  1630. class ArtifactNameTag;
  1631. class ArtifactPathTag;
  1632. class ArtifactPdbTag;
  1633. class ArtifactSonameTag;
  1634. class ArtifactBundleDirTag;
  1635. class ArtifactBundleContentDirTag;
  1636. template <typename ArtifactT>
  1637. struct TargetFilesystemArtifactResultCreator
  1638. {
  1639. static std::string Create(cmGeneratorTarget* target,
  1640. cmGeneratorExpressionContext* context,
  1641. const GeneratorExpressionContent* content);
  1642. };
  1643. template <>
  1644. struct TargetFilesystemArtifactResultCreator<ArtifactSonameTag>
  1645. {
  1646. static std::string Create(cmGeneratorTarget* target,
  1647. cmGeneratorExpressionContext* context,
  1648. const GeneratorExpressionContent* content)
  1649. {
  1650. // The target soname file (.so.1).
  1651. if (target->IsDLLPlatform()) {
  1652. ::reportError(context, content->GetOriginalExpression(),
  1653. "TARGET_SONAME_FILE is not allowed "
  1654. "for DLL target platforms.");
  1655. return std::string();
  1656. }
  1657. if (target->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1658. ::reportError(context, content->GetOriginalExpression(),
  1659. "TARGET_SONAME_FILE is allowed only for "
  1660. "SHARED libraries.");
  1661. return std::string();
  1662. }
  1663. std::string result = target->GetDirectory(context->Config);
  1664. result += "/";
  1665. result += target->GetSOName(context->Config);
  1666. return result;
  1667. }
  1668. };
  1669. template <>
  1670. struct TargetFilesystemArtifactResultCreator<ArtifactPdbTag>
  1671. {
  1672. static std::string Create(cmGeneratorTarget* target,
  1673. cmGeneratorExpressionContext* context,
  1674. const GeneratorExpressionContent* content)
  1675. {
  1676. if (target->IsImported()) {
  1677. ::reportError(context, content->GetOriginalExpression(),
  1678. "TARGET_PDB_FILE not allowed for IMPORTED targets.");
  1679. return std::string();
  1680. }
  1681. std::string language = target->GetLinkerLanguage(context->Config);
  1682. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1683. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1684. ::reportError(context, content->GetOriginalExpression(),
  1685. "TARGET_PDB_FILE is not supported by the target linker.");
  1686. return std::string();
  1687. }
  1688. cmStateEnums::TargetType targetType = target->GetType();
  1689. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1690. targetType != cmStateEnums::MODULE_LIBRARY &&
  1691. targetType != cmStateEnums::EXECUTABLE) {
  1692. ::reportError(context, content->GetOriginalExpression(),
  1693. "TARGET_PDB_FILE is allowed only for "
  1694. "targets with linker created artifacts.");
  1695. return std::string();
  1696. }
  1697. std::string result = target->GetPDBDirectory(context->Config);
  1698. result += "/";
  1699. result += target->GetPDBName(context->Config);
  1700. return result;
  1701. }
  1702. };
  1703. template <>
  1704. struct TargetFilesystemArtifactResultCreator<ArtifactLinkerTag>
  1705. {
  1706. static std::string Create(cmGeneratorTarget* target,
  1707. cmGeneratorExpressionContext* context,
  1708. const GeneratorExpressionContent* content)
  1709. {
  1710. // The file used to link to the target (.so, .lib, .a).
  1711. if (!target->IsLinkable()) {
  1712. ::reportError(context, content->GetOriginalExpression(),
  1713. "TARGET_LINKER_FILE is allowed only for libraries and "
  1714. "executables with ENABLE_EXPORTS.");
  1715. return std::string();
  1716. }
  1717. cmStateEnums::ArtifactType artifact =
  1718. target->HasImportLibrary(context->Config)
  1719. ? cmStateEnums::ImportLibraryArtifact
  1720. : cmStateEnums::RuntimeBinaryArtifact;
  1721. return target->GetFullPath(context->Config, artifact);
  1722. }
  1723. };
  1724. template <>
  1725. struct TargetFilesystemArtifactResultCreator<ArtifactBundleDirTag>
  1726. {
  1727. static std::string Create(cmGeneratorTarget* target,
  1728. cmGeneratorExpressionContext* context,
  1729. const GeneratorExpressionContent* content)
  1730. {
  1731. if (target->IsImported()) {
  1732. ::reportError(context, content->GetOriginalExpression(),
  1733. "TARGET_BUNDLE_DIR not allowed for IMPORTED targets.");
  1734. return std::string();
  1735. }
  1736. if (!target->IsBundleOnApple()) {
  1737. ::reportError(context, content->GetOriginalExpression(),
  1738. "TARGET_BUNDLE_DIR is allowed only for Bundle targets.");
  1739. return std::string();
  1740. }
  1741. std::string outpath = target->GetDirectory(context->Config) + '/';
  1742. return target->BuildBundleDirectory(outpath, context->Config,
  1743. cmGeneratorTarget::BundleDirLevel);
  1744. }
  1745. };
  1746. template <>
  1747. struct TargetFilesystemArtifactResultCreator<ArtifactBundleContentDirTag>
  1748. {
  1749. static std::string Create(cmGeneratorTarget* target,
  1750. cmGeneratorExpressionContext* context,
  1751. const GeneratorExpressionContent* content)
  1752. {
  1753. if (target->IsImported()) {
  1754. ::reportError(
  1755. context, content->GetOriginalExpression(),
  1756. "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets.");
  1757. return std::string();
  1758. }
  1759. if (!target->IsBundleOnApple()) {
  1760. ::reportError(
  1761. context, content->GetOriginalExpression(),
  1762. "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets.");
  1763. return std::string();
  1764. }
  1765. std::string outpath = target->GetDirectory(context->Config) + '/';
  1766. return target->BuildBundleDirectory(outpath, context->Config,
  1767. cmGeneratorTarget::ContentLevel);
  1768. }
  1769. };
  1770. template <>
  1771. struct TargetFilesystemArtifactResultCreator<ArtifactNameTag>
  1772. {
  1773. static std::string Create(cmGeneratorTarget* target,
  1774. cmGeneratorExpressionContext* context,
  1775. const GeneratorExpressionContent* /*unused*/)
  1776. {
  1777. return target->GetFullPath(context->Config,
  1778. cmStateEnums::RuntimeBinaryArtifact, true);
  1779. }
  1780. };
  1781. template <typename ArtifactT>
  1782. struct TargetFilesystemArtifactResultGetter
  1783. {
  1784. static std::string Get(const std::string& result);
  1785. };
  1786. template <>
  1787. struct TargetFilesystemArtifactResultGetter<ArtifactNameTag>
  1788. {
  1789. static std::string Get(const std::string& result)
  1790. {
  1791. return cmSystemTools::GetFilenameName(result);
  1792. }
  1793. };
  1794. template <>
  1795. struct TargetFilesystemArtifactResultGetter<ArtifactDirTag>
  1796. {
  1797. static std::string Get(const std::string& result)
  1798. {
  1799. return cmSystemTools::GetFilenamePath(result);
  1800. }
  1801. };
  1802. template <>
  1803. struct TargetFilesystemArtifactResultGetter<ArtifactPathTag>
  1804. {
  1805. static std::string Get(const std::string& result) { return result; }
  1806. };
  1807. struct TargetArtifactBase : public cmGeneratorExpressionNode
  1808. {
  1809. TargetArtifactBase() {} // NOLINT(modernize-use-equals-default)
  1810. protected:
  1811. cmGeneratorTarget* GetTarget(
  1812. const std::vector<std::string>& parameters,
  1813. cmGeneratorExpressionContext* context,
  1814. const GeneratorExpressionContent* content,
  1815. cmGeneratorExpressionDAGChecker* dagChecker) const
  1816. {
  1817. // Lookup the referenced target.
  1818. std::string name = parameters.front();
  1819. if (!cmGeneratorExpression::IsValidTargetName(name)) {
  1820. ::reportError(context, content->GetOriginalExpression(),
  1821. "Expression syntax not recognized.");
  1822. return nullptr;
  1823. }
  1824. cmGeneratorTarget* target = context->LG->FindGeneratorTargetToUse(name);
  1825. if (!target) {
  1826. ::reportError(context, content->GetOriginalExpression(),
  1827. "No target \"" + name + "\"");
  1828. return nullptr;
  1829. }
  1830. if (target->GetType() >= cmStateEnums::OBJECT_LIBRARY &&
  1831. target->GetType() != cmStateEnums::UNKNOWN_LIBRARY) {
  1832. ::reportError(context, content->GetOriginalExpression(),
  1833. "Target \"" + name +
  1834. "\" is not an executable or library.");
  1835. return nullptr;
  1836. }
  1837. if (dagChecker &&
  1838. (dagChecker->EvaluatingLinkLibraries(target) ||
  1839. (dagChecker->EvaluatingSources() &&
  1840. target == dagChecker->TopTarget()))) {
  1841. ::reportError(context, content->GetOriginalExpression(),
  1842. "Expressions which require the linker language may not "
  1843. "be used while evaluating link libraries");
  1844. return nullptr;
  1845. }
  1846. return target;
  1847. }
  1848. };
  1849. template <typename ArtifactT, typename ComponentT>
  1850. struct TargetFilesystemArtifact : public TargetArtifactBase
  1851. {
  1852. TargetFilesystemArtifact() {} // NOLINT(modernize-use-equals-default)
  1853. int NumExpectedParameters() const override { return 1; }
  1854. std::string Evaluate(
  1855. const std::vector<std::string>& parameters,
  1856. cmGeneratorExpressionContext* context,
  1857. const GeneratorExpressionContent* content,
  1858. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1859. {
  1860. cmGeneratorTarget* target =
  1861. this->GetTarget(parameters, context, content, dagChecker);
  1862. if (!target) {
  1863. return std::string();
  1864. }
  1865. context->DependTargets.insert(target);
  1866. context->AllTargets.insert(target);
  1867. std::string result =
  1868. TargetFilesystemArtifactResultCreator<ArtifactT>::Create(target, context,
  1869. content);
  1870. if (context->HadError) {
  1871. return std::string();
  1872. }
  1873. return TargetFilesystemArtifactResultGetter<ComponentT>::Get(result);
  1874. }
  1875. };
  1876. template <typename ArtifactT>
  1877. struct TargetFilesystemArtifactNodeGroup
  1878. {
  1879. TargetFilesystemArtifactNodeGroup() // NOLINT(modernize-use-equals-default)
  1880. {
  1881. }
  1882. TargetFilesystemArtifact<ArtifactT, ArtifactPathTag> File;
  1883. TargetFilesystemArtifact<ArtifactT, ArtifactNameTag> FileName;
  1884. TargetFilesystemArtifact<ArtifactT, ArtifactDirTag> FileDir;
  1885. };
  1886. static const TargetFilesystemArtifactNodeGroup<ArtifactNameTag>
  1887. targetNodeGroup;
  1888. static const TargetFilesystemArtifactNodeGroup<ArtifactLinkerTag>
  1889. targetLinkerNodeGroup;
  1890. static const TargetFilesystemArtifactNodeGroup<ArtifactSonameTag>
  1891. targetSoNameNodeGroup;
  1892. static const TargetFilesystemArtifactNodeGroup<ArtifactPdbTag>
  1893. targetPdbNodeGroup;
  1894. static const TargetFilesystemArtifact<ArtifactBundleDirTag, ArtifactPathTag>
  1895. targetBundleDirNode;
  1896. static const TargetFilesystemArtifact<ArtifactBundleContentDirTag,
  1897. ArtifactPathTag>
  1898. targetBundleContentDirNode;
  1899. //
  1900. // To retrieve base name for various artifacts
  1901. //
  1902. template <typename ArtifactT>
  1903. struct TargetOutputNameArtifactResultGetter
  1904. {
  1905. static std::string Get(cmGeneratorTarget* target,
  1906. cmGeneratorExpressionContext* context,
  1907. const GeneratorExpressionContent* content);
  1908. };
  1909. template <>
  1910. struct TargetOutputNameArtifactResultGetter<ArtifactNameTag>
  1911. {
  1912. static std::string Get(cmGeneratorTarget* target,
  1913. cmGeneratorExpressionContext* context,
  1914. const GeneratorExpressionContent* /*unused*/)
  1915. {
  1916. return target->GetOutputName(context->Config,
  1917. cmStateEnums::RuntimeBinaryArtifact);
  1918. }
  1919. };
  1920. template <>
  1921. struct TargetOutputNameArtifactResultGetter<ArtifactLinkerTag>
  1922. {
  1923. static std::string Get(cmGeneratorTarget* target,
  1924. cmGeneratorExpressionContext* context,
  1925. const GeneratorExpressionContent* content)
  1926. {
  1927. // The file used to link to the target (.so, .lib, .a).
  1928. if (!target->IsLinkable()) {
  1929. ::reportError(context, content->GetOriginalExpression(),
  1930. "TARGET_LINKER_OUTPUT_NAME is allowed only for libraries "
  1931. "and executables with ENABLE_EXPORTS.");
  1932. return std::string();
  1933. }
  1934. cmStateEnums::ArtifactType artifact =
  1935. target->HasImportLibrary(context->Config)
  1936. ? cmStateEnums::ImportLibraryArtifact
  1937. : cmStateEnums::RuntimeBinaryArtifact;
  1938. return target->GetOutputName(context->Config, artifact);
  1939. }
  1940. };
  1941. template <>
  1942. struct TargetOutputNameArtifactResultGetter<ArtifactPdbTag>
  1943. {
  1944. static std::string Get(cmGeneratorTarget* target,
  1945. cmGeneratorExpressionContext* context,
  1946. const GeneratorExpressionContent* content)
  1947. {
  1948. if (target->IsImported()) {
  1949. ::reportError(
  1950. context, content->GetOriginalExpression(),
  1951. "TARGET_PDB_OUTPUT_NAME not allowed for IMPORTED targets.");
  1952. return std::string();
  1953. }
  1954. std::string language = target->GetLinkerLanguage(context->Config);
  1955. std::string pdbSupportVar = "CMAKE_" + language + "_LINKER_SUPPORTS_PDB";
  1956. if (!context->LG->GetMakefile()->IsOn(pdbSupportVar)) {
  1957. ::reportError(
  1958. context, content->GetOriginalExpression(),
  1959. "TARGET_PDB_OUTPUT_NAME is not supported by the target linker.");
  1960. return std::string();
  1961. }
  1962. cmStateEnums::TargetType targetType = target->GetType();
  1963. if (targetType != cmStateEnums::SHARED_LIBRARY &&
  1964. targetType != cmStateEnums::MODULE_LIBRARY &&
  1965. targetType != cmStateEnums::EXECUTABLE) {
  1966. ::reportError(context, content->GetOriginalExpression(),
  1967. "TARGET_PDB_OUTPUT_NAME is allowed only for "
  1968. "targets with linker created artifacts.");
  1969. return std::string();
  1970. }
  1971. return target->GetPDBOutputName(context->Config);
  1972. }
  1973. };
  1974. template <typename ArtifactT>
  1975. struct TargetOutputNameArtifact : public TargetArtifactBase
  1976. {
  1977. TargetOutputNameArtifact() {} // NOLINT(modernize-use-equals-default)
  1978. int NumExpectedParameters() const override { return 1; }
  1979. std::string Evaluate(
  1980. const std::vector<std::string>& parameters,
  1981. cmGeneratorExpressionContext* context,
  1982. const GeneratorExpressionContent* content,
  1983. cmGeneratorExpressionDAGChecker* dagChecker) const override
  1984. {
  1985. cmGeneratorTarget* target =
  1986. this->GetTarget(parameters, context, content, dagChecker);
  1987. if (!target) {
  1988. return std::string();
  1989. }
  1990. std::string result = TargetOutputNameArtifactResultGetter<ArtifactT>::Get(
  1991. target, context, content);
  1992. if (context->HadError) {
  1993. return std::string();
  1994. }
  1995. return result;
  1996. }
  1997. };
  1998. static const TargetOutputNameArtifact<ArtifactNameTag> targetOutputNameNode;
  1999. static const TargetOutputNameArtifact<ArtifactLinkerTag>
  2000. targetLinkerOutputNameNode;
  2001. static const TargetOutputNameArtifact<ArtifactPdbTag> targetPdbOutputNameNode;
  2002. class ArtifactFilePrefixTag;
  2003. class ArtifactLinkerFilePrefixTag;
  2004. class ArtifactFileSuffixTag;
  2005. class ArtifactLinkerFileSuffixTag;
  2006. template <typename ArtifactT>
  2007. struct TargetFileArtifactResultGetter
  2008. {
  2009. static std::string Get(cmGeneratorTarget* target,
  2010. cmGeneratorExpressionContext* context,
  2011. const GeneratorExpressionContent* content);
  2012. };
  2013. template <>
  2014. struct TargetFileArtifactResultGetter<ArtifactFilePrefixTag>
  2015. {
  2016. static std::string Get(cmGeneratorTarget* target,
  2017. cmGeneratorExpressionContext* context,
  2018. const GeneratorExpressionContent*)
  2019. {
  2020. return target->GetFilePrefix(context->Config);
  2021. }
  2022. };
  2023. template <>
  2024. struct TargetFileArtifactResultGetter<ArtifactLinkerFilePrefixTag>
  2025. {
  2026. static std::string Get(cmGeneratorTarget* target,
  2027. cmGeneratorExpressionContext* context,
  2028. const GeneratorExpressionContent* content)
  2029. {
  2030. if (!target->IsLinkable()) {
  2031. ::reportError(context, content->GetOriginalExpression(),
  2032. "TARGET_LINKER_PREFIX is allowed only for libraries and "
  2033. "executables with ENABLE_EXPORTS.");
  2034. return std::string();
  2035. }
  2036. cmStateEnums::ArtifactType artifact =
  2037. target->HasImportLibrary(context->Config)
  2038. ? cmStateEnums::ImportLibraryArtifact
  2039. : cmStateEnums::RuntimeBinaryArtifact;
  2040. return target->GetFilePrefix(context->Config, artifact);
  2041. }
  2042. };
  2043. template <>
  2044. struct TargetFileArtifactResultGetter<ArtifactFileSuffixTag>
  2045. {
  2046. static std::string Get(cmGeneratorTarget* target,
  2047. cmGeneratorExpressionContext* context,
  2048. const GeneratorExpressionContent*)
  2049. {
  2050. return target->GetFileSuffix(context->Config);
  2051. }
  2052. };
  2053. template <>
  2054. struct TargetFileArtifactResultGetter<ArtifactLinkerFileSuffixTag>
  2055. {
  2056. static std::string Get(cmGeneratorTarget* target,
  2057. cmGeneratorExpressionContext* context,
  2058. const GeneratorExpressionContent* content)
  2059. {
  2060. if (!target->IsLinkable()) {
  2061. ::reportError(context, content->GetOriginalExpression(),
  2062. "TARGET_LINKER_SUFFIX is allowed only for libraries and "
  2063. "executables with ENABLE_EXPORTS.");
  2064. return std::string();
  2065. }
  2066. cmStateEnums::ArtifactType artifact =
  2067. target->HasImportLibrary(context->Config)
  2068. ? cmStateEnums::ImportLibraryArtifact
  2069. : cmStateEnums::RuntimeBinaryArtifact;
  2070. return target->GetFileSuffix(context->Config, artifact);
  2071. }
  2072. };
  2073. template <typename ArtifactT>
  2074. struct TargetFileArtifact : public TargetArtifactBase
  2075. {
  2076. TargetFileArtifact() {} // NOLINT(modernize-use-equals-default)
  2077. int NumExpectedParameters() const override { return 1; }
  2078. std::string Evaluate(
  2079. const std::vector<std::string>& parameters,
  2080. cmGeneratorExpressionContext* context,
  2081. const GeneratorExpressionContent* content,
  2082. cmGeneratorExpressionDAGChecker* dagChecker) const override
  2083. {
  2084. cmGeneratorTarget* target =
  2085. this->GetTarget(parameters, context, content, dagChecker);
  2086. if (!target) {
  2087. return std::string();
  2088. }
  2089. std::string result =
  2090. TargetFileArtifactResultGetter<ArtifactT>::Get(target, context, content);
  2091. if (context->HadError) {
  2092. return std::string();
  2093. }
  2094. return result;
  2095. }
  2096. };
  2097. static const TargetFileArtifact<ArtifactFilePrefixTag> targetFilePrefixNode;
  2098. static const TargetFileArtifact<ArtifactLinkerFilePrefixTag>
  2099. targetLinkerFilePrefixNode;
  2100. static const TargetFileArtifact<ArtifactFileSuffixTag> targetFileSuffixNode;
  2101. static const TargetFileArtifact<ArtifactLinkerFileSuffixTag>
  2102. targetLinkerFileSuffixNode;
  2103. static const struct ShellPathNode : public cmGeneratorExpressionNode
  2104. {
  2105. ShellPathNode() {} // NOLINT(modernize-use-equals-default)
  2106. std::string Evaluate(
  2107. const std::vector<std::string>& parameters,
  2108. cmGeneratorExpressionContext* context,
  2109. const GeneratorExpressionContent* content,
  2110. cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
  2111. {
  2112. std::vector<std::string> listIn;
  2113. cmSystemTools::ExpandListArgument(parameters.front(), listIn);
  2114. if (listIn.empty()) {
  2115. reportError(context, content->GetOriginalExpression(),
  2116. "\"\" is not an absolute path.");
  2117. return std::string();
  2118. }
  2119. cmStateSnapshot snapshot = context->LG->GetStateSnapshot();
  2120. cmOutputConverter converter(snapshot);
  2121. const char* separator = snapshot.GetState()->UseWindowsShell() ? ";" : ":";
  2122. std::vector<std::string> listOut;
  2123. listOut.reserve(listIn.size());
  2124. for (auto const& in : listIn) {
  2125. if (!cmSystemTools::FileIsFullPath(in)) {
  2126. reportError(context, content->GetOriginalExpression(),
  2127. "\"" + in + "\" is not an absolute path.");
  2128. return std::string();
  2129. }
  2130. listOut.emplace_back(converter.ConvertDirectorySeparatorsForShell(in));
  2131. }
  2132. return cmJoin(listOut, separator);
  2133. }
  2134. } shellPathNode;
  2135. const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode(
  2136. const std::string& identifier)
  2137. {
  2138. static std::map<std::string, cmGeneratorExpressionNode const*> const nodeMap{
  2139. { "0", &zeroNode },
  2140. { "1", &oneNode },
  2141. { "AND", &andNode },
  2142. { "OR", &orNode },
  2143. { "NOT", &notNode },
  2144. { "C_COMPILER_ID", &cCompilerIdNode },
  2145. { "CXX_COMPILER_ID", &cxxCompilerIdNode },
  2146. { "CUDA_COMPILER_ID", &cudaCompilerIdNode },
  2147. { "Fortran_COMPILER_ID", &fortranCompilerIdNode },
  2148. { "VERSION_GREATER", &versionGreaterNode },
  2149. { "VERSION_GREATER_EQUAL", &versionGreaterEqNode },
  2150. { "VERSION_LESS", &versionLessNode },
  2151. { "VERSION_LESS_EQUAL", &versionLessEqNode },
  2152. { "VERSION_EQUAL", &versionEqualNode },
  2153. { "C_COMPILER_VERSION", &cCompilerVersionNode },
  2154. { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode },
  2155. { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode },
  2156. { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode },
  2157. { "PLATFORM_ID", &platformIdNode },
  2158. { "COMPILE_FEATURES", &compileFeaturesNode },
  2159. { "CONFIGURATION", &configurationNode },
  2160. { "CONFIG", &configurationTestNode },
  2161. { "TARGET_FILE", &targetNodeGroup.File },
  2162. { "TARGET_LINKER_FILE", &targetLinkerNodeGroup.File },
  2163. { "TARGET_SONAME_FILE", &targetSoNameNodeGroup.File },
  2164. { "TARGET_PDB_FILE", &targetPdbNodeGroup.File },
  2165. { "TARGET_FILE_PREFIX", &targetFilePrefixNode },
  2166. { "TARGET_LINKER_FILE_PREFIX", &targetLinkerFilePrefixNode },
  2167. { "TARGET_FILE_SUFFIX", &targetFileSuffixNode },
  2168. { "TARGET_LINKER_FILE_SUFFIX", &targetLinkerFileSuffixNode },
  2169. { "TARGET_FILE_NAME", &targetNodeGroup.FileName },
  2170. { "TARGET_LINKER_FILE_NAME", &targetLinkerNodeGroup.FileName },
  2171. { "TARGET_SONAME_FILE_NAME", &targetSoNameNodeGroup.FileName },
  2172. { "TARGET_PDB_FILE_NAME", &targetPdbNodeGroup.FileName },
  2173. { "TARGET_FILE_DIR", &targetNodeGroup.FileDir },
  2174. { "TARGET_LINKER_FILE_DIR", &targetLinkerNodeGroup.FileDir },
  2175. { "TARGET_SONAME_FILE_DIR", &targetSoNameNodeGroup.FileDir },
  2176. { "TARGET_PDB_FILE_DIR", &targetPdbNodeGroup.FileDir },
  2177. { "TARGET_BUNDLE_DIR", &targetBundleDirNode },
  2178. { "TARGET_BUNDLE_CONTENT_DIR", &targetBundleContentDirNode },
  2179. { "TARGET_OUTPUT_NAME", &targetOutputNameNode },
  2180. { "TARGET_LINKER_OUTPUT_NAME", &targetLinkerOutputNameNode },
  2181. { "TARGET_PDB_OUTPUT_NAME", &targetPdbOutputNameNode },
  2182. { "STREQUAL", &strEqualNode },
  2183. { "EQUAL", &equalNode },
  2184. { "IN_LIST", &inListNode },
  2185. { "FILTER", &filterNode },
  2186. { "REMOVE_DUPLICATES", &removeDuplicatesNode },
  2187. { "LOWER_CASE", &lowerCaseNode },
  2188. { "UPPER_CASE", &upperCaseNode },
  2189. { "MAKE_C_IDENTIFIER", &makeCIdentifierNode },
  2190. { "BOOL", &boolNode },
  2191. { "IF", &ifNode },
  2192. { "ANGLE-R", &angle_rNode },
  2193. { "COMMA", &commaNode },
  2194. { "SEMICOLON", &semicolonNode },
  2195. { "TARGET_PROPERTY", &targetPropertyNode },
  2196. { "TARGET_NAME", &targetNameNode },
  2197. { "TARGET_OBJECTS", &targetObjectsNode },
  2198. { "TARGET_POLICY", &targetPolicyNode },
  2199. { "TARGET_EXISTS", &targetExistsNode },
  2200. { "TARGET_NAME_IF_EXISTS", &targetNameIfExistsNode },
  2201. { "TARGET_GENEX_EVAL", &targetGenexEvalNode },
  2202. { "GENEX_EVAL", &genexEvalNode },
  2203. { "BUILD_INTERFACE", &buildInterfaceNode },
  2204. { "INSTALL_INTERFACE", &installInterfaceNode },
  2205. { "INSTALL_PREFIX", &installPrefixNode },
  2206. { "JOIN", &joinNode },
  2207. { "LINK_ONLY", &linkOnlyNode },
  2208. { "COMPILE_LANGUAGE", &languageNode },
  2209. { "SHELL_PATH", &shellPathNode }
  2210. };
  2211. {
  2212. auto itr = nodeMap.find(identifier);
  2213. if (itr != nodeMap.end()) {
  2214. return itr->second;
  2215. }
  2216. }
  2217. return nullptr;
  2218. }
  2219. void reportError(cmGeneratorExpressionContext* context,
  2220. const std::string& expr, const std::string& result)
  2221. {
  2222. context->HadError = true;
  2223. if (context->Quiet) {
  2224. return;
  2225. }
  2226. std::ostringstream e;
  2227. /* clang-format off */
  2228. e << "Error evaluating generator expression:\n"
  2229. << " " << expr << "\n"
  2230. << result;
  2231. /* clang-format on */
  2232. context->LG->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2233. e.str(), context->Backtrace);
  2234. }