cmGeneratorExpression.cxx 12 KB

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