Skip to content

Commit dd6239d

Browse files
committed
move logic to helpers
1 parent a45daea commit dd6239d

File tree

3 files changed

+86
-90
lines changed

3 files changed

+86
-90
lines changed

build.zig

+35
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn build(b: *std.Build) !void {
2121
});
2222

2323
const config_header = getConfigHeader(b, t);
24+
// b.addInstallHeaderFile(source: LazyPath, dest_rel_path: []const u8)
2425

2526
// zig build of python
2627
const libpython = try buildLibPython(b, target, optimize, config_header);
@@ -66,6 +67,17 @@ pub fn build(b: *std.Build) !void {
6667
run_step.dependOn(&run_cmd.step);
6768
examples_step.dependOn(&exe.step);
6869
}
70+
71+
const translate_step = b.step("translate", "Translates the c files");
72+
73+
const config_translate = translateFile(b, b.path("Include/cpython/initconfig.h"), target, optimize, config_header);
74+
const translate_install = b.addInstallDirectory(.{
75+
.source_dir = config_translate.getOutput(),
76+
.install_dir = .{ .custom = "translate" },
77+
.install_subdir = "headers",
78+
});
79+
80+
translate_step.dependOn(&translate_install.step);
6981
}
7082

7183
fn getModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode) *std.Build.Module {
@@ -103,6 +115,29 @@ fn addIncludes(
103115
step.addConfigHeader(config_header);
104116
}
105117

118+
fn translateFile(
119+
b: *std.Build,
120+
file: std.Build.LazyPath,
121+
target: std.Build.ResolvedTarget,
122+
optimize: std.builtin.OptimizeMode,
123+
config_header: *std.Build.Step.ConfigHeader,
124+
) *Step.TranslateC {
125+
const step = b.addTranslateC(.{
126+
.root_source_file = file,
127+
.target = target,
128+
.optimize = optimize,
129+
});
130+
131+
step.step.dependOn(&config_header.step);
132+
133+
step.addIncludeDir(b.path("Include/internal").getPath(b));
134+
step.addIncludeDir(b.path(".").getPath(b));
135+
step.addIncludeDir(b.path("Include").getPath(b));
136+
// step.addIncludeDir(config_header.output_file.getPath());
137+
138+
return step;
139+
}
140+
106141
fn buildCpython(
107142
b: *std.Build,
108143
target: std.Build.ResolvedTarget,

examples/simple_string.zig

+1-64
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,10 @@
11
const std = @import("std");
22
const py = @import("python");
33

4-
pub fn utf8ToUtf32Z(
5-
in: []const u8,
6-
allocator: std.mem.Allocator,
7-
) ![:0]const u32 {
8-
var buffer = std.ArrayList(u32).init(allocator);
9-
for (in) |char| {
10-
try buffer.append(char);
11-
}
12-
return buffer.toOwnedSliceSentinel(0);
13-
}
14-
15-
fn init_from_config(config: *py.types.PyConfig) !void {
16-
const status = py.externs.Py_InitializeFromConfig(config);
17-
18-
// std.debug.print("END STATUS: {}\n", .{&status});
19-
20-
if (py.externs.PyStatus_Exception(status)) {
21-
return error.Expection;
22-
// py.externs.Py_ExitStatusException(status);
23-
}
24-
}
25-
26-
// https://github.com/Rexicon226/osmium/blob/e83ac667e006cf3a233c1868f76e57b155ba1739/src/frontend/Python.zig#L72
27-
pub fn Initialize(
28-
allocator: std.mem.Allocator,
29-
) !void {
30-
// _ = allocator;
31-
var config: py.types.PyConfig = undefined;
32-
py.externs.PyConfig_InitIsolatedConfig(&config);
33-
defer py.externs.PyConfig_Clear(&config);
34-
35-
var status = py.externs.PyConfig_SetBytesString(
36-
&config,
37-
&config.program_name,
38-
"./test",
39-
);
40-
41-
if (py.externs.PyStatus_Exception(status)) {
42-
py.externs.Py_ExitStatusException(status);
43-
}
44-
45-
const utf32_path = try utf8ToUtf32Z(
46-
py.constants.LIB_PATH,
47-
allocator,
48-
);
49-
50-
// need to set the search path to the python "Lib" folder
51-
// https://docs.python.org/3/c-api/init_config.html#python-path-configuration
52-
config.module_search_paths_set = 1;
53-
status = py.externs.PyWideStringList_Append(
54-
&config.module_search_paths,
55-
utf32_path.ptr,
56-
);
57-
58-
if (py.externs.PyStatus_Exception(status)) {
59-
py.externs.Py_ExitStatusException(status);
60-
}
61-
62-
try init_from_config(&config);
63-
64-
py.externs.PyConfig_Clear(&config);
65-
}
66-
674
pub fn main() !void {
685
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
696
const allocator = gpa.allocator();
70-
try Initialize(allocator);
7+
try py.helpers.Initialize(allocator);
718
defer py.externs.Py_Finalize();
729

7310
py.externs.PyRun_SimpleString(

src/helpers.zig

+50-26
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,56 @@
11
const externs = @import("externs.zig");
2+
const types = @import("types.zig");
23
const constants = @import("constants.zig");
34

4-
pub fn Sys_SetPath(path: [:0]const u16) void {
5-
externs.PySys_SetPath(path.ptr);
5+
const std = @import("std");
6+
7+
pub fn utf8ToUtf32Z(
8+
in: []const u8,
9+
allocator: std.mem.Allocator,
10+
) ![:0]const u32 {
11+
var buffer = std.ArrayList(u32).init(allocator);
12+
for (in) |char| {
13+
try buffer.append(char);
14+
}
15+
return buffer.toOwnedSliceSentinel(0);
616
}
717

8-
pub fn SetProgramName(name: [:0]const u8) void {
9-
externs.Py_SetProgramName(name.ptr);
10-
}
11-
12-
pub fn CompileString(source: [:0]const u8, filename: [:0]const u8) ?*anyopaque {
13-
return externs.Py_CompileString(source.ptr, filename.ptr, constants.Py_file_input);
14-
}
15-
16-
pub fn Marshal_WriteObjectToString(code: ?*anyopaque) ?*anyopaque {
17-
return externs.PyMarshal_WriteObjectToString(code, constants.Py_MARSHAL_VERSION);
18-
}
19-
20-
pub fn Bytes_Size(code: ?*anyopaque) usize {
21-
return externs.PyBytes_Size(code);
22-
}
23-
24-
pub fn Bytes_AsString(code: ?*anyopaque) ?[*:0]u8 {
25-
return externs.PyBytes_AsString(code);
26-
}
27-
28-
pub fn PrintError() void {
29-
externs.PyErr_Print();
30-
31-
// TODO: fetch and normalize here
18+
pub fn Initialize(
19+
allocator: std.mem.Allocator,
20+
) !void {
21+
var config: types.PyConfig = undefined;
22+
externs.PyConfig_InitIsolatedConfig(&config);
23+
defer externs.PyConfig_Clear(&config);
24+
25+
var status = externs.PyConfig_SetBytesString(
26+
&config,
27+
&config.program_name,
28+
"./test",
29+
);
30+
31+
if (externs.PyStatus_Exception(status)) {
32+
externs.Py_ExitStatusException(status);
33+
}
34+
35+
const utf32_path = try utf8ToUtf32Z(
36+
constants.LIB_PATH,
37+
allocator,
38+
);
39+
40+
// need to set the search path to the python "Lib" folder
41+
// https://docs.python.org/3/c-api/init_config.html#python-path-configuration
42+
config.module_search_paths_set = 1;
43+
status = externs.PyWideStringList_Append(
44+
&config.module_search_paths,
45+
utf32_path.ptr,
46+
);
47+
48+
if (externs.PyStatus_Exception(status)) {
49+
externs.Py_ExitStatusException(status);
50+
}
51+
52+
status = externs.Py_InitializeFromConfig(&config);
53+
if (externs.PyStatus_Exception(status)) {
54+
externs.Py_ExitStatusException(status);
55+
}
3256
}

0 commit comments

Comments
 (0)