Skip to content

Commit d784e0e

Browse files
DefaultRyanCopilot
andcommitted
Namespace modules now check version against imported winrt_base
Co-authored-by: Copilot <copilot@github.com>
1 parent b8b0cdd commit d784e0e

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

cppwinrt/file_writers.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ namespace cppwinrt
418418
{
419419
writer w;
420420
write_preamble(w);
421-
w.write(strings::base_macros);
421+
w.write(strings::base_macros, CPPWINRT_VERSION_STRING);
422422
w.flush_to_file(settings.output_folder + "winrt/base_macros.h");
423423
}
424424

@@ -486,7 +486,11 @@ namespace cppwinrt
486486
w.write("import winrt.%;\n", dep);
487487
}
488488

489-
w.write("\n");
489+
// Version mismatch check: ensure this namespace module was generated by the
490+
// same version of cppwinrt.exe as the winrt_base module it imports.
491+
// winrt::cppwinrt_version is exported from winrt_base; CPPWINRT_VERSION is
492+
// the macro from this module's own base_macros.h in the global module fragment.
493+
w.write("\nstatic_assert(winrt::check_version(winrt::cppwinrt_version, CPPWINRT_VERSION), \"Mismatched C++/WinRT headers.\");\n\n");
490494

491495
// Include namespace headers in module purview
492496
w.write("#include \"winrt/impl/%.0.h\"\n", ns);
@@ -542,7 +546,11 @@ namespace cppwinrt
542546
w.write("import winrt.%;\n", dep);
543547
}
544548

545-
w.write("\n");
549+
// Version mismatch check: ensure this namespace module was generated by the
550+
// same version of cppwinrt.exe as the winrt_base module it imports.
551+
// winrt::cppwinrt_version is exported from winrt_base; CPPWINRT_VERSION is
552+
// the macro from this module's own base_macros.h in the global module fragment.
553+
w.write("\nstatic_assert(winrt::check_version(winrt::cppwinrt_version, CPPWINRT_VERSION), \"Mismatched C++/WinRT headers.\");\n");
546554

547555
// Forward declarations for all projected types in this SCC.
548556
// This is required because SCC members have cyclic type references,

strings/base_macros.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,11 @@ typedef struct _GUID GUID;
123123
#define WINRT_IMPL_CONSTEVAL constexpr
124124
#endif
125125

126+
// CPPWINRT_VERSION is defined here so it is available in module global fragments.
127+
// In header builds, base_version_odr.h defines it first (before base_macros.h is included),
128+
// so the #ifndef guard prevents redefinition.
129+
#ifndef CPPWINRT_VERSION
130+
#define CPPWINRT_VERSION "%"
131+
#endif
132+
126133
#endif // WINRT_BASE_MACROS_H

strings/base_module_numerics_ixx.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
export module winrt_numerics;
55

6+
#if defined(_MSC_VER)
7+
#pragma detect_mismatch("C++/WinRT version", CPPWINRT_VERSION)
8+
#endif
9+
610
#if __has_include(<windowsnumerics.impl.h>)
711
#ifdef _MSC_VER
812
#pragma warning(push)

strings/base_version.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ char const * const WINRT_version = "C++/WinRT version:" CPPWINRT_VERSION;
1616

1717
WINRT_EXPORT namespace winrt
1818
{
19+
inline constexpr char cppwinrt_version[] = CPPWINRT_VERSION;
20+
1921
template <std::size_t BaseSize, std::size_t ComponentSize>
2022
constexpr bool check_version(char const(&base)[BaseSize], char const(&component)[ComponentSize]) noexcept
2123
{

0 commit comments

Comments
 (0)