Skip to content

bpo-33351: Patches to build on clang-cl #18371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .azure-pipelines/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,21 @@ jobs:
win32:
arch: win32
buildOpt:
installClang: no
testRunTitle: '$(Build.SourceBranchName)-win32'
testRunPlatform: win32
win64:
arch: amd64
buildOpt: '-p x64'
installClang: no
testRunTitle: '$(Build.SourceBranchName)-win64'
testRunPlatform: win64
win64_clang_cl:
arch: amd64
buildOpt: '-p x64 "/p:CLToolExe=clang-cl.exe" "/p:CLToolPath=C:\Program Files\LLVM\bin"'
installClang: yes
testRunTitle: '$(Build.SourceBranchName)-clang_cl-win64'
testRunPlatform: win64
maxParallel: 4

steps:
Expand Down
Empty file added .azure-pipelines/pr
Empty file.
9 changes: 9 additions & 0 deletions .azure-pipelines/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,25 @@ jobs:
win32:
arch: win32
buildOpt:
installClang: no
testRunTitle: '$(System.PullRequest.TargetBranch)-win32'
testRunPlatform: win32
win64:
arch: amd64
buildOpt: '-p x64'
installClang: no
testRunTitle: '$(System.PullRequest.TargetBranch)-win64'
testRunPlatform: win64
winarm64:
arch: arm64
buildOpt: '-p arm64'
installClang: no
win64_clang_cl:
arch: amd64
buildOpt: '-p x64 "/p:CLToolExe=clang-cl.exe" "/p:CLToolPath=C:\Program Files\LLVM\bin"'
installClang: yes
testRunTitle: '$(Build.SourceBranchName)-clang_cl-win64'
testRunPlatform: win64
maxParallel: 4

steps:
Expand Down
12 changes: 12 additions & 0 deletions .azure-pipelines/windows-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ steps:
Write-Host '##vso[task.setvariable variable=EXTERNALS_DIR]$(Build.BinariesDirectory)\externals'
displayName: Update build locations

- powershell: |
(New-Object System.Net.WebClient).DownloadFile("https://github.com/llvm/llvm-project/releases/download/llvmorg-9.0.1/LLVM-9.0.1-win32.exe", "LLVM-win.exe")
Start-Process .\LLVM-win.exe -ArgumentList '/S' -Wait
displayName: Install clang on win32
condition: and(eq('yes', variables['installClang']), eq('win32', variables['arch']))

- powershell: |
(New-Object System.Net.WebClient).DownloadFile("https://github.com/llvm/llvm-project/releases/download/llvmorg-9.0.1/LLVM-9.0.1-win64.exe", "LLVM-win.exe")
Start-Process .\LLVM-win.exe -ArgumentList '/S' -Wait
displayName: Install clang on win64
condition: and(eq('yes', variables['installClang']), eq('amd64', variables['arch']))

- script: PCbuild\build.bat -e $(buildOpt)
displayName: 'Build CPython'
env:
Expand Down
6 changes: 6 additions & 0 deletions Include/pytime.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ functions and constants
extern "C" {
#endif

#if defined(_MSC_VER)
/* Forward declare struct timeval so that clang-cl doesn't complain about it
being a local declaration later on in _PyTime_AsTimeval.*/
struct timeval;
#endif /* _MSC_VER */

/* _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). */
Expand Down
34 changes: 32 additions & 2 deletions Lib/distutils/_msvccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ def _find_exe(exe, paths=None):
'win-arm64' : 'x86_arm64'
}

# A map keyed by get_platform() return values to value accepted by
# clang as the triple.
PLAT_TO_LLVM_TARGETS = {
'win32': 'i686-pc-windows-msvc',
'win-amd64': 'x86_64-pc-windows-msvc',
'win-arm32': 'arm-pc-windows-msvc',
'win-arm64': 'aarch64-pc-windows-msvc',
}

class MSVCCompiler(CCompiler) :
"""Concrete class that implements an interface to Microsoft Visual C++,
as defined by the CCompiler abstract class."""
Expand Down Expand Up @@ -198,11 +207,12 @@ class MSVCCompiler(CCompiler) :
exe_extension = '.exe'


def __init__(self, verbose=0, dry_run=0, force=0):
def __init__(self, verbose=0, dry_run=0, force=0, use_clang_cl=False):
CCompiler.__init__ (self, verbose, dry_run, force)
# target platform (.plat_name is consistent with 'bdist')
self.plat_name = None
self.initialized = False
self.use_clang_cl = use_clang_cl

def initialize(self, plat_name=None):
# multi-init means we would need to check platform same each time...
Expand All @@ -224,7 +234,10 @@ def initialize(self, plat_name=None):

