From 7b069510c5440f0193f1626be5000cf5efe90442 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Thu, 2 Feb 2023 15:53:28 -0500 Subject: cmImportedCxxModuleInfo: introduce code to parse exported BMI properties --- Source/CMakeLists.txt | 2 + Source/cmImportedCxxModuleInfo.cxx | 76 ++++++++++++++++++++++++++++++++++++++ Source/cmImportedCxxModuleInfo.h | 37 +++++++++++++++++++ bootstrap | 1 + 4 files changed, 116 insertions(+) create mode 100644 Source/cmImportedCxxModuleInfo.cxx create mode 100644 Source/cmImportedCxxModuleInfo.h diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 3690357..12af5d0 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -298,6 +298,8 @@ add_library( cmGraphAdjacencyList.h cmGraphVizWriter.cxx cmGraphVizWriter.h + cmImportedCxxModuleInfo.cxx + cmImportedCxxModuleInfo.h cmInstallGenerator.h cmInstallGenerator.cxx cmInstallGetRuntimeDependenciesGenerator.h diff --git a/Source/cmImportedCxxModuleInfo.cxx b/Source/cmImportedCxxModuleInfo.cxx new file mode 100644 index 0000000..9e3ac9a --- /dev/null +++ b/Source/cmImportedCxxModuleInfo.cxx @@ -0,0 +1,76 @@ +/* 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 +#include +#include +#include +#include + +#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 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; + auto hasher = cmCryptoHash::New("SHA3_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; +} diff --git a/Source/cmImportedCxxModuleInfo.h b/Source/cmImportedCxxModuleInfo.h new file mode 100644 index 0000000..e052283 --- /dev/null +++ b/Source/cmImportedCxxModuleInfo.h @@ -0,0 +1,37 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#pragma once + +#include "cmConfigure.h" // IWYU pragma: keep + +#include +#include +#include + +struct ImportedCxxModuleInfo +{ + std::string const Name; + std::vector const AvailableBmis; +}; + +struct ImportedCxxModuleGeneratorInfo +{ + ImportedCxxModuleInfo const* ImportedInfo; + std::string const BmiName; +}; + +struct ImportedCxxModuleLookup +{ + ImportedCxxModuleLookup() = default; + ~ImportedCxxModuleLookup() = default; + + bool Initialized() const; + void Initialize(std::string const& importedModules); + + std::string BmiNameForSource(std::string const& path); + +private: + bool DoneInit = false; + std::map ImportedInfo; + std::map GeneratorInfo; +}; diff --git a/bootstrap b/bootstrap index 8b43d20..2c54dbf 100755 --- a/bootstrap +++ b/bootstrap @@ -392,6 +392,7 @@ CMAKE_CXX_SOURCES="\ cmGlobVerificationManager \ cmHexFileConverter \ cmIfCommand \ + cmImportedCxxModuleInfo \ cmIncludeCommand \ cmIncludeGuardCommand \ cmIncludeDirectoryCommand \ -- cgit v0.12