Skip to content

Commit cd0dbc3

Browse files
author
Martin Larralde
committed
Allow generating a .cargo/config file to change the target directory
1 parent cabe14a commit cd0dbc3

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

setuptools_rust/tomlgen.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ class tomlgen_rust(setuptools.Command):
2626
description = "Generate `Cargo.toml` for rust extensions"
2727

2828
user_options = [
29+
("force", 'f',
30+
"overwrite existing files if any"),
2931
("create-workspace", 'w',
3032
"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")
3335
]
3436

3537
boolean_options = ['create_workspace', 'force']
@@ -39,8 +41,12 @@ def initialize_options(self):
3941
self.dependencies = None
4042
self.authors = None
4143
self.create_workspace = None
44+
self.no_config = None
4245
self.force = None
4346

47+
# use the build command to find build directories
48+
self.build = build(self.distribution)
49+
4450
# parse config files
4551
self.cfg = configparser.ConfigParser()
4652
self.cfg.read(self.distribution.find_config_files())
@@ -49,6 +55,7 @@ def finalize_options(self):
4955

5056
# Finalize previous commands
5157
self.distribution.finalize_options()
58+
self.build.ensure_finalized()
5259

5360
# Shortcuts
5461
self.extensions = self.distribution.rust_extensions
@@ -87,6 +94,28 @@ def run(self):
8794
else:
8895
log.warn("skipping 'Cargo.toml' for workspace -- already exists")
8996

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+
90119
def build_cargo_toml(self, ext):
91120

92121
# Shortcuts

0 commit comments

Comments
 (0)