self._paths = vc_env.get('path', '')
paths = self._paths.split(os.pathsep)
self.cc = _find_exe("cl.exe", paths)
if self.use_clang_cl:
self.cc = _find_exe("clang-cl.exe")
else:
self.cc = _find_exe("cl.exe", paths)
self.linker = _find_exe("link.exe", paths)
self.lib = _find_exe("lib.exe", paths)
self.rc = _find_exe("rc.exe", paths) # resource compiler
Expand Down Expand Up @@ -258,6 +271,16 @@ def initialize(self, plat_name=None):
ldflags_debug = [
'/nologo', '/INCREMENTAL:NO', '/LTCG', '/DEBUG:FULL'
]
if self.use_clang_cl:
# Add target for clang
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if CPython makes use of any intrinsic or built-ins, but I had problems with both.

Intrinsics required removing some of the include directories that are used with MSVC since clang doesn't need the .h files.

Correct builtin support (on 32 bit Windows) required linking agsint the builtin .lib file.

target_flag = "--target=" + PLAT_TO_LLVM_TARGETS[plat_name]
self.compile_options.append(target_flag)
self.compile_options_debug.append(target_flag)
# Remove whole program optimization flags to avoid warnings about
# unrecognized options
self.compile_options.remove('/GL')
ldflags.remove('/LTCG')
ldflags_debug.remove('/LTCG')

self.ldflags_exe = [*ldflags, '/MANIFEST:EMBED,ID=1']
self.ldflags_exe_debug = [*ldflags_debug, '/MANIFEST:EMBED,ID=1']
Expand Down Expand Up @@ -537,3 +560,10 @@ def find_library_file(self, dirs, lib, debug=0):
else:
# Oops, didn't find it in *any* of 'dirs'
return None


class ClangMSVCCompiler(MSVCCompiler):
compiler_type = 'clang-cl'

def __init__(self, verbose=0, dry_run=0, force=0):
MSVCCompiler.__init__(self, verbose, dry_run, force, True)
2 changes: 2 additions & 0 deletions Lib/distutils/ccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,8 @@ def get_default_compiler(osname=None, platform=None):
"Mingw32 port of GNU C Compiler for Win32"),
'bcpp': ('bcppcompiler', 'BCPPCompiler',
"Borland C++ Compiler"),
'clang-cl':('_msvccompiler', 'ClangMSVCCompiler',
"clang-cl for Microsoft Visual C++"),
}

def show_compilers():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Port CPython to build with clang-cl on Windows.

Patch by Ethan Smith
5 changes: 3 additions & 2 deletions Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ static PyThread_type_lock tables_lock;

/* 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
typedef struct
#ifdef __GNUC__
__attribute__((packed))
#elif defined(_MSC_VER)
#pragma pack(push, 4)
#endif
{
/* filename cannot be NULL: "<unknown>" is used if the Python frame
Expand Down
14 changes: 6 additions & 8 deletions PC/pyconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,9 @@ WIN32 is still required for the locale module.
/* e.g., this produces, after compile-time string catenation,
* ("[MSC v.1200 32 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"
* The double-stringize hack, a method to get the string version of _MSC_VER
*/
#define _Py_STRINGIZE(X) _Py_STRINGIZE1((X))
#define _Py_STRINGIZE1(X) _Py_STRINGIZE2 ## X
#define _Py_STRINGIZE(X) _Py_STRINGIZE2(X)
#define _Py_STRINGIZE2(X) #X

/* MSVC defines _WINxx to differentiate the windows platform types
Expand All @@ -122,6 +116,8 @@ WIN32 is still required for the locale module.
#if defined(_M_X64) || defined(_M_AMD64)
#if defined(__INTEL_COMPILER)
#define COMPILER ("[ICC v." _Py_STRINGIZE(__INTEL_COMPILER) " 64 bit (amd64) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
#elif defined(__clang__)
#define COMPILER ("[clang v." _Py_STRINGIZE(__clang_version__) " 64 bit (amd64) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
#else
#define COMPILER _Py_PASTE_VERSION("64 bit (AMD64)")
#endif /* __INTEL_COMPILER */
Expand Down Expand Up @@ -175,6 +171,8 @@ typedef _W64 int ssize_t;
#if defined(_M_IX86)
#if defined(__INTEL_COMPILER)
#define COMPILER ("[ICC v." _Py_STRINGIZE(__INTEL_COMPILER) " 32 bit (Intel) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
#elif defined(__clang__)
#define COMPILER ("[clang v." _Py_STRINGIZE(__clang_version__) "32 bit (Intel) with MSC v." _Py_STRINGIZE(_MSC_VER) " CRT]")
#else
#define COMPILER _Py_PASTE_VERSION("32 bit (Intel)")
#endif /* __INTEL_COMPILER */
Expand Down