forked from google/cpu_features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
248 lines (223 loc) · 8.1 KB
/
build.zig
File metadata and controls
248 lines (223 loc) · 8.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const std = @import("std");
pub fn build(b: *std.Build) void {
// Standard target and optimization options
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Build options (mirroring CMake options)
const build_executable = b.option(bool, "BUILD_EXECUTABLE", "Build list_cpu_features executable") orelse true;
const enable_install = b.option(bool, "ENABLE_INSTALL", "Enable install targets") orelse true;
// Create the cpu_features static library
const cpu_features = b.addLibrary(.{
.name = "cpu_features",
.linkage = .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
cpu_features.addIncludePath(b.path("include"));
cpu_features.addIncludePath(b.path("include/internal"));
// Public compile definitions
cpu_features.root_module.addCMacro("STACK_LINE_READER_BUFFER_SIZE", "1024");
// Platform-specific defines
const os_tag = target.result.os.tag;
const cpu_arch = target.result.cpu.arch;
if (os_tag.isDarwin()) {
cpu_features.root_module.addCMacro("HAVE_SYSCTLBYNAME", "1");
} else if (os_tag == .linux) {
// Linux (including musl) provides getauxval() for hardware capability detection
cpu_features.root_module.addCMacro("HAVE_STRONG_GETAUXVAL", "1");
cpu_features.root_module.addCMacro("HAVE_DLFCN_H", "1");
}
// Utility sources (always included)
const utility_sources = [_][]const u8{
"src/filesystem.c",
"src/stack_line_reader.c",
"src/string_view.c",
};
// Common C flags for all source files
const c_flags = [_][]const u8{
"-Wall",
"-Wextra",
"-Wmissing-declarations",
"-Wmissing-prototypes",
"-Wno-implicit-fallthrough",
"-Wno-unused-function",
"-Wold-style-definition",
"-Wshadow",
"-Wsign-compare",
"-Wstrict-prototypes",
"-std=c99",
"-fno-sanitize=undefined", // Disable UBSan for C code with intentional unaligned access
};
for (utility_sources) |source| {
cpu_features.addCSourceFile(.{
.file = b.path(source),
.flags = &c_flags,
});
}
// Unix-based hardware detection (for non-x86 Unix platforms)
// Note: Android is represented as Linux in Zig's target system
if (os_tag != .windows and !cpu_arch.isX86()) {
const hwcaps_sources = [_][]const u8{
"src/hwcaps.c",
"src/hwcaps_linux_or_android.c",
"src/hwcaps_freebsd_or_openbsd.c",
};
for (hwcaps_sources) |source| {
cpu_features.addCSourceFile(.{
.file = b.path(source),
.flags = &c_flags,
});
}
}
// Architecture-specific implementation files
// Determine which implementation files to include based on target architecture
// Note: Android is represented as Linux in Zig's target system
switch (cpu_arch) {
.x86, .x86_64 => {
// x86/x86_64 architecture
const source: ?[]const u8 = if (os_tag == .linux)
"src/impl_x86_linux_or_android.c"
else if (os_tag.isDarwin())
"src/impl_x86_macos.c"
else if (os_tag == .windows)
"src/impl_x86_windows.c"
else if (os_tag == .freebsd)
"src/impl_x86_freebsd.c"
else
null;
if (source) |s| {
cpu_features.addCSourceFile(.{
.file = b.path(s),
.flags = &c_flags,
});
}
},
.aarch64, .aarch64_be => {
// AArch64 architecture - always needs cpuid
cpu_features.addCSourceFile(.{
.file = b.path("src/impl_aarch64_cpuid.c"),
.flags = &c_flags,
});
const source: ?[]const u8 = if (os_tag == .linux)
"src/impl_aarch64_linux_or_android.c"
else if (os_tag.isDarwin())
"src/impl_aarch64_macos_or_iphone.c"
else if (os_tag == .windows)
"src/impl_aarch64_windows.c"
else if (os_tag == .freebsd or os_tag == .openbsd)
"src/impl_aarch64_freebsd_or_openbsd.c"
else
null;
if (source) |s| {
cpu_features.addCSourceFile(.{
.file = b.path(s),
.flags = &c_flags,
});
}
},
.arm, .armeb, .thumb, .thumbeb => {
// ARM (32-bit) architecture
if (os_tag == .linux) {
cpu_features.addCSourceFile(.{
.file = b.path("src/impl_arm_linux_or_android.c"),
.flags = &c_flags,
});
}
},
.mips, .mipsel, .mips64, .mips64el => {
// MIPS architecture
if (os_tag == .linux) {
cpu_features.addCSourceFile(.{
.file = b.path("src/impl_mips_linux_or_android.c"),
.flags = &c_flags,
});
}
},
.powerpc, .powerpcle, .powerpc64, .powerpc64le => {
// PowerPC architecture
if (os_tag == .linux) {
cpu_features.addCSourceFile(.{
.file = b.path("src/impl_ppc_linux.c"),
.flags = &c_flags,
});
}
},
.riscv32, .riscv64 => {
// RISC-V architecture
if (os_tag == .linux) {
cpu_features.addCSourceFile(.{
.file = b.path("src/impl_riscv_linux.c"),
.flags = &c_flags,
});
}
},
.s390x => {
// s390x architecture
if (os_tag == .linux) {
cpu_features.addCSourceFile(.{
.file = b.path("src/impl_s390x_linux.c"),
.flags = &c_flags,
});
}
},
.loongarch64 => {
// LoongArch architecture
if (os_tag == .linux) {
cpu_features.addCSourceFile(.{
.file = b.path("src/impl_loongarch_linux.c"),
.flags = &c_flags,
});
}
},
else => {
std.debug.print("Warning: Unsupported architecture {s}\n", .{@tagName(cpu_arch)});
},
}
// Link against dl library on Unix-like systems
if (os_tag != .windows and os_tag != .wasi) {
cpu_features.linkSystemLibrary("dl");
}
// Install the library if enabled
if (enable_install) {
b.installArtifact(cpu_features);
// Install headers
const install_headers = b.addInstallDirectory(.{
.source_dir = b.path("include"),
.install_dir = .header,
.install_subdir = "cpu_features",
.exclude_extensions = &.{},
});
b.getInstallStep().dependOn(&install_headers.step);
}
// Build list_cpu_features executable if requested
if (build_executable) {
const list_cpu_features = b.addExecutable(.{
.name = "list_cpu_features",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
list_cpu_features.linkLibrary(cpu_features);
list_cpu_features.addIncludePath(b.path("include"));
list_cpu_features.addCSourceFile(.{
.file = b.path("src/utils/list_cpu_features.c"),
.flags = &c_flags,
});
if (enable_install) {
b.installArtifact(list_cpu_features);
}
// Add a run step for convenience
const run_cmd = b.addRunArtifact(list_cpu_features);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run list_cpu_features");
run_step.dependOn(&run_cmd.step);
}
}