cmGeneratorExpression.cxx 13 KB

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