-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
148 lines (135 loc) · 5.8 KB
/
Copy pathbuild.zig
File metadata and controls
148 lines (135 loc) · 5.8 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
const std = @import("std");
pub const Phpz = @import("./build/Phpz.zig");
const BuildOptions = struct {
php_include_dir: []const u8,
php_lib_dir: ?[]const u8,
libc_file: ?[]const u8,
windows_zts: bool,
windows_debug: bool,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
fn getLibCFilePath(self: BuildOptions) ?std.Build.LazyPath {
return if (self.libc_file) |libc_file| .{ .cwd_relative = libc_file } else null;
}
};
pub fn build(b: *std.Build) void {
const options = BuildOptions{
.php_include_dir = b.option([]const u8, "php-include-dir", "PHP include directory (main/, Zend/, TSRM/, ext/)") orelse "/usr/include/php",
.php_lib_dir = b.option([]const u8, "php-lib-dir", "Windows only: required PHP SDK library directory containing php8*.lib"),
.windows_zts = b.option(bool, "windows-zts", "Windows only: link against the thread-safe PHP library") orelse false,
.windows_debug = b.option(bool, "windows-debug", "Windows only: build against a debug PHP SDK") orelse false,
.libc_file = b.option([]const u8, "libc-file", "Libc paths file for C translation and extension compilation"),
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
};
const mod = createPhpzModule(b, options);
b.default_step = addCheckStep(b, mod, options);
addGenerateDocsStep(b, mod, options);
addTestExamplesStep(b, options);
addTestStep(b, mod, options);
}
fn createPhpzModule(b: *std.Build, options: BuildOptions) *std.Build.Module {
return Phpz.initInner(b, .{
.translator = .{
.c_source_file = b.path("build/phpz.h"),
.target = options.target,
.optimize = options.optimize,
},
.libc_file = options.getLibCFilePath(),
.php_include_dir = .{ .cwd_relative = options.php_include_dir },
.php_lib_dir = if (options.php_lib_dir) |d| .{ .cwd_relative = d } else null,
.windows_zts = options.windows_zts,
.windows_debug = options.windows_debug,
}).mod;
}
fn addCheckStep(b: *std.Build, mod: *std.Build.Module, options: BuildOptions) *std.Build.Step {
const lib_check = b.addLibrary(.{
.name = "phpz",
.root_module = mod,
});
lib_check.setLibCFile(options.getLibCFilePath());
const check = b.step("check", "Check that phpz builds correctly");
check.dependOn(&lib_check.step);
return check;
}
fn addGenerateDocsStep(b: *std.Build, mod: *std.Build.Module, options: BuildOptions) void {
const doc_step = b.step("docs", "Generate documentation for phpz");
// Generate docs for main library (src/root.zig)
const doc_obj = b.addObject(.{
.name = "phpz",
.root_module = mod,
});
doc_obj.setLibCFile(options.getLibCFilePath());
const install_docs = b.addInstallDirectory(.{
.source_dir = doc_obj.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs/lib",
});
doc_step.dependOn(&install_docs.step);
// Generate docs for build system (build/Phpz.zig)
const build_doc_obj = b.addObject(.{
.name = "phpz-build",
.root_module = b.createModule(.{
.root_source_file = b.path("build/Phpz.zig"),
.target = mod.resolved_target,
.optimize = mod.optimize,
}),
});
build_doc_obj.setLibCFile(options.getLibCFilePath());
const install_build_docs = b.addInstallDirectory(.{
.source_dir = build_doc_obj.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs/build",
});
doc_step.dependOn(&install_build_docs.step);
}
fn addTestStep(b: *std.Build, mod: *std.Build.Module, options: BuildOptions) void {
const test_lib = b.addTest(.{
.root_module = mod,
});
test_lib.setLibCFile(options.getLibCFilePath());
const step = b.step("test", "Run unit tests");
step.dependOn(&test_lib.step);
for ([_][]const u8{
"tools/check_arginfo.zig",
"tools/windows_patcher.zig",
}) |path| {
const tool_tests = b.addTest(.{
.name = b.fmt("{s}-tests", .{std.fs.path.stem(path)}),
.root_module = b.createModule(.{
.root_source_file = b.path(path),
.target = b.graph.host,
.optimize = options.optimize,
}),
});
step.dependOn(&b.addRunArtifact(tool_tests).step);
}
}
fn addTestExamplesStep(b: *std.Build, options: BuildOptions) void {
const step = b.step("test-examples", "Run example tests for phpz");
const examples = [_][]const u8{
"skeleton",
"my_php_extension",
};
inline for (examples) |test_example| {
const test_cmd = b.addSystemCommand(&[_][]const u8{ "zig", "build", "run-tests" });
test_cmd.addArg(b.fmt("-Dphp-include-dir={s}", .{options.php_include_dir}));
test_cmd.addArg(b.fmt("-Doptimize={s}", .{@tagName(options.optimize)}));
if (options.libc_file) |libc_file| {
const absolute_path = std.fs.path.resolve(b.allocator, &.{libc_file}) catch @panic("OOM");
test_cmd.addArg(b.fmt("-Dlibc-file={s}", .{absolute_path}));
}
if (!options.target.query.isNativeTriple()) {
test_cmd.addArg(b.fmt("-Dtarget={s}", .{options.target.query.zigTriple(b.allocator) catch unreachable}));
}
if (options.target.result.os.tag == .windows) {
if (options.php_lib_dir) |lib_dir| {
test_cmd.addArg(b.fmt("-Dphp-lib-dir={s}", .{lib_dir}));
}
test_cmd.addArg(b.fmt("-Dwindows-zts={}", .{options.windows_zts}));
test_cmd.addArg(b.fmt("-Dwindows-debug={}", .{options.windows_debug}));
}
test_cmd.setCwd(b.path("examples").join(b.allocator, test_example) catch unreachable);
step.dependOn(&test_cmd.step);
}
}