1
1
#!/usr/bin/env python
2
2
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
5
9
10
+ import os
6
11
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 :
8
17
def __init__ (self , name , path , ** kwargs ):
18
+ self .name = name
9
19
self .path = path
10
20
self .args = kwargs
11
- super ().__init__ (name , sources = [])
12
21
13
22
14
- class BuildDotnet (Command ):
23
+ class build_dotnet (Command ):
15
24
"""Build command for dotnet-cli based builds"""
16
25
17
26
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
+ ]
19
36
20
37
def initialize_options (self ):
21
- self .dotnet_config = "release"
38
+ self .dotnet_config = None
39
+ self .build_lib = None
40
+ self .inplace = False
22
41
23
42
def finalize_options (self ):
24
- pass
43
+ if self .dotnet_config is None :
44
+ self .dotnet_config = "release"
25
45
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
28
52
29
53
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
+
31
62
opts = sum (
32
63
[
33
64
["--" + name .replace ("_" , "-" ), value ]
@@ -36,34 +67,79 @@ def run(self):
36
67
[],
37
68
)
38
69
39
- opts .append ( "--configuration" )
40
- opts .append ( self . dotnet_config )
70
+ opts .extend ([ "--configuration" , self . dotnet_config ] )
71
+ opts .extend ([ "--output" , output ] )
41
72
73
+ self .announce ("Running dotnet build..." , level = distutils .log .INFO )
42
74
self .spawn (["dotnet" , "build" , lib .path ] + opts )
43
75
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
+
44
94
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 ):
46
110
def finalize_options (self ):
47
111
# Monkey patch bdist_wheel to think the package is pure even though we
48
112
# include DLLs
49
113
super ().finalize_options ()
50
114
self .root_is_pure = True
51
115
52
116
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
+
53
142
setup (
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 ,
69
145
)
0 commit comments