cmGeneratorExpression.cxx 12 KB

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