Merge topic 'file-LOCK-windows'
[kiteware-cmake.git] / Source / cmImportedCxxModuleInfo.cxx
blobad49a8dce5a9c790b60051a27c2d3582b016e513
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
4 #include "cmImportedCxxModuleInfo.h"
6 #include <cstddef>
7 #include <string>
8 #include <utility>
9 #include <vector>
11 #include "cmCryptoHash.h"
12 #include "cmList.h"
13 #include "cmStringAlgorithms.h"
14 #include "cmSystemTools.h"
16 bool ImportedCxxModuleLookup::Initialized() const
18 return this->DoneInit;
21 void ImportedCxxModuleLookup::Initialize(std::string const& importedModules)
23 for (auto const& entry : cmList{ importedModules }) {
24 auto nameSep = entry.find('=');
25 if (nameSep == std::string::npos) {
26 // Invalid entry; ignore.
27 continue;
30 auto name = entry.substr(0, nameSep);
32 auto sourceSep = entry.find(',', nameSep);
33 std::string source;
34 if (sourceSep == std::string::npos) {
35 source = entry.substr(nameSep + 1);
36 } else {
37 source = entry.substr(nameSep + 1, sourceSep - nameSep - 1);
40 std::vector<std::string> bmis;
41 if (sourceSep != std::string::npos) {
42 auto bmiPaths = entry.substr(sourceSep + 1);
43 bmis = cmSystemTools::SplitString(bmiPaths, ',');
46 this->ImportedInfo.emplace(source,
47 ImportedCxxModuleInfo{ name, std::move(bmis) });
50 this->DoneInit = true;
53 std::string ImportedCxxModuleLookup::BmiNameForSource(std::string const& path)
55 auto genit = this->GeneratorInfo.find(path);
56 if (genit != this->GeneratorInfo.end()) {
57 return genit->second.BmiName;
60 auto importit = this->ImportedInfo.find(path);
61 std::string bmiName;
62 cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_512);
63 constexpr size_t HASH_TRUNCATION = 12;
64 if (importit != this->ImportedInfo.end()) {
65 auto safename = hasher.HashString(importit->second.Name);
66 bmiName = cmStrCat(safename.substr(0, HASH_TRUNCATION), ".bmi");
67 } else {
68 auto dirhash = hasher.HashString(path);
69 bmiName = cmStrCat(dirhash.substr(0, HASH_TRUNCATION), ".bmi");
72 this->GeneratorInfo.emplace(path, ImportedCxxModuleGeneratorInfo{ bmiName });
73 return bmiName;