Skip to content

Commit 737d367

Browse files
authored
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.
1 parent 7956e0c commit 737d367

File tree

7 files changed

+46
-24
lines changed

7 files changed

+46
-24
lines changed

Include/cpython/pytime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ functions and constants
5353
extern "C" {
5454
#endif
5555

56+
#ifdef __clang__
57+
struct timeval;
58+
#endif
59+
5660
/* _PyTime_t: Python timestamp with subsecond precision. It can be used to
5761
store a duration, and so indirectly a date (related to another date, like
5862
UNIX epoch). */

Include/internal/pycore_tracemalloc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ struct _PyTraceMalloc_Config {
3636

3737
/* Pack the frame_t structure to reduce the memory footprint on 64-bit
3838
architectures: 12 bytes instead of 16. */
39+
#if defined(_MSC_VER)
40+
#pragma pack(push, 4)
41+
#endif
42+
3943
struct
4044
#ifdef __GNUC__
4145
__attribute__((packed))
42-
#elif defined(_MSC_VER)
43-
#pragma pack(push, 4)
4446
#endif
4547
tracemalloc_frame {
4648
/* filename cannot be NULL: "<unknown>" is used if the Python frame
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Minor fixes to allow building with ``PlatformToolset=ClangCL`` on Windows.

Modules/_decimal/libmpdec/mpdecimal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@
7676
#endif
7777
#endif
7878

79+
/* ClangCL claims to support 128-bit int, but doesn't */
80+
#if defined(__SIZEOF_INT128__) && defined(__clang__) && defined(_MSC_VER)
81+
#undef __SIZEOF_INT128__
82+
#endif
83+
84+
7985

8086
#define MPD_NEWTONDIV_CUTOFF 1024L
8187

PC/launcher2.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,14 @@ dumpSearchInfo(SearchInfo *search)
465465
return;
466466
}
467467

468-
#define DEBUGNAME(s) L"SearchInfo." ## s
469-
#define DEBUG(s) debug(DEBUGNAME(#s) L": %s\n", (search->s) ? (search->s) : L"(null)")
470-
#define DEBUG_2(s, sl) _debugStringAndLength((search->s), (search->sl), DEBUGNAME(#s))
471-
#define DEBUG_BOOL(s) debug(DEBUGNAME(#s) L": %s\n", (search->s) ? L"True" : L"False")
468+
#ifdef __clang__
469+
#define DEBUGNAME(s) L # s
470+
#else
471+
#define DEBUGNAME(s) # s
472+
#endif
473+
#define DEBUG(s) debug(L"SearchInfo." DEBUGNAME(s) L": %s\n", (search->s) ? (search->s) : L"(null)")
474+
#define DEBUG_2(s, sl) _debugStringAndLength((search->s), (search->sl), L"SearchInfo." DEBUGNAME(s))
475+
#define DEBUG_BOOL(s) debug(L"SearchInfo." DEBUGNAME(s) L": %s\n", (search->s) ? L"True" : L"False")
472476
DEBUG(originalCmdLine);
473477
DEBUG(restOfCmdLine);
474478
DEBUG(executablePath);

PC/pyconfig.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,26 @@ WIN32 is still required for the locale module.
7676
/* Compiler specific defines */
7777

7878
/* ------------------------------------------------------------------------*/
79-
/* Microsoft C defines _MSC_VER */
79+
/* Microsoft C defines _MSC_VER, as does clang-cl.exe */
8080
#ifdef _MSC_VER
8181

8282
/* We want COMPILER to expand to a string containing _MSC_VER's *value*.
8383
* This is horridly tricky, because the stringization operator only works
8484
* on macro arguments, and doesn't evaluate macros passed *as* arguments.
85-
* Attempts simpler than the following appear doomed to produce "_MSC_VER"
86-
* literally in the string.
8785
*/
8886
#define _Py_PASTE_VERSION(SUFFIX) \
8987
("[MSC v." _Py_STRINGIZE(_MSC_VER) " " SUFFIX "]")
9088
/* e.g., this produces, after compile-time string catenation,
91-
* ("[MSC v.1200 32 bit (Intel)]")
89+
* ("[MSC v.1900 64 bit (Intel)]")
9290
*
9391
* _Py_STRINGIZE(_MSC_VER) expands to
94-
* _Py_STRINGIZE1((_MSC_VER)) expands to
95-
* _Py_STRINGIZE2(_MSC_VER) but as this call is the result of token-pasting
96-
* it's scanned again for macros and so further expands to (under MSVC 6)
97-
* _Py_STRINGIZE2(1200) which then expands to
98-
* "1200"
92+
* _Py_STRINGIZE1(_MSC_VER) and this second macro call is scanned
93+
* again for macros and so further expands to
94+
* _Py_STRINGIZE1(1900) which then expands to
95+
* "1900"
9996
*/
100-
#define _Py_STRINGIZE(X) _Py_STRINGIZE1((X))
101-
#define _Py_STRINGIZE1(X) _Py_STRINGIZE2 ## X
102-
#define _Py_STRINGIZE2(X) #X
97+
#define _Py_STRINGIZE(X) _Py_STRINGIZE1(X)
98+
#define _Py_STRINGIZE1(X) #X
10399

104100
/* MSVC defines _WINxx to differentiate the windows platform types
105101
@@ -122,13 +118,16 @@ WIN32 is still required for the locale module.
122118
*/
123119
#ifdef MS_WIN64
124120
#if defined(_M_X64) || defined(_M_AMD64)
125-
#if defined(__INTEL_COMPILER)
121+
#if defined(__clang__)
122+
#define COMPILER ("[Clang " __clang_version__ "] 64 bit (AMD64) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
123+
#define PY_SUPPORT_TIER 0
124+
#elif defined(__INTEL_COMPILER)
126125
#define COMPILER ("[ICC v." _Py_STRINGIZE(__INTEL_COMPILER) " 64 bit (amd64) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
127126
#define PY_SUPPORT_TIER 0
128127
#else
129128
#define COMPILER _Py_PASTE_VERSION("64 bit (AMD64)")
130129
#define PY_SUPPORT_TIER 1
131-
#endif /* __INTEL_COMPILER */
130+
#endif /* __clang__ */
132131
#define PYD_PLATFORM_TAG "win_amd64"
133132
#elif defined(_M_ARM64)
134133
#define COMPILER _Py_PASTE_VERSION("64 bit (ARM64)")
@@ -181,13 +180,16 @@ typedef _W64 int Py_ssize_t;
181180

182181
#if defined(MS_WIN32) && !defined(MS_WIN64)
183182
#if defined(_M_IX86)
184-
#if defined(__INTEL_COMPILER)
183+
#if defined(__clang__)
184+
#define COMPILER ("[Clang " __clang_version__ "] 32 bit (Intel) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
185+
#define PY_SUPPORT_TIER 0
186+
#elif defined(__INTEL_COMPILER)
185187
#define COMPILER ("[ICC v." _Py_STRINGIZE(__INTEL_COMPILER) " 32 bit (Intel) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
186188
#define PY_SUPPORT_TIER 0
187189
#else
188190
#define COMPILER _Py_PASTE_VERSION("32 bit (Intel)")
189191
#define PY_SUPPORT_TIER 1
190-
#endif /* __INTEL_COMPILER */
192+
#endif /* __clang__ */
191193
#define PYD_PLATFORM_TAG "win32"
192194
#elif defined(_M_ARM)
193195
#define COMPILER _Py_PASTE_VERSION("32 bit (ARM)")

PCbuild/pyproject.props

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
<_DebugPreprocessorDefinition>NDEBUG;</_DebugPreprocessorDefinition>
2626
<_DebugPreprocessorDefinition Condition="$(Configuration) == 'Debug'">_DEBUG;</_DebugPreprocessorDefinition>
2727
<_PlatformPreprocessorDefinition>_WIN32;</_PlatformPreprocessorDefinition>
28-
<_PlatformPreprocessorDefinition Condition="$(Platform) == 'x64'">_WIN64;_M_X64;</_PlatformPreprocessorDefinition>
28+
<_PlatformPreprocessorDefinition Condition="$(Platform) == 'x64'">_WIN64;</_PlatformPreprocessorDefinition>
29+
<_PlatformPreprocessorDefinition Condition="$(Platform) == 'x64' and $(PlatformToolset) != 'ClangCL'">_M_X64;$(_PlatformPreprocessorDefinition)</_PlatformPreprocessorDefinition>
2930
<_Py3NamePreprocessorDefinition>PY3_DLLNAME=L"$(Py3DllName)";</_Py3NamePreprocessorDefinition>
3031
</PropertyGroup>
3132
<ItemDefinitionGroup>
@@ -45,8 +46,10 @@
4546
<SuppressStartupBanner>true</SuppressStartupBanner>
4647
<WholeProgramOptimization>true</WholeProgramOptimization>
4748
<ControlFlowGuard Condition="$(EnableControlFlowGuard) != ''">$(EnableControlFlowGuard)</ControlFlowGuard>
48-
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
4949
<MultiProcessorCompilation>true</MultiProcessorCompilation>
50+
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
51+
<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>
52+
<AdditionalOptions Condition="$(Configuration) != 'Debug' and $(PlatformToolset) == 'ClangCL'">-flto %(AdditionalOptions)</AdditionalOptions>
5053
</ClCompile>
5154
<ClCompile Condition="$(Configuration) == 'Debug'">
5255
<InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>

0 commit comments

Comments
 (0)