-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyz3.build.zig
More file actions
526 lines (469 loc) · 22.1 KB
/
pyz3.build.zig
File metadata and controls
526 lines (469 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const builtin = @import("builtin");
const std = @import("std");
const Step = std.Build.Step;
const LazyPath = std.Build.LazyPath;
const GeneratedFile = std.Build.GeneratedFile;
pub const PyZ3Options = struct {
// Optionally pass your test_step and we will hook up the pyZ3 Zig tests.
test_step: ?*Step = null,
};
pub const PythonModuleOptions = struct {
name: [:0]const u8,
root_source_file: std.Build.LazyPath,
limited_api: bool = true,
target: std.Target.Query,
optimize: std.builtin.OptimizeMode,
main_pkg_path: ?std.Build.LazyPath = null,
// C/C++ integration options
c_sources: []const []const u8 = &.{},
c_include_dirs: []const []const u8 = &.{},
c_libraries: []const []const u8 = &.{},
c_flags: []const []const u8 = &.{},
ld_flags: []const []const u8 = &.{},
pub fn short_name(self: *const PythonModuleOptions) [:0]const u8 {
if (std.mem.lastIndexOfScalar(u8, self.name, '.')) |short_name_idx| {
return self.name[short_name_idx + 1 .. :0];
}
return self.name;
}
};
pub const PythonModule = struct {
library_step: *std.Build.Step.Compile,
test_step: *std.Build.Step.Compile,
};
/// Configure a pyZ3 step in the build. From this, you can define Python modules.
pub fn addPyZ3(b: *std.Build, options: PyZ3Options) *PyZ3Step {
return PyZ3Step.add(b, options);
}
pub const PyZ3Step = struct {
owner: *std.Build,
allocator: std.mem.Allocator,
options: PyZ3Options,
test_build_step: *Step,
generate_stubs: *Step,
check_stubs: bool,
python_exe: []const u8,
libpython: []const u8,
hexversion: []const u8,
ext_suffix: []const u8,
pyz3_source_file: []const u8,
ffi_header_file: []const u8,
numpy_ffi_header_file: []const u8,
numpy_include_dir: []const u8,
python_include_dir: []const u8,
python_library_dir: []const u8,
pub fn add(b: *std.Build, options: PyZ3Options) *PyZ3Step {
const test_build_step = b.step("pyz3-test-build", "Build pyZ3 test runners");
const generate_stubs = b.step("generate-stubs", "Generate pyi stubs for the compiled binary");
const check_stubs = b.option(bool, "check-stubs", "Check that existing stubs are up to date instead of generating new ones") orelse false;
const python_exe = blk: {
if (b.option([]const u8, "python-exe", "Python executable to use")) |exe| {
break :blk exe;
}
if (getStdOutput(b.allocator, &.{ "poetry", "env", "info", "--executable" })) |exe| {
// Strip off the trailing newline
break :blk exe[0 .. exe.len - 1];
} else |_| {
break :blk "python3";
}
};
const libpython = getLibpython(
b.allocator,
python_exe,
) catch |err| {
std.debug.print("\n❌ Failed to locate Python library (libpython)\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.debug.print("\n Possible solutions:\n", .{});
std.debug.print(" - Ensure Python development headers are installed:\n", .{});
std.debug.print(" • Ubuntu/Debian: sudo apt install python3-dev\n", .{});
std.debug.print(" • Fedora/RHEL: sudo dnf install python3-devel\n", .{});
std.debug.print(" • macOS: brew install python@3.11\n", .{});
std.debug.print(" - Verify Python executable: {s}\n", .{python_exe});
std.debug.print(" - Try specifying Python explicitly: zig build -Dpython-exe=/path/to/python\n", .{});
std.process.exit(1);
};
const hexversion = getPythonOutput(
b.allocator,
python_exe,
"import sys; print(f'{sys.hexversion:#010x}', end='')",
) catch |err| {
std.debug.print("\n❌ Failed to get Python version information\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.debug.print(" Python executable: {s}\n", .{python_exe});
std.debug.print(" Ensure Python is working correctly: {s} --version\n", .{python_exe});
std.process.exit(1);
};
var self = b.allocator.create(PyZ3Step) catch |err| {
std.debug.print("\n❌ Out of memory creating PyZ3Step\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.process.exit(1);
};
self.* = .{
.owner = b,
.allocator = b.allocator,
.options = options,
.test_build_step = test_build_step,
.generate_stubs = generate_stubs,
.check_stubs = check_stubs,
.python_exe = python_exe,
.libpython = libpython,
.hexversion = hexversion,
.ext_suffix = "",
.pyz3_source_file = "",
.ffi_header_file = "",
.numpy_ffi_header_file = "",
.numpy_include_dir = "",
.python_include_dir = "",
.python_library_dir = "",
};
// Eagerly run path discovery to work around ZLS support.
self.python_include_dir = self.pythonOutput(
"import os, sysconfig; print(os.path.relpath(sysconfig.get_path('include')), end='')",
) catch |err| {
std.debug.print("\n❌ Failed to get Python include directory\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.debug.print(" Python executable: {s}\n", .{python_exe});
std.debug.print(" This usually means Python's sysconfig module is not working correctly.\n", .{});
std.process.exit(1);
};
self.python_library_dir = self.pythonOutput(
"import os, sysconfig; print(os.path.relpath(sysconfig.get_config_var('LIBDIR')), end='')",
) catch |err| {
std.debug.print("\n❌ Failed to get Python library directory\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.debug.print(" Python executable: {s}\n", .{python_exe});
std.process.exit(1);
};
self.pyz3_source_file = self.pythonOutput(
"import pyz3; import os; print(os.path.relpath(os.path.join(os.path.dirname(pyz3.__file__), 'src/pyz3.zig')), end='')",
) catch |err| {
std.debug.print("\n❌ Failed to locate pyz3 source files\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.debug.print(" Ensure pyz3 package is installed: pip install pyz3\n", .{});
std.debug.print(" Or install in development mode: pip install -e .\n", .{});
std.process.exit(1);
};
self.ffi_header_file = self.pythonOutput(
"import pyz3; import os; print(os.path.abspath(os.path.join(os.path.dirname(pyz3.__file__), 'src/ffi.h')), end='')",
) catch |err| {
std.debug.print("\n❌ Failed to locate pyz3 FFI header file\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.debug.print(" Ensure pyz3 package is installed: pip install pyz3\n", .{});
std.debug.print(" Or install in development mode: pip install -e .\n", .{});
std.process.exit(1);
};
self.numpy_ffi_header_file = self.pythonOutput(
"import pyz3; import os; print(os.path.abspath(os.path.join(os.path.dirname(pyz3.__file__), 'src/numpy_ffi.h')), end='')",
) catch |err| {
std.debug.print("\n❌ Failed to locate pyz3 NumPy FFI header file\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.debug.print(" Ensure pyz3 package is installed: pip install pyz3\n", .{});
std.debug.print(" Or install in development mode: pip install -e .\n", .{});
std.process.exit(1);
};
self.numpy_include_dir = self.pythonOutput(
"import numpy; print(numpy.get_include(), end='')",
) catch "";
self.ext_suffix = self.pythonOutput(
"import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX') or '.so', end='')",
) catch |err| {
std.debug.print("\n❌ Failed to get Python extension suffix\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.debug.print(" Python executable: {s}\n", .{python_exe});
std.process.exit(1);
};
// Option for emitting test binary based on the given root source. This can be helpful for debugging.
const debugRoot = b.option(
[]const u8,
"debug-root",
"The root path of a file emitted as a binary for use with the debugger",
);
if (debugRoot) |root| {
const pyconf = b.addOptions();
pyconf.addOption([:0]const u8, "module_name", "debug");
pyconf.addOption(bool, "limited_api", false);
pyconf.addOption([]const u8, "hexversion", hexversion);
const testdebug = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path(root),
.target = b.resolveTargetQuery(.{}),
.optimize = .Debug,
}),
});
testdebug.root_module.addOptions("pyconf", pyconf);
const testdebug_module = b.createModule(.{
.root_source_file = b.path(self.pyz3_source_file),
.imports = &.{.{ .name = "pyconf", .module = pyconf.createModule() }},
});
testdebug_module.addIncludePath(b.path(self.python_include_dir));
testdebug.root_module.addImport("pyz3", testdebug_module);
testdebug.linkLibC();
testdebug.linkSystemLibrary(libpython);
testdebug.addLibraryPath(b.path(self.python_library_dir));
// Note: addLibraryPath automatically adds RPATH on macOS in Zig 0.15+
// Only add explicit RPATH for non-macOS (needed for miniconda on other platforms)
if (builtin.os.tag != .macos) {
testdebug.addRPath(b.path(self.python_library_dir));
}
const debugBin = b.addInstallBinFile(testdebug.getEmittedBin(), "debug.bin");
b.getInstallStep().dependOn(&debugBin.step);
}
return self;
}
/// Adds a pyZ3 Python module. The resulting library and test binaries can be further configured with
/// additional dependencies or modules.
pub fn addPythonModule(self: *PyZ3Step, options: PythonModuleOptions) PythonModule {
const b = self.owner;
const short_name = options.short_name();
const pyconf = b.addOptions();
pyconf.addOption([:0]const u8, "module_name", options.name);
pyconf.addOption(bool, "limited_api", options.limited_api);
pyconf.addOption([]const u8, "hexversion", self.hexversion);
// Configure and install the Python module shared library
const lib = b.addLibrary(.{
.name = short_name,
.linkage = .dynamic,
.root_module = b.createModule(.{
.root_source_file = options.root_source_file,
.target = b.resolveTargetQuery(options.target),
.optimize = options.optimize,
//.main_pkg_path = options.main_pkg_path,
}),
});
lib.root_module.addOptions("pyconf", pyconf);
const translate_c = self.addTranslateC(options);
translate_c.addIncludePath(b.path(self.python_include_dir));
const translate_numpy = self.addTranslateCNumpy(options);
translate_numpy.addIncludePath(b.path(self.python_include_dir));
if (self.numpy_include_dir.len > 0) {
translate_numpy.addIncludePath(.{ .cwd_relative = self.numpy_include_dir });
}
const lib_module = b.createModule(.{
.root_source_file = b.path(self.pyz3_source_file),
.imports = &.{
.{ .name = "pyconf", .module = pyconf.createModule() },
.{ .name = "ffi", .module = translate_c.createModule() },
.{ .name = "numpy_ffi", .module = translate_numpy.createModule() },
},
});
lib_module.addIncludePath(b.path(self.python_include_dir));
lib.root_module.addImport("pyz3", lib_module);
lib.linkLibC();
lib.linker_allow_shlib_undefined = true;
// Apply C/C++ integration configuration
for (options.c_include_dirs) |include_dir| {
lib.addIncludePath(b.path(include_dir));
lib_module.addIncludePath(b.path(include_dir));
}
for (options.c_sources) |c_source| {
lib.addCSourceFile(.{
.file = b.path(c_source),
.flags = options.c_flags,
});
}
for (options.c_libraries) |c_lib| {
lib.linkSystemLibrary(c_lib);
}
// Apply linker flags
for (options.ld_flags) |ld_flag| {
lib.root_module.addRPathSpecial(ld_flag);
}
// Install the shared library within the source tree (not zig-out/).
// Python's import mechanism expects extension modules to be co-located with
// Python source files (e.g., package/module.abi3.so), not in a separate build directory.
//
// We use .{ .custom = ".." } to install relative to the project root instead of zig-out/.
// This resolves: zig-out/../package/module.so → package/module.so
//
// This is intentional and aligns with Python build system conventions.
const install = b.addInstallFileWithDir(
lib.getEmittedBin(),
.{ .custom = ".." }, // Escape zig-out/ to install in project root
libraryDestRelPath(self.allocator, options, self.ext_suffix) catch |err| {
std.debug.print("\n❌ Out of memory computing library destination path\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.process.exit(1);
},
);
b.getInstallStep().dependOn(&install.step);
// Invoke stub generator on the emitted binary
const workingDir = std.fs.cwd().realpathAlloc(self.allocator, ".") catch |err| {
std.debug.print("\n❌ Failed to get current working directory\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.process.exit(1);
};
const genArgs: []const []const u8 = if (self.check_stubs)
&.{ self.python_exe, "-m", "pyz3.generate_stubs", options.name, workingDir, "--check" }
else
&.{ self.python_exe, "-m", "pyz3.generate_stubs", options.name, workingDir };
const stubs = b.addSystemCommand(genArgs);
stubs.step.dependOn(&install.step);
self.generate_stubs.dependOn(&stubs.step);
// Configure a test runner for the module
const libtest = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = options.root_source_file,
// .main_pkg_path = options.main_pkg_path,
.target = b.resolveTargetQuery(options.target),
.optimize = options.optimize,
}),
});
libtest.root_module.addOptions("pyconf", pyconf);
const libtest_module = b.createModule(.{
.root_source_file = b.path(self.pyz3_source_file),
.imports = &.{
.{ .name = "pyconf", .module = pyconf.createModule() },
.{ .name = "ffi", .module = translate_c.createModule() },
.{ .name = "numpy_ffi", .module = translate_numpy.createModule() },
},
});
libtest_module.addIncludePath(b.path(self.python_include_dir));
libtest.root_module.addImport("pyz3", libtest_module);
libtest.linkLibC();
libtest.linkSystemLibrary(self.libpython);
libtest.addLibraryPath(b.path(self.python_library_dir));
// Note: addLibraryPath automatically adds RPATH on macOS in Zig 0.15+
// Only add explicit RPATH for non-macOS (needed for miniconda on other platforms)
if (builtin.os.tag != .macos) {
libtest.addRPath(b.path(self.python_library_dir));
}
// Apply C/C++ integration configuration to test build
for (options.c_include_dirs) |include_dir| {
libtest.addIncludePath(b.path(include_dir));
libtest_module.addIncludePath(b.path(include_dir));
}
for (options.c_sources) |c_source| {
libtest.addCSourceFile(.{
.file = b.path(c_source),
.flags = options.c_flags,
});
}
for (options.c_libraries) |c_lib| {
libtest.linkSystemLibrary(c_lib);
}
for (options.ld_flags) |ld_flag| {
libtest.root_module.addRPathSpecial(ld_flag);
}
// Install the test binary
const install_libtest = b.addInstallBinFile(
libtest.getEmittedBin(),
testDestRelPath(self.allocator, short_name) catch |err| {
std.debug.print("\n❌ Out of memory computing test destination path\n", .{});
std.debug.print(" Error: {}\n", .{err});
std.process.exit(1);
},
);
self.test_build_step.dependOn(&install_libtest.step);
// Run the tests as part of zig build test.
if (self.options.test_step) |test_step| {
const run_libtest = b.addRunArtifact(libtest);
test_step.dependOn(&run_libtest.step);
}
return .{
.library_step = lib,
.test_step = libtest,
};
}
fn libraryDestRelPath(allocator: std.mem.Allocator, options: PythonModuleOptions, ext_suffix: []const u8) ![]const u8 {
const name = options.name;
// Use .abi3.so for limited API, platform-specific suffix otherwise
const suffix = if (options.limited_api) ".abi3.so" else ext_suffix;
const destPath = try allocator.alloc(u8, name.len + suffix.len);
// Take the module name, replace dots for slashes.
@memcpy(destPath[0..name.len], name);
std.mem.replaceScalar(u8, destPath[0..name.len], '.', '/');
// Append the suffix
@memcpy(destPath[name.len..], suffix);
return destPath;
}
fn testDestRelPath(allocator: std.mem.Allocator, short_name: []const u8) ![]const u8 {
const suffix = ".test.bin";
const destPath = try allocator.alloc(u8, short_name.len + suffix.len);
@memcpy(destPath[0..short_name.len], short_name);
@memcpy(destPath[short_name.len..], suffix);
return destPath;
}
fn pythonOutput(self: *PyZ3Step, code: []const u8) ![]const u8 {
return getPythonOutput(self.allocator, self.python_exe, code);
}
fn addTranslateC(self: PyZ3Step, options: PythonModuleOptions) *std.Build.Step.TranslateC {
const b = self.owner;
const translate_c = b.addTranslateC(.{
.root_source_file = .{ .cwd_relative = self.ffi_header_file },
.target = b.resolveTargetQuery(options.target),
.optimize = options.optimize,
});
if (options.limited_api)
translate_c.defineCMacro("Py_LIMITED_API", self.hexversion);
return translate_c;
}
fn addTranslateCNumpy(self: PyZ3Step, options: PythonModuleOptions) *std.Build.Step.TranslateC {
const b = self.owner;
const translate_numpy = b.addTranslateC(.{
.root_source_file = .{ .cwd_relative = self.numpy_ffi_header_file },
.target = b.resolveTargetQuery(options.target),
.optimize = options.optimize,
});
return translate_numpy;
}
};
fn getLibpython(allocator: std.mem.Allocator, python_exe: []const u8) ![]const u8 {
const ldlibrary = try getPythonOutput(
allocator,
python_exe,
"import sysconfig; print(sysconfig.get_config_var('LDLIBRARY'), end='')",
);
var libname = ldlibrary;
// Handle macOS Framework Python (e.g., "Python.framework/Versions/3.11/Python" or "Python")
if (std.mem.indexOf(u8, ldlibrary, ".framework/") != null) {
// Extract just the library name from framework path
// Python.framework/Versions/3.11/Python => Python
if (std.mem.lastIndexOfScalar(u8, ldlibrary, '/')) |last_slash| {
libname = ldlibrary[last_slash + 1 ..];
}
// Remove any extensions
const lastIdx = std.mem.lastIndexOfScalar(u8, libname, '.') orelse libname.len;
return libname[0..lastIdx];
}
// Strip libpython3.11.a.so => python3.11.a.so
if (libname.len >= 3 and std.mem.eql(u8, ldlibrary[0..3], "lib")) {
libname = libname[3..];
}
// Strip python3.11.a.so => python3.11.a
const lastIdx = std.mem.lastIndexOfScalar(u8, libname, '.') orelse libname.len;
libname = libname[0..lastIdx];
return libname;
}
fn getPythonOutput(allocator: std.mem.Allocator, python_exe: []const u8, code: []const u8) ![]const u8 {
const result = try runProcess(.{
.allocator = allocator,
.argv = &.{ python_exe, "-c", code },
});
if (result.term.Exited != 0) {
std.debug.print("Failed to execute {s}:\n{s}\n", .{ code, result.stderr });
std.process.exit(1);
}
allocator.free(result.stderr);
return result.stdout;
}
fn getStdOutput(allocator: std.mem.Allocator, argv: []const []const u8) ![]const u8 {
const result = try runProcess(.{ .allocator = allocator, .argv = argv });
if (result.term.Exited != 0) {
std.debug.print("Failed to execute {any}:\n{s}\n", .{ argv, result.stderr });
std.process.exit(1);
}
allocator.free(result.stderr);
return result.stdout;
}
const runProcess = if (builtin.zig_version.minor >= 12) std.process.Child.run else std.process.Child.exec;