cmGeneratorExpression.cxx 13 KB

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