From d250b67722abcfe1bcd22511943f3e032ef1ef3d Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 6 Jan 2021 15:29:24 -0500 Subject: cmGlobalXCodeGenerator: Adopt pbxproj object id generation --- Source/cmGlobalXCodeGenerator.cxx | 24 ++++++++++++++++++++++-- Source/cmGlobalXCodeGenerator.h | 1 + Source/cmXCode21Object.cxx | 5 +++-- Source/cmXCode21Object.h | 2 +- Source/cmXCodeObject.cxx | 24 ++---------------------- Source/cmXCodeObject.h | 2 +- 6 files changed, 30 insertions(+), 28 deletions(-) diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index 7ee94b2..af8b2f0 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -799,7 +799,8 @@ void cmGlobalXCodeGenerator::addObject(std::unique_ptr obj) cmXCodeObject* cmGlobalXCodeGenerator::CreateObject( cmXCodeObject::PBXType ptype) { - auto obj = cm::make_unique(ptype, cmXCodeObject::OBJECT); + auto obj = cm::make_unique(ptype, cmXCodeObject::OBJECT, + this->GetObjectId()); auto ptr = obj.get(); this->addObject(std::move(obj)); return ptr; @@ -807,7 +808,9 @@ cmXCodeObject* cmGlobalXCodeGenerator::CreateObject( cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::Type type) { - auto obj = cm::make_unique(cmXCodeObject::None, type); + auto obj = cm::make_unique( + cmXCodeObject::None, type, + "Temporary cmake object, should not be referred to in Xcode file"); auto ptr = obj.get(); this->addObject(std::move(obj)); return ptr; @@ -3138,6 +3141,23 @@ cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget( return i->second; } +std::string cmGlobalXCodeGenerator::GetObjectId() +{ + std::string objectId; + char cUuid[40] = { 0 }; + CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); + 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; +} + std::string cmGlobalXCodeGenerator::GetOrCreateId(const std::string& name, const std::string& id) { diff --git a/Source/cmGlobalXCodeGenerator.h b/Source/cmGlobalXCodeGenerator.h index ab5eeb2..277ee82 100644 --- a/Source/cmGlobalXCodeGenerator.h +++ b/Source/cmGlobalXCodeGenerator.h @@ -162,6 +162,7 @@ private: const std::string& configName); cmXCodeObject* FindXCodeTarget(const cmGeneratorTarget*); + std::string GetObjectId(); std::string GetOrCreateId(const std::string& name, const std::string& id); // create cmXCodeObject from these functions so that memory can be managed diff --git a/Source/cmXCode21Object.cxx b/Source/cmXCode21Object.cxx index 1cf9a95..9b0dc58 100644 --- a/Source/cmXCode21Object.cxx +++ b/Source/cmXCode21Object.cxx @@ -4,11 +4,12 @@ #include #include +#include #include "cmSystemTools.h" -cmXCode21Object::cmXCode21Object(PBXType ptype, Type type) - : cmXCodeObject(ptype, type) +cmXCode21Object::cmXCode21Object(PBXType ptype, Type type, std::string id) + : cmXCodeObject(ptype, type, std::move(id)) { this->Version = 21; } diff --git a/Source/cmXCode21Object.h b/Source/cmXCode21Object.h index eb017447..f3fc438 100644 --- a/Source/cmXCode21Object.h +++ b/Source/cmXCode21Object.h @@ -13,7 +13,7 @@ class cmXCode21Object : public cmXCodeObject { public: - cmXCode21Object(PBXType ptype, Type type); + cmXCode21Object(PBXType ptype, Type type, std::string id); void PrintComment(std::ostream&) override; static void PrintList(std::vector> const&, std::ostream& out, PBXType t); diff --git a/Source/cmXCodeObject.cxx b/Source/cmXCodeObject.cxx index b301ab1..d5c5275 100644 --- a/Source/cmXCodeObject.cxx +++ b/Source/cmXCodeObject.cxx @@ -40,7 +40,7 @@ cmXCodeObject::~cmXCodeObject() this->Version = 15; } -cmXCodeObject::cmXCodeObject(PBXType ptype, Type type) +cmXCodeObject::cmXCodeObject(PBXType ptype, Type type, std::string id) { this->Version = 15; this->Target = nullptr; @@ -48,27 +48,7 @@ cmXCodeObject::cmXCodeObject(PBXType ptype, Type type) this->IsA = ptype; - if (type == OBJECT) { - // Set the Id of an Xcode object to a unique string for each instance. - // However the Xcode user file references certain Ids: for those cases, - // override the generated Id using SetId(). - // - char cUuid[40] = { 0 }; - CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); - CFStringRef s = CFUUIDCreateString(kCFAllocatorDefault, uuid); - CFStringGetCString(s, cUuid, sizeof(cUuid), kCFStringEncodingUTF8); - this->Id = cUuid; - CFRelease(s); - CFRelease(uuid); - } else { - this->Id = - "Temporary cmake object, should not be referred to in Xcode file"; - } - - cmSystemTools::ReplaceString(this->Id, "-", ""); - if (this->Id.size() > 24) { - this->Id = this->Id.substr(0, 24); - } + this->Id = std::move(id); this->TypeValue = type; if (this->TypeValue == OBJECT) { diff --git a/Source/cmXCodeObject.h b/Source/cmXCodeObject.h index 78d4727..ac5be3f 100644 --- a/Source/cmXCodeObject.h +++ b/Source/cmXCodeObject.h @@ -57,7 +57,7 @@ public: }; static const char* PBXTypeNames[]; virtual ~cmXCodeObject(); - cmXCodeObject(PBXType ptype, Type type); + cmXCodeObject(PBXType ptype, Type type, std::string id); Type GetType() const { return this->TypeValue; } PBXType GetIsA() const { return this->IsA; } -- cgit v0.12 From 2892228dc9e70aab3a724058a043068f87c860f6 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 6 Jan 2021 16:15:11 -0500 Subject: cmGlobalXCodeGenerator: Add infrastructure for deterministic object ids --- Source/cmGlobalXCodeGenerator.cxx | 36 +++++++++++++++++++++++------------- Source/cmGlobalXCodeGenerator.h | 7 +++++-- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index af8b2f0..419cf1e 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -18,6 +18,7 @@ #include "cmsys/RegularExpression.hxx" #include "cmComputeLinkInformation.h" +#include "cmCryptoHash.h" #include "cmCustomCommand.h" #include "cmCustomCommandGenerator.h" #include "cmCustomCommandLines.h" @@ -797,10 +798,10 @@ void cmGlobalXCodeGenerator::addObject(std::unique_ptr obj) } cmXCodeObject* cmGlobalXCodeGenerator::CreateObject( - cmXCodeObject::PBXType ptype) + cmXCodeObject::PBXType ptype, cm::string_view key) { auto obj = cm::make_unique(ptype, cmXCodeObject::OBJECT, - this->GetObjectId()); + this->GetObjectId(ptype, key)); auto ptr = obj.get(); this->addObject(std::move(obj)); return ptr; @@ -3141,19 +3142,28 @@ cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget( return i->second; } -std::string cmGlobalXCodeGenerator::GetObjectId() +std::string cmGlobalXCodeGenerator::GetObjectId(cmXCodeObject::PBXType ptype, + cm::string_view key) { std::string objectId; - char cUuid[40] = { 0 }; - CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); - 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); + if (!key.empty()) { + cmCryptoHash hash(cmCryptoHash::AlgoSHA256); + hash.Initialize(); + hash.Append(&ptype, sizeof(ptype)); + hash.Append(key); + objectId = cmSystemTools::UpperCase(hash.FinalizeHex().substr(0, 24)); + } else { + char cUuid[40] = { 0 }; + CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); + 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; } diff --git a/Source/cmGlobalXCodeGenerator.h b/Source/cmGlobalXCodeGenerator.h index 277ee82..e9b38fc 100644 --- a/Source/cmGlobalXCodeGenerator.h +++ b/Source/cmGlobalXCodeGenerator.h @@ -11,6 +11,8 @@ #include #include +#include + #include "cmGlobalGenerator.h" #include "cmXCodeObject.h" @@ -162,12 +164,13 @@ private: const std::string& configName); 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); // create cmXCodeObject from these functions so that memory can be managed // 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* CreateString(const std::string& s); cmXCodeObject* CreateObjectReference(cmXCodeObject*); -- cgit v0.12 From b8b6573db81327fc1800d9b1a92eb97820d7f972 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 6 Jan 2021 16:15:35 -0500 Subject: Xcode: Use deterministic object ids for script build phases The Xcode "new build system" only considers a script build phase up to date if it has run before, even if outputs are newer than inputs. Use a deterministic object id for script build phases associated with custom commands so that they do not need to re-run after CMake re-generates the project. Fixes: #21669 --- Source/cmGlobalXCodeGenerator.cxx | 21 ++++++++++++--------- Source/cmGlobalXCodeGenerator.h | 3 ++- .../BuildDepends/RepeatCMake-Custom-Script.cmake | 4 ++++ .../RunCMake/BuildDepends/RepeatCMake-Custom.cmake | 5 +++++ Tests/RunCMake/BuildDepends/RunCMakeTest.cmake | 17 +++++++++++++++++ 5 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 Tests/RunCMake/BuildDepends/RepeatCMake-Custom-Script.cmake create mode 100644 Tests/RunCMake/BuildDepends/RepeatCMake-Custom.cmake diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index 419cf1e..df45b35 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -1729,13 +1729,13 @@ void cmGlobalXCodeGenerator::CreateCustomCommands( if (this->XcodeBuildSystem >= BuildSystem::Twelve) { // create prebuild phase preBuildPhase = - this->CreateRunScriptBuildPhase("CMake PreBuild Rules", prebuild); + this->CreateRunScriptBuildPhase("CMake PreBuild Rules", gtgt, prebuild); // create prelink phase preLinkPhase = - this->CreateRunScriptBuildPhase("CMake PreLink Rules", prelink); + this->CreateRunScriptBuildPhase("CMake PreLink Rules", gtgt, prelink); // create postbuild phase - postBuildPhase = - this->CreateRunScriptBuildPhase("CMake PostBuild Rules", postbuild); + postBuildPhase = this->CreateRunScriptBuildPhase("CMake PostBuild Rules", + gtgt, postbuild); } else { std::vector classes; if (!gtgt->GetConfigCommonSourceFiles(classes)) { @@ -1863,7 +1863,8 @@ cmXCodeObject* cmGlobalXCodeGenerator::CreateRunScriptBuildPhase( } cmXCodeObject* buildPhase = - this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase); + this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase, + cmStrCat(gt->GetName(), ':', sf->GetFullPath())); buildPhase->AddAttribute("buildActionMask", this->CreateString("2147483647")); cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST); @@ -1922,7 +1923,8 @@ cmXCodeObject* cmGlobalXCodeGenerator::CreateRunScriptBuildPhase( } cmXCodeObject* cmGlobalXCodeGenerator::CreateRunScriptBuildPhase( - std::string const& name, std::vector const& commands) + std::string const& name, cmGeneratorTarget const* gt, + std::vector const& commands) { if (commands.empty()) { return nullptr; @@ -1945,7 +1947,8 @@ cmXCodeObject* cmGlobalXCodeGenerator::CreateRunScriptBuildPhase( } cmXCodeObject* buildPhase = - this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase); + this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase, + cmStrCat(gt->GetName(), ':', name)); buildPhase->AddAttribute("buildActionMask", this->CreateString("2147483647")); cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST); @@ -2913,8 +2916,8 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmGeneratorTarget* gtgt, cmXCodeObject* cmGlobalXCodeGenerator::CreateUtilityTarget( cmGeneratorTarget* gtgt) { - cmXCodeObject* shellBuildPhase = - this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase); + cmXCodeObject* shellBuildPhase = this->CreateObject( + cmXCodeObject::PBXShellScriptBuildPhase, gtgt->GetName()); shellBuildPhase->AddAttribute("buildActionMask", this->CreateString("2147483647")); cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST); diff --git a/Source/cmGlobalXCodeGenerator.h b/Source/cmGlobalXCodeGenerator.h index e9b38fc..8ff6846 100644 --- a/Source/cmGlobalXCodeGenerator.h +++ b/Source/cmGlobalXCodeGenerator.h @@ -254,7 +254,8 @@ private: cmGeneratorTarget const* gt, cmCustomCommand const& cc); cmXCodeObject* CreateRunScriptBuildPhase( - std::string const& name, std::vector const& commands); + std::string const& name, cmGeneratorTarget const* gt, + std::vector const& commands); std::string ConstructScript(cmCustomCommandGenerator const& ccg); void CreateReRunCMakeFile(cmLocalGenerator* root, std::vector const& gens); diff --git a/Tests/RunCMake/BuildDepends/RepeatCMake-Custom-Script.cmake b/Tests/RunCMake/BuildDepends/RepeatCMake-Custom-Script.cmake new file mode 100644 index 0000000..3e953b3 --- /dev/null +++ b/Tests/RunCMake/BuildDepends/RepeatCMake-Custom-Script.cmake @@ -0,0 +1,4 @@ +if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/exists-for-build2") + message(FATAL_ERROR "Custom command incorrectly re-ran after CMake re-ran!") +endif() +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/out.txt") diff --git a/Tests/RunCMake/BuildDepends/RepeatCMake-Custom.cmake b/Tests/RunCMake/BuildDepends/RepeatCMake-Custom.cmake new file mode 100644 index 0000000..697e485 --- /dev/null +++ b/Tests/RunCMake/BuildDepends/RepeatCMake-Custom.cmake @@ -0,0 +1,5 @@ +add_custom_command(OUTPUT out.txt + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/RepeatCMake-Custom-Script.cmake + DEPENDS ${CMAKE_CURRENT_LIST_DIR}/RepeatCMake-Custom-Script.cmake + ) +add_custom_target(drive ALL DEPENDS out.txt) diff --git a/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake b/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake index 7a68c4b..6c6d548 100644 --- a/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake +++ b/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake @@ -70,6 +70,23 @@ if(RunCMake_GENERATOR MATCHES "Make") endif() endif() +function(run_RepeatCMake CASE) + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${CASE}-build) + if(RunCMake_GENERATOR_IS_MULTI_CONFIG) + set(RunCMake_TEST_OPTIONS -DCMAKE_CONFIGURATION_TYPES=Debug) + else() + set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug) + endif() + run_cmake(${CASE}) + set(RunCMake_TEST_NO_CLEAN 1) + run_cmake_command(${CASE}-build1 ${CMAKE_COMMAND} --build . --config Debug) + run_cmake_command(${CASE}-rerun1 ${CMAKE_COMMAND} .) + file(WRITE ${RunCMake_TEST_BINARY_DIR}/exists-for-build2 "") + run_cmake_command(${CASE}-build2 ${CMAKE_COMMAND} --build . --config Debug) +endfunction() + +run_RepeatCMake(RepeatCMake-Custom) + function(run_ReGeneration) # test re-generation of project even if CMakeLists.txt files disappeared -- cgit v0.12