11#!/usr/bin/env python
22
3- from setuptools import setup , Command , Extension
4- from wheel .bdist_wheel import bdist_wheel
3+ import distutils
4+ from distutils .command .build import build as _build
5+ from setuptools .command .develop import develop as _develop
6+ from wheel .bdist_wheel import bdist_wheel as _bdist_wheel
7+ from setuptools import Distribution
8+ from setuptools import setup , Command
59
10+ import os
611
7- class DotnetLib (Extension ):
12+ # Disable SourceLink during the build until it can read repo-format v1, #1613
13+ os .environ ["EnableSourceControlManagerQueries" ] = "false"
14+
15+
16+ class DotnetLib :
817 def __init__ (self , name , path , ** kwargs ):
18+ self .name = name
919 self .path = path
1020 self .args = kwargs
11- super ().__init__ (name , sources = [])
1221
1322
14- class BuildDotnet (Command ):
23+ class build_dotnet (Command ):
1524 """Build command for dotnet-cli based builds"""
1625
1726 description = "Build DLLs with dotnet-cli"
18- user_options = [("dotnet-config=" , None , "dotnet build configuration" )]
27+ user_options = [
28+ ("dotnet-config=" , None , "dotnet build configuration" ),
29+ (
30+ "inplace" ,
31+ "i" ,
32+ "ignore build-lib and put compiled extensions into the source "
33+ + "directory alongside your pure Python modules" ,
34+ ),
35+ ]
1936
2037 def initialize_options (self ):
21- self .dotnet_config = "release"
38+ self .dotnet_config = None
39+ self .build_lib = None
40+ self .inplace = False
2241
2342 def finalize_options (self ):
24- pass
43+ if self .dotnet_config is None :
44+ self .dotnet_config = "release"
2545
26- def get_source_files (self ):
27- return []
46+ build = self .distribution .get_command_obj ("build" )
47+ build .ensure_finalized ()
48+ if self .inplace :
49+ self .build_lib = "."
50+ else :
51+ self .build_lib = build .build_lib
2852
2953 def run (self ):
30- for lib in self .distribution .ext_modules :
54+ dotnet_modules = self .distribution .dotnet_libs
55+
56+ for lib in dotnet_modules :
57+ output = os .path .join (
58+ os .path .abspath (self .build_lib ), lib .args .pop ("output" )
59+ )
60+ rename = lib .args .pop ("rename" , {})
61+
3162 opts = sum (
3263 [
3364 ["--" + name .replace ("_" , "-" ), value ]
@@ -36,34 +67,79 @@ def run(self):
3667 [],
3768 )
3869
39- opts .append ( "--configuration" )
40- opts .append ( self . dotnet_config )
70+ opts .extend ([ "--configuration" , self . dotnet_config ] )
71+ opts .extend ([ "--output" , output ] )
4172
73+ self .announce ("Running dotnet build..." , level = distutils .log .INFO )
4274 self .spawn (["dotnet" , "build" , lib .path ] + opts )
4375
76+ for k , v in rename .items ():
77+ source = os .path .join (output , k )
78+ dest = os .path .join (output , v )
79+
80+ if os .path .isfile (source ):
81+ try :
82+ os .remove (dest )
83+ except OSError :
84+ pass
85+
86+ self .move_file (src = source , dst = dest , level = distutils .log .INFO )
87+ else :
88+ self .warn (
89+ "Can't find file to rename: {}, current dir: {}" .format (
90+ source , os .getcwd ()
91+ )
92+ )
93+
4494
45- class bdist_wheel_patched (bdist_wheel ):
95+ # Add build_dotnet to the build tasks:
96+ class build (_build ):
97+ sub_commands = _build .sub_commands + [("build_dotnet" , None )]
98+
99+
100+ class develop (_develop ):
101+ def install_for_development (self ):
102+ # Build extensions in-place
103+ self .reinitialize_command ("build_dotnet" , inplace = 1 )
104+ self .run_command ("build_dotnet" )
105+
106+ return super ().install_for_development ()
107+
108+
109+ class bdist_wheel (_bdist_wheel ):
46110 def finalize_options (self ):
47111 # Monkey patch bdist_wheel to think the package is pure even though we
48112 # include DLLs
49113 super ().finalize_options ()
50114 self .root_is_pure = True
51115
52116
117+ # Monkey-patch Distribution s.t. it supports the dotnet_libs attribute
118+ Distribution .dotnet_libs = None
119+
120+ cmdclass = {
121+ "build" : build ,
122+ "build_dotnet" : build_dotnet ,
123+ "develop" : develop ,
124+ "bdist_wheel" : bdist_wheel ,
125+ }
126+
127+ dotnet_libs = [
128+ DotnetLib (
129+ "netfx-loader-x86" ,
130+ "netfx_loader/ClrLoader.csproj" ,
131+ runtime = "win-x86" ,
132+ output = "clr_loader/ffi/dlls/x86" ,
133+ ),
134+ DotnetLib (
135+ "netfx-loader-amd64" ,
136+ "netfx_loader/ClrLoader.csproj" ,
137+ runtime = "win-x64" ,
138+ output = "clr_loader/ffi/dlls/amd64" ,
139+ ),
140+ ]
141+
53142setup (
54- cmdclass = {"build_ext" : BuildDotnet , "bdist_wheel" : bdist_wheel_patched },
55- ext_modules = {
56- DotnetLib (
57- "netfx-loader-x86" ,
58- "netfx_loader/ClrLoader.csproj" ,
59- runtime = "win-x86" ,
60- output = "clr_loader/ffi/dlls/x86" ,
61- ),
62- DotnetLib (
63- "netfx-loader-amd64" ,
64- "netfx_loader/ClrLoader.csproj" ,
65- runtime = "win-x64" ,
66- output = "clr_loader/ffi/dlls/amd64" ,
67- ),
68- },
143+ cmdclass = cmdclass ,
144+ dotnet_libs = dotnet_libs ,
69145)
0 commit comments