-
Notifications
You must be signed in to change notification settings - Fork 20
/
build.zig
121 lines (106 loc) · 5.24 KB
/
build.zig
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
// 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.
const builtin = @import("builtin");
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const python_exe = b.option([]const u8, "python-exe", "Python executable to use") orelse "python";
const pythonInc = getPythonIncludePath(python_exe, b.allocator) catch @panic("Missing python");
const pythonLib = getPythonLibraryPath(python_exe, b.allocator) catch @panic("Missing python");
const pythonVer = getPythonLDVersion(python_exe, b.allocator) catch @panic("Missing python");
const pythonLibName = std.fmt.allocPrint(b.allocator, "python{s}", .{pythonVer}) catch @panic("Missing python");
const test_step = b.step("test", "Run library tests");
const docs_step = b.step("docs", "Generate docs");
// We never build this lib, but we use it to generate docs.
const pydust_lib = b.addSharedLibrary(.{
.name = "pydust",
.root_source_file = .{ .path = "pydust/src/pydust.zig" },
.main_pkg_path = .{ .path = "pydust/src" },
.target = target,
.optimize = optimize,
});
pydust_lib.addIncludePath(.{ .path = pythonInc });
pydust_lib.addAnonymousModule("pyconf", .{ .source_file = .{ .path = "./pyconf.dummy.zig" } });
const pydust_docs = b.addInstallDirectory(.{
.source_dir = pydust_lib.getEmittedDocs(),
// Emit the Zig docs into zig-out/../docs/zig
.install_dir = .{ .custom = "../docs" },
.install_subdir = "zig",
});
docs_step.dependOn(&pydust_docs.step);
const main_tests = b.addTest(.{
.root_source_file = .{ .path = "pydust/src/pydust.zig" },
.main_pkg_path = .{ .path = "pydust/src" },
.target = target,
.optimize = optimize,
});
main_tests.linkLibC();
main_tests.addIncludePath(.{ .path = pythonInc });
main_tests.addLibraryPath(.{ .path = pythonLib });
main_tests.linkSystemLibrary(pythonLibName);
main_tests.addRPath(.{ .path = pythonLib });
main_tests.addAnonymousModule("pyconf", .{ .source_file = .{ .path = "./pyconf.dummy.zig" } });
const run_main_tests = b.addRunArtifact(main_tests);
test_step.dependOn(&run_main_tests.step);
// Setup a library target to trick the Zig Language Server into providing completions for @import("pydust")
const example_lib = b.addSharedLibrary(.{
.name = "example",
.root_source_file = .{ .path = "example/hello.zig" },
.main_pkg_path = .{ .path = "example" },
.target = target,
.optimize = optimize,
});
example_lib.linkLibC();
example_lib.addIncludePath(.{ .path = pythonInc });
example_lib.addLibraryPath(.{ .path = pythonLib });
example_lib.linkSystemLibrary(pythonLibName);
example_lib.addRPath(.{ .path = pythonLib });
example_lib.addAnonymousModule("pydust", .{ .source_file = .{ .path = "pydust/src/pydust.zig" } });
example_lib.addAnonymousModule("pyconf", .{ .source_file = .{ .path = "./pyconf.dummy.zig" } });
// Option for emitting test binary based on the given root source.
// This is used for debugging as in .vscode/tasks.json
const test_debug_root = b.option([]const u8, "test-debug-root", "The root path of a file emitted as a binary for use with the debugger");
if (test_debug_root) |root| {
main_tests.root_src = .{ .path = root };
const test_bin_install = b.addInstallBinFile(main_tests.getEmittedBin(), "test.bin");
b.getInstallStep().dependOn(&test_bin_install.step);
}
}
fn getPythonIncludePath(
python_exe: []const u8,
allocator: std.mem.Allocator,
) ![]const u8 {
const includeResult = try runProcess(.{
.allocator = allocator,
.argv = &.{ python_exe, "-c", "import sysconfig; print(sysconfig.get_path('include'), end='')" },
});
defer allocator.free(includeResult.stderr);
return includeResult.stdout;
}
fn getPythonLibraryPath(python_exe: []const u8, allocator: std.mem.Allocator) ![]const u8 {
const includeResult = try runProcess(.{
.allocator = allocator,
.argv = &.{ python_exe, "-c", "import sysconfig; print(sysconfig.get_config_var('LIBDIR'), end='')" },
});
defer allocator.free(includeResult.stderr);
return includeResult.stdout;
}
fn getPythonLDVersion(python_exe: []const u8, allocator: std.mem.Allocator) ![]const u8 {
const includeResult = try runProcess(.{
.allocator = allocator,
.argv = &.{ python_exe, "-c", "import sysconfig; print(sysconfig.get_config_var('LDVERSION'), end='')" },
});
defer allocator.free(includeResult.stderr);
return includeResult.stdout;
}
const runProcess = if (builtin.zig_version.minor >= 12) std.process.Child.run else std.process.Child.exec;