cmGeneratorExpression.cxx 12 KB

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