構成
build.zig
c
src/main.zig
実行
zig build run
| const std = @import("std"); | |
| pub fn build(b: *std.Build) void { | |
| const target = b.standardTargetOptions(.{}); | |
| const optimize = b.standardOptimizeOption(.{}); | |
| const exe = b.addExecutable(.{ | |
| .name = "zig-embed-php", | |
| .root_module = b.createModule(.{ | |
| .root_source_file = b.path("src/main.zig"), | |
| .target = target, | |
| .optimize = optimize, | |
| }), | |
| }); | |
| exe.addIncludePath(b.path("c")); | |
| exe.addIncludePath(.{ .cwd_relative = "/usr/include/php/20250925" }); | |
| exe.addIncludePath(.{ .cwd_relative = "/usr/include/php/20250925/main" }); | |
| exe.addIncludePath(.{ .cwd_relative = "/usr/include/php/20250925/TSRM" }); | |
| exe.addIncludePath(.{ .cwd_relative = "/usr/include/php/20250925/Zend" }); | |
| exe.addIncludePath(.{ .cwd_relative = "/usr/include/php/20250925/ext" }); | |
| exe.addIncludePath(.{ .cwd_relative = "/usr/include/php/20250925/ext/date/lib" }); | |
| exe.addCSourceFile(.{ | |
| .file = b.path("c/php_embed_wrap.c"), | |
| .flags = &.{"-std=c11"}, | |
| }); | |
| exe.linkSystemLibrary("php"); | |
| exe.linkLibC(); | |
| b.installArtifact(exe); | |
| const run_cmd = b.addRunArtifact(exe); | |
| run_cmd.step.dependOn(b.getInstallStep()); | |
| if (b.args) |args| { | |
| run_cmd.addArgs(args); | |
| } | |
| const run_step = b.step("run", "Run the application"); | |
| run_step.dependOn(&run_cmd.step); | |
| } |
| const std = @import("std"); | |
| const c = @cImport({ | |
| @cInclude("php_embed_wrap.h"); | |
| }); | |
| pub fn main() !void { | |
| var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| defer _ = gpa.deinit(); | |
| const allocator = gpa.allocator(); | |
| const args = try std.process.argsAlloc(allocator); | |
| defer std.process.argsFree(allocator, args); | |
| var c_argv = try allocator.alloc(?[*:0]u8, args.len); | |
| defer allocator.free(c_argv); | |
| for (args, 0..) |arg, i| { | |
| c_argv[i] = @constCast(arg.ptr); | |
| } | |
| const init_rc = c.zig_php_init( | |
| @intCast(args.len), | |
| @ptrCast(c_argv.ptr), | |
| ); | |
| if (init_rc != 0) { | |
| std.log.err("php init failed: {d}", .{init_rc}); | |
| return error.PhpInitFailed; | |
| } | |
| defer c.zig_php_shutdown(); | |
| const php_code = "echo \"Hello from embedded PHP\\n\";"; | |
| //const php_code = "print(\"hello\\n\");"; | |
| std.debug.print("PHP code:\n{s}\n", .{php_code}); | |
| const eval_rc = c.zig_php_eval(php_code); | |
| if (eval_rc != 0) { | |
| std.log.err("php eval failed: {d}", .{eval_rc}); | |
| return error.PhpEvalFailed; | |
| } | |
| } |
| #include <php.h> | |
| #include <Zend/zend_exceptions.h> | |
| #include <sapi/embed/php_embed.h> | |
| int zig_php_init(int argc, char **argv) { | |
| return php_embed_init(argc, argv); | |
| } | |
| int zig_php_eval(const char *code) { | |
| if (zend_eval_string_ex((char *)code, NULL, "zig-eval", 1) == FAILURE) { | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| void zig_php_shutdown(void) { | |
| php_embed_shutdown(); | |
| } |
| #ifndef PHP_EMBED_WRAP_H | |
| #define PHP_EMBED_WRAP_H | |
| #ifdef __cplusplus | |
| extern "C" { | |
| #endif | |
| int zig_php_init(int argc, char **argv); | |
| int zig_php_eval(const char *code); | |
| void zig_php_shutdown(void); | |
| #ifdef __cplusplus | |
| } | |
| #endif | |
| #endif |