cmPkgConfigResolver.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmPkgConfigResolver.h"
  4. #include <algorithm>
  5. #include <cctype>
  6. #include <cstring>
  7. #include <iterator>
  8. #include <string>
  9. #include <unordered_map>
  10. #include <utility>
  11. #include <vector>
  12. #include <cm/optional>
  13. #include <cm/string_view>
  14. #include "cmPkgConfigParser.h"
  15. #include "cmStringAlgorithms.h"
  16. namespace {
  17. void TrimBack(std::string& str)
  18. {
  19. if (!str.empty()) {
  20. auto it = str.end() - 1;
  21. for (; std::isspace(*it); --it) {
  22. if (it == str.begin()) {
  23. str.clear();
  24. return;
  25. }
  26. }
  27. str.erase(++it, str.end());
  28. }
  29. }
  30. std::string AppendAndTrim(std::string& str, cm::string_view sv)
  31. {
  32. auto size = str.length();
  33. str += sv;
  34. if (str.empty()) {
  35. return {};
  36. }
  37. auto begin = str.begin() + size;
  38. auto cur = str.end() - 1;
  39. while (cur != begin && std::isspace(*cur)) {
  40. --cur;
  41. }
  42. if (std::isspace(*cur)) {
  43. return {};
  44. }
  45. return { &*begin, static_cast<std::size_t>(cur - begin) + 1 };
  46. }
  47. cm::string_view TrimFlag(cm::string_view flag)
  48. {
  49. std::size_t trim_size = 2;
  50. for (auto c = flag.rbegin(); c != flag.rend() && std::isspace(*c); ++c) {
  51. ++trim_size;
  52. }
  53. return { flag.data() + 2, flag.size() - trim_size };
  54. }
  55. } // namespace
  56. std::string cmPkgConfigVersionReq::string() const
  57. {
  58. switch (Operation) {
  59. case ANY:
  60. return "";
  61. case LT:
  62. return cmStrCat('<', Version);
  63. case LT_EQ:
  64. return cmStrCat("<=", Version);
  65. case EQ:
  66. return cmStrCat('=', Version);
  67. case NEQ:
  68. return cmStrCat("!=", Version);
  69. case GT_EQ:
  70. return cmStrCat(">=", Version);
  71. case GT:
  72. return cmStrCat('>', Version);
  73. }
  74. return "";
  75. }
  76. std::string cmPkgConfigResult::StrOrDefault(std::string const& key,
  77. cm::string_view def)
  78. {
  79. auto it = Keywords.find(key);
  80. return it == Keywords.end() ? std::string{ def } : it->second;
  81. };
  82. std::string cmPkgConfigResult::Name()
  83. {
  84. return StrOrDefault("Name");
  85. }
  86. std::string cmPkgConfigResult::Description()
  87. {
  88. return StrOrDefault("Description");
  89. }
  90. std::string cmPkgConfigResult::Version()
  91. {
  92. return StrOrDefault("Version");
  93. }
  94. std::vector<cmPkgConfigDependency> cmPkgConfigResult::Conflicts()
  95. {
  96. auto it = Keywords.find("Conflicts");
  97. if (it == Keywords.end()) {
  98. return {};
  99. }
  100. return cmPkgConfigResolver::ParseDependencies(it->second);
  101. }
  102. std::vector<cmPkgConfigDependency> cmPkgConfigResult::Provides()
  103. {
  104. auto it = Keywords.find("Provides");
  105. if (it == Keywords.end()) {
  106. return {};
  107. }
  108. return cmPkgConfigResolver::ParseDependencies(it->second);
  109. }
  110. std::vector<cmPkgConfigDependency> cmPkgConfigResult::Requires(bool priv)
  111. {
  112. auto it = Keywords.find(priv ? "Requires.private" : "Requires");
  113. if (it == Keywords.end()) {
  114. return {};
  115. }
  116. return cmPkgConfigResolver::ParseDependencies(it->second);
  117. }
  118. cmPkgConfigCflagsResult cmPkgConfigResult::Cflags(bool priv)
  119. {
  120. std::string cflags;
  121. auto it = Keywords.find(priv ? "Cflags.private" : "Cflags");
  122. if (it != Keywords.end()) {
  123. cflags += it->second;
  124. }
  125. it = Keywords.find(priv ? "CFlags.private" : "CFlags");
  126. if (it != Keywords.end()) {
  127. if (!cflags.empty()) {
  128. cflags += " ";
  129. }
  130. cflags += it->second;
  131. }
  132. auto tokens = cmPkgConfigResolver::TokenizeFlags(cflags);
  133. if (env->AllowSysCflags) {
  134. if (env->SysrootDir) {
  135. return cmPkgConfigResolver::MangleCflags(tokens, *env->SysrootDir);
  136. }
  137. return cmPkgConfigResolver::MangleCflags(tokens);
  138. }
  139. if (env->SysCflags) {
  140. if (env->SysrootDir) {
  141. return cmPkgConfigResolver::MangleCflags(tokens, *env->SysrootDir,
  142. *env->SysCflags);
  143. }
  144. return cmPkgConfigResolver::MangleCflags(tokens, *env->SysCflags);
  145. }
  146. if (env->SysrootDir) {
  147. return cmPkgConfigResolver::MangleCflags(
  148. tokens, *env->SysrootDir, std::vector<std::string>{ "/usr/include" });
  149. }
  150. return cmPkgConfigResolver::MangleCflags(
  151. tokens, std::vector<std::string>{ "/usr/include" });
  152. }
  153. cmPkgConfigLibsResult cmPkgConfigResult::Libs(bool priv)
  154. {
  155. auto it = Keywords.find(priv ? "Libs.private" : "Libs");
  156. if (it == Keywords.end()) {
  157. return cmPkgConfigLibsResult();
  158. }
  159. auto tokens = cmPkgConfigResolver::TokenizeFlags(it->second);
  160. if (env->AllowSysLibs) {
  161. if (env->SysrootDir) {
  162. return cmPkgConfigResolver::MangleLibs(tokens, *env->SysrootDir);
  163. }
  164. return cmPkgConfigResolver::MangleLibs(tokens);
  165. }
  166. if (env->SysLibs) {
  167. if (env->SysrootDir) {
  168. return cmPkgConfigResolver::MangleLibs(tokens, *env->SysrootDir,
  169. *env->SysLibs);
  170. }
  171. return cmPkgConfigResolver::MangleLibs(tokens, *env->SysLibs);
  172. }
  173. if (env->SysrootDir) {
  174. return cmPkgConfigResolver::MangleLibs(
  175. tokens, *env->SysrootDir, std::vector<std::string>{ "/usr/lib" });
  176. }
  177. return cmPkgConfigResolver::MangleLibs(
  178. tokens, std::vector<std::string>{ "/usr/lib" });
  179. }
  180. void cmPkgConfigResolver::ReplaceSep(std::string& list)
  181. {
  182. #ifndef _WIN32
  183. std::replace(list.begin(), list.end(), ':', ';');
  184. #else
  185. static_cast<void>(list); // Unused parameter
  186. #endif
  187. }
  188. cm::optional<cmPkgConfigResult> cmPkgConfigResolver::ResolveStrict(
  189. std::vector<cmPkgConfigEntry> const& entries, cmPkgConfigEnv env)
  190. {
  191. cm::optional<cmPkgConfigResult> result;
  192. cmPkgConfigResult config;
  193. auto& keys = config.Keywords;
  194. if (env.SysrootDir) {
  195. config.Variables["pc_sysrootdir"] = *env.SysrootDir;
  196. } else {
  197. config.Variables["pc_sysrootdir"] = "/";
  198. }
  199. if (env.TopBuildDir) {
  200. config.Variables["pc_top_builddir"] = *env.TopBuildDir;
  201. }
  202. config.env = &env;
  203. for (auto const& entry : entries) {
  204. std::string key(entry.Key);
  205. if (entry.IsVariable) {
  206. if (config.Variables.find(key) != config.Variables.end()) {
  207. return result;
  208. }
  209. auto var = HandleVariableStrict(entry, config.Variables);
  210. if (!var) {
  211. return result;
  212. }
  213. config.Variables[key] = *var;
  214. } else {
  215. if (key == "Cflags" && keys.find("CFlags") != keys.end()) {
  216. return result;
  217. }
  218. if (key == "CFlags" && keys.find("Cflags") != keys.end()) {
  219. return result;
  220. }
  221. if (key == "Cflags.private" &&
  222. keys.find("CFlags.private") != keys.end()) {
  223. return result;
  224. }
  225. if (key == "CFlags.private" &&
  226. keys.find("Cflags.private") != keys.end()) {
  227. return result;
  228. }
  229. if (keys.find(key) != keys.end()) {
  230. return result;
  231. }
  232. keys[key] = HandleKeyword(entry, config.Variables);
  233. }
  234. }
  235. if (keys.find("Name") == keys.end() ||
  236. keys.find("Description") == keys.end() ||
  237. keys.find("Version") == keys.end()) {
  238. return result;
  239. }
  240. result = std::move(config);
  241. return result;
  242. }
  243. cm::optional<cmPkgConfigResult> cmPkgConfigResolver::ResolvePermissive(
  244. std::vector<cmPkgConfigEntry> const& entries, cmPkgConfigEnv env)
  245. {
  246. cm::optional<cmPkgConfigResult> result;
  247. cmPkgConfigResult config = ResolveBestEffort(entries, std::move(env));
  248. auto const& keys = config.Keywords;
  249. if (keys.find("Name") == keys.end() ||
  250. keys.find("Description") == keys.end() ||
  251. keys.find("Version") == keys.end()) {
  252. return result;
  253. }
  254. result = std::move(config);
  255. return result;
  256. }
  257. cmPkgConfigResult cmPkgConfigResolver::ResolveBestEffort(
  258. std::vector<cmPkgConfigEntry> const& entries, cmPkgConfigEnv env)
  259. {
  260. cmPkgConfigResult result;
  261. if (env.SysrootDir) {
  262. result.Variables["pc_sysrootdir"] = *env.SysrootDir;
  263. } else {
  264. result.Variables["pc_sysrootdir"] = "/";
  265. }
  266. if (env.TopBuildDir) {
  267. result.Variables["pc_top_builddir"] = *env.TopBuildDir;
  268. }
  269. result.env = &env;
  270. for (auto const& entry : entries) {
  271. std::string key(entry.Key);
  272. if (entry.IsVariable) {
  273. result.Variables[key] =
  274. HandleVariablePermissive(entry, result.Variables);
  275. } else {
  276. result.Keywords[key] += HandleKeyword(entry, result.Variables);
  277. }
  278. }
  279. return result;
  280. }
  281. std::string cmPkgConfigResolver::HandleVariablePermissive(
  282. cmPkgConfigEntry const& entry,
  283. std::unordered_map<std::string, std::string> const& variables)
  284. {
  285. std::string result;
  286. for (auto const& segment : entry.Val) {
  287. if (!segment.IsVariable) {
  288. result += segment.Data;
  289. } else if (entry.Key != segment.Data) {
  290. auto it = variables.find(std::string{ segment.Data });
  291. if (it != variables.end()) {
  292. result += it->second;
  293. }
  294. }
  295. }
  296. TrimBack(result);
  297. return result;
  298. }
  299. cm::optional<std::string> cmPkgConfigResolver::HandleVariableStrict(
  300. cmPkgConfigEntry const& entry,
  301. std::unordered_map<std::string, std::string> const& variables)
  302. {
  303. cm::optional<std::string> result;
  304. std::string value;
  305. for (auto const& segment : entry.Val) {
  306. if (!segment.IsVariable) {
  307. value += segment.Data;
  308. } else if (entry.Key == segment.Data) {
  309. return result;
  310. } else {
  311. auto it = variables.find(std::string{ segment.Data });
  312. if (it != variables.end()) {
  313. value += it->second;
  314. } else {
  315. return result;
  316. }
  317. }
  318. }
  319. TrimBack(value);
  320. result = std::move(value);
  321. return result;
  322. }
  323. std::string cmPkgConfigResolver::HandleKeyword(
  324. cmPkgConfigEntry const& entry,
  325. std::unordered_map<std::string, std::string> const& variables)
  326. {
  327. std::string result;
  328. for (auto const& segment : entry.Val) {
  329. if (!segment.IsVariable) {
  330. result += segment.Data;
  331. } else {
  332. auto it = variables.find(std::string{ segment.Data });
  333. if (it != variables.end()) {
  334. result += it->second;
  335. }
  336. }
  337. }
  338. TrimBack(result);
  339. return result;
  340. }
  341. std::vector<cm::string_view> cmPkgConfigResolver::TokenizeFlags(
  342. std::string const& flagline)
  343. {
  344. std::vector<cm::string_view> result;
  345. auto it = flagline.begin();
  346. while (it != flagline.end() && std::isspace(*it)) {
  347. ++it;
  348. }
  349. while (it != flagline.end()) {
  350. char const* start = &(*it);
  351. std::size_t len = 0;
  352. for (; it != flagline.end() && !std::isspace(*it); ++it) {
  353. ++len;
  354. }
  355. for (; it != flagline.end() && std::isspace(*it); ++it) {
  356. ++len;
  357. }
  358. result.emplace_back(start, len);
  359. }
  360. return result;
  361. }
  362. cmPkgConfigCflagsResult cmPkgConfigResolver::MangleCflags(
  363. std::vector<cm::string_view> const& flags)
  364. {
  365. cmPkgConfigCflagsResult result;
  366. for (auto flag : flags) {
  367. if (flag.rfind("-I", 0) == 0) {
  368. result.Includes.emplace_back(AppendAndTrim(result.Flagline, flag));
  369. } else {
  370. result.CompileOptions.emplace_back(AppendAndTrim(result.Flagline, flag));
  371. }
  372. }
  373. return result;
  374. }
  375. cmPkgConfigCflagsResult cmPkgConfigResolver::MangleCflags(
  376. std::vector<cm::string_view> const& flags, std::string const& sysroot)
  377. {
  378. cmPkgConfigCflagsResult result;
  379. for (auto flag : flags) {
  380. if (flag.rfind("-I", 0) == 0) {
  381. std::string reroot = Reroot(flag, "-I", sysroot);
  382. result.Includes.emplace_back(AppendAndTrim(result.Flagline, reroot));
  383. } else {
  384. result.CompileOptions.emplace_back(AppendAndTrim(result.Flagline, flag));
  385. }
  386. }
  387. return result;
  388. }
  389. cmPkgConfigCflagsResult cmPkgConfigResolver::MangleCflags(
  390. std::vector<cm::string_view> const& flags,
  391. std::vector<std::string> const& syspaths)
  392. {
  393. cmPkgConfigCflagsResult result;
  394. for (auto flag : flags) {
  395. if (flag.rfind("-I", 0) == 0) {
  396. cm::string_view trimmed = TrimFlag(flag);
  397. if (std::all_of(
  398. syspaths.begin(), syspaths.end(),
  399. [&](std::string const& path) { return path != trimmed; })) {
  400. result.Includes.emplace_back(AppendAndTrim(result.Flagline, flag));
  401. }
  402. } else {
  403. result.CompileOptions.emplace_back(AppendAndTrim(result.Flagline, flag));
  404. }
  405. }
  406. return result;
  407. }
  408. cmPkgConfigCflagsResult cmPkgConfigResolver::MangleCflags(
  409. std::vector<cm::string_view> const& flags, std::string const& sysroot,
  410. std::vector<std::string> const& syspaths)
  411. {
  412. cmPkgConfigCflagsResult result;
  413. for (auto flag : flags) {
  414. if (flag.rfind("-I", 0) == 0) {
  415. std::string reroot = Reroot(flag, "-I", sysroot);
  416. cm::string_view trimmed = TrimFlag(reroot);
  417. if (std::all_of(
  418. syspaths.begin(), syspaths.end(),
  419. [&](std::string const& path) { return path != trimmed; })) {
  420. result.Includes.emplace_back(AppendAndTrim(result.Flagline, reroot));
  421. }
  422. } else {
  423. result.CompileOptions.emplace_back(AppendAndTrim(result.Flagline, flag));
  424. }
  425. }
  426. return result;
  427. }
  428. cmPkgConfigLibsResult cmPkgConfigResolver::MangleLibs(
  429. std::vector<cm::string_view> const& flags)
  430. {
  431. cmPkgConfigLibsResult result;
  432. for (auto flag : flags) {
  433. if (flag.rfind("-L", 0) == 0) {
  434. result.LibDirs.emplace_back(AppendAndTrim(result.Flagline, flag));
  435. } else if (flag.rfind("-l", 0) == 0) {
  436. result.LibNames.emplace_back(AppendAndTrim(result.Flagline, flag));
  437. } else {
  438. result.LinkOptions.emplace_back(AppendAndTrim(result.Flagline, flag));
  439. }
  440. }
  441. return result;
  442. }
  443. cmPkgConfigLibsResult cmPkgConfigResolver::MangleLibs(
  444. std::vector<cm::string_view> const& flags, std::string const& sysroot)
  445. {
  446. cmPkgConfigLibsResult result;
  447. for (auto flag : flags) {
  448. if (flag.rfind("-L", 0) == 0) {
  449. std::string reroot = Reroot(flag, "-L", sysroot);
  450. result.LibDirs.emplace_back(AppendAndTrim(result.Flagline, reroot));
  451. } else if (flag.rfind("-l", 0) == 0) {
  452. result.LibNames.emplace_back(AppendAndTrim(result.Flagline, flag));
  453. } else {
  454. result.LinkOptions.emplace_back(AppendAndTrim(result.Flagline, flag));
  455. }
  456. }
  457. return result;
  458. }
  459. cmPkgConfigLibsResult cmPkgConfigResolver::MangleLibs(
  460. std::vector<cm::string_view> const& flags,
  461. std::vector<std::string> const& syspaths)
  462. {
  463. cmPkgConfigLibsResult result;
  464. for (auto flag : flags) {
  465. if (flag.rfind("-L", 0) == 0) {
  466. cm::string_view trimmed = TrimFlag(flag);
  467. if (std::all_of(
  468. syspaths.begin(), syspaths.end(),
  469. [&](std::string const& path) { return path != trimmed; })) {
  470. result.LibDirs.emplace_back(AppendAndTrim(result.Flagline, flag));
  471. }
  472. } else if (flag.rfind("-l", 0) == 0) {
  473. result.LibNames.emplace_back(AppendAndTrim(result.Flagline, flag));
  474. } else {
  475. result.LinkOptions.emplace_back(AppendAndTrim(result.Flagline, flag));
  476. }
  477. }
  478. return result;
  479. }
  480. cmPkgConfigLibsResult cmPkgConfigResolver::MangleLibs(
  481. std::vector<cm::string_view> const& flags, std::string const& sysroot,
  482. std::vector<std::string> const& syspaths)
  483. {
  484. cmPkgConfigLibsResult result;
  485. for (auto flag : flags) {
  486. if (flag.rfind("-L", 0) == 0) {
  487. std::string reroot = Reroot(flag, "-L", sysroot);
  488. cm::string_view trimmed = TrimFlag(reroot);
  489. if (std::all_of(
  490. syspaths.begin(), syspaths.end(),
  491. [&](std::string const& path) { return path != trimmed; })) {
  492. result.LibDirs.emplace_back(AppendAndTrim(result.Flagline, reroot));
  493. }
  494. } else if (flag.rfind("-l", 0) == 0) {
  495. result.LibNames.emplace_back(AppendAndTrim(result.Flagline, flag));
  496. } else {
  497. result.LinkOptions.emplace_back(AppendAndTrim(result.Flagline, flag));
  498. }
  499. }
  500. return result;
  501. }
  502. std::string cmPkgConfigResolver::Reroot(cm::string_view flag,
  503. cm::string_view prefix,
  504. std::string const& sysroot)
  505. {
  506. std::string result = std::string{ prefix };
  507. result += sysroot;
  508. result += cm::string_view{ flag.data() + prefix.length(),
  509. flag.size() - prefix.length() };
  510. return result;
  511. }
  512. cmPkgConfigVersionReq cmPkgConfigResolver::ParseVersion(
  513. std::string::const_iterator& cur, std::string::const_iterator end)
  514. {
  515. cmPkgConfigVersionReq result;
  516. if (*cur == '=') {
  517. result.Operation = result.EQ;
  518. ++cur;
  519. } else if (*cur == '>') {
  520. ++cur;
  521. if (cur == end) {
  522. result.Operation = result.GT;
  523. return result;
  524. }
  525. if (*cur == '=') {
  526. result.Operation = result.GT_EQ;
  527. ++cur;
  528. } else {
  529. result.Operation = result.GT;
  530. }
  531. } else if (*cur == '<') {
  532. ++cur;
  533. if (cur == end) {
  534. result.Operation = result.LT;
  535. return result;
  536. }
  537. if (*cur == '=') {
  538. result.Operation = result.LT_EQ;
  539. ++cur;
  540. } else {
  541. result.Operation = result.LT;
  542. }
  543. } else if (*cur == '!') {
  544. ++cur;
  545. if (cur == end) {
  546. result.Operation = result.ANY;
  547. return result;
  548. }
  549. if (*cur == '=') {
  550. result.Operation = result.NEQ;
  551. ++cur;
  552. } else {
  553. result.Operation = result.ANY;
  554. }
  555. }
  556. for (;; ++cur) {
  557. if (cur == end) {
  558. return result;
  559. }
  560. if (!std::isspace(*cur)) {
  561. break;
  562. }
  563. }
  564. for (; cur != end && !std::isspace(*cur) && *cur != ','; ++cur) {
  565. result.Version += *cur;
  566. }
  567. return result;
  568. }
  569. std::vector<cmPkgConfigDependency> cmPkgConfigResolver::ParseDependencies(
  570. std::string const& deps)
  571. {
  572. std::vector<cmPkgConfigDependency> result;
  573. auto cur = deps.begin();
  574. auto end = deps.end();
  575. while (cur != end) {
  576. while ((std::isspace(*cur) || *cur == ',')) {
  577. if (++cur == end) {
  578. return result;
  579. }
  580. }
  581. result.emplace_back();
  582. auto& dep = result.back();
  583. while (!std::isspace(*cur) && *cur != ',') {
  584. dep.Name += *cur;
  585. if (++cur == end) {
  586. return result;
  587. }
  588. }
  589. auto in_operator = [&]() -> bool {
  590. for (;; ++cur) {
  591. if (cur == end) {
  592. return false;
  593. }
  594. if (*cur == '>' || *cur == '=' || *cur == '<' || *cur == '!') {
  595. return true;
  596. }
  597. if (!std::isspace(*cur)) {
  598. return false;
  599. }
  600. }
  601. };
  602. if (!in_operator()) {
  603. continue;
  604. }
  605. dep.VerReq = ParseVersion(cur, end);
  606. }
  607. return result;
  608. }
  609. bool cmPkgConfigResolver::CheckVersion(cmPkgConfigVersionReq const& desired,
  610. std::string const& provided)
  611. {
  612. if (desired.Operation == cmPkgConfigVersionReq::ANY) {
  613. return true;
  614. }
  615. // https://blog.jasonantman.com/2014/07/how-yum-and-rpm-compare-versions/
  616. auto check_with_op = [&](int comp) -> bool {
  617. switch (desired.Operation) {
  618. case cmPkgConfigVersionReq::EQ:
  619. return comp == 0;
  620. case cmPkgConfigVersionReq::NEQ:
  621. return comp != 0;
  622. case cmPkgConfigVersionReq::GT:
  623. return comp < 0;
  624. case cmPkgConfigVersionReq::GT_EQ:
  625. return comp <= 0;
  626. case cmPkgConfigVersionReq::LT:
  627. return comp > 0;
  628. case cmPkgConfigVersionReq::LT_EQ:
  629. return comp >= 0;
  630. default:
  631. return true;
  632. }
  633. };
  634. if (desired.Version == provided) {
  635. return check_with_op(0);
  636. }
  637. auto a_cur = desired.Version.begin();
  638. auto a_end = desired.Version.end();
  639. auto b_cur = provided.begin();
  640. auto b_end = provided.end();
  641. while (a_cur != a_end && b_cur != b_end) {
  642. while (a_cur != a_end && !std::isalnum(*a_cur) && *a_cur != '~') {
  643. ++a_cur;
  644. }
  645. while (b_cur != b_end && !std::isalnum(*b_cur) && *b_cur != '~') {
  646. ++b_cur;
  647. }
  648. if (a_cur == a_end || b_cur == b_end) {
  649. break;
  650. }
  651. if (*a_cur == '~' || *b_cur == '~') {
  652. if (*a_cur != '~') {
  653. return check_with_op(1);
  654. }
  655. if (*b_cur != '~') {
  656. return check_with_op(-1);
  657. }
  658. ++a_cur;
  659. ++b_cur;
  660. continue;
  661. }
  662. auto a_seg = a_cur;
  663. auto b_seg = b_cur;
  664. bool is_num;
  665. if (std::isdigit(*a_cur)) {
  666. is_num = true;
  667. while (a_cur != a_end && std::isdigit(*a_cur)) {
  668. ++a_cur;
  669. }
  670. while (b_cur != b_end && std::isdigit(*b_cur)) {
  671. ++b_cur;
  672. }
  673. } else {
  674. is_num = false;
  675. while (a_cur != a_end && std::isalpha(*a_cur)) {
  676. ++a_cur;
  677. }
  678. while (b_cur != b_end && std::isalpha(*b_cur)) {
  679. ++b_cur;
  680. }
  681. }
  682. auto a_len = std::distance(a_seg, a_cur);
  683. auto b_len = std::distance(b_seg, b_cur);
  684. if (!b_len) {
  685. return check_with_op(is_num ? 1 : -1);
  686. }
  687. if (is_num) {
  688. while (a_seg != a_cur && *a_seg == '0') {
  689. ++a_seg;
  690. }
  691. while (b_seg != b_cur && *b_seg == '0') {
  692. ++b_seg;
  693. }
  694. a_len = std::distance(a_seg, a_cur);
  695. b_len = std::distance(b_seg, b_cur);
  696. if (a_len != b_len) {
  697. return check_with_op(a_len > b_len ? 1 : -1);
  698. }
  699. auto cmp = std::memcmp(&*a_seg, &*b_seg, a_len);
  700. if (cmp) {
  701. return check_with_op(cmp);
  702. }
  703. } else {
  704. auto cmp = std::memcmp(&*a_seg, &*b_seg, std::min(a_len, b_len));
  705. if (cmp) {
  706. return check_with_op(cmp);
  707. }
  708. if (a_len != b_len) {
  709. return check_with_op(a_len > b_len ? 1 : -1);
  710. }
  711. }
  712. }
  713. if (a_cur == a_end) {
  714. if (b_cur == b_end) {
  715. return check_with_op(0);
  716. }
  717. return check_with_op(-1);
  718. }
  719. return check_with_op(1);
  720. }
  721. cmPkgConfigVersionReq cmPkgConfigResolver::ParseVersion(
  722. std::string const& version)
  723. {
  724. cmPkgConfigVersionReq result;
  725. auto cur = version.begin();
  726. auto end = version.end();
  727. if (cur == end) {
  728. result.Operation = cmPkgConfigVersionReq::EQ;
  729. return result;
  730. }
  731. result = ParseVersion(cur, end);
  732. cur = version.begin();
  733. if (*cur != '=' && *cur != '!' && *cur != '<' && *cur != '>') {
  734. result.Operation = cmPkgConfigVersionReq::EQ;
  735. }
  736. return result;
  737. }