cmImportedCxxModuleInfo.cxx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "cmImportedCxxModuleInfo.h"
  4. #include <cstddef>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include "cmCryptoHash.h"
  9. #include "cmList.h"
  10. #include "cmStringAlgorithms.h"
  11. #include "cmSystemTools.h"
  12. bool ImportedCxxModuleLookup::Initialized() const
  13. {
  14. return this->DoneInit;
  15. }
  16. void ImportedCxxModuleLookup::Initialize(std::string const& importedModules)
  17. {
  18. for (auto const& entry : cmList{ importedModules }) {
  19. auto nameSep = entry.find('=');
  20. if (nameSep == std::string::npos) {
  21. // Invalid entry; ignore.
  22. continue;
  23. }
  24. auto name = entry.substr(0, nameSep);
  25. auto sourceSep = entry.find(',', nameSep);
  26. std::string source;
  27. if (sourceSep == std::string::npos) {
  28. source = entry.substr(nameSep + 1);
  29. } else {
  30. source = entry.substr(nameSep + 1, sourceSep - nameSep - 1);
  31. }
  32. std::vector<std::string> bmis;
  33. if (sourceSep != std::string::npos) {
  34. auto bmiPaths = entry.substr(sourceSep + 1);
  35. bmis = cmSystemTools::SplitString(bmiPaths, ',');
  36. }
  37. this->ImportedInfo.emplace(source,
  38. ImportedCxxModuleInfo{ name, std::move(bmis) });
  39. }
  40. this->DoneInit = true;
  41. }
  42. std::string ImportedCxxModuleLookup::BmiNameForSource(std::string const& path)
  43. {
  44. auto genit = this->GeneratorInfo.find(path);
  45. if (genit != this->GeneratorInfo.end()) {
  46. return genit->second.BmiName;
  47. }
  48. auto importit = this->ImportedInfo.find(path);
  49. std::string bmiName;
  50. cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_512);
  51. constexpr size_t HASH_TRUNCATION = 12;
  52. if (importit != this->ImportedInfo.end()) {
  53. auto safename = hasher.HashString(importit->second.Name);
  54. bmiName = cmStrCat(safename.substr(0, HASH_TRUNCATION), ".bmi");
  55. } else {
  56. auto dirhash = hasher.HashString(path);
  57. bmiName = cmStrCat(dirhash.substr(0, HASH_TRUNCATION), ".bmi");
  58. }
  59. this->GeneratorInfo.emplace(
  60. path, ImportedCxxModuleGeneratorInfo{ &importit->second, bmiName });
  61. return bmiName;
  62. }