From 0fd711348c70eba309308b278124e20584b09d61 Mon Sep 17 00:00:00 2001 From: Joerg Bornemann Date: Fri, 18 Sep 2026 11:34:49 +0200 Subject: [PATCH] Autogen: Pass WIN32 to moc when targeting the MSVC ABI WIN32 was added to the moc definitions only when no moc_predefs.h was generated, as of commit 5f0f84c7e0 (Autogen: Don't add a WIN32 moc definition if we have a moc_predefs.h file, 2018-11-27, v3.14.0-rc1~298^2). For the MSVC ABI that is not enough: WIN32 comes from our default Windows flags rather than from the compiler, so a generated moc_predefs.h does not carry it either. Keep the existing fallback for the other toolchains. GCC predefines WIN32 on Windows, but only with the GNU dialects, so injecting it unconditionally would make moc disagree with a strict ISO compile. Issue: #28093 --- Source/cmQtAutoGenInitializer.cxx | 17 +++++++++++++---- Source/cmQtAutoGenInitializer.h | 3 +++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Source/cmQtAutoGenInitializer.cxx b/Source/cmQtAutoGenInitializer.cxx index afd68328a2..435fc13286 100644 --- a/Source/cmQtAutoGenInitializer.cxx +++ b/Source/cmQtAutoGenInitializer.cxx @@ -713,6 +713,13 @@ bool cmQtAutoGenInitializer::InitCustomTargets() return true; } +bool cmQtAutoGenInitializer::IsMsvcAbi() const +{ + return this->Makefile->GetSafeDefinition("CMAKE_CXX_COMPILER_ID") == + "MSVC" || + this->Makefile->GetSafeDefinition("CMAKE_CXX_SIMULATE_ID") == "MSVC"; +} + bool cmQtAutoGenInitializer::InitMoc() { // Mocs compilation file @@ -814,10 +821,12 @@ bool cmQtAutoGenInitializer::InitMoc() auto getDefs = [this](std::string const& cfg) -> std::set { std::set defines; this->LocalGen->GetTargetDefines(this->GenTarget, cfg, "CXX", defines); - if (this->Moc.PredefsCmd.empty() && - this->Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME") == - "Windows") { - // Add WIN32 definition if we don't have a moc_predefs.h + if (this->Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME") == + "Windows" && + (this->Moc.PredefsCmd.empty() || this->IsMsvcAbi())) { + // Add WIN32 definition if moc_predefs.h cannot supply it. Targeting + // the MSVC ABI it comes from our default flags rather than from the + // compiler, so it never appears in moc_predefs.h. defines.insert("WIN32"); } return defines; diff --git a/Source/cmQtAutoGenInitializer.h b/Source/cmQtAutoGenInitializer.h index 879d98d4d4..f4e444edb6 100644 --- a/Source/cmQtAutoGenInitializer.h +++ b/Source/cmQtAutoGenInitializer.h @@ -111,6 +111,9 @@ private: return (this->Moc.Enabled || this->Uic.Enabled); } + /** Whether the CXX compiler targets the MSVC ABI. */ + bool IsMsvcAbi() const; + bool InitMoc(); bool InitUic(); bool InitRcc();