-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild.zig
More file actions
136 lines (117 loc) · 4.62 KB
/
build.zig
File metadata and controls
136 lines (117 loc) · 4.62 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
const std = @import("std");
const linux = @import("src/linux.zig");
const windows = @import("src/windows.zig");
const macos = @import("src/macos.zig");
const build_zon = @import("build.zig.zon");
const assert = std.debug.assert;
pub const sources = @import("src/sdl.zon");
pub const flags = &.{
"-fno-strict-aliasing",
"-fvisibility=hidden",
};
pub fn build(b: *std.Build) !void {
// Get the upstream source and build options
const upstream = b.dependency("sdl", .{});
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const default_target_config = b.option(
bool,
"default_target_config",
\\provides a default `SDL_build_config.h` and dependencies for the current target, defaults
\\to true
,
) orelse true;
const linkage = b.option(
std.builtin.LinkMode,
"linkage",
\\whether to build a static or dynamic library, defaults to static
,
) orelse .static;
// Get the so version. This is the same as the SDL version, but the major version is elided
// since it's baked into the name. This mirrors the official build process.
var sdl_so_version = comptime std.SemanticVersion.parse(build_zon.dependencies.sdl.version) catch unreachable;
assert(sdl_so_version.major == 3);
sdl_so_version.major = 0;
// Create the library
const lib = b.addLibrary(.{
.name = "SDL3",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
.linkage = linkage,
.version = sdl_so_version,
});
switch (linkage) {
.dynamic => {
lib.root_module.addCMacro("DLL_EXPORT", "1");
lib.setVersionScript(upstream.path("src/dynapi/SDL_dynapi.sym"));
},
.static => lib.root_module.addCMacro("SDL_STATIC_LIB", "1"),
}
lib.root_module.addCMacro("SDL_VENDOR_INFO", std.fmt.comptimePrint("\"{s} {s} (SDL {s})\"", .{
"https://github.com/allyourcodebase/SDL3",
build_zon.version,
build_zon.dependencies.sdl.version,
}));
lib.installHeadersDirectory(upstream.path("include/SDL3"), "SDL3", .{});
b.installArtifact(lib);
// Set the include path
lib.addIncludePath(upstream.path("include"));
lib.addIncludePath(upstream.path("src"));
// Compile the generic sources
lib.addCSourceFiles(.{
.files = &sources.generic,
.root = upstream.path("src"),
.flags = flags,
});
if (default_target_config) {
const build_config_h = b.addConfigHeader(.{
.style = .{ .cmake = upstream.path("include/build_config/SDL_build_config.h.cmake") },
.include_path = "SDL_build_config.h",
}, .{
// Don't allow including the default config
.USING_GENERATED_CONFIG_H = true,
// Generic audio drivers
.SDL_AUDIO_DRIVER_DUMMY = true,
.SDL_AUDIO_DRIVER_DISK = true,
// Generic video drivers
.SDL_VIDEO_DRIVER_DUMMY = true,
.SDL_VIDEO_DRIVER_OFFSCREEN = true,
// Set the assert level, this logic mirrors the default SDL options with release
// safe added.
// https://wiki.libsdl.org/SDL3/SDL_ASSERT_LEVEL
.SDL_DEFAULT_ASSERT_LEVEL_CONFIGURED = true,
.SDL_DEFAULT_ASSERT_LEVEL = switch (optimize) {
.Debug, .ReleaseSafe => @as(i64, 2),
.ReleaseSmall, .ReleaseFast => @as(i64, 1),
},
});
lib.addConfigHeader(build_config_h);
// Configure the build for the target platform
switch (target.result.os.tag) {
.linux => linux.build(b, target.result, lib, build_config_h),
.windows => windows.build(b, target.result, lib, build_config_h),
.macos => macos.build(b, target.result, lib, build_config_h),
else => @panic("target has no default config"),
}
}
// Add the Wayland scanner step
linux.addWaylandScannerStep(b);
// Add the example
const example = b.addExecutable(.{
.name = "example",
.root_module = b.createModule(.{
.root_source_file = b.path("src/example.zig"),
.target = target,
.optimize = optimize,
}),
});
example.linkLibrary(lib);
const build_example_step = b.step("example", "Build the example app");
build_example_step.dependOn(&example.step);
const run_example = b.addRunArtifact(example);
const run_step = b.step("run-example", "Run the example app");
run_step.dependOn(&run_example.step);
}