From 6dad5dd50edc531677ef814bc8a0b7a1ab601307 Mon Sep 17 00:00:00 2001 From: ivansantiagojr Date: Mon, 1 Sep 2025 23:22:15 -0300 Subject: [PATCH] Revert the addition of translate c usage This because translate-c does not work with files outside of the source directory, and pydust.build.zig depends on it --- build.zig | 11 ----------- pydust/src/builtins.zig | 2 +- pydust/src/ffi.h | 20 -------------------- pydust/src/ffi.zig | 22 ++++++++++++++++++++++ pydust/src/functions.zig | 2 +- pydust/src/mem.zig | 2 +- pydust/src/modules.zig | 2 +- pydust/src/pydust.build.zig | 24 ++---------------------- pydust/src/pydust.zig | 2 +- pydust/src/pytypes.zig | 2 +- pydust/src/trampoline.zig | 2 +- pydust/src/types/error.zig | 2 +- pydust/src/types/module.zig | 2 +- pydust/src/types/obj.zig | 2 +- 14 files changed, 34 insertions(+), 63 deletions(-) delete mode 100644 pydust/src/ffi.h create mode 100644 pydust/src/ffi.zig diff --git a/build.zig b/build.zig index c740fe0b..d93a2185 100644 --- a/build.zig +++ b/build.zig @@ -27,14 +27,6 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Run library tests"); const docs_step = b.step("docs", "Generate docs"); - const translate_c = b.addTranslateC(.{ - .root_source_file = b.path("pydust/src/ffi.h"), - .target = target, - .optimize = optimize, - }); - translate_c.defineCMacro("Py_LIMITED_API", "0x030D0000"); - translate_c.addIncludePath(.{ .cwd_relative = pythonInc }); - // We never build this lib, but we use it to generate docs. const pydust_lib = b.addSharedLibrary(.{ .name = "pydust", @@ -44,7 +36,6 @@ pub fn build(b: *std.Build) void { }); const pydust_lib_mod = b.createModule(.{ .root_source_file = b.path("./pyconf.dummy.zig") }); pydust_lib_mod.addIncludePath(.{ .cwd_relative = pythonInc }); - pydust_lib.root_module.addImport("ffi", translate_c.createModule()); pydust_lib.root_module.addImport("pyconf", pydust_lib_mod); const pydust_docs = b.addInstallDirectory(.{ @@ -66,7 +57,6 @@ pub fn build(b: *std.Build) void { main_tests.addRPath(.{ .cwd_relative = pythonLib }); const main_tests_mod = b.createModule(.{ .root_source_file = b.path("./pyconf.dummy.zig") }); main_tests_mod.addIncludePath(.{ .cwd_relative = pythonInc }); - main_tests.root_module.addImport("ffi", translate_c.createModule()); main_tests.root_module.addImport("pyconf", main_tests_mod); const run_main_tests = b.addRunArtifact(main_tests); @@ -85,7 +75,6 @@ pub fn build(b: *std.Build) void { example_lib.addRPath(.{ .cwd_relative = pythonLib }); const example_lib_mod = b.createModule(.{ .root_source_file = b.path("pydust/src/pydust.zig") }); example_lib_mod.addIncludePath(.{ .cwd_relative = pythonInc }); - example_lib.root_module.addImport("ffi", translate_c.createModule()); example_lib.root_module.addImport("pydust", example_lib_mod); example_lib.root_module.addImport( "pyconf", diff --git a/pydust/src/builtins.zig b/pydust/src/builtins.zig index 0296f2d7..c8ff3c9b 100644 --- a/pydust/src/builtins.zig +++ b/pydust/src/builtins.zig @@ -18,7 +18,7 @@ const std = @import("std"); const py = @import("./pydust.zig"); const pytypes = @import("./pytypes.zig"); const State = @import("./discovery.zig").State; -const ffi = @import("ffi"); +const ffi = @import("ffi.zig").ffi; const PyError = @import("./errors.zig").PyError; /// Zig enum for python richcompare op int. diff --git a/pydust/src/ffi.h b/pydust/src/ffi.h deleted file mode 100644 index 121c0404..00000000 --- a/pydust/src/ffi.h +++ /dev/null @@ -1,20 +0,0 @@ -// 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. - -// Export the Limited Python C API for use within PyDust. - -#define PY_SSIZE_T_CLEAN -#include - -// From 3.12 onwards, structmember.h is fixed to be including in Python.h -// See https://github.com/python/cpython/pull/99014 -#include diff --git a/pydust/src/ffi.zig b/pydust/src/ffi.zig new file mode 100644 index 00000000..a10ce754 --- /dev/null +++ b/pydust/src/ffi.zig @@ -0,0 +1,22 @@ +// limitations under the License. + +// Export the Limited Python C API for use within PyDust. + +const pyconf = @import("pyconf"); + +pub const ffi = @cImport({ + if (pyconf.limited_api) { + @cDefine("Py_LIMITED_API", pyconf.hexversion); + } + + @cDefine("PY_SSIZE_T_CLEAN", {}); + + @cInclude("Python.h"); + + // From 3.12 onwards, structmember.h is fixed to be including in Python.h + + // See https://github.com/python/cpython/pull/99014 + + @cInclude("structmember.h"); +}); + diff --git a/pydust/src/functions.zig b/pydust/src/functions.zig index 6e8c1655..18bd7e30 100644 --- a/pydust/src/functions.zig +++ b/pydust/src/functions.zig @@ -11,7 +11,7 @@ // limitations under the License. const std = @import("std"); -const ffi = @import("ffi"); +const ffi = @import("ffi.zig").ffi; const py = @import("pydust.zig"); const tramp = @import("trampoline.zig"); const State = @import("discovery.zig").State; diff --git a/pydust/src/mem.zig b/pydust/src/mem.zig index 23ac27b2..dcc75a6a 100644 --- a/pydust/src/mem.zig +++ b/pydust/src/mem.zig @@ -13,7 +13,7 @@ const std = @import("std"); const mem = std.mem; const Allocator = std.mem.Allocator; -const ffi = @import("ffi"); +const ffi = @import("ffi.zig").ffi; const py = @import("./pydust.zig"); pub const PyMemAllocator = struct { diff --git a/pydust/src/modules.zig b/pydust/src/modules.zig index b7f23b08..a1453151 100644 --- a/pydust/src/modules.zig +++ b/pydust/src/modules.zig @@ -12,7 +12,7 @@ const std = @import("std"); const State = @import("discovery.zig").State; -const ffi = @import("ffi"); +const ffi = @import("ffi.zig").ffi; const py = @import("pydust.zig"); const PyError = py.PyError; const Attributes = @import("attributes.zig").Attributes; diff --git a/pydust/src/pydust.build.zig b/pydust/src/pydust.build.zig index f43e4d5e..8de99f83 100644 --- a/pydust/src/pydust.build.zig +++ b/pydust/src/pydust.build.zig @@ -175,14 +175,9 @@ pub const PydustStep = struct { //.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 lib_module = b.createModule(.{ .root_source_file = b.path(self.pydust_source_file), - .imports = &.{ - .{ .name = "pyconf", .module = pyconf.createModule() }, - .{ .name = "ffi", .module = translate_c.createModule() }, - }, + .imports = &.{.{ .name = "pyconf", .module = pyconf.createModule() }}, }); lib_module.addIncludePath(b.path(self.python_include_dir)); lib.root_module.addImport("pydust", lib_module); @@ -218,10 +213,7 @@ pub const PydustStep = struct { libtest.root_module.addOptions("pyconf", pyconf); const libtest_module = b.createModule(.{ .root_source_file = b.path(self.pydust_source_file), - .imports = &.{ - .{ .name = "pyconf", .module = pyconf.createModule() }, - .{ .name = "ffi", .module = translate_c.createModule() }, - }, + .imports = &.{.{ .name = "pyconf", .module = pyconf.createModule() }}, }); libtest_module.addIncludePath(b.path(self.python_include_dir)); libtest.root_module.addImport("pydust", libtest_module); @@ -283,18 +275,6 @@ pub const PydustStep = struct { fn pythonOutput(self: *PydustStep, code: []const u8) ![]const u8 { return getPythonOutput(self.allocator, self.python_exe, code); } - - fn addTranslateC(self: PydustStep, options: PythonModuleOptions) *std.Build.Step.TranslateC { - const b = self.owner; - const translate_c = b.addTranslateC(.{ - .root_source_file = b.path("pydust/src/ffi.h"), - .target = b.resolveTargetQuery(options.target), - .optimize = options.optimize, - }); - if (options.limited_api) - translate_c.defineCMacro("Py_LIMITED_API", self.hexversion); - return translate_c; - } }; fn getLibpython(allocator: std.mem.Allocator, python_exe: []const u8) ![]const u8 { diff --git a/pydust/src/pydust.zig b/pydust/src/pydust.zig index 67f5fd41..afb7e091 100644 --- a/pydust/src/pydust.zig +++ b/pydust/src/pydust.zig @@ -153,7 +153,7 @@ pub const ValueError = err.ValueError; pub const Warning = err.Warning; pub const WindowsError = err.WindowsError; pub const ZeroDivisionError = err.ZeroDivisionError; -pub const ffi = @import("ffi"); +pub const ffi = @import("ffi.zig").ffi; pub const PyError = @import("errors.zig").PyError; pub const allocator: std.mem.Allocator = mem.PyMemAllocator.allocator(); diff --git a/pydust/src/pytypes.zig b/pydust/src/pytypes.zig index 68a831b4..29e5d14a 100644 --- a/pydust/src/pytypes.zig +++ b/pydust/src/pytypes.zig @@ -13,7 +13,7 @@ // https://docs.python.org/3/extending/newtypes_tutorial.html const std = @import("std"); -const ffi = @import("ffi"); +const ffi = @import("ffi.zig").ffi; const py = @import("pydust.zig"); const discovery = @import("discovery.zig"); const Attributes = @import("attributes.zig").Attributes; diff --git a/pydust/src/trampoline.zig b/pydust/src/trampoline.zig index 9092db07..d4ca327c 100644 --- a/pydust/src/trampoline.zig +++ b/pydust/src/trampoline.zig @@ -13,7 +13,7 @@ /// Utilities for bouncing CPython calls into Zig functions and back again. const std = @import("std"); const Type = std.builtin.Type; -const ffi = @import("ffi"); +const ffi = @import("ffi.zig").ffi; const py = @import("pydust.zig"); const State = @import("discovery.zig").State; const funcs = @import("functions.zig"); diff --git a/pydust/src/types/error.zig b/pydust/src/types/error.zig index 9b3a864f..b492a4e0 100644 --- a/pydust/src/types/error.zig +++ b/pydust/src/types/error.zig @@ -12,7 +12,7 @@ const builtin = @import("builtin"); const std = @import("std"); -const ffi = @import("ffi"); +const ffi = @import("../ffi.zig").ffi; const py = @import("../pydust.zig"); const PyError = @import("../errors.zig").PyError; const State = @import("../discovery.zig").State; diff --git a/pydust/src/types/module.zig b/pydust/src/types/module.zig index c472d927..0642b38a 100644 --- a/pydust/src/types/module.zig +++ b/pydust/src/types/module.zig @@ -13,7 +13,7 @@ const std = @import("std"); const Allocator = @import("std").mem.Allocator; const mem = @import("../mem.zig"); -const ffi = @import("ffi"); +const ffi = @import("../ffi.zig").ffi; const py = @import("../pydust.zig"); const PyObjectMixin = @import("./obj.zig").PyObjectMixin; const pytypes = @import("../pytypes.zig"); diff --git a/pydust/src/types/obj.zig b/pydust/src/types/obj.zig index 0d6fd747..b05b404d 100644 --- a/pydust/src/types/obj.zig +++ b/pydust/src/types/obj.zig @@ -11,7 +11,7 @@ // limitations under the License. const std = @import("std"); -const ffi = @import("ffi"); +const ffi = @import("../ffi.zig").ffi; const py = @import("../pydust.zig"); const PyError = @import("../errors.zig").PyError; const State = @import("../discovery.zig").State;