-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
149 lines (131 loc) · 5.09 KB
/
Copy pathbuild.zig
File metadata and controls
149 lines (131 loc) · 5.09 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
149
const std = @import("std");
const sources = @import("src/bundled/sources.zig");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const system_gmp = b.option(
bool,
"system_gmp",
"Link the system libgmp (default) instead of bundling GMP from source",
) orelse true;
// The translate-c module and (for bundled mode) the generated-files
// directory and source dependency are produced together, since both the
// translate-c step and the libgmp compilation consume them.
const Bundled = struct {
c_module: *std.Build.Module,
dep: *std.Build.Dependency,
gen_dir: std.Build.LazyPath,
};
const bundled: ?Bundled = if (!system_gmp) blk: {
const dep = b.dependency("gmp_src", .{});
// One build-time program generates gmp.h (from gmp-h.in) and the seven
// table headers / two table sources, by compiling and running GMP's own
// gen-*.c programs with `zig cc`. This replaces running autoconf/m4.
const gen_exe = b.addExecutable(.{
.name = "gen_tables",
.root_module = b.createModule(.{
.root_source_file = b.path("src/bundled/gen_tables.zig"),
.target = b.graph.host,
.optimize = .Debug,
}),
});
const gen_run = b.addRunArtifact(gen_exe);
gen_run.addArg(b.graph.zig_exe);
gen_run.addDirectoryArg(dep.path("."));
gen_run.addFileArg(dep.path("gmp-h.in"));
const gen_dir = gen_run.addOutputDirectoryArg("generated");
const translate_c = b.addTranslateC(.{
.root_source_file = gen_dir.path(b, "gmp.h"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
break :blk Bundled{
.c_module = translate_c.createModule(),
.dep = dep,
.gen_dir = gen_dir,
};
} else null;
const c_module = if (system_gmp) blk: {
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("src/c.h"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
break :blk translate_c.createModule();
} else bundled.?.c_module;
const gmp = b.addModule("gmp", .{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.link_libc = true,
.imports = &.{
.{ .name = "c", .module = c_module },
},
});
if (system_gmp) {
gmp.linkSystemLibrary("gmp", .{});
} else if (bundled) |bs| {
// Include paths: committed config.h + gmp-mparam.h, the GMP source tree
// (gmp-impl.h, longlong.h, mpn/generic/*), and the generated tables.
gmp.addIncludePath(b.path("src/bundled"));
gmp.addIncludePath(bs.dep.path("."));
gmp.addIncludePath(bs.gen_dir);
const cflags = [_][]const u8{ "-DHAVE_CONFIG_H", "-D__GMP_WITHIN_GMP", "-std=gnu11" };
// 498 regular .c files (relative to the GMP source root).
gmp.addCSourceFiles(.{
.root = bs.dep.path("."),
.files = sources.regular,
.flags = &cflags,
});
// 16 templated .c files: each shared template compiled with its own
// -DOPERATION_<name> to select the routine it produces.
for (sources.templates) |t| {
const flags = [_][]const u8{
"-DHAVE_CONFIG_H",
"-D__GMP_WITHIN_GMP",
"-std=gnu11",
b.fmt("-DOPERATION_{s}", .{t.operation}),
};
gmp.addCSourceFile(.{
.file = bs.dep.path(t.src),
.flags = &flags,
});
}
// 2 generated table sources (fib_table.c, mp_bases.c).
gmp.addCSourceFile(.{ .file = bs.gen_dir.path(b, "fib_table.c"), .flags = &cflags });
gmp.addCSourceFile(.{ .file = bs.gen_dir.path(b, "mp_bases.c"), .flags = &cflags });
gmp.linkSystemLibrary("m", .{});
}
{ // Run tests
const tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("test/main.zig"),
.optimize = optimize,
.target = target,
.link_libc = true,
.imports = &.{
.{ .name = "gmp", .module = gmp },
},
}),
});
const run_tests = b.addRunArtifact(tests);
b.step("test", "Run GMP tests").dependOn(&run_tests.step);
}
{ // Run example
const exe = b.addExecutable(.{
.name = "gmp-example",
.root_module = b.createModule(.{
.root_source_file = b.path("example.zig"),
.optimize = optimize,
.target = target,
.link_libc = true,
.imports = &.{
.{ .name = "gmp", .module = gmp },
},
}),
});
const run_exe = b.addRunArtifact(exe);
b.step("run", "Run example").dependOn(&run_exe.step);
}
}