cmGeneratorExpressionNode.cxx 86 KB

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