1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 20:46:37 +08:00

cmGlobalXCodeGenerator: Add infrastructure for deterministic object ids

This commit is contained in:
Brad King
2021-01-06 16:15:11 -05:00
parent d250b67722
commit 2892228dc9
2 changed files with 28 additions and 15 deletions

View File

@@ -18,6 +18,7 @@
#include "cmsys/RegularExpression.hxx" #include "cmsys/RegularExpression.hxx"
#include "cmComputeLinkInformation.h" #include "cmComputeLinkInformation.h"
#include "cmCryptoHash.h"
#include "cmCustomCommand.h" #include "cmCustomCommand.h"
#include "cmCustomCommandGenerator.h" #include "cmCustomCommandGenerator.h"
#include "cmCustomCommandLines.h" #include "cmCustomCommandLines.h"
@@ -797,10 +798,10 @@ void cmGlobalXCodeGenerator::addObject(std::unique_ptr<cmXCodeObject> obj)
} }
cmXCodeObject* cmGlobalXCodeGenerator::CreateObject( cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(
cmXCodeObject::PBXType ptype) cmXCodeObject::PBXType ptype, cm::string_view key)
{ {
auto obj = cm::make_unique<cmXCode21Object>(ptype, cmXCodeObject::OBJECT, auto obj = cm::make_unique<cmXCode21Object>(ptype, cmXCodeObject::OBJECT,
this->GetObjectId()); this->GetObjectId(ptype, key));
auto ptr = obj.get(); auto ptr = obj.get();
this->addObject(std::move(obj)); this->addObject(std::move(obj));
return ptr; return ptr;
@@ -3141,19 +3142,28 @@ cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(
return i->second; return i->second;
} }
std::string cmGlobalXCodeGenerator::GetObjectId() std::string cmGlobalXCodeGenerator::GetObjectId(cmXCodeObject::PBXType ptype,
cm::string_view key)
{ {
std::string objectId; std::string objectId;
char cUuid[40] = { 0 }; if (!key.empty()) {
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
CFStringRef s = CFUUIDCreateString(kCFAllocatorDefault, uuid); hash.Initialize();
CFStringGetCString(s, cUuid, sizeof(cUuid), kCFStringEncodingUTF8); hash.Append(&ptype, sizeof(ptype));
objectId = cUuid; hash.Append(key);
CFRelease(s); objectId = cmSystemTools::UpperCase(hash.FinalizeHex().substr(0, 24));
CFRelease(uuid); } else {
cmSystemTools::ReplaceString(objectId, "-", ""); char cUuid[40] = { 0 };
if (objectId.size() > 24) { CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
objectId = objectId.substr(0, 24); CFStringRef s = CFUUIDCreateString(kCFAllocatorDefault, uuid);
CFStringGetCString(s, cUuid, sizeof(cUuid), kCFStringEncodingUTF8);
objectId = cUuid;
CFRelease(s);
CFRelease(uuid);
cmSystemTools::ReplaceString(objectId, "-", "");
if (objectId.size() > 24) {
objectId = objectId.substr(0, 24);
}
} }
return objectId; return objectId;
} }

View File

@@ -11,6 +11,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <cm/string_view>
#include "cmGlobalGenerator.h" #include "cmGlobalGenerator.h"
#include "cmXCodeObject.h" #include "cmXCodeObject.h"
@@ -162,12 +164,13 @@ private:
const std::string& configName); const std::string& configName);
cmXCodeObject* FindXCodeTarget(const cmGeneratorTarget*); cmXCodeObject* FindXCodeTarget(const cmGeneratorTarget*);
std::string GetObjectId(); std::string GetObjectId(cmXCodeObject::PBXType ptype, cm::string_view key);
std::string GetOrCreateId(const std::string& name, const std::string& id); std::string GetOrCreateId(const std::string& name, const std::string& id);
// create cmXCodeObject from these functions so that memory can be managed // create cmXCodeObject from these functions so that memory can be managed
// correctly. All objects created are stored in this->XCodeObjects. // correctly. All objects created are stored in this->XCodeObjects.
cmXCodeObject* CreateObject(cmXCodeObject::PBXType ptype); cmXCodeObject* CreateObject(cmXCodeObject::PBXType ptype,
cm::string_view key = {});
cmXCodeObject* CreateObject(cmXCodeObject::Type type); cmXCodeObject* CreateObject(cmXCodeObject::Type type);
cmXCodeObject* CreateString(const std::string& s); cmXCodeObject* CreateString(const std::string& s);
cmXCodeObject* CreateObjectReference(cmXCodeObject*); cmXCodeObject* CreateObjectReference(cmXCodeObject*);