cmGeneratorExpression.cxx 12 KB

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