cmGeneratorExpressionNode.cxx 89 KB

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