diff --git a/build.zig b/build.zig index 258c4e8b..60f192bf 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.addLibrary(.{ .linkage = .dynamic, @@ -47,7 +39,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(.{ @@ -71,7 +62,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); @@ -93,7 +83,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 b18d72e2..aa04a11b 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 77a7c1a2..ad659b97 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 ca05426b..414f327c 100644 --- a/pydust/src/pydust.build.zig +++ b/pydust/src/pydust.build.zig @@ -180,14 +180,9 @@ pub const PydustStep = struct { }), }); 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); @@ -225,10 +220,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); @@ -290,18 +282,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 4c48b8fe..8a1a7f78 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 feb09c0f..39cf7a4b 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 63d136f9..c8b333da 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;