1
0

cmGeneratorExpression.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 <utility>
  6. #include "assert.h"
  7. #include "cmAlgorithms.h"
  8. #include "cmGeneratorExpressionContext.h"
  9. #include "cmGeneratorExpressionEvaluator.h"
  10. #include "cmGeneratorExpressionLexer.h"
  11. #include "cmGeneratorExpressionParser.h"
  12. #include "cmSystemTools.h"
  13. #include "cm_auto_ptr.hxx"
  14. cmGeneratorExpression::cmGeneratorExpression(
  15. const cmListFileBacktrace& backtrace)
  16. : Backtrace(backtrace)
  17. {
  18. }
  19. CM_AUTO_PTR<cmCompiledGeneratorExpression> cmGeneratorExpression::Parse(
  20. std::string const& input)
  21. {
  22. return CM_AUTO_PTR<cmCompiledGeneratorExpression>(
  23. new cmCompiledGeneratorExpression(this->Backtrace, input));
  24. }
  25. CM_AUTO_PTR<cmCompiledGeneratorExpression> cmGeneratorExpression::Parse(
  26. const char* input)
  27. {
  28. return this->Parse(std::string(input ? input : ""));
  29. }
  30. cmGeneratorExpression::~cmGeneratorExpression()
  31. {
  32. }
  33. const char* cmCompiledGeneratorExpression::Evaluate(
  34. cmLocalGenerator* lg, const std::string& config, bool quiet,
  35. const cmGeneratorTarget* headTarget,
  36. cmGeneratorExpressionDAGChecker* dagChecker,
  37. std::string const& language) const
  38. {
  39. return this->Evaluate(lg, config, quiet, headTarget, headTarget, dagChecker,
  40. language);
  41. }
  42. const char* cmCompiledGeneratorExpression::Evaluate(
  43. cmLocalGenerator* lg, const std::string& config, bool quiet,
  44. const cmGeneratorTarget* headTarget, const cmGeneratorTarget* currentTarget,
  45. cmGeneratorExpressionDAGChecker* dagChecker,
  46. std::string const& language) const
  47. {
  48. cmGeneratorExpressionContext context(
  49. lg, config, quiet, headTarget, currentTarget ? currentTarget : headTarget,
  50. this->EvaluateForBuildsystem, this->Backtrace, language);
  51. return this->EvaluateWithContext(context, dagChecker);
  52. }
  53. const char* cmCompiledGeneratorExpression::EvaluateWithContext(
  54. cmGeneratorExpressionContext& context,
  55. cmGeneratorExpressionDAGChecker* dagChecker) const
  56. {
  57. if (!this->NeedsEvaluation) {
  58. return this->Input.c_str();
  59. }
  60. this->Output = "";
  61. std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
  62. this->Evaluators.begin();
  63. const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
  64. this->Evaluators.end();
  65. for (; it != end; ++it) {
  66. this->Output += (*it)->Evaluate(&context, dagChecker);
  67. this->SeenTargetProperties.insert(context.SeenTargetProperties.begin(),
  68. context.SeenTargetProperties.end());
  69. if (context.HadError) {
  70. this->Output = "";
  71. break;
  72. }
  73. }
  74. this->MaxLanguageStandard = context.MaxLanguageStandard;
  75. if (!context.HadError) {
  76. this->HadContextSensitiveCondition = context.HadContextSensitiveCondition;
  77. this->HadHeadSensitiveCondition = context.HadHeadSensitiveCondition;
  78. this->SourceSensitiveTargets = context.SourceSensitiveTargets;
  79. }
  80. this->DependTargets = context.DependTargets;
  81. this->AllTargetsSeen = context.AllTargets;
  82. // TODO: Return a std::string from here instead?
  83. return this->Output.c_str();
  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(';') == input.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)) != input.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::vector<std::string>::const_iterator ei = entries.begin();
  177. ei != entries.end(); ++ei) {
  178. result += sep;
  179. sep = ";";
  180. if (!cmSystemTools::FileIsFullPath(ei->c_str()) &&
  181. cmGeneratorExpression::Find(*ei) != 0) {
  182. result += prefix;
  183. }
  184. result += *ei;
  185. }
  186. }
  187. static std::string stripExportInterface(
  188. const std::string& input, cmGeneratorExpression::PreprocessContext context,
  189. bool resolveRelative)
  190. {
  191. std::string result;
  192. int nestingLevel = 0;
  193. std::string::size_type pos = 0;
  194. std::string::size_type lastPos = pos;
  195. while (true) {
  196. std::string::size_type bPos = input.find("$<BUILD_INTERFACE:", lastPos);
  197. std::string::size_type iPos = input.find("$<INSTALL_INTERFACE:", lastPos);
  198. if (bPos == std::string::npos && iPos == std::string::npos) {
  199. break;
  200. }
  201. if (bPos == std::string::npos) {
  202. pos = iPos;
  203. } else if (iPos == std::string::npos) {
  204. pos = bPos;
  205. } else {
  206. pos = (bPos < iPos) ? bPos : iPos;
  207. }
  208. result += input.substr(lastPos, pos - lastPos);
  209. const bool gotInstallInterface = input[pos + 2] == 'I';
  210. pos += gotInstallInterface ? sizeof("$<INSTALL_INTERFACE:") - 1
  211. : sizeof("$<BUILD_INTERFACE:") - 1;
  212. nestingLevel = 1;
  213. const char* c = input.c_str() + pos;
  214. const char* const cStart = c;
  215. for (; *c; ++c) {
  216. if (c[0] == '$' && c[1] == '<') {
  217. ++nestingLevel;
  218. ++c;
  219. continue;
  220. }
  221. if (c[0] == '>') {
  222. --nestingLevel;
  223. if (nestingLevel != 0) {
  224. continue;
  225. }
  226. if (context == cmGeneratorExpression::BuildInterface &&
  227. !gotInstallInterface) {
  228. result += input.substr(pos, c - cStart);
  229. } else if (context == cmGeneratorExpression::InstallInterface &&
  230. gotInstallInterface) {
  231. const std::string content = input.substr(pos, c - cStart);
  232. if (resolveRelative) {
  233. prefixItems(content, result, "${_IMPORT_PREFIX}/");
  234. } else {
  235. result += content;
  236. }
  237. }
  238. break;
  239. }
  240. }
  241. const std::string::size_type traversed = (c - cStart) + 1;
  242. if (!*c) {
  243. result += std::string(gotInstallInterface ? "$<INSTALL_INTERFACE:"
  244. : "$<BUILD_INTERFACE:") +
  245. input.substr(pos, traversed);
  246. }
  247. pos += traversed;
  248. lastPos = pos;
  249. }
  250. if (nestingLevel == 0) {
  251. result += input.substr(lastPos);
  252. }
  253. return cmGeneratorExpression::StripEmptyListElements(result);
  254. }
  255. void cmGeneratorExpression::Split(const std::string& input,
  256. std::vector<std::string>& output)
  257. {
  258. std::string::size_type pos = 0;
  259. std::string::size_type lastPos = pos;
  260. while ((pos = input.find("$<", lastPos)) != input.npos) {
  261. std::string part = input.substr(lastPos, pos - lastPos);
  262. std::string preGenex;
  263. if (!part.empty()) {
  264. std::string::size_type startPos = input.rfind(';', pos);
  265. if (startPos == std::string::npos) {
  266. preGenex = part;
  267. part = "";
  268. } else if (startPos != pos - 1 && startPos >= lastPos) {
  269. part = input.substr(lastPos, startPos - lastPos);
  270. preGenex = input.substr(startPos + 1, pos - startPos - 1);
  271. }
  272. if (!part.empty()) {
  273. cmSystemTools::ExpandListArgument(part, output);
  274. }
  275. }
  276. pos += 2;
  277. int nestingLevel = 1;
  278. const char* c = input.c_str() + pos;
  279. const char* const cStart = c;
  280. for (; *c; ++c) {
  281. if (c[0] == '$' && c[1] == '<') {
  282. ++nestingLevel;
  283. ++c;
  284. continue;
  285. }
  286. if (c[0] == '>') {
  287. --nestingLevel;
  288. if (nestingLevel == 0) {
  289. break;
  290. }
  291. }
  292. }
  293. for (; *c; ++c) {
  294. // Capture the part after the genex and before the next ';'
  295. if (c[0] == ';') {
  296. --c;
  297. break;
  298. }
  299. }
  300. const std::string::size_type traversed = (c - cStart) + 1;
  301. output.push_back(preGenex + "$<" + input.substr(pos, traversed));
  302. pos += traversed;
  303. lastPos = pos;
  304. }
  305. if (lastPos < input.size()) {
  306. cmSystemTools::ExpandListArgument(input.substr(lastPos), output);
  307. }
  308. }
  309. std::string cmGeneratorExpression::Preprocess(const std::string& input,
  310. PreprocessContext context,
  311. bool resolveRelative)
  312. {
  313. if (context == StripAllGeneratorExpressions) {
  314. return stripAllGeneratorExpressions(input);
  315. }
  316. if (context == BuildInterface || context == InstallInterface) {
  317. return stripExportInterface(input, context, resolveRelative);
  318. }
  319. assert(0 && "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. }