[Python-checkins] gh-77532: Minor tweaks to allow compiling with PlatformToolset=ClangCL on Windows (GH-101352)

zooba webhook-mailer at python.org
Fri Jan 27 09:45:15 EST 2023


https://github.com/python/cpython/commit/737d367b1f4bad76914173a64d6bbe19a984cd5f
commit: 737d367b1f4bad76914173a64d6bbe19a984cd5f
branch: main
author: Steve Dower <steve.dower at python.org>
committer: zooba <steve.dower at microsoft.com>
date: 2023-01-27T14:45:08Z
summary:

gh-77532: Minor tweaks to allow compiling with PlatformToolset=ClangCL on Windows (GH-101352)

To use this, ensure that clang support was selected in Visual Studio Installer, then set the PlatformToolset environment variable to "ClangCL" and build as normal from the command line.
It remains unsupported, but at least is possible now for experimentation.

files:
A Misc/NEWS.d/next/Build/2023-01-26-19-02-11.gh-issue-77532.cXD8bg.rst
M Include/cpython/pytime.h
M Include/internal/pycore_tracemalloc.h
M Modules/_decimal/libmpdec/mpdecimal.c
M PC/launcher2.c
M PC/pyconfig.h
M PCbuild/pyproject.props

diff --git a/Include/cpython/pytime.h b/Include/cpython/pytime.h
index e64f3b13e75c..16d88d191e9e 100644
--- a/Include/cpython/pytime.h
+++ b/Include/cpython/pytime.h
@@ -53,6 +53,10 @@ functions and constants
 extern "C" {
 #endif
 
+#ifdef __clang__
+struct timeval;
+#endif
+
 /* _PyTime_t: Python timestamp with subsecond precision. It can be used to
    store a duration, and so indirectly a date (related to another date, like
    UNIX epoch). */
diff --git a/Include/internal/pycore_tracemalloc.h b/Include/internal/pycore_tracemalloc.h
index 08d7d1096c78..d086adc61c31 100644
--- a/Include/internal/pycore_tracemalloc.h
+++ b/Include/internal/pycore_tracemalloc.h
@@ -36,11 +36,13 @@ struct _PyTraceMalloc_Config {
 
 /* Pack the frame_t structure to reduce the memory footprint on 64-bit
    architectures: 12 bytes instead of 16. */
+#if defined(_MSC_VER)
+#pragma pack(push, 4)
+#endif
+
 struct
 #ifdef __GNUC__
 __attribute__((packed))
-#elif defined(_MSC_VER)
-#pragma pack(push, 4)
 #endif
 tracemalloc_frame {
     /* filename cannot be NULL: "<unknown>" is used if the Python frame
diff --git a/Misc/NEWS.d/next/Build/2023-01-26-19-02-11.gh-issue-77532.cXD8bg.rst b/Misc/NEWS.d/next/Build/2023-01-26-19-02-11.gh-issue-77532.cXD8bg.rst
new file mode 100644
index 000000000000..5a746dca2e7d
--- /dev/null
+++ b/Misc/NEWS.d/next/Build/2023-01-26-19-02-11.gh-issue-77532.cXD8bg.rst
@@ -0,0 +1 @@
+Minor fixes to allow building with ``PlatformToolset=ClangCL`` on Windows.
diff --git a/Modules/_decimal/libmpdec/mpdecimal.c b/Modules/_decimal/libmpdec/mpdecimal.c
index f1626df46ed4..959934bda7a4 100644
--- a/Modules/_decimal/libmpdec/mpdecimal.c
+++ b/Modules/_decimal/libmpdec/mpdecimal.c
@@ -76,6 +76,12 @@
   #endif
 #endif
 
+/* ClangCL claims to support 128-bit int, but doesn't */
+#if defined(__SIZEOF_INT128__) && defined(__clang__) && defined(_MSC_VER)
+#undef __SIZEOF_INT128__
+#endif
+
+
 
 #define MPD_NEWTONDIV_CUTOFF 1024L
 
diff --git a/PC/launcher2.c b/PC/launcher2.c
index 4c77ec0be439..2052a2ffeb57 100644
--- a/PC/launcher2.c
+++ b/PC/launcher2.c
@@ -465,10 +465,14 @@ dumpSearchInfo(SearchInfo *search)
         return;
     }
 
-#define DEBUGNAME(s) L"SearchInfo." ## s
-#define DEBUG(s) debug(DEBUGNAME(#s) L": %s\n", (search->s) ? (search->s) : L"(null)")
-#define DEBUG_2(s, sl) _debugStringAndLength((search->s), (search->sl), DEBUGNAME(#s))
-#define DEBUG_BOOL(s) debug(DEBUGNAME(#s) L": %s\n", (search->s) ? L"True" : L"False")
+#ifdef __clang__
+#define DEBUGNAME(s) L # s
+#else
+#define DEBUGNAME(s) # s
+#endif
+#define DEBUG(s) debug(L"SearchInfo." DEBUGNAME(s) L": %s\n", (search->s) ? (search->s) : L"(null)")
+#define DEBUG_2(s, sl) _debugStringAndLength((search->s), (search->sl), L"SearchInfo." DEBUGNAME(s))
+#define DEBUG_BOOL(s) debug(L"SearchInfo." DEBUGNAME(s) L": %s\n", (search->s) ? L"True" : L"False")
     DEBUG(originalCmdLine);
     DEBUG(restOfCmdLine);
     DEBUG(executablePath);
diff --git a/PC/pyconfig.h b/PC/pyconfig.h
index 1d8408b363a6..f5166a1506c9 100644
--- a/PC/pyconfig.h
+++ b/PC/pyconfig.h
@@ -76,30 +76,26 @@ WIN32 is still required for the locale module.
 /* Compiler specific defines */
 
 /* ------------------------------------------------------------------------*/
-/* Microsoft C defines _MSC_VER */
+/* Microsoft C defines _MSC_VER, as does clang-cl.exe */
 #ifdef _MSC_VER
 
 /* We want COMPILER to expand to a string containing _MSC_VER's *value*.
  * This is horridly tricky, because the stringization operator only works
  * on macro arguments, and doesn't evaluate macros passed *as* arguments.
- * Attempts simpler than the following appear doomed to produce "_MSC_VER"
- * literally in the string.
  */
 #define _Py_PASTE_VERSION(SUFFIX) \
         ("[MSC v." _Py_STRINGIZE(_MSC_VER) " " SUFFIX "]")
 /* e.g., this produces, after compile-time string catenation,
- *      ("[MSC v.1200 32 bit (Intel)]")
+ *      ("[MSC v.1900 64 bit (Intel)]")
  *
  * _Py_STRINGIZE(_MSC_VER) expands to
- * _Py_STRINGIZE1((_MSC_VER)) expands to
- * _Py_STRINGIZE2(_MSC_VER) but as this call is the result of token-pasting
- *      it's scanned again for macros and so further expands to (under MSVC 6)
- * _Py_STRINGIZE2(1200) which then expands to
- * "1200"
+ * _Py_STRINGIZE1(_MSC_VER) and this second macro call is scanned
+ *      again for macros and so further expands to
+ * _Py_STRINGIZE1(1900) which then expands to
+ * "1900"
  */
-#define _Py_STRINGIZE(X) _Py_STRINGIZE1((X))
-#define _Py_STRINGIZE1(X) _Py_STRINGIZE2 ## X
-#define _Py_STRINGIZE2(X) #X
+#define _Py_STRINGIZE(X) _Py_STRINGIZE1(X)
+#define _Py_STRINGIZE1(X) #X
 
 /* MSVC defines _WINxx to differentiate the windows platform types
 
@@ -122,13 +118,16 @@ WIN32 is still required for the locale module.
  */
 #ifdef MS_WIN64
 #if defined(_M_X64) || defined(_M_AMD64)
-#if defined(__INTEL_COMPILER)
+#if defined(__clang__)
+#define COMPILER ("[Clang " __clang_version__ "] 64 bit (AMD64) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
+#define PY_SUPPORT_TIER 0
+#elif defined(__INTEL_COMPILER)
 #define COMPILER ("[ICC v." _Py_STRINGIZE(__INTEL_COMPILER) " 64 bit (amd64) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
 #define PY_SUPPORT_TIER 0
 #else
 #define COMPILER _Py_PASTE_VERSION("64 bit (AMD64)")
 #define PY_SUPPORT_TIER 1
-#endif /* __INTEL_COMPILER */
+#endif /* __clang__ */
 #define PYD_PLATFORM_TAG "win_amd64"
 #elif defined(_M_ARM64)
 #define COMPILER _Py_PASTE_VERSION("64 bit (ARM64)")
@@ -181,13 +180,16 @@ typedef _W64 int Py_ssize_t;
 
 #if defined(MS_WIN32) && !defined(MS_WIN64)
 #if defined(_M_IX86)
-#if defined(__INTEL_COMPILER)
+#if defined(__clang__)
+#define COMPILER ("[Clang " __clang_version__ "] 32 bit (Intel) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
+#define PY_SUPPORT_TIER 0
+#elif defined(__INTEL_COMPILER)
 #define COMPILER ("[ICC v." _Py_STRINGIZE(__INTEL_COMPILER) " 32 bit (Intel) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
 #define PY_SUPPORT_TIER 0
 #else
 #define COMPILER _Py_PASTE_VERSION("32 bit (Intel)")
 #define PY_SUPPORT_TIER 1
-#endif /* __INTEL_COMPILER */
+#endif /* __clang__ */
 #define PYD_PLATFORM_TAG "win32"
 #elif defined(_M_ARM)
 #define COMPILER _Py_PASTE_VERSION("32 bit (ARM)")
diff --git a/PCbuild/pyproject.props b/PCbuild/pyproject.props
index e398b333572e..92c7849d3bcf 100644
--- a/PCbuild/pyproject.props
+++ b/PCbuild/pyproject.props
@@ -25,7 +25,8 @@
     <_DebugPreprocessorDefinition>NDEBUG;</_DebugPreprocessorDefinition>
     <_DebugPreprocessorDefinition Condition="$(Configuration) == 'Debug'">_DEBUG;</_DebugPreprocessorDefinition>
     <_PlatformPreprocessorDefinition>_WIN32;</_PlatformPreprocessorDefinition>
-    <_PlatformPreprocessorDefinition Condition="$(Platform) == 'x64'">_WIN64;_M_X64;</_PlatformPreprocessorDefinition>
+    <_PlatformPreprocessorDefinition Condition="$(Platform) == 'x64'">_WIN64;</_PlatformPreprocessorDefinition>
+    <_PlatformPreprocessorDefinition Condition="$(Platform) == 'x64' and $(PlatformToolset) != 'ClangCL'">_M_X64;$(_PlatformPreprocessorDefinition)</_PlatformPreprocessorDefinition>
     <_Py3NamePreprocessorDefinition>PY3_DLLNAME=L"$(Py3DllName)";</_Py3NamePreprocessorDefinition>
   </PropertyGroup>
   <ItemDefinitionGroup>
@@ -45,8 +46,10 @@
       <SuppressStartupBanner>true</SuppressStartupBanner>
       <WholeProgramOptimization>true</WholeProgramOptimization>
       <ControlFlowGuard Condition="$(EnableControlFlowGuard) != ''">$(EnableControlFlowGuard)</ControlFlowGuard>
-      <AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
       <MultiProcessorCompilation>true</MultiProcessorCompilation>
+      <AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
+      <AdditionalOptions Condition="$(PlatformToolset) == 'ClangCL'">-Wno-deprecated-non-prototype -Wno-unused-label -Wno-pointer-sign -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-function %(AdditionalOptions)</AdditionalOptions>
+      <AdditionalOptions Condition="$(Configuration) != 'Debug' and $(PlatformToolset) == 'ClangCL'">-flto %(AdditionalOptions)</AdditionalOptions>
     </ClCompile>
     <ClCompile Condition="$(Configuration) == 'Debug'">
       <InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>



More information about the Python-checkins mailing list