From 003830f14f457c1a27e2a617abbaa5bd736bc0ff Mon Sep 17 00:00:00 2001
From: Orkun Tokdemir <ilhanorkuntokdemir@gmail.com>
Date: Wed, 26 Jun 2024 15:28:52 +0200
Subject: Autogen: Remove redundant push_back

---
 Source/cmQtAutoGenInitializer.cxx | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/Source/cmQtAutoGenInitializer.cxx b/Source/cmQtAutoGenInitializer.cxx
index ee53a8e..60d7a5f 100644
--- a/Source/cmQtAutoGenInitializer.cxx
+++ b/Source/cmQtAutoGenInitializer.cxx
@@ -1671,13 +1671,10 @@ bool cmQtAutoGenInitializer::InitRccTargets()
       sf->SetProperty("SKIP_UNITY_BUILD_INCLUSION", "On");
     }
 
-    std::vector<std::string> ccOutput;
-    ccOutput.push_back(qrc.OutputFile);
+    std::vector<std::string> ccOutput{ qrc.OutputFile };
 
-    std::vector<std::string> ccDepends;
     // Add the .qrc and info file to the custom command dependencies
-    ccDepends.push_back(qrc.QrcFile);
-    ccDepends.push_back(qrc.InfoFile);
+    std::vector<std::string> ccDepends{ qrc.QrcFile, qrc.InfoFile };
 
     cmCustomCommandLines commandLines;
     AddCMakeProcessToCommandLines(qrc.InfoFile, "cmake_autorcc", commandLines);
-- 
cgit v0.12


From 8d99e71b7e8e745cafb7e8daa6769c5268958466 Mon Sep 17 00:00:00 2001
From: Orkun Tokdemir <ilhanorkuntokdemir@gmail.com>
Date: Mon, 24 Jun 2024 18:28:27 +0200
Subject: Autogen: Add timestamp file for CMAKE_GLOBAL_AUTORCC_TARGET

When `CMAKE_GLOBAL_AUTORCC_TARGET` is ON and qrc files are not
generated, `<target_name>_arcc_data` is always dirty. So this commit
adds a timestamp file which depens on what `<target_name>_arcc_data`
depends before and ``<target_name>_arcc_data` depends the timestamp
file.

The dependency graph before
(qrcFile, InfoFile) -> _arcc_target

The dependency graph after
(qrcFile, InfoFile) -> global_rcc_timestamp ->_arcc_target

Fixes: #26059
---
 Source/cmQtAutoGenInitializer.cxx           | 44 ++++++++++++++++++++++++-----
 Tests/RunCMake/Autogen_5/RunCMakeTest.cmake | 16 +++++++++++
 2 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/Source/cmQtAutoGenInitializer.cxx b/Source/cmQtAutoGenInitializer.cxx
index 60d7a5f..e444299 100644
--- a/Source/cmQtAutoGenInitializer.cxx
+++ b/Source/cmQtAutoGenInitializer.cxx
@@ -1697,13 +1697,43 @@ bool cmQtAutoGenInitializer::InitRccTargets()
         if (!qrc.Unique) {
           ccName += cmStrCat('_', qrc.QrcPathChecksum);
         }
-
-        cc->SetByproducts(ccOutput);
-        cc->SetDepends(ccDepends);
-        cc->SetEscapeOldStyle(false);
-        cmTarget* autoRccTarget =
-          this->LocalGen->AddUtilityCommand(ccName, true, std::move(cc));
-
+        cmTarget* autoRccTarget = nullptr;
+        // When CMAKE_GLOBAL_AUTORCC_TARGET is ON and qrc is not generated,
+        // Add generate a timestamp file and a custom command to touch it.
+        // This will ensure that the global autorcc target is run only when the
+        // qrc file changes.
+        if (!qrc.Generated && this->Rcc.GlobalTarget) {
+          cm::string_view const timestampFileName = "global_rcc_timestamp";
+          auto const outputFile =
+            cmStrCat(this->Dir.Build, "/", timestampFileName);
+          commandLines.push_back(cmMakeCommandLine(
+            { cmSystemTools::GetCMakeCommand(), "-E", "touch", outputFile }));
+          cc->SetByproducts(ccOutput);
+          cc->SetDepends(ccDepends);
+          cc->SetEscapeOldStyle(false);
+          cc->SetOutputs(outputFile);
+          cc->SetCommandLines(commandLines);
+          this->LocalGen->AddCustomCommandToOutput(std::move(cc));
+          this->AddGeneratedSource(outputFile, this->Rcc);
+          ccDepends.clear();
+          ccDepends.push_back(outputFile);
+
+          auto ccRccTarget = cm::make_unique<cmCustomCommand>();
+          ccRccTarget->SetWorkingDirectory(this->Dir.Work.c_str());
+          ccRccTarget->SetComment(ccComment.c_str());
+          ccRccTarget->SetStdPipesUTF8(true);
+          ccRccTarget->SetDepends(ccDepends);
+          ccRccTarget->SetEscapeOldStyle(false);
+
+          autoRccTarget = this->LocalGen->AddUtilityCommand(
+            ccName, true, std::move(ccRccTarget));
+        } else {
+          cc->SetByproducts(ccOutput);
+          cc->SetDepends(ccDepends);
+          cc->SetEscapeOldStyle(false);
+          autoRccTarget =
+            this->LocalGen->AddUtilityCommand(ccName, true, std::move(cc));
+        }
         // Create autogen generator target
         this->LocalGen->AddGeneratorTarget(
           cm::make_unique<cmGeneratorTarget>(autoRccTarget, this->LocalGen));
diff --git a/Tests/RunCMake/Autogen_5/RunCMakeTest.cmake b/Tests/RunCMake/Autogen_5/RunCMakeTest.cmake
index 8060ec4..1f6f7d4 100644
--- a/Tests/RunCMake/Autogen_5/RunCMakeTest.cmake
+++ b/Tests/RunCMake/Autogen_5/RunCMakeTest.cmake
@@ -27,5 +27,21 @@ if (DEFINED with_qt_version)
         endblock()
       endforeach()
     endif()
+    if (RunCMake_GENERATOR MATCHES "Ninja")
+      block()
+        set(RunCMake_TEST_BINARY_DIR
+          ${RunCMake_BINARY_DIR}/RccGlobalAutoRcc-build)
+        run_cmake_with_options(RccExample ${RunCMake_TEST_OPTIONS}
+          -DCMAKE_GLOBAL_AUTORCC_TARGET=ON)
+        set(RunCMake_TEST_NO_CLEAN 1)
+        set(RunCMake_TEST_VARIANT_DESCRIPTION "-First-build")
+        run_cmake_command(RccGlobalAutoRcc-build ${CMAKE_COMMAND}
+          --build . --config Debug)
+        set(RunCMake_TEST_VARIANT_DESCRIPTION "-Second-build-nothing-to-do")
+        set(RunCMake_TEST_NOT_EXPECT_stdout "Automatic RCC for data.qrc")
+        run_cmake_command(RccGlobalAutoRcc-build ${CMAKE_COMMAND}
+          --build . --config Debug)
+      endblock()
+    endif()
   endif()
 endif ()
-- 
cgit v0.12