cmGeneratorExpression.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 "cmGeneratorExpression.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <memory>
  7. #include <utility>
  8. #include <cm/string_view>
  9. #include "cmsys/RegularExpression.hxx"
  10. #include "cmGeneratorExpressionContext.h"
  11. #include "cmGeneratorExpressionDAGChecker.h"
  12. #include "cmGeneratorExpressionEvaluator.h"
  13. #include "cmGeneratorExpressionLexer.h"
  14. #include "cmGeneratorExpressionParser.h"
  15. #include "cmList.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmStringAlgorithms.h"
  18. #include "cmSystemTools.h"
  19. #include "cmake.h"
  20. cmGeneratorExpression::cmGeneratorExpression(cmake& cmakeInstance,
  21. cmListFileBacktrace backtrace)
  22. : CMakeInstance(cmakeInstance)
  23. , Backtrace(std::move(backtrace))
  24. {
  25. }
  26. cmCompiledGeneratorExpression::~cmCompiledGeneratorExpression() = default;
  27. cmGeneratorExpression::~cmGeneratorExpression() = default;
  28. std::unique_ptr<cmCompiledGeneratorExpression> cmGeneratorExpression::Parse(
  29. std::string input) const
  30. {
  31. return std::unique_ptr<cmCompiledGeneratorExpression>(
  32. new cmCompiledGeneratorExpression(this->CMakeInstance, this->Backtrace,
  33. std::move(input)));
  34. }
  35. std::string cmGeneratorExpression::Evaluate(
  36. std::string input, cmLocalGenerator* lg, const std::string& config,
  37. cmGeneratorTarget const* headTarget,
  38. cmGeneratorExpressionDAGChecker* dagChecker,
  39. cmGeneratorTarget const* currentTarget, std::string const& language)
  40. {
  41. if (Find(input) != std::string::npos) {
  42. #ifndef CMAKE_BOOTSTRAP
  43. auto profilingRAII = lg->GetCMakeInstance()->CreateProfilingEntry(
  44. "genex_compile_eval", input);
  45. #endif
  46. cmCompiledGeneratorExpression cge(*lg->GetCMakeInstance(),
  47. cmListFileBacktrace(), std::move(input));
  48. return cge.Evaluate(lg, config, headTarget, dagChecker, currentTarget,
  49. language);
  50. }
  51. return input;
  52. }
  53. const std::string& cmCompiledGeneratorExpression::Evaluate(
  54. cmLocalGenerator* lg, const std::string& config,
  55. const cmGeneratorTarget* headTarget,
  56. cmGeneratorExpressionDAGChecker* dagChecker,
  57. const cmGeneratorTarget* currentTarget, std::string const& language) const
  58. {
  59. cmGeneratorExpressionContext context(
  60. lg, config, this->Quiet, headTarget,
  61. currentTarget ? currentTarget : headTarget, this->EvaluateForBuildsystem,
  62. this->Backtrace, language);
  63. if (!this->NeedsEvaluation) {
  64. return this->Input;
  65. }
  66. this->Output.clear();
  67. for (const auto& it : this->Evaluators) {
  68. this->Output += it->Evaluate(&context, dagChecker);
  69. this->SeenTargetProperties.insert(context.SeenTargetProperties.cbegin(),
  70. context.SeenTargetProperties.cend());
  71. if (context.HadError) {
  72. this->Output.clear();
  73. break;
  74. }
  75. }
  76. this->MaxLanguageStandard = context.MaxLanguageStandard;
  77. if (!context.HadError) {
  78. this->HadContextSensitiveCondition = context.HadContextSensitiveCondition;
  79. this->HadHeadSensitiveCondition = context.HadHeadSensitiveCondition;
  80. this->HadLinkLanguageSensitiveCondition =
  81. context.HadLinkLanguageSensitiveCondition;
  82. this->SourceSensitiveTargets = context.SourceSensitiveTargets;
  83. }
  84. this->DependTargets = context.DependTargets;
  85. this->AllTargetsSeen = context.AllTargets;
  86. return this->Output;
  87. }
  88. cmCompiledGeneratorExpression::cmCompiledGeneratorExpression(
  89. cmake& cmakeInstance, cmListFileBacktrace backtrace, std::string input)
  90. : Backtrace(std::move(backtrace))
  91. , Input(std::move(input))
  92. {
  93. #ifndef CMAKE_BOOTSTRAP
  94. auto profilingRAII =
  95. cmakeInstance.CreateProfilingEntry("genex_compile", this->Input);
  96. #endif
  97. cmGeneratorExpressionLexer l;
  98. std::vector<cmGeneratorExpressionToken> tokens = l.Tokenize(this->Input);
  99. this->NeedsEvaluation = l.GetSawGeneratorExpression();
  100. if (this->NeedsEvaluation) {
  101. cmGeneratorExpressionParser p(tokens);
  102. p.Parse(this->Evaluators);
  103. }
  104. }
  105. std::string cmGeneratorExpression::StripEmptyListElements(
  106. const std::string& input)
  107. {
  108. if (input.find(';') == std::string::npos) {
  109. return input;
  110. }
  111. std::string result;
  112. result.reserve(input.size());
  113. const char* c = input.c_str();
  114. const char* last = c;
  115. bool skipSemiColons = true;
  116. for (; *c; ++c) {
  117. if (*c == ';') {
  118. if (skipSemiColons) {
  119. result.append(last, c - last);
  120. last = c + 1;
  121. }
  122. skipSemiColons = true;
  123. } else {
  124. skipSemiColons = false;
  125. }
  126. }
  127. result.append(last);
  128. if (!result.empty() && *(result.end() - 1) == ';') {
  129. result.resize(result.size() - 1);
  130. }
  131. return result;
  132. }
  133. static std::string stripAllGeneratorExpressions(const std::string& input)
  134. {
  135. std::string result;
  136. std::string::size_type pos = 0;
  137. std::string::size_type lastPos = pos;
  138. int nestingLevel = 0;
  139. while ((pos = input.find("$<", lastPos)) != std::string::npos) {
  140. result += input.substr(lastPos, pos - lastPos);
  141. pos += 2;
  142. nestingLevel = 1;
  143. const char* c = input.c_str() + pos;
  144. const char* const cStart = c;
  145. for (; *c; ++c) {
  146. if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
  147. ++nestingLevel;
  148. ++c;
  149. continue;
  150. }
  151. if (c[0] == '>') {
  152. --nestingLevel;
  153. if (nestingLevel == 0) {
  154. break;
  155. }
  156. }
  157. }
  158. const std::string::size_type traversed = (c - cStart) + 1;
  159. if (!*c) {
  160. result += "$<" + input.substr(pos, traversed);
  161. }
  162. pos += traversed;
  163. lastPos = pos;
  164. }
  165. if (nestingLevel == 0) {
  166. result += input.substr(lastPos);
  167. }
  168. return cmGeneratorExpression::StripEmptyListElements(result);
  169. }
  170. static void prefixItems(const std::string& content, std::string& result,
  171. const cm::string_view& prefix)
  172. {
  173. std::vector<std::string> entries;
  174. cmGeneratorExpression::Split(content, entries);
  175. const char* sep = "";
  176. for (std::string const& e : entries) {
  177. result += sep;
  178. sep = ";";
  179. if (!cmSystemTools::FileIsFullPath(e) &&
  180. cmGeneratorExpression::Find(e) != 0) {
  181. result += prefix;
  182. }
  183. result += e;
  184. }
  185. }
  186. static std::string stripExportInterface(
  187. const std::string& input, cmGeneratorExpression::PreprocessContext context,
  188. cm::string_view importPrefix)
  189. {
  190. std::string result;
  191. int nestingLevel = 0;
  192. std::string::size_type pos = 0;
  193. std::string::size_type lastPos = pos;
  194. while (true) {
  195. std::string::size_type bPos = input.find("$<BUILD_INTERFACE:", lastPos);
  196. std::string::size_type iPos = input.find("$<INSTALL_INTERFACE:", lastPos);
  197. std::string::size_type lPos =
  198. input.find("$<BUILD_LOCAL_INTERFACE:", lastPos);
  199. pos = std::min({ bPos, iPos, lPos });
  200. if (pos == std::string::npos) {
  201. break;
  202. }
  203. result += input.substr(lastPos, pos - lastPos);
  204. enum class FoundGenex
  205. {
  206. BuildInterface,
  207. InstallInterface,
  208. BuildLocalInterface,
  209. } foundGenex = FoundGenex::BuildInterface;
  210. if (pos == bPos) {
  211. foundGenex = FoundGenex::BuildInterface;
  212. pos += cmStrLen("$<BUILD_INTERFACE:");
  213. } else if (pos == iPos) {
  214. foundGenex = FoundGenex::InstallInterface;
  215. pos += cmStrLen("$<INSTALL_INTERFACE:");
  216. } else if (pos == lPos) {
  217. foundGenex = FoundGenex::BuildLocalInterface;
  218. pos += cmStrLen("$<BUILD_LOCAL_INTERFACE:");
  219. } else {
  220. assert(false && "Invalid position found");
  221. }
  222. nestingLevel = 1;
  223. const char* c = input.c_str() + pos;
  224. const char* const cStart = c;
  225. for (; *c; ++c) {
  226. if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
  227. ++nestingLevel;
  228. ++c;
  229. continue;
  230. }
  231. if (c[0] == '>') {
  232. --nestingLevel;
  233. if (nestingLevel != 0) {
  234. continue;
  235. }
  236. if (context == cmGeneratorExpression::BuildInterface &&
  237. foundGenex == FoundGenex::BuildInterface) {
  238. result += input.substr(pos, c - cStart);
  239. } else if (context == cmGeneratorExpression::InstallInterface &&
  240. foundGenex == FoundGenex::InstallInterface) {
  241. const std::string content = input.substr(pos, c - cStart);
  242. if (!importPrefix.empty()) {
  243. prefixItems(content, result, importPrefix);
  244. } else {
  245. result += content;
  246. }
  247. }
  248. break;
  249. }
  250. }
  251. const std::string::size_type traversed = (c - cStart) + 1;
  252. if (!*c) {
  253. auto remaining = input.substr(pos, traversed);
  254. switch (foundGenex) {
  255. case FoundGenex::BuildInterface:
  256. result = cmStrCat(result, "$<BUILD_INTERFACE:", remaining);
  257. break;
  258. case FoundGenex::InstallInterface:
  259. result = cmStrCat(result, "$<INSTALL_INTERFACE:", remaining);
  260. break;
  261. case FoundGenex::BuildLocalInterface:
  262. result = cmStrCat(result, "$<BUILD_LOCAL_INTERFACE:", remaining);
  263. break;
  264. }
  265. }
  266. pos += traversed;
  267. lastPos = pos;
  268. }
  269. if (nestingLevel == 0) {
  270. result += input.substr(lastPos);
  271. }
  272. return cmGeneratorExpression::StripEmptyListElements(result);
  273. }
  274. void cmGeneratorExpression::Split(const std::string& input,
  275. std::vector<std::string>& output)
  276. {
  277. std::string::size_type pos = 0;
  278. std::string::size_type lastPos = pos;
  279. while ((pos = input.find("$<", lastPos)) != std::string::npos) {
  280. std::string part = input.substr(lastPos, pos - lastPos);
  281. std::string preGenex;
  282. if (!part.empty()) {
  283. std::string::size_type startPos = input.rfind(';', pos);
  284. if (startPos == std::string::npos) {
  285. preGenex = part;
  286. part.clear();
  287. } else if (startPos != pos - 1 && startPos >= lastPos) {
  288. part = input.substr(lastPos, startPos - lastPos);
  289. preGenex = input.substr(startPos + 1, pos - startPos - 1);
  290. }
  291. if (!part.empty()) {
  292. cmExpandList(part, output);
  293. }
  294. }
  295. pos += 2;
  296. int nestingLevel = 1;
  297. const char* c = input.c_str() + pos;
  298. const char* const cStart = c;
  299. for (; *c; ++c) {
  300. if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
  301. ++nestingLevel;
  302. ++c;
  303. continue;
  304. }
  305. if (c[0] == '>') {
  306. --nestingLevel;
  307. if (nestingLevel == 0) {
  308. break;
  309. }
  310. }
  311. }
  312. for (; *c; ++c) {
  313. // Capture the part after the genex and before the next ';'
  314. if (c[0] == ';') {
  315. --c;
  316. break;
  317. }
  318. }
  319. const std::string::size_type traversed = (c - cStart) + 1;
  320. output.push_back(preGenex + "$<" + input.substr(pos, traversed));
  321. pos += traversed;
  322. lastPos = pos;
  323. }
  324. if (lastPos < input.size()) {
  325. cmExpandList(input.substr(lastPos), output);
  326. }
  327. }
  328. std::string cmGeneratorExpression::Preprocess(const std::string& input,
  329. PreprocessContext context,
  330. cm::string_view importPrefix)
  331. {
  332. if (context == StripAllGeneratorExpressions) {
  333. return stripAllGeneratorExpressions(input);
  334. }
  335. if (context == BuildInterface || context == InstallInterface) {
  336. return stripExportInterface(input, context, importPrefix);
  337. }
  338. assert(false &&
  339. "cmGeneratorExpression::Preprocess called with invalid args");
  340. return std::string();
  341. }
  342. cm::string_view::size_type cmGeneratorExpression::Find(
  343. const cm::string_view& input)
  344. {
  345. const cm::string_view::size_type openpos = input.find("$<");
  346. if (openpos != cm::string_view::npos &&
  347. input.find('>', openpos) != cm::string_view::npos) {
  348. return openpos;
  349. }
  350. return cm::string_view::npos;
  351. }
  352. bool cmGeneratorExpression::IsValidTargetName(const std::string& input)
  353. {
  354. // The ':' is supported to allow use with IMPORTED targets. At least
  355. // Qt 4 and 5 IMPORTED targets use ':' as the namespace delimiter.
  356. static cmsys::RegularExpression targetNameValidator("^[A-Za-z0-9_.:+-]+$");
  357. return targetNameValidator.find(input);
  358. }
  359. void cmGeneratorExpression::ReplaceInstallPrefix(
  360. std::string& input, const std::string& replacement)
  361. {
  362. std::string::size_type pos = 0;
  363. std::string::size_type lastPos = pos;
  364. while ((pos = input.find("$<INSTALL_PREFIX>", lastPos)) !=
  365. std::string::npos) {
  366. std::string::size_type endPos = pos + cmStrLen("$<INSTALL_PREFIX>");
  367. input.replace(pos, endPos - pos, replacement);
  368. lastPos = endPos;
  369. }
  370. }
  371. void cmCompiledGeneratorExpression::GetMaxLanguageStandard(
  372. const cmGeneratorTarget* tgt, std::map<std::string, std::string>& mapping)
  373. {
  374. auto it = this->MaxLanguageStandard.find(tgt);
  375. if (it != this->MaxLanguageStandard.end()) {
  376. mapping = it->second;
  377. }
  378. }
  379. const std::string& cmGeneratorExpressionInterpreter::Evaluate(
  380. std::string expression, const std::string& property)
  381. {
  382. this->CompiledGeneratorExpression =
  383. this->GeneratorExpression.Parse(std::move(expression));
  384. // Specify COMPILE_OPTIONS to DAGchecker, same semantic as COMPILE_FLAGS
  385. cmGeneratorExpressionDAGChecker dagChecker{
  386. this->HeadTarget,
  387. property == "COMPILE_FLAGS" ? "COMPILE_OPTIONS" : property,
  388. nullptr,
  389. nullptr,
  390. this->LocalGenerator,
  391. this->Config,
  392. };
  393. return this->CompiledGeneratorExpression->Evaluate(
  394. this->LocalGenerator, this->Config, this->HeadTarget, &dagChecker, nullptr,
  395. this->Language);
  396. }