zig-zon 0.2.3__tar.gz → 0.2.4__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.
Files changed (25) hide show
  1. {zig-zon-0.2.3 → zig-zon-0.2.4}/PKG-INFO +5 -4
  2. {zig-zon-0.2.3 → zig-zon-0.2.4}/build.zig +11 -8
  3. {zig-zon-0.2.3 → zig-zon-0.2.4}/build.zig.zon +3 -3
  4. {zig-zon-0.2.3 → zig-zon-0.2.4}/setup.py +2 -1
  5. zig-zon-0.2.4/setup.zig.json +1 -0
  6. zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3/README.md +114 -0
  7. {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3}/build.zig +45 -25
  8. {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3}/build.zig.zon +2 -2
  9. {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3}/src/build/CPython.zig +49 -62
  10. zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3/src/build/LowerDashString.zig +20 -0
  11. {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3}/src/build/SDistStep.zig +55 -23
  12. zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3/src/build/VenvInstallTest.zig +138 -0
  13. zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3/src/build/python.zig +416 -0
  14. zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3/src/build/setup_json.zig +20 -0
  15. zig-zon-0.2.3/setup.zig.json +0 -1
  16. zig-zon-0.2.3/zig-pkg/zig_python_sdist/README.md +0 -7
  17. zig-zon-0.2.3/zig-pkg/zig_python_sdist/src/build/LowerDashStringFormatter.zig +0 -12
  18. zig-zon-0.2.3/zig-pkg/zig_python_sdist/src/build/VenvInstallTest.zig +0 -101
  19. zig-zon-0.2.3/zig-pkg/zig_python_sdist/src/build/python.zig +0 -217
  20. {zig-zon-0.2.3 → zig-zon-0.2.4}/pkg-info.zon +0 -0
  21. {zig-zon-0.2.3 → zig-zon-0.2.4}/src/zig_zon.zig +0 -0
  22. {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3}/LICENSE +0 -0
  23. {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3}/pkg-info.zon +0 -0
  24. {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3}/src/build/extract_link_info.py +0 -0
  25. {zig-zon-0.2.3/zig-pkg/zig_python_sdist → zig-zon-0.2.4/zig-pkg/zig_python_sdist-0.0.3}/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
3
+ Version: 0.2.4
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)
@@ -11,7 +11,7 @@ pub fn build(b: *Build) void {
11
11
  const ext_install_step = b.step("bdist-wheel", "build python extension module");
12
12
 
13
13
  // this generates a python sdist archive
14
- const sdist = zig_python_sdist.create(b, .{
14
+ const sdist = zig_python_sdist.createSdistStep(b, .{
15
15
  .name = "zig-zon",
16
16
  .version = zon.version,
17
17
  .description = b.path("README.md"),
@@ -39,11 +39,11 @@ pub fn build(b: *Build) void {
39
39
 
40
40
  // we can use a python virtual environment inside
41
41
  // the zig cache to test pip installation
42
- const test_pip_install = zig_python_sdist.VenvInstallTest.create(
42
+ const test_pip_install = zig_python_sdist.createVenvInstallTest(
43
43
  b,
44
44
  .{
45
45
  .sdist = sdist.getEmittedArchive(),
46
- .test_py = b.path("src/test_zig_zon.py"),
46
+ .unittest_py = &.{b.path("src/test_zig_zon.py")},
47
47
  .twine_check = true,
48
48
  .no_clean = b.option(bool, "pip-no-clean", "dont cleanup pip temp dirs") orelse false,
49
49
  },
@@ -51,20 +51,23 @@ pub fn build(b: *Build) void {
51
51
  b.step("test-pip-install", "").dependOn(test_pip_install.step);
52
52
 
53
53
  {
54
+ const cpython = zig_python_sdist.createCPythonModule(b, .{
55
+ .target = target,
56
+ .version = zon.version,
57
+ .optimize = .ReleaseSmall,
58
+ });
54
59
  const lib = b.addLibrary(.{
55
60
  .name = "zig_zon",
56
61
  .root_module = b.addModule("zig_zon", .{
57
62
  .root_source_file = b.path("src/zig_zon.zig"),
58
63
  .target = target,
59
64
  .optimize = optimize,
65
+ .imports = &.{
66
+ .{ .name = "cpython", .module = cpython },
67
+ },
60
68
  }),
61
69
  .linkage = .dynamic,
62
70
  });
63
- zig_python_sdist.linkCPython(b, .{
64
- .lib = lib,
65
- .version = zon.version,
66
- .optimize = .ReleaseSmall,
67
- });
68
71
  const ext_install = zig_python_sdist.installExtLib(b, lib);
69
72
 
70
73
  ext_install_step.dependOn(&ext_install.step);
@@ -1,12 +1,12 @@
1
1
  .{
2
2
  .name = .python_zig_zon,
3
3
  .fingerprint = 0x3d332656a3644fc9,
4
- .version = "0.2.3",
4
+ .version = "0.2.4",
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.2#3dd2c2eaf62a7007d6c257c357ded7911537621a",
9
- .hash = "zig_python_sdist-0.0.2-wjgdOpyrAADPCiUfh4LoKFNZkHstj4a1bIyEuwnNKkcc",
8
+ .url = "git+https://codeberg.org/dasimmet/zig-python-sdist.git#b09744edafe683ae6000a96498cc68e86173dbe7",
9
+ .hash = "zig_python_sdist-0.0.3-wjgdOlzvAADTtFV1n4xTLUwlgCsiLDcmhebfZ7kWM05z",
10
10
  },
11
11
  },
12
12
  .paths = .{
@@ -39,7 +39,7 @@ class ZigBuildWheel(bdist_wheel):
39
39
  subprocess.check_call(build_cmd)
40
40
 
41
41
  os.makedirs(self.bdist_dir, exist_ok=True)
42
- copy_pattern = "zig-out/{}".format(setup_json['build']['copy_pattern'])
42
+ copy_pattern = setup_json['build']['copy_pattern']
43
43
  artifacts = glob.glob(copy_pattern)
44
44
  if len(artifacts) == 0:
45
45
  raise Exception(
@@ -57,6 +57,7 @@ setup(
57
57
  version=mkversion(setup_json['version']),
58
58
  cmdclass={'bdist_wheel': ZigBuildWheel},
59
59
  install_requires=setup_json['install_requires'],
60
+ entry_points=setup_json['entry_points'],
60
61
  setup_requires=[
61
62
  "setuptools",
62
63
  "wheel",
@@ -0,0 +1 @@
1
+ {"name":"zig_zon","version":{"major":0,"minor":2,"patch":4,"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.3","bdist-wheel","-Doptimize=ReleaseSmall"],"requires":[],"copy_pattern":"zig-out/lib/*"},"install_requires":[],"entry_points":{"console_scripts":[],"gui_scripts":[]}}
@@ -0,0 +1,114 @@
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
@@ -3,9 +3,15 @@ const Build = std.Build;
3
3
  const LazyPath = Build.LazyPath;
4
4
  const zon = @import("build.zig.zon");
5
5
 
6
+ // access to the types of the public interface
6
7
  pub const CPython = @import("src/build/CPython.zig");
7
- pub const VenvInstallTest = @import("src/build/VenvInstallTest.zig");
8
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;
9
15
 
10
16
  pub fn build(b: *Build) void {
11
17
  const target = b.standardTargetOptions(.{});
@@ -24,32 +30,36 @@ pub fn build(b: *Build) void {
24
30
 
25
31
  b.installArtifact(exe);
26
32
 
33
+ const cpython = CPython.createModuleInternal(b, b, .{
34
+ .target = target,
35
+ .version = zon.version,
36
+ .optimize = .Debug,
37
+ });
38
+
27
39
  const sdist_dummy = b.addLibrary(.{
28
40
  .name = "sdist_dummy",
29
41
  .root_module = b.createModule(.{
30
42
  .root_source_file = b.path("example/sdist_dummy.zig"),
31
43
  .target = target,
32
44
  .optimize = optimize,
45
+ .imports = &.{
46
+ .{ .name = "cpython", .module = cpython },
47
+ },
33
48
  }),
34
49
  .linkage = .dynamic,
35
50
  });
36
51
 
37
- linkCPython(b, .{
38
- .lib = sdist_dummy,
39
- .version = zon.version,
40
- });
41
52
  const install_ext = installExtLib(b, sdist_dummy);
42
53
 
43
54
  const build_ext_step = b.step("bdist-wheel", "build the sdist dummy python module");
44
55
  build_ext_step.dependOn(&install_ext.step);
45
56
 
46
- const sdist = SDistStep.createInternal(b, .{
47
- .sdist_builder = exe,
48
- .name = "zig-python-sdist-dummy",
57
+ const sdist = SDistStep.createInternal(b, exe, .{
58
+ .name = "sdist-dummy",
49
59
  .version = zon.version,
50
60
  .description = b.path("README.md"),
51
61
  .build = .{
52
- .args = &.{ build_ext_step.name, b.fmt("-Doptimize={s}", .{@tagName(optimize)}) },
62
+ .args = &.{ b.fmt("-Doptimize={s}", .{@tagName(optimize)}), build_ext_step.name },
53
63
  },
54
64
  .extra_pkg_info = @import("pkg-info.zon"),
55
65
  .files = &.{
@@ -59,9 +69,13 @@ pub fn build(b: *Build) void {
59
69
  .{ .src = b.path("example/sdist_dummy.zig"), .dest = "example/sdist_dummy.zig" },
60
70
  },
61
71
  .directories = &.{
62
- .{ .src = b.path("src"), .dest = "src" },
63
72
  .{ .src = b.path("src/build"), .dest = "src/build" },
64
73
  },
74
+ .entry_points = .{
75
+ .console_scripts = &.{
76
+ "sdist-dummy = sdist_dummy:__main__",
77
+ },
78
+ },
65
79
  .install_requires = &.{"ruamel.yaml.clibz"},
66
80
  .zig_version = std.SemanticVersion.parse("0.16.0") catch unreachable,
67
81
  });
@@ -73,33 +87,39 @@ pub fn build(b: *Build) void {
73
87
  b,
74
88
  .{
75
89
  .sdist = sdist.getEmittedArchive(),
76
- .test_py = b.path("example/test_ext.py"),
90
+ .unittest_py = &.{b.path("example/test_ext.py")},
77
91
  .twine_check = true,
92
+ .entry_points = &.{
93
+ // TODO: make "some more" one argument on windows
94
+ &.{ "sdist-dummy", "with", "some more", "args" },
95
+ },
78
96
  .no_clean = b.option(bool, "pip-no-clean", "cleanup pip temp dirs") orelse false,
79
97
  },
80
98
  );
81
99
  b.step("test-pip-install", "").dependOn(test_pip_install.step);
82
100
  }
83
101
 
84
- pub const linkCPython = CPython.link;
85
- pub const installExtLib = CPython.installExtLib;
102
+ fn createModule(b: *Build, options: CPython.LinkOptions) *Build.Module {
103
+ const this_dep = b.dependencyFromBuildZig(@This(), .{});
104
+ return CPython.createModuleInternal(b, this_dep.builder, options);
105
+ }
86
106
 
87
- pub fn create(b: *Build, options: SDistStep.Options) *SDistStep {
107
+ fn createSdist(b: *Build, options: SDistStep.Options) *SDistStep {
88
108
  var opt = options;
89
- const sdist = b.dependencyFromBuildZig(@This(), .{
109
+ const this_dep = b.dependencyFromBuildZig(@This(), .{
90
110
  .target = b.graph.host,
91
- .optimize = .ReleaseFast,
111
+ .optimize = .Debug,
92
112
  });
93
- opt.sdist_builder = opt.sdist_builder orelse sdist.artifact("sdist-builder");
113
+ const builder = this_dep.artifact("sdist-builder");
94
114
 
95
115
  {
96
116
  var files: std.ArrayList(SDistStep.Content) = .empty;
97
117
  const extra_files: []const SDistStep.Content = &.{
98
- .{ .src = sdist.path("build.zig.zon"), .dest = "zig-pkg/zig_python_sdist/build.zig.zon" },
99
- .{ .src = sdist.path("build.zig"), .dest = "zig-pkg/zig_python_sdist/build.zig" },
100
- .{ .src = sdist.path("pkg-info.zon"), .dest = "zig-pkg/zig_python_sdist/pkg-info.zon" },
101
- .{ .src = sdist.path("README.md"), .dest = "zig-pkg/zig_python_sdist/README.md" },
102
- .{ .src = sdist.path("LICENSE"), .dest = "zig-pkg/zig_python_sdist/LICENSE" },
118
+ .{ .src = this_dep.path("build.zig.zon"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/build.zig.zon" },
119
+ .{ .src = this_dep.path("build.zig"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/build.zig" },
120
+ .{ .src = this_dep.path("pkg-info.zon"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/pkg-info.zon" },
121
+ .{ .src = this_dep.path("README.md"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/README.md" },
122
+ .{ .src = this_dep.path("LICENSE"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/LICENSE" },
103
123
  };
104
124
  files.ensureTotalCapacity(
105
125
  b.allocator,
@@ -113,7 +133,7 @@ pub fn create(b: *Build, options: SDistStep.Options) *SDistStep {
113
133
  {
114
134
  var dirs: std.ArrayList(SDistStep.Content) = .empty;
115
135
  const extra_dirs: []const SDistStep.Content = &.{
116
- .{ .src = sdist.path("src/build"), .dest = "zig-pkg/zig_python_sdist/src/build" },
136
+ .{ .src = this_dep.path("src/build"), .dest = "zig-pkg/zig_python_sdist-" ++ zon.version ++ "/src/build" },
117
137
  };
118
138
  dirs.ensureTotalCapacity(
119
139
  b.allocator,
@@ -130,10 +150,10 @@ pub fn create(b: *Build, options: SDistStep.Options) *SDistStep {
130
150
  b.allocator,
131
151
  opt.build.args.len + 1,
132
152
  ) catch @panic("OOM");
153
+ args.appendAssumeCapacity("--fork=zig-pkg/zig_python_sdist-" ++ zon.version);
133
154
  args.appendSliceAssumeCapacity(opt.build.args);
134
- args.appendAssumeCapacity("--fork=zig-pkg/zig_python_sdist");
135
155
  opt.build.args = args.toOwnedSlice(b.allocator) catch @panic("OOM");
136
156
  }
137
157
 
138
- return SDistStep.createInternal(b, opt);
158
+ return SDistStep.createInternal(b, builder, opt);
139
159
  }
@@ -1,7 +1,7 @@
1
1
  .{
2
2
  .name = .zig_python_sdist,
3
- .version = "0.0.2",
4
- .minimum_zig_version = "0.17.0-dev.947+36069a2a7",
3
+ .version = "0.0.3",
4
+ .minimum_zig_version = "0.17.0-dev.1245+efd6f190f",
5
5
  .paths = .{
6
6
  "build.zig",
7
7
  "build.zig.zon",
@@ -1,101 +1,76 @@
1
+ //! a module for building cpython extension libraries,
2
+ //! linking them with system python headers and libs
3
+
1
4
  const std = @import("std");
2
5
  const builtin = @import("builtin");
3
6
  const Build = std.Build;
4
7
  const LazyPath = Build.LazyPath;
5
8
  const mem = std.mem;
6
9
 
7
- /// a module for building cpython extension libraries,
8
- /// linking them with system python headers and libs
9
10
  pub const CPython = @This();
10
11
 
11
- exe: LazyPath,
12
-
13
- pub fn find(b: *Build) *CPython {
14
- const self = b.allocator.create(CPython) catch @panic("OOM");
15
- self.* = .{
16
- .exe = findProgramCompat(b, &.{
17
- "python",
18
- "python3",
19
- }) orelse {
20
- const err_wf = b.addWriteFiles();
21
- const err_step = b.addFail("finding python failed. please provide a path to the exe using -Dpython=");
22
- err_wf.step.dependOn(&err_step.step);
23
- self.* = .{ .exe = err_wf.getDirectory() };
24
- return self;
25
- },
26
- };
27
- return self;
28
- }
29
-
30
12
  pub const LinkOptions = struct {
31
- /// library to link against libpython
32
- lib: *std.Build.Step.Compile,
13
+ target: Build.ResolvedTarget,
14
+ optimize: std.builtin.OptimizeMode,
33
15
  version: ?[]const u8 = null,
16
+ // pass in the python executable for finding headers. use "python" from PATH if omitted.
34
17
  python_exe: ?LazyPath = null,
35
- /// optimize mode for the c module. will fall back to lib's optimize mode.
36
- optimize: ?std.builtin.OptimizeMode = null,
37
18
  macros: []const [2][]const u8 = &default_macros,
38
19
  pub const default_macros = .{
39
20
  .{ "Py_LIMITED_API", "0x03060500" }, // use the limited api from 3.6.5
40
21
  };
41
22
  };
42
23
 
43
- pub fn link(b: *Build, options: LinkOptions) void {
44
- const self: *const CPython = if (options.python_exe) |python_exe|
45
- &.{ .exe = python_exe }
24
+ // generate a module linked against the python library headers found using
25
+ // a cpython runtime
26
+ pub fn createModuleInternal(b: *Build, sdist_build: *Build, options: LinkOptions) *Build.Module {
27
+ const python = if (options.python_exe) |python_exe|
28
+ python_exe
46
29
  else
47
30
  CPython.find(b);
48
31
 
49
- const target = Build.ResolvedTarget{
50
- .query = .{},
51
- .result = options.lib.rootModuleTarget(),
52
- };
53
-
54
- const python_inc = self.generateIncludeDir(b);
32
+ const python_inc = generateIncludeDir(b, python);
55
33
 
56
34
  const cpython = b.addTranslateC(.{
57
35
  .root_source_file = python_inc.path(b, "zig_python_include.h"),
58
- .optimize = options.optimize orelse options.lib.root_module.optimize orelse .ReleaseSafe,
59
- .target = target,
36
+ .optimize = options.optimize,
37
+ .target = options.target,
60
38
  });
61
39
  cpython.addIncludePath(python_inc);
62
40
 
63
- const wf = b.addWriteFiles();
41
+ for (options.macros) |macro| {
42
+ cpython.defineCMacro(macro[0], macro[1]);
43
+ }
44
+ const cpy_mod = cpython.createModule();
64
45
 
65
- const pymod = b.createModule(.{
66
- .root_source_file = wf.add("python.zig", @embedFile("python.zig")),
67
- .imports = &.{
68
- .{ .name = "c", .module = cpython.createModule() },
69
- },
70
- });
46
+ const wf = b.addWriteFiles();
71
47
 
72
- if (options.version) |version| {
73
- pymod.addImport("version", b.createModule(.{
48
+ const version = if (options.version) |version|
49
+ b.createModule(.{
74
50
  .root_source_file = wf.add("version.txt", version),
75
- }));
76
- } else {
77
- pymod.addImport("version", b.createModule(.{
51
+ })
52
+ else
53
+ b.createModule(.{
78
54
  .root_source_file = wf.add("version.txt", "version not provided at configure time!"),
79
- }));
80
- }
55
+ });
81
56
 
82
- for (options.macros) |macro| {
83
- cpython.defineCMacro(macro[0], macro[1]);
57
+ if (options.target.result.os.tag == .windows) {
58
+ cpy_mod.addLibraryPath(python_inc);
59
+ cpy_mod.linkSystemLibrary("python", .{});
84
60
  }
85
61
 
86
- if (target.result.os.tag == .windows) {
87
- options.lib.root_module.addLibraryPath(python_inc);
88
- options.lib.root_module.linkSystemLibrary("python", .{});
89
- }
90
- options.lib.root_module.addImport(
91
- "cpython",
92
- pymod,
93
- );
62
+ return b.createModule(.{
63
+ .root_source_file = sdist_build.path("src/build/python.zig"),
64
+ .imports = &.{
65
+ .{ .name = "c", .module = cpy_mod },
66
+ .{ .name = "version", .module = version },
67
+ },
68
+ });
94
69
  }
95
70
 
96
- pub fn generateIncludeDir(self: CPython, b: *Build) LazyPath {
71
+ pub fn generateIncludeDir(b: *Build, python: LazyPath) LazyPath {
97
72
  const extract_link_info = Build.Step.Run.create(b, "python header extraction run");
98
- extract_link_info.addFileArg(self.exe);
73
+ extract_link_info.addFileArg(python);
99
74
  const wf = b.addWriteFiles();
100
75
  extract_link_info.addFileArg(wf.add("extract_link_info.py", @embedFile("extract_link_info.py")));
101
76
  return extract_link_info.addOutputDirectoryArg("pyinclude");
@@ -114,6 +89,18 @@ pub fn lib_ext_name(b: *Build, lib: *Build.Step.Compile) []const u8 {
114
89
  b.fmt("{s}.so", .{lib.name});
115
90
  }
116
91
 
92
+ pub fn find(b: *Build) LazyPath {
93
+ return findProgramCompat(b, &.{
94
+ "python",
95
+ "python3",
96
+ }) orelse {
97
+ const err_wf = b.addWriteFiles();
98
+ const err_step = b.addFail("finding python failed. please provide a path to the exe using -Dpython=");
99
+ err_wf.step.dependOn(&err_step.step);
100
+ return err_wf.getDirectory();
101
+ };
102
+ }
103
+
117
104
  // compat for 0.16.0 and 0.17.0
118
105
  pub fn findProgramCompat(b: *std.Build, names: []const []const u8) ?LazyPath {
119
106
  if (comptime (@import("builtin").zig_version.order(std.SemanticVersion.parse("0.16.0") catch unreachable) == .gt)) {
@@ -0,0 +1,20 @@
1
+ //! replaces dashes with lower dashes
2
+ const LowerDashString = @This();
3
+
4
+ const std = @import("std");
5
+ str: []const u8,
6
+
7
+ /// replaces dashes with lower dashes
8
+ pub fn fmt(str: []const u8) LowerDashString {
9
+ return .{ .str = str };
10
+ }
11
+
12
+ pub fn format(self: LowerDashString, w: *std.Io.Writer) !void {
13
+ var rest = self.str;
14
+ while (std.mem.findScalar(u8, rest, '-')) |idx| {
15
+ try w.writeAll(rest[0..idx]);
16
+ try w.writeByte('_');
17
+ rest = rest[idx + 1 ..];
18
+ }
19
+ try w.writeAll(rest);
20
+ }