cmGeneratorExpression.cxx 12 KB

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