Skip to content

Commit 1fbf3a7

Browse files
authored
Merge pull request #202 from timkpaine/fix-native-sdist-artifacts
Keep native binaries out of template source distributions
2 parents d5da73e + c5f689b commit 1fbf3a7

6 files changed

Lines changed: 73 additions & 23 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Check native source distributions after compiled extensions exist."""
2+
3+
import shutil
4+
import sys
5+
import tarfile
6+
import tempfile
7+
import tomllib
8+
from pathlib import Path
9+
10+
from hatchling.builders.sdist import SdistBuilder
11+
12+
source = Path(sys.argv[1]).resolve()
13+
with tempfile.TemporaryDirectory() as temporary:
14+
project = Path(temporary) / "project"
15+
shutil.copytree(
16+
source,
17+
project,
18+
ignore=shutil.ignore_patterns(".git", ".venv", "__pycache__", "node_modules", "target", "dist", "vcpkg", "vcpkg_installed", "emsdk"),
19+
)
20+
config = tomllib.loads((project / "pyproject.toml").read_text())
21+
module = config["tool"]["hatch"]["build"]["targets"]["wheel"]["packages"][0]
22+
extensions = (".so", ".dll", ".dylib", ".pyd")
23+
for suffix in extensions:
24+
(project / module / f"_sdist_binary_check{suffix}").write_bytes(b"prebuilt binary")
25+
26+
output = Path(temporary) / "dist"
27+
output.mkdir()
28+
filename = next(SdistBuilder(str(project)).build(directory=str(output)))
29+
with tarfile.open(output / filename) as archive:
30+
names = archive.getnames()
31+
32+
binaries = [name for name in names if name.endswith(extensions)]
33+
assert not binaries, f"Source distribution contains compiled extensions: {binaries}"
34+
assert any(name.endswith((".cpp", ".rs")) for name in names), "Source distribution is missing native sources"
35+
print(f"{source.name}: source distribution excludes compiled extensions and retains native sources")

‎.github/workflows/build.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,9 @@ jobs:
122122

123123
- name: Test template
124124
run: make test-${{ matrix.template }}
125+
126+
- name: Check source distribution artifacts
127+
run: python .github/scripts/check_sdist_artifacts.py ../python-template-${{ matrix.template }}
128+
env:
129+
HATCH_BUILD_NO_HOOKS: '1'
130+
if: matrix.template == 'cpp' || matrix.template == 'cppjswasm' || matrix.template == 'rust' || matrix.template == 'rustjswasm'

‎python/cpp/pyproject.toml.jinja‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,6 @@ exclude_also = [
121121
ignore_errors = true
122122
fail_under = 50
123123

124-
[tool.hatch.build]
125-
artifacts = [
126-
"{{ module }}/*.dll",
127-
"{{ module }}/*.dylib",
128-
"{{ module }}/*.so",
129-
]
130-
131124
[tool.hatch.build.sources]
132125
src = "/"
133126

@@ -138,6 +131,7 @@ libraries = [
138131
]
139132

140133
[tool.hatch.build.targets.sdist]
134+
exclude = ["*.dll", "*.dylib", "*.so", "*.pyd"]
141135
packages = [
142136
"{{ module }}",
143137
"cpp",
@@ -149,6 +143,11 @@ packages = [
149143
{% endif %}
150144

151145
[tool.hatch.build.targets.wheel]
146+
artifacts = [
147+
"{{ module }}/*.dll",
148+
"{{ module }}/*.dylib",
149+
"{{ module }}/*.so",
150+
]
152151
packages = [
153152
"{{ module }}",
154153
]

‎python/cppjswasm/pyproject.toml.jinja‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,6 @@ fail_under = 50
135135

136136
[tool.hatch.build]
137137
artifacts = [
138-
"{{ module }}/*.dll",
139-
"{{ module }}/*.dylib",
140-
"{{ module }}/*.so",
141138
"{{ module }}/extension",
142139
]
143140

@@ -159,6 +156,7 @@ libraries = [
159156
]
160157

161158
[tool.hatch.build.targets.sdist]
159+
exclude = ["*.dll", "*.dylib", "*.so", "*.pyd"]
162160
packages = [
163161
"{{ module }}",
164162
"cpp",
@@ -171,6 +169,12 @@ packages = [
171169
{% endif %}
172170

173171
[tool.hatch.build.targets.wheel]
172+
artifacts = [
173+
"{{ module }}/*.dll",
174+
"{{ module }}/*.dylib",
175+
"{{ module }}/*.so",
176+
"{{ module }}/extension",
177+
]
174178
packages = [
175179
"{{ module }}",
176180
]

‎python/rust/pyproject.toml.jinja‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,6 @@ exclude_also = [
141141
ignore_errors = true
142142
fail_under = 50
143143

144-
[tool.hatch.build]
145-
artifacts = [
146-
"{{ module }}/*.dll",
147-
"{{ module }}/*.dylib",
148-
"{{ module }}/*.so",
149-
]
150-
151144
[tool.hatch.build.sources]
152145
src = "/"
153146

@@ -165,11 +158,20 @@ packages = [
165158
"Cargo.lock",
166159
]
167160
exclude = [
161+
"*.dll",
162+
"*.dylib",
163+
"*.so",
164+
"*.pyd",
168165
"rust/Makefile",
169166
"target",
170167
]
171168

172169
[tool.hatch.build.targets.wheel]
170+
artifacts = [
171+
"{{ module }}/*.dll",
172+
"{{ module }}/*.dylib",
173+
"{{ module }}/*.so",
174+
]
173175
packages = [
174176
"{{ module }}",
175177
]

‎python/rustjswasm/pyproject.toml.jinja‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,6 @@ fail_under = 50
156156

157157
[tool.hatch.build]
158158
artifacts = [
159-
"{{ module }}/*.dll",
160-
"{{ module }}/*.dylib",
161-
"{{ module }}/*.so",
162159
"{{ module }}/extension",
163160
]
164161

@@ -188,9 +185,10 @@ packages = [
188185
"Cargo.lock",
189186
]
190187
exclude = [
191-
"{{ module }}/*.dll",
192-
"{{ module }}/*.dylib",
193-
"{{ module }}/*.so",
188+
"*.dll",
189+
"*.dylib",
190+
"*.so",
191+
"*.pyd",
194192
"{{ module }}/extension",
195193
"js/dist",
196194
"js/node_modules",
@@ -199,6 +197,12 @@ exclude = [
199197
]
200198

201199
[tool.hatch.build.targets.wheel]
200+
artifacts = [
201+
"{{ module }}/*.dll",
202+
"{{ module }}/*.dylib",
203+
"{{ module }}/*.so",
204+
"{{ module }}/extension",
205+
]
202206
packages = [
203207
"{{ module }}",
204208
]

0 commit comments

Comments
 (0)