zig-zon 0.2.3__tar.gz → 0.2.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {zig-zon-0.2.3 → zig-zon-0.2.5}/PKG-INFO +5 -4
- zig-zon-0.2.5/build.zig +118 -0
- {zig-zon-0.2.3 → zig-zon-0.2.5}/build.zig.zon +3 -5
- {zig-zon-0.2.3 → zig-zon-0.2.5}/setup.py +12 -8
- zig-zon-0.2.5/setup.zig.json +1 -0
- zig-zon-0.2.5/src/zig_zon.pyi +5 -0
- {zig-zon-0.2.3 → zig-zon-0.2.5}/src/zig_zon.zig +39 -13
- zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4/README.md +121 -0
- zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4/build.zig +202 -0
- {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4}/build.zig.zon +2 -2
- zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4/src/build/CPython.zig +128 -0
- zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4/src/build/Compression.zig +33 -0
- zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4/src/build/LowerDashString.zig +20 -0
- {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4}/src/build/SDistStep.zig +66 -37
- zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4/src/build/VenvInstallTest.zig +138 -0
- {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4}/src/build/extract_link_info.py +4 -12
- zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4/src/build/python-translatec.h +6 -0
- zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4/src/build/python.zig +486 -0
- zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4/src/build/python_allocator.zig +201 -0
- zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4/src/build/setup_json.zig +28 -0
- zig-zon-0.2.3/build.zig +0 -72
- zig-zon-0.2.3/setup.zig.json +0 -1
- zig-zon-0.2.3/zig-pkg/zig_python_sdist/README.md +0 -7
- zig-zon-0.2.3/zig-pkg/zig_python_sdist/build.zig +0 -139
- zig-zon-0.2.3/zig-pkg/zig_python_sdist/src/build/CPython.zig +0 -125
- zig-zon-0.2.3/zig-pkg/zig_python_sdist/src/build/LowerDashStringFormatter.zig +0 -12
- zig-zon-0.2.3/zig-pkg/zig_python_sdist/src/build/VenvInstallTest.zig +0 -101
- zig-zon-0.2.3/zig-pkg/zig_python_sdist/src/build/python.zig +0 -217
- {zig-zon-0.2.3 → zig-zon-0.2.5}/pkg-info.zon +0 -0
- {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4}/LICENSE +0 -0
- {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4}/pkg-info.zon +0 -0
- {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.5/zig-pkg/zig_python_sdist-0.0.4}/src/build/pkg_info.zig +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: zig-zon
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.5
|
|
4
4
|
Summary: a native cpython extension to parse zig object notation strings
|
|
5
5
|
Author-email: Tobias Simetsreiter <dasimmet@gmail.com>
|
|
6
6
|
Maintainer-email: Tobias Simetsreiter <dasimmet@gmail.com>
|
|
@@ -38,9 +38,10 @@ pip install zig-zon
|
|
|
38
38
|
running:
|
|
39
39
|
|
|
40
40
|
```python
|
|
41
|
-
import zig_zon
|
|
42
|
-
parsed = zig_zon.parse('.{.allyourcode = .are_belong_to_us, .asd = 123}')
|
|
43
|
-
print(parsed)
|
|
41
|
+
>>> import zig_zon
|
|
42
|
+
>>> parsed = zig_zon.parse('.{.allyourcode = .are_belong_to_us, .asd = 123}')
|
|
43
|
+
>>> print(parsed)
|
|
44
|
+
{'allyourcode': 'are_belong_to_us', 'asd': 123}
|
|
44
45
|
```
|
|
45
46
|
|
|
46
47
|
or, look at the [test file](https://codeberg.org/dasimmet/python-zig-zon/src/branch/main/src/test_zig_zon.py)
|
zig-zon-0.2.5/build.zig
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
const Build = std.Build;
|
|
3
|
+
const zig_python_sdist = @import("zig_python_sdist");
|
|
4
|
+
const zon = @import("build.zig.zon");
|
|
5
|
+
|
|
6
|
+
pub fn build(b: *Build) void {
|
|
7
|
+
const target = b.standardTargetOptions(.{});
|
|
8
|
+
const optimize = b.standardOptimizeOption(.{});
|
|
9
|
+
|
|
10
|
+
// this target and section of our build is used at pip install time
|
|
11
|
+
const ext_install_step = b.step("bdist-wheel", "build python extension module");
|
|
12
|
+
ext_install_step.dependOn(
|
|
13
|
+
&b.addInstallFile(b.path("src/zig_zon.pyi"), "lib/zig_zon.pyi").step,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
// this generates a python sdist archive
|
|
17
|
+
const sdist = zig_python_sdist.createSdistStep(b, .{
|
|
18
|
+
.name = "zig-zon",
|
|
19
|
+
.version = zon.version,
|
|
20
|
+
.description = b.path("README.md"),
|
|
21
|
+
.build = .{
|
|
22
|
+
.args = &.{
|
|
23
|
+
ext_install_step.name,
|
|
24
|
+
b.fmt("-Doptimize={s}", .{@tagName(optimize)}),
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
.extra_pkg_info = @import("pkg-info.zon"),
|
|
28
|
+
.files = &.{
|
|
29
|
+
.{ .src = b.path("pkg-info.zon"), .dest = "pkg-info.zon" },
|
|
30
|
+
.{ .src = b.path("build.zig.zon"), .dest = "build.zig.zon" },
|
|
31
|
+
.{ .src = b.path("build.zig"), .dest = "build.zig" },
|
|
32
|
+
.{ .src = b.path("src/zig_zon.zig"), .dest = "src/zig_zon.zig" },
|
|
33
|
+
.{ .src = b.path("src/zig_zon.pyi"), .dest = "src/zig_zon.pyi" },
|
|
34
|
+
},
|
|
35
|
+
.compression = .best,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const sdistfile = b.addInstallFileWithDir(
|
|
39
|
+
sdist.getEmittedArchive(),
|
|
40
|
+
.{ .custom = "src" },
|
|
41
|
+
sdist.basename,
|
|
42
|
+
);
|
|
43
|
+
b.getInstallStep().dependOn(&sdistfile.step);
|
|
44
|
+
|
|
45
|
+
// we can use a python virtual environment inside
|
|
46
|
+
// the zig cache to test pip installation
|
|
47
|
+
const test_pip_install = zig_python_sdist.createVenvInstallTest(
|
|
48
|
+
b,
|
|
49
|
+
.{
|
|
50
|
+
.sdist = sdist.getEmittedArchive(),
|
|
51
|
+
.unittest_py = &.{b.path("src/test_zig_zon.py")},
|
|
52
|
+
.twine_check = true,
|
|
53
|
+
.no_clean = b.option(bool, "pip-no-clean", "dont cleanup pip temp dirs") orelse false,
|
|
54
|
+
},
|
|
55
|
+
);
|
|
56
|
+
b.step("test-pip-install", "").dependOn(test_pip_install.step);
|
|
57
|
+
|
|
58
|
+
const cpython = zig_python_sdist.createCPythonModule(b, .{
|
|
59
|
+
.target = target,
|
|
60
|
+
.version = zon.version,
|
|
61
|
+
.optimize = .ReleaseSmall,
|
|
62
|
+
});
|
|
63
|
+
const lib = b.addLibrary(.{
|
|
64
|
+
.name = "zig_zon",
|
|
65
|
+
.root_module = b.addModule("zig_zon", .{
|
|
66
|
+
.root_source_file = b.path("src/zig_zon.zig"),
|
|
67
|
+
.target = target,
|
|
68
|
+
.optimize = optimize,
|
|
69
|
+
.imports = &.{
|
|
70
|
+
.{ .name = "cpython", .module = cpython },
|
|
71
|
+
},
|
|
72
|
+
}),
|
|
73
|
+
.linkage = .dynamic,
|
|
74
|
+
});
|
|
75
|
+
const ext_install = zig_python_sdist.installExtLib(b, lib);
|
|
76
|
+
ext_install_step.dependOn(&ext_install.step);
|
|
77
|
+
|
|
78
|
+
ext_install_step.dependOn(&ext_install.step);
|
|
79
|
+
|
|
80
|
+
const ext_path = zig_python_sdist.CPython.renameExtLib(b, lib);
|
|
81
|
+
|
|
82
|
+
{
|
|
83
|
+
const ext_tests = ext_path.wf.addCopyFile(b.path("src/test_zig_zon.py"), "test_zig_zon.py");
|
|
84
|
+
_ = ext_path.wf.addCopyFile(b.path("build.zig.zon"), "build.zig.zon");
|
|
85
|
+
const test_run = Build.Step.Run.create(b, "unittest");
|
|
86
|
+
test_run.setCwd(ext_path.wf.getDirectory());
|
|
87
|
+
test_run.setEnvironmentVariable("PYTHONPATH", ".");
|
|
88
|
+
test_run.addFileArg(zig_python_sdist.CPython.find(b));
|
|
89
|
+
if (b.option(bool, "tracetest", "run unittest with tracing") orelse false) {
|
|
90
|
+
test_run.addArgs(&.{ "-m", "trace", "--trace" });
|
|
91
|
+
} else {
|
|
92
|
+
test_run.addArgs(&.{ "-m", "unittest" });
|
|
93
|
+
}
|
|
94
|
+
test_run.addDirectoryArg(ext_tests);
|
|
95
|
+
b.step("test-bdist", "run unittests against the extension").dependOn(&test_run.step);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const usf = b.addUpdateSourceFiles();
|
|
99
|
+
b.step("update-src", "updates the pyi file. needs 'mypy' installed").dependOn(&usf.step);
|
|
100
|
+
|
|
101
|
+
{
|
|
102
|
+
const stubgen_run = Build.Step.Run.create(b, "stubgen");
|
|
103
|
+
stubgen_run.setCwd(ext_path.wf.getDirectory());
|
|
104
|
+
stubgen_run.addArgs(&.{
|
|
105
|
+
"stubgen",
|
|
106
|
+
"-m",
|
|
107
|
+
lib.name,
|
|
108
|
+
"-o",
|
|
109
|
+
});
|
|
110
|
+
const stub_dir = stubgen_run.addOutputDirectoryArg("stubgen");
|
|
111
|
+
stubgen_run.addArg("--search-path");
|
|
112
|
+
stubgen_run.addDirectoryArg(ext_path.wf.getDirectory());
|
|
113
|
+
usf.addCopyFileToSource(
|
|
114
|
+
stub_dir.path(b, b.fmt("{s}.pyi", .{lib.name})),
|
|
115
|
+
b.fmt("src/{s}.pyi", .{lib.name}),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
.{
|
|
2
2
|
.name = .python_zig_zon,
|
|
3
3
|
.fingerprint = 0x3d332656a3644fc9,
|
|
4
|
-
.version = "0.2.
|
|
4
|
+
.version = "0.2.5",
|
|
5
5
|
.minimum_zig_version = "0.16.0",
|
|
6
6
|
.dependencies = .{
|
|
7
7
|
.zig_python_sdist = .{
|
|
8
|
-
.url = "git+https://codeberg.org/dasimmet/zig-python-sdist.git?ref=0.0.
|
|
9
|
-
.hash = "zig_python_sdist-0.0.
|
|
8
|
+
.url = "git+https://codeberg.org/dasimmet/zig-python-sdist.git?ref=0.0.4#79469a3698fe6c11ad0420282e71ff858642a4ac",
|
|
9
|
+
.hash = "zig_python_sdist-0.0.4-wjgdOukkAQBzC1YymFsy5h5_nZTUlWKUFI9oseCgGkKP",
|
|
10
10
|
},
|
|
11
11
|
},
|
|
12
12
|
.paths = .{
|
|
@@ -16,7 +16,5 @@
|
|
|
16
16
|
"pkg-info.zon",
|
|
17
17
|
"LICENSE",
|
|
18
18
|
"README.md",
|
|
19
|
-
"setup.py",
|
|
20
|
-
"pyproject.toml",
|
|
21
19
|
},
|
|
22
20
|
}
|
|
@@ -14,6 +14,7 @@ with open("setup.zig.json") as fd:
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
def mkversion(v, need_release=False):
|
|
17
|
+
'''assembles a dict of semantic version to string'''
|
|
17
18
|
if need_release:
|
|
18
19
|
assert v['pre'] == None, "cannot use version with pre: " + str(v)
|
|
19
20
|
assert v['build'] == None, "cannot use version with build: " + str(v)
|
|
@@ -39,15 +40,16 @@ class ZigBuildWheel(bdist_wheel):
|
|
|
39
40
|
subprocess.check_call(build_cmd)
|
|
40
41
|
|
|
41
42
|
os.makedirs(self.bdist_dir, exist_ok=True)
|
|
42
|
-
copy_pattern =
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
copy_pattern = setup_json['build']['copy_pattern']
|
|
44
|
+
for pattern in copy_pattern:
|
|
45
|
+
artifacts = glob.glob(pattern)
|
|
46
|
+
if len(artifacts) == 0:
|
|
47
|
+
raise Exception(
|
|
48
|
+
'no zig artifacts found in {}/{}'.format(cwd, pattern))
|
|
47
49
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
for it in artifacts:
|
|
51
|
+
shutil.copyfile(it, os.path.join(
|
|
52
|
+
self.bdist_dir, os.path.basename(it)))
|
|
51
53
|
|
|
52
54
|
return super().run(*args, **kwargs)
|
|
53
55
|
|
|
@@ -57,6 +59,8 @@ setup(
|
|
|
57
59
|
version=mkversion(setup_json['version']),
|
|
58
60
|
cmdclass={'bdist_wheel': ZigBuildWheel},
|
|
59
61
|
install_requires=setup_json['install_requires'],
|
|
62
|
+
entry_points=setup_json['entry_points'],
|
|
63
|
+
packages=setup_json['build']['packages'],
|
|
60
64
|
setup_requires=[
|
|
61
65
|
"setuptools",
|
|
62
66
|
"wheel",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"zig_zon","version":{"major":0,"minor":2,"patch":5,"pre":null,"build":null},"zig_version":{"major":0,"minor":16,"patch":0,"pre":null,"build":null},"build":{"args":["--fork=zig-pkg/zig_python_sdist-0.0.4","bdist-wheel","-Doptimize=ReleaseSmall"],"requires":[],"packages":[],"copy_pattern":["zig-out/lib/*"]},"install_requires":[],"entry_points":{"console_scripts":[],"gui_scripts":[]}}
|
|
@@ -8,28 +8,54 @@ const cpython = @import("cpython");
|
|
|
8
8
|
const c = cpython.c;
|
|
9
9
|
const PyObject = c.PyObject;
|
|
10
10
|
|
|
11
|
-
pub var
|
|
12
|
-
c.PyMethodDef{
|
|
13
|
-
.ml_name = "parse",
|
|
14
|
-
.ml_meth = @ptrCast(@alignCast(&parse)),
|
|
15
|
-
.ml_flags = @as(c_int, 1),
|
|
16
|
-
.ml_doc = "parse a zon literal to a python object",
|
|
17
|
-
},
|
|
18
|
-
c.PyMethodDef{},
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
pub var zigmodule = c.PyModuleDef{
|
|
11
|
+
pub var zigmodule: c.PyModuleDef = .{
|
|
22
12
|
.m_name = "zig_zon",
|
|
23
|
-
.m_methods = &
|
|
13
|
+
.m_methods = @constCast(&[_]c.PyMethodDef{
|
|
14
|
+
.{
|
|
15
|
+
.ml_name = "parse",
|
|
16
|
+
.ml_meth = @ptrCast(@alignCast(&parse)),
|
|
17
|
+
.ml_flags = 1,
|
|
18
|
+
.ml_doc = "parse a zon literal to a python object",
|
|
19
|
+
},
|
|
20
|
+
.{},
|
|
21
|
+
}),
|
|
24
22
|
};
|
|
25
23
|
|
|
26
24
|
pub export fn PyInit_zig_zon() [*c]c.PyObject {
|
|
27
25
|
const mod = c.PyModule_Create(&zigmodule);
|
|
26
|
+
|
|
27
|
+
{
|
|
28
|
+
// const annotations = c.PyDict_New();
|
|
29
|
+
// defer c.Py_DecRef(annotations);
|
|
30
|
+
// const builtins = c.PyImport_ImportModule("builtins");
|
|
31
|
+
// defer c.Py_DecRef(builtins);
|
|
32
|
+
|
|
33
|
+
// const str_type = c.PyObject_GetAttrString(builtins, "str") orelse PyErrPanic();
|
|
34
|
+
// defer c.Py_DecRef(str_type);
|
|
35
|
+
|
|
36
|
+
// assert(0 <= c.PyDict_SetItemString(annotations, "value", str_type));
|
|
37
|
+
|
|
38
|
+
// const parse_ref = c.PyObject_GetAttrString(mod, "parse") orelse PyErrPanic();
|
|
39
|
+
// defer c.Py_DecRef(parse_ref);
|
|
40
|
+
|
|
41
|
+
//TODO: find another way to set annotations, since a builtin function is immutable.
|
|
42
|
+
|
|
43
|
+
// if (0 > c.PyObject_SetAttrString(parse_ref, "__annotations__", annotations)) {
|
|
44
|
+
// c.PyErr_Print();
|
|
45
|
+
// @panic("WTF");
|
|
46
|
+
// }
|
|
47
|
+
}
|
|
48
|
+
|
|
28
49
|
cpython.addVersion(mod);
|
|
29
50
|
cpython.addZigBuiltin(mod);
|
|
30
51
|
return mod;
|
|
31
52
|
}
|
|
32
53
|
|
|
54
|
+
pub fn PyErrPanic() noreturn {
|
|
55
|
+
c.PyErr_Print();
|
|
56
|
+
@panic("Unexpected PyErr!");
|
|
57
|
+
}
|
|
58
|
+
|
|
33
59
|
pub fn parse(self: [*]PyObject, args: [*]PyObject) callconv(.c) [*c]PyObject {
|
|
34
60
|
_ = self;
|
|
35
61
|
const zon_slice: [:0]u8 = zon_slice: {
|
|
@@ -38,7 +64,7 @@ pub fn parse(self: [*]PyObject, args: [*]PyObject) callconv(.c) [*c]PyObject {
|
|
|
38
64
|
break :zon_slice std.mem.sliceTo(zon_txt, 0);
|
|
39
65
|
};
|
|
40
66
|
|
|
41
|
-
var arena_instance = std.heap.ArenaAllocator.init(
|
|
67
|
+
var arena_instance = std.heap.ArenaAllocator.init(cpython.py_allocator);
|
|
42
68
|
defer arena_instance.deinit();
|
|
43
69
|
const arena = arena_instance.allocator();
|
|
44
70
|
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# Zig Python Sdist
|
|
2
|
+
|
|
3
|
+
this is a toolset to build python source distributions for pypi directly
|
|
4
|
+
using `zig build`. It's inspired by Loris Cro's talk
|
|
5
|
+
[Deinventing the Wheel](https://www.youtube.com/watch?v=HPmefnqirHk).
|
|
6
|
+
|
|
7
|
+
It allows to add a python distribution to zig packages
|
|
8
|
+
without dealing with python package metadata the traditional way.
|
|
9
|
+
|
|
10
|
+
You find a full usage example in the [example/](example/) directory.
|
|
11
|
+
|
|
12
|
+
## `build.zig` api
|
|
13
|
+
|
|
14
|
+
to generate a python `sdist` archive,
|
|
15
|
+
add the zig package to the `build.zig.zon` file of your existing
|
|
16
|
+
zig >= 0.16.0 project by running:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
zig fetch --save git+https://codeberg.org/dasimmet/zig-python-sdist
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
then, import the dependency and create a source distribution.
|
|
23
|
+
You will have to specify all files or directories (subdirectories are not added
|
|
24
|
+
recursively) necessary to build the extension to add to the sdist archive.
|
|
25
|
+
the `zig-python-sdist` dependency is added automatically for you as well as a `--fork`
|
|
26
|
+
command line argument to `zig build` at wheel build time.
|
|
27
|
+
|
|
28
|
+
```zig
|
|
29
|
+
const zig_python_sdist = @import("zig_python_sdist");
|
|
30
|
+
|
|
31
|
+
pub fn build(b: *std.Build) void {
|
|
32
|
+
const build_ext_step = b.step("bdist-wheel", "build the extension module");
|
|
33
|
+
{
|
|
34
|
+
// generate a module from cpython headers
|
|
35
|
+
const cpython = zig_python_sdist.createCPythonModule(b, .{
|
|
36
|
+
.target = target,
|
|
37
|
+
.version = zon.version,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// create a shared library, linked against cpython
|
|
41
|
+
const sdist_dummy = b.addLibrary(.{
|
|
42
|
+
.name = "sdist_dummy",
|
|
43
|
+
.linkage = .dynamic,
|
|
44
|
+
.root_module = b.createModule(.{
|
|
45
|
+
.root_source_file = b.path("sdist_dummy.zig"),
|
|
46
|
+
.target = target,
|
|
47
|
+
.optimize = optimize,
|
|
48
|
+
.imports = &.{
|
|
49
|
+
.{ .name = "cpython", .module = cpython },
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// install the extension on the "bdist-wheel" top level step
|
|
55
|
+
const sdist_dummy_install = zig_python_sdist.installExtLib(b, sdist_dummy);
|
|
56
|
+
build_ext_step.dependOn(&sdist_dummy_install.step);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const sdist = zig_python_sdist.createSdistStep(b, .{
|
|
60
|
+
.name = "python-package-name",
|
|
61
|
+
.version = @import("build.zig.zon).version,
|
|
62
|
+
.description = b.path("README.md"),
|
|
63
|
+
|
|
64
|
+
.build = .{
|
|
65
|
+
// the source distribution will build the "bdist-wheel" step on wheel build
|
|
66
|
+
.args = &.{
|
|
67
|
+
b.fmt("-Doptimize={s}", .{@tagName(optimize)}),
|
|
68
|
+
build_ext_step.name,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
// add all files and directories necessary to build to the source distribution
|
|
73
|
+
.files = &.{
|
|
74
|
+
.{ .src = b.path("build.zig.zon"), .dest = "build.zig.zon" },
|
|
75
|
+
.{ .src = b.path("build.zig"), .dest = "build.zig" },
|
|
76
|
+
.{ .src = b.path("README.md"), .dest = "README.md" },
|
|
77
|
+
.{ .src = b.path("LICENSE"), .dest = "LICENSE" },
|
|
78
|
+
},
|
|
79
|
+
.directories = &.{
|
|
80
|
+
.{ .src = b.path("src"), .dest = "src" },
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
// tip: when adding dependency directories, pass a `--fork=<path inside sdist>` to
|
|
84
|
+
// the build arguments.
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// the install step only builds the source distribution
|
|
88
|
+
const sdist_install = sdist.install();
|
|
89
|
+
b.getInstallStep().dependOn(&sdist_install.step);
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## `cpython` zig module
|
|
94
|
+
|
|
95
|
+
the generated `cpython` module provides a `c` member holding the translated cpython
|
|
96
|
+
headers as well as some utility functions to:
|
|
97
|
+
|
|
98
|
+
- `addVersion` adds `__version__` and `__version_str__` symbols to your module.
|
|
99
|
+
- `addZigBuiltin` adds a `__zig_builtin__` symbol to your module populated by zig's
|
|
100
|
+
build information.
|
|
101
|
+
- `wrapMain` wraps a Zig "JuicyMain" function (`fn (std.process.Init) !void`) to be used as an entrypoint for your
|
|
102
|
+
python module, by populating the `std.process.init` from python's builtin `os` and `sys` modules.
|
|
103
|
+
|
|
104
|
+
take a look at [example/sdist_dummy.zig](example/sdist_dummy.zig)
|
|
105
|
+
|
|
106
|
+
## Testing the distribution installation
|
|
107
|
+
|
|
108
|
+
to test the built source distribution, you can create a `VenvInstallTest` step
|
|
109
|
+
that will:
|
|
110
|
+
|
|
111
|
+
- create a python virtual environment
|
|
112
|
+
- compile and install your package
|
|
113
|
+
- run python unittest files against the installed package
|
|
114
|
+
- run entrypoints from the installed package to test them
|
|
115
|
+
|
|
116
|
+
## References
|
|
117
|
+
|
|
118
|
+
- [Deinventing the Wheel - Loris Cro](https://www.youtube.com/watch?v=HPmefnqirHk)
|
|
119
|
+
- <https://pypi.org/project/setuptools-zig/>
|
|
120
|
+
- <https://pypi.org/project/zig-zon>
|
|
121
|
+
- <https://codeberg.org/dasimmet/python-zig-zon>
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
const Build = std.Build;
|
|
3
|
+
const LazyPath = Build.LazyPath;
|
|
4
|
+
const zon = @import("build.zig.zon");
|
|
5
|
+
|
|
6
|
+
// access to the types of the public interface
|
|
7
|
+
pub const CPython = @import("src/build/CPython.zig");
|
|
8
|
+
pub const SDistStep = @import("src/build/SDistStep.zig");
|
|
9
|
+
pub const VenvInstallTest = @import("src/build/VenvInstallTest.zig");
|
|
10
|
+
// public zig build interface
|
|
11
|
+
pub const createSdistStep = createSdist;
|
|
12
|
+
pub const createVenvInstallTest = VenvInstallTest.create;
|
|
13
|
+
pub const createCPythonModule = createModule;
|
|
14
|
+
pub const installExtLib = CPython.installExtLib;
|
|
15
|
+
pub const renameExtLib = CPython.renameExtLib;
|
|
16
|
+
|
|
17
|
+
pub fn build(b: *Build) void {
|
|
18
|
+
const target = b.standardTargetOptions(.{});
|
|
19
|
+
const optimize = b.standardOptimizeOption(.{});
|
|
20
|
+
|
|
21
|
+
const mod = b.addModule("sdist-builder", .{
|
|
22
|
+
.root_source_file = b.path("src/sdist_builder.zig"),
|
|
23
|
+
.target = target,
|
|
24
|
+
.optimize = optimize,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const exe = b.addExecutable(.{
|
|
28
|
+
.name = "sdist-builder",
|
|
29
|
+
.root_module = mod,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
b.installArtifact(exe);
|
|
33
|
+
|
|
34
|
+
const cpython = CPython.createModuleInternal(b, b, .{
|
|
35
|
+
.target = target,
|
|
36
|
+
.version = zon.version,
|
|
37
|
+
.optimize = .Debug,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const sdist_dummy = b.addLibrary(.{
|
|
41
|
+
.name = "sdist_dummy",
|
|
42
|
+
.root_module = b.createModule(.{
|
|
43
|
+
.root_source_file = b.path("example/sdist_dummy.zig"),
|
|
44
|
+
.target = target,
|
|
45
|
+
.optimize = optimize,
|
|
46
|
+
.imports = &.{
|
|
47
|
+
.{ .name = "cpython", .module = cpython },
|
|
48
|
+
},
|
|
49
|
+
}),
|
|
50
|
+
.linkage = .dynamic,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const install_ext = installExtLib(b, sdist_dummy);
|
|
54
|
+
|
|
55
|
+
const build_ext_step = b.step("bdist-wheel", "build the sdist dummy python module");
|
|
56
|
+
build_ext_step.dependOn(&install_ext.step);
|
|
57
|
+
|
|
58
|
+
{
|
|
59
|
+
const ext_path = CPython.renameExtLib(b, sdist_dummy);
|
|
60
|
+
const ext_tests = ext_path.wf.addCopyFile(b.path("example/test_ext.py"), "test_ext.py");
|
|
61
|
+
const test_run = Build.Step.Run.create(b, "unittest");
|
|
62
|
+
test_run.setCwd(ext_path.wf.getDirectory());
|
|
63
|
+
test_run.setEnvironmentVariable("PYTHONPATH", ".");
|
|
64
|
+
test_run.addFileArg(CPython.find(b));
|
|
65
|
+
test_run.addArgs(&.{ "-m", "unittest" });
|
|
66
|
+
test_run.addDirectoryArg(ext_tests);
|
|
67
|
+
b.step("test-bdist", "test the extension against the local python version").dependOn(&test_run.step);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
{
|
|
71
|
+
const ext_path = CPython.renameExtLib(b, sdist_dummy);
|
|
72
|
+
const stubgen_run = Build.Step.Run.create(b, "stubgen");
|
|
73
|
+
stubgen_run.setCwd(ext_path.wf.getDirectory());
|
|
74
|
+
stubgen_run.addArgs(&.{
|
|
75
|
+
"stubgen",
|
|
76
|
+
"-m",
|
|
77
|
+
sdist_dummy.name,
|
|
78
|
+
"-o",
|
|
79
|
+
});
|
|
80
|
+
const stub_dir = stubgen_run.addOutputDirectoryArg("stubgen");
|
|
81
|
+
stubgen_run.addArg("--search-path");
|
|
82
|
+
stubgen_run.addDirectoryArg(ext_path.wf.getDirectory());
|
|
83
|
+
const usf = b.addUpdateSourceFiles();
|
|
84
|
+
usf.addCopyFileToSource(
|
|
85
|
+
stub_dir.path(b, b.fmt("{s}.pyi", .{sdist_dummy.name})),
|
|
86
|
+
b.fmt("example/{s}.pyi", .{sdist_dummy.name}),
|
|
87
|
+
);
|
|
88
|
+
b.step("stubgen", "generate .pyi from module with mypy stubgen").dependOn(&usf.step);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const sdist = SDistStep.createInternal(b, exe, .{
|
|
92
|
+
.name = "sdist-dummy",
|
|
93
|
+
.version = zon.version,
|
|
94
|
+
.description = b.path("README.md"),
|
|
95
|
+
.build = .{
|
|
96
|
+
.args = &.{
|
|
97
|
+
b.fmt("-Doptimize={s}", .{@tagName(optimize)}),
|
|
98
|
+
build_ext_step.name,
|
|
99
|
+
},
|
|
100
|
+
.copy_pattern = &.{
|
|
101
|
+
"zig-out/lib/*",
|
|
102
|
+
"example/sdist_dummy.pyi",
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
.extra_pkg_info = @import("pkg-info.zon"),
|
|
106
|
+
.files = &.{
|
|
107
|
+
.{ .src = b.path("build.zig.zon"), .dest = "build.zig.zon" },
|
|
108
|
+
.{ .src = b.path("build.zig"), .dest = "build.zig" },
|
|
109
|
+
.{ .src = b.path("pkg-info.zon"), .dest = "pkg-info.zon" },
|
|
110
|
+
.{ .src = b.path("example/sdist_dummy.zig"), .dest = "example/sdist_dummy.zig" },
|
|
111
|
+
.{ .src = b.path("example/sdist_dummy.pyi"), .dest = "example/sdist_dummy.pyi" },
|
|
112
|
+
},
|
|
113
|
+
.directories = &.{
|
|
114
|
+
.{ .src = b.path("src/build"), .dest = "src/build" },
|
|
115
|
+
},
|
|
116
|
+
.entry_points = .{
|
|
117
|
+
.console_scripts = &.{
|
|
118
|
+
"sdist-dummy = sdist_dummy:__main__",
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
.install_requires = &.{"ruamel.yaml.clibz"},
|
|
122
|
+
.zig_version = std.SemanticVersion.parse("0.16.0") catch unreachable,
|
|
123
|
+
.compression = .best,
|
|
124
|
+
});
|
|
125
|
+
const sdist_install = sdist.install();
|
|
126
|
+
b.getInstallStep().dependOn(&sdist_install.step);
|
|
127
|
+
b.step("sdist", "only build and install sdist").dependOn(&sdist_install.step);
|
|
128
|
+
|
|
129
|
+
const test_pip_install = VenvInstallTest.create(
|
|
130
|
+
b,
|
|
131
|
+
.{
|
|
132
|
+
.sdist = sdist.getEmittedArchive(),
|
|
133
|
+
.unittest_py = &.{b.path("example/test_ext.py")},
|
|
134
|
+
.twine_check = true,
|
|
135
|
+
.entry_points = &.{
|
|
136
|
+
// TODO: make "some more" one argument on windows
|
|
137
|
+
&.{ "sdist-dummy", "with", "some more", "args" },
|
|
138
|
+
},
|
|
139
|
+
.no_clean = b.option(bool, "pip-no-clean", "cleanup pip temp dirs") orelse false,
|
|
140
|
+
},
|
|
141
|
+
);
|
|
142
|
+
b.step("test-pip-install", "test build and install sdist with pip").dependOn(test_pip_install.step);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
fn createModule(b: *Build, options: CPython.LinkOptions) *Build.Module {
|
|
146
|
+
const this_dep = b.dependencyFromBuildZig(@This(), .{});
|
|
147
|
+
return CPython.createModuleInternal(b, this_dep.builder, options);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
fn createSdist(b: *Build, options: SDistStep.Options) *SDistStep {
|
|
151
|
+
var opt = options;
|
|
152
|
+
const this_dep = b.dependencyFromBuildZig(@This(), .{
|
|
153
|
+
.target = b.graph.host,
|
|
154
|
+
.optimize = .Debug,
|
|
155
|
+
});
|
|
156
|
+
const builder = this_dep.artifact("sdist-builder");
|
|
157
|
+
|
|
158
|
+
{
|
|
159
|
+
var files: std.ArrayList(SDistStep.Content) = .empty;
|
|
160
|
+
const extra_files: []const SDistStep.Content = &.{
|
|
161
|
+
.{ .src = this_dep.path("build.zig.zon"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/build.zig.zon" },
|
|
162
|
+
.{ .src = this_dep.path("build.zig"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/build.zig" },
|
|
163
|
+
.{ .src = this_dep.path("pkg-info.zon"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/pkg-info.zon" },
|
|
164
|
+
.{ .src = this_dep.path("README.md"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/README.md" },
|
|
165
|
+
.{ .src = this_dep.path("LICENSE"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/LICENSE" },
|
|
166
|
+
};
|
|
167
|
+
files.ensureTotalCapacity(
|
|
168
|
+
b.allocator,
|
|
169
|
+
opt.files.len + extra_files.len,
|
|
170
|
+
) catch @panic("OOM");
|
|
171
|
+
files.appendSliceAssumeCapacity(opt.files);
|
|
172
|
+
files.appendSliceAssumeCapacity(extra_files);
|
|
173
|
+
opt.files = files.toOwnedSlice(b.allocator) catch @panic("OOM");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
{
|
|
177
|
+
var dirs: std.ArrayList(SDistStep.Content) = .empty;
|
|
178
|
+
const extra_dirs: []const SDistStep.Content = &.{
|
|
179
|
+
.{ .src = this_dep.path("src/build"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/src/build" },
|
|
180
|
+
};
|
|
181
|
+
dirs.ensureTotalCapacity(
|
|
182
|
+
b.allocator,
|
|
183
|
+
opt.directories.len + extra_dirs.len,
|
|
184
|
+
) catch @panic("OOM");
|
|
185
|
+
dirs.appendSliceAssumeCapacity(opt.directories);
|
|
186
|
+
dirs.appendSliceAssumeCapacity(extra_dirs);
|
|
187
|
+
opt.directories = dirs.toOwnedSlice(b.allocator) catch @panic("OOM");
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
{
|
|
191
|
+
var args: std.ArrayList([]const u8) = .empty;
|
|
192
|
+
args.ensureTotalCapacity(
|
|
193
|
+
b.allocator,
|
|
194
|
+
opt.build.args.len + 1,
|
|
195
|
+
) catch @panic("OOM");
|
|
196
|
+
args.appendAssumeCapacity("--fork=zig-pkg/zig_python_sdist-" ++ zon.version);
|
|
197
|
+
args.appendSliceAssumeCapacity(opt.build.args);
|
|
198
|
+
opt.build.args = args.toOwnedSlice(b.allocator) catch @panic("OOM");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return SDistStep.createInternal(b, builder, opt);
|
|
202
|
+
}
|