cmGeneratorExpression.cxx 12 KB

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