cmGeneratorExpression.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 char* 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 char* 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 char* cmCompiledGeneratorExpression::EvaluateWithContext(
  55. cmGeneratorExpressionContext& context,
  56. cmGeneratorExpressionDAGChecker* dagChecker) const
  57. {
  58. if (!this->NeedsEvaluation) {
  59. return this->Input.c_str();
  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. // TODO: Return a std::string from here instead?
  84. return this->Output.c_str();
  85. }
  86. cmCompiledGeneratorExpression::cmCompiledGeneratorExpression(
  87. cmListFileBacktrace const& backtrace, const std::string& input)
  88. : Backtrace(backtrace)
  89. , Input(input)
  90. , HadContextSensitiveCondition(false)
  91. , HadHeadSensitiveCondition(false)
  92. , EvaluateForBuildsystem(false)
  93. {
  94. cmGeneratorExpressionLexer l;
  95. std::vector<cmGeneratorExpressionToken> tokens = l.Tokenize(this->Input);
  96. this->NeedsEvaluation = l.GetSawGeneratorExpression();
  97. if (this->NeedsEvaluation) {
  98. cmGeneratorExpressionParser p(tokens);
  99. p.Parse(this->Evaluators);
  100. }
  101. }
  102. cmCompiledGeneratorExpression::~cmCompiledGeneratorExpression()
  103. {
  104. cmDeleteAll(this->Evaluators);
  105. }
  106. std::string cmGeneratorExpression::StripEmptyListElements(
  107. const std::string& input)
  108. {
  109. if (input.find(';') == std::string::npos) {
  110. return input;
  111. }
  112. std::string result;
  113. result.reserve(input.size());
  114. const char* c = input.c_str();
  115. const char* last = c;
  116. bool skipSemiColons = true;
  117. for (; *c; ++c) {
  118. if (*c == ';') {
  119. if (skipSemiColons) {
  120. result.append(last, c - last);
  121. last = c + 1;
  122. }
  123. skipSemiColons = true;
  124. } else {
  125. skipSemiColons = false;
  126. }
  127. }
  128. result.append(last);
  129. if (!result.empty() && *(result.end() - 1) == ';') {
  130. result.resize(result.size() - 1);
  131. }
  132. return result;
  133. }
  134. static std::string stripAllGeneratorExpressions(const std::string& input)
  135. {
  136. std::string result;
  137. std::string::size_type pos = 0;
  138. std::string::size_type lastPos = pos;
  139. int nestingLevel = 0;
  140. while ((pos = input.find("$<", lastPos)) != std::string::npos) {
  141. result += input.substr(lastPos, pos - lastPos);
  142. pos += 2;
  143. nestingLevel = 1;
  144. const char* c = input.c_str() + pos;
  145. const char* const cStart = c;
  146. for (; *c; ++c) {
  147. if (c[0] == '$' && c[1] == '<') {
  148. ++nestingLevel;
  149. ++c;
  150. continue;
  151. }
  152. if (c[0] == '>') {
  153. --nestingLevel;
  154. if (nestingLevel == 0) {
  155. break;
  156. }
  157. }
  158. }
  159. const std::string::size_type traversed = (c - cStart) + 1;
  160. if (!*c) {
  161. result += "$<" + input.substr(pos, traversed);
  162. }
  163. pos += traversed;
  164. lastPos = pos;
  165. }
  166. if (nestingLevel == 0) {
  167. result += input.substr(lastPos);
  168. }
  169. return cmGeneratorExpression::StripEmptyListElements(result);
  170. }
  171. static void prefixItems(const std::string& content, std::string& result,
  172. const std::string& prefix)
  173. {
  174. std::vector<std::string> entries;
  175. cmGeneratorExpression::Split(content, entries);
  176. const char* sep = "";
  177. for (std::string const& e : entries) {
  178. result += sep;
  179. sep = ";";
  180. if (!cmSystemTools::FileIsFullPath(e) &&
  181. cmGeneratorExpression::Find(e) != 0) {
  182. result += prefix;
  183. }
  184. result += e;
  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)) != std::string::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.clear();
  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(false &&
  320. "cmGeneratorExpression::Preprocess called with invalid args");
  321. return std::string();
  322. }
  323. std::string::size_type cmGeneratorExpression::Find(const std::string& input)
  324. {
  325. const std::string::size_type openpos = input.find("$<");
  326. if (openpos != std::string::npos &&
  327. input.find('>', openpos) != std::string::npos) {
  328. return openpos;
  329. }
  330. return std::string::npos;
  331. }
  332. bool cmGeneratorExpression::IsValidTargetName(const std::string& input)
  333. {
  334. // The ':' is supported to allow use with IMPORTED targets. At least
  335. // Qt 4 and 5 IMPORTED targets use ':' as the namespace delimiter.
  336. static cmsys::RegularExpression targetNameValidator("^[A-Za-z0-9_.:+-]+$");
  337. return targetNameValidator.find(input);
  338. }
  339. void cmCompiledGeneratorExpression::GetMaxLanguageStandard(
  340. const cmGeneratorTarget* tgt, std::map<std::string, std::string>& mapping)
  341. {
  342. typedef std::map<cmGeneratorTarget const*,
  343. std::map<std::string, std::string>>
  344. MapType;
  345. MapType::const_iterator it = this->MaxLanguageStandard.find(tgt);
  346. if (it != this->MaxLanguageStandard.end()) {
  347. mapping = it->second;
  348. }
  349. }
  350. const char* cmGeneratorExpressionInterpreter::Evaluate(
  351. const char* expression, const std::string& property)
  352. {
  353. if (this->Target.empty()) {
  354. return this->EvaluateExpression(expression);
  355. }
  356. // Specify COMPILE_OPTIONS to DAGchecker, same semantic as COMPILE_FLAGS
  357. cmGeneratorExpressionDAGChecker dagChecker(
  358. this->Target, property == "COMPILE_FLAGS" ? "COMPILE_OPTIONS" : property,
  359. nullptr, nullptr);
  360. return this->EvaluateExpression(expression, &dagChecker);
  361. }