cmImportedCxxModuleInfo.cxx 2.1 KB

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