cmGeneratorExpression.cxx 12 KB

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