summaryrefslogtreecommitdiffstats
path: root/Source/cmImportedCxxModuleInfo.cxx
blob: c54b265cba9021bd255758928808a8ae2d00b0f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */

#include "cmImportedCxxModuleInfo.h"

#include <cstddef>
#include <string>
#include <utility>
#include <vector>

#include "cmCryptoHash.h"
#include "cmList.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"

bool ImportedCxxModuleLookup::Initialized() const
{
  return this->DoneInit;
}

void ImportedCxxModuleLookup::Initialize(std::string const& importedModules)
{
  for (auto const& entry : cmList{ importedModules }) {
    auto nameSep = entry.find('=');
    if (nameSep == std::string::npos) {
      // Invalid entry; ignore.
      continue;
    }

    auto name = entry.substr(0, nameSep);

    auto sourceSep = entry.find(',', nameSep);
    std::string source;
    if (sourceSep == std::string::npos) {
      source = entry.substr(nameSep + 1);
    } else {
      source = entry.substr(nameSep + 1, sourceSep - nameSep - 1);
    }

    std::vector<std::string> bmis;
    if (sourceSep != std::string::npos) {
      auto bmiPaths = entry.substr(sourceSep + 1);
      bmis = cmSystemTools::SplitString(bmiPaths, ',');
    }

    this->ImportedInfo.emplace(source,
                               ImportedCxxModuleInfo{ name, std::move(bmis) });
  }

  this->DoneInit = true;
}

std::string ImportedCxxModuleLookup::BmiNameForSource(std::string const& path)
{
  auto genit = this->GeneratorInfo.find(path);
  if (genit != this->GeneratorInfo.end()) {
    return genit->second.BmiName;
  }

  auto importit = this->ImportedInfo.find(path);
  std::string bmiName;
  cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_512);
  constexpr size_t HASH_TRUNCATION = 12;
  if (importit != this->ImportedInfo.end()) {
    auto safename = hasher.HashString(importit->second.Name);
    bmiName = cmStrCat(safename.substr(0, HASH_TRUNCATION), ".bmi");
  } else {
    auto dirhash = hasher.HashString(path);
    bmiName = cmStrCat(dirhash.substr(0, HASH_TRUNCATION), ".bmi");
  }

  this->GeneratorInfo.emplace(
    path, ImportedCxxModuleGeneratorInfo{ &importit->second, bmiName });
  return bmiName;
}