@@ -26,10 +26,12 @@ class tomlgen_rust(setuptools.Command):
26
26
description = "Generate `Cargo.toml` for rust extensions"
27
27
28
28
user_options = [
29
+ ("force" , 'f' ,
30
+ "overwrite existing files if any" ),
29
31
("create-workspace" , 'w' ,
30
32
"create a workspace file at the root of the project" ),
31
- ("force " , 'f' ,
32
- "overwrite existing files if any " )
33
+ ("no-config " , "C" ,
34
+ "do not create a `.cargo/config` file when generating a workspace " )
33
35
]
34
36
35
37
boolean_options = ['create_workspace' , 'force' ]
@@ -39,8 +41,12 @@ def initialize_options(self):
39
41
self .dependencies = None
40
42
self .authors = None
41
43
self .create_workspace = None
44
+ self .no_config = None
42
45
self .force = None
43
46
47
+ # use the build command to find build directories
48
+ self .build = build (self .distribution )
49
+
44
50
# parse config files
45
51
self .cfg = configparser .ConfigParser ()
46
52
self .cfg .read (self .distribution .find_config_files ())
@@ -49,6 +55,7 @@ def finalize_options(self):
49
55
50
56
# Finalize previous commands
51
57
self .distribution .finalize_options ()
58
+ self .build .ensure_finalized ()
52
59
53
60
# Shortcuts
54
61
self .extensions = self .distribution .rust_extensions
@@ -87,6 +94,28 @@ def run(self):
87
94
else :
88
95
log .warn ("skipping 'Cargo.toml' for workspace -- already exists" )
89
96
97
+ if self .create_workspace and self .extensions and not self .no_config :
98
+
99
+ dist = self .distribution
100
+ targetdir = os .path .join (self .build .build_temp , dist .get_name ())
101
+ cfgdir = os .path .abspath (
102
+ os .path .join (os .getcwd (), dist .script_name , ".." , ".cargo" )
103
+ )
104
+
105
+ if not os .path .exists (os .path .join (cfgdir , "config" )) or self .force :
106
+ os .makedirs (cfgdir , exist_ok = True )
107
+ with open (os .path .join (cfgdir , 'config' ), 'w' ) as config :
108
+ log .info ("creating '.cargo/config' for workspace" )
109
+
110
+ config .write ("[build]\n " )
111
+ config .write ('target-dir = "{}"\n ' .format (
112
+ os .path .relpath (targetdir , cfgdir )
113
+ ))
114
+
115
+ else :
116
+ log .warn ("skipping '.cargo/config' -- already exists" )
117
+
118
+
90
119
def build_cargo_toml (self , ext ):
91
120
92
121
# Shortcuts
0 commit comments