cmGeneratorExpression.cxx 13 KB

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