cmGeneratorExpression.cxx 12 KB

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