build.zig: Add ability to install a shared library

Initial build.zig only installed a static library. This is fine for
Zig itself but not sufficient for other ecosystems.

Current patch doesn't break backwards compatibility but does a little
refactoring where the old function addRaylib uses the newly added
function addRaylibTo, which is more flexible. In fact, addRaylib is an
alias for addStaticRaylib. addRaylib still generates a static library,
whereas the newly added addSharedRaylib generates a shared library.

Additionally, since a shared library is also a common artifact,
build.zig also installs it to a default location of
zig-out/lib/libraylib.dll.

Example code how to generate a shared library artifact in Zig:

    const libraylib = raylib.addSharedRaylib(b, target, optimize, .{});

    // Define a USE_LIBTYPE_SHARED macro for your project that is used
    // in raylib.h when using a shared library. Your project must use
    // raylib.h in order for this macro to work. It is not necessary
    // but saves performance when using on Windows. For more see:
    // https://github.com/raysan5/raylib/pull/3680#issuecomment-1870644148
    my_project.defineCMacro("USE_LIBTYPE_SHARED", null);
This commit is contained in:
Dmitry Matveyev 2023-12-27 21:53:45 +03:00
parent e46b6147fc
commit 334f34e040

View File

@ -1,152 +1,215 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
// This has been tested to work with zig 0.11.0 and zig 0.12.0-dev.1390+94cee4fb2 // This has been tested to work with zig 0.11.0 and zig 0.12.0-dev.1834+f36ac227b
pub fn addRaylib(b: *std.Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode, options: Options) *std.Build.CompileStep { pub fn addRaylib(
const raylib_flags = &[_][]const u8{ b: *std.Build,
"-std=gnu99", target: std.zig.CrossTarget,
"-D_GNU_SOURCE", optimize: std.builtin.OptimizeMode,
"-DGL_SILENCE_DEPRECATION=199309L", options: Options,
}; ) *std.Build.CompileStep {
return addStaticRaylib(b, target, optimize, options);
}
const raylib = b.addStaticLibrary(.{ pub fn addStaticRaylib(
b: *std.Build,
target: std.zig.CrossTarget,
optimize: std.builtin.OptimizeMode,
options: Options,
) *std.Build.CompileStep {
const lib = b.addStaticLibrary(.{
.name = "raylib", .name = "raylib",
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
raylib.linkLibC(); if (addRaylibTo(b, lib, target, options)) {} else |err| switch (err) {
error.OutOfMemory => {
std.debug.print("Not enough memory!", .{});
std.os.exit(1);
},
}
return lib;
}
pub fn addSharedRaylib(
b: *std.Build,
target: std.zig.CrossTarget,
optimize: std.builtin.OptimizeMode,
options: Options,
) *std.Build.CompileStep {
const lib = b.addSharedLibrary(.{
.name = "raylib",
.target = target,
.optimize = optimize,
});
lib.defineCMacro("BUILD_LIBTYPE_SHARED", null);
if (addRaylibTo(b, lib, target, options)) {} else |err| switch (err) {
error.OutOfMemory => {
std.debug.print("Not enough memory!", .{});
std.os.exit(1);
},
}
return lib;
}
/// Generic function for an either static, shared or object file that is passed to the `lib`
/// variable, see `addStaticRaylib` for an example.
fn addRaylibTo(
b: *std.Build,
lib: *std.Build.CompileStep,
target: std.zig.CrossTarget,
options: Options,
) !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const common_flags = &[_][]const u8{
"-std=gnu99",
"-D_GNU_SOURCE",
"-DGL_SILENCE_DEPRECATION=199309L",
};
var raylib_flags = std.ArrayList([]const u8).init(allocator);
try raylib_flags.appendSlice(common_flags);
if (lib.linkage == .dynamic) {
switch (target.getOsTag()) {
.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .macos => {
try raylib_flags.append("-fvisibility=hidden");
},
else => {},
}
}
var raylib_flags_no_sanitize = try raylib_flags.clone();
try raylib_flags_no_sanitize.append("-fno-sanitize=undefined");
lib.linkLibC();
// No GLFW required on PLATFORM_DRM // No GLFW required on PLATFORM_DRM
if (!options.platform_drm) { if (!options.platform_drm) {
raylib.addIncludePath(.{ .path = srcdir ++ "/external/glfw/include" }); lib.addIncludePath(.{ .path = srcdir ++ "/external/glfw/include" });
} }
addCSourceFilesVersioned(raylib, &.{ addCSourceFilesVersioned(lib, &.{
srcdir ++ "/rcore.c", srcdir ++ "/rcore.c",
srcdir ++ "/utils.c", srcdir ++ "/utils.c",
}, raylib_flags); }, raylib_flags.items);
if (options.raudio) { if (options.raudio) {
addCSourceFilesVersioned(raylib, &.{ addCSourceFilesVersioned(lib, &.{
srcdir ++ "/raudio.c", srcdir ++ "/raudio.c",
}, &[_][]const u8{ }, raylib_flags_no_sanitize.items); // https://github.com/raysan5/raylib/issues/3674
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/3674
} ++ raylib_flags);
} }
if (options.rmodels) { if (options.rmodels) {
addCSourceFilesVersioned(raylib, &.{ addCSourceFilesVersioned(lib, &.{
srcdir ++ "/rmodels.c", srcdir ++ "/rmodels.c",
}, &[_][]const u8{ }, raylib_flags_no_sanitize.items); // https://github.com/raysan5/raylib/issues/1891
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/1891
} ++ raylib_flags);
} }
if (options.rshapes) { if (options.rshapes) {
addCSourceFilesVersioned(raylib, &.{ addCSourceFilesVersioned(lib, &.{
srcdir ++ "/rshapes.c", srcdir ++ "/rshapes.c",
}, raylib_flags); }, raylib_flags.items);
} }
if (options.rtext) { if (options.rtext) {
addCSourceFilesVersioned(raylib, &.{ addCSourceFilesVersioned(lib, &.{
srcdir ++ "/rtext.c", srcdir ++ "/rtext.c",
}, raylib_flags); }, raylib_flags.items);
} }
if (options.rtextures) { if (options.rtextures) {
addCSourceFilesVersioned(raylib, &.{ addCSourceFilesVersioned(lib, &.{
srcdir ++ "/rtextures.c", srcdir ++ "/rtextures.c",
}, &[_][]const u8{ }, raylib_flags_no_sanitize.items); // https://github.com/raysan5/raylib/issues/3674
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/3674
} ++ raylib_flags);
} }
var gen_step = b.addWriteFiles(); var gen_step = b.addWriteFiles();
raylib.step.dependOn(&gen_step.step); lib.step.dependOn(&gen_step.step);
if (options.raygui) { if (options.raygui) {
const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n"); const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
raylib.addCSourceFile(.{ .file = raygui_c_path, .flags = raylib_flags }); lib.addCSourceFile(.{ .file = raygui_c_path, .flags = raylib_flags.items });
raylib.addIncludePath(.{ .path = srcdir }); lib.addIncludePath(.{ .path = srcdir });
raylib.addIncludePath(.{ .path = srcdir ++ "/../../raygui/src" }); lib.addIncludePath(.{ .path = srcdir ++ "/../../raygui/src" });
} }
switch (target.getOsTag()) { switch (target.getOsTag()) {
.windows => { .windows => {
addCSourceFilesVersioned(raylib, &.{ addCSourceFilesVersioned(lib, &.{
srcdir ++ "/rglfw.c", srcdir ++ "/rglfw.c",
}, raylib_flags); }, raylib_flags.items);
raylib.linkSystemLibrary("winmm"); lib.linkSystemLibrary("winmm");
raylib.linkSystemLibrary("gdi32"); lib.linkSystemLibrary("gdi32");
raylib.linkSystemLibrary("opengl32"); lib.linkSystemLibrary("opengl32");
raylib.addIncludePath(.{ .path = "external/glfw/deps/mingw" }); lib.addIncludePath(.{ .path = "external/glfw/deps/mingw" });
raylib.defineCMacro("PLATFORM_DESKTOP", null); lib.defineCMacro("PLATFORM_DESKTOP", null);
}, },
.linux => { .linux => {
if (!options.platform_drm) { if (!options.platform_drm) {
addCSourceFilesVersioned(raylib, &.{ addCSourceFilesVersioned(lib, &.{
srcdir ++ "/rglfw.c", srcdir ++ "/rglfw.c",
}, raylib_flags); }, raylib_flags.items);
raylib.linkSystemLibrary("GL"); lib.linkSystemLibrary("GL");
raylib.linkSystemLibrary("rt"); lib.linkSystemLibrary("rt");
raylib.linkSystemLibrary("dl"); lib.linkSystemLibrary("dl");
raylib.linkSystemLibrary("m"); lib.linkSystemLibrary("m");
raylib.linkSystemLibrary("X11"); lib.linkSystemLibrary("X11");
raylib.addLibraryPath(.{ .path = "/usr/lib" }); lib.addLibraryPath(.{ .path = "/usr/lib" });
raylib.addIncludePath(.{ .path = "/usr/include" }); lib.addIncludePath(.{ .path = "/usr/include" });
raylib.defineCMacro("PLATFORM_DESKTOP", null); lib.defineCMacro("PLATFORM_DESKTOP", null);
} else { } else {
raylib.linkSystemLibrary("GLESv2"); lib.linkSystemLibrary("GLESv2");
raylib.linkSystemLibrary("EGL"); lib.linkSystemLibrary("EGL");
raylib.linkSystemLibrary("drm"); lib.linkSystemLibrary("drm");
raylib.linkSystemLibrary("gbm"); lib.linkSystemLibrary("gbm");
raylib.linkSystemLibrary("pthread"); lib.linkSystemLibrary("pthread");
raylib.linkSystemLibrary("rt"); lib.linkSystemLibrary("rt");
raylib.linkSystemLibrary("m"); lib.linkSystemLibrary("m");
raylib.linkSystemLibrary("dl"); lib.linkSystemLibrary("dl");
raylib.addIncludePath(.{ .path = "/usr/include/libdrm" }); lib.addIncludePath(.{ .path = "/usr/include/libdrm" });
raylib.defineCMacro("PLATFORM_DRM", null); lib.defineCMacro("PLATFORM_DRM", null);
raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null); lib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
raylib.defineCMacro("EGL_NO_X11", null); lib.defineCMacro("EGL_NO_X11", null);
raylib.defineCMacro("DEFAULT_BATCH_BUFFER_ELEMENT", "2048"); lib.defineCMacro("DEFAULT_BATCH_BUFFER_ELEMENT", "2048");
} }
}, },
.freebsd, .openbsd, .netbsd, .dragonfly => { .freebsd, .openbsd, .netbsd, .dragonfly => {
addCSourceFilesVersioned(raylib, &.{ addCSourceFilesVersioned(lib, &.{
srcdir ++ "/rglfw.c", srcdir ++ "/rglfw.c",
}, raylib_flags); }, raylib_flags.items);
raylib.linkSystemLibrary("GL"); lib.linkSystemLibrary("GL");
raylib.linkSystemLibrary("rt"); lib.linkSystemLibrary("rt");
raylib.linkSystemLibrary("dl"); lib.linkSystemLibrary("dl");
raylib.linkSystemLibrary("m"); lib.linkSystemLibrary("m");
raylib.linkSystemLibrary("X11"); lib.linkSystemLibrary("X11");
raylib.linkSystemLibrary("Xrandr"); lib.linkSystemLibrary("Xrandr");
raylib.linkSystemLibrary("Xinerama"); lib.linkSystemLibrary("Xinerama");
raylib.linkSystemLibrary("Xi"); lib.linkSystemLibrary("Xi");
raylib.linkSystemLibrary("Xxf86vm"); lib.linkSystemLibrary("Xxf86vm");
raylib.linkSystemLibrary("Xcursor"); lib.linkSystemLibrary("Xcursor");
raylib.defineCMacro("PLATFORM_DESKTOP", null); lib.defineCMacro("PLATFORM_DESKTOP", null);
}, },
.macos => { .macos => {
var new_flags = try raylib_flags.clone();
// On macos rglfw.c include Objective-C files. // On macos rglfw.c include Objective-C files.
const raylib_flags_extra_macos = &[_][]const u8{ const raylib_flags_extra_macos = &[_][]const u8{
"-ObjC", "-ObjC",
}; };
addCSourceFilesVersioned(raylib, &.{ try new_flags.appendSlice(raylib_flags_extra_macos);
addCSourceFilesVersioned(lib, &.{
srcdir ++ "/rglfw.c", srcdir ++ "/rglfw.c",
}, raylib_flags ++ raylib_flags_extra_macos); }, new_flags.items);
raylib.linkFramework("Foundation"); lib.linkFramework("Foundation");
raylib.linkFramework("CoreServices"); lib.linkFramework("CoreServices");
raylib.linkFramework("CoreGraphics"); lib.linkFramework("CoreGraphics");
raylib.linkFramework("AppKit"); lib.linkFramework("AppKit");
raylib.linkFramework("IOKit"); lib.linkFramework("IOKit");
raylib.defineCMacro("PLATFORM_DESKTOP", null); lib.defineCMacro("PLATFORM_DESKTOP", null);
}, },
.emscripten => { .emscripten => {
raylib.defineCMacro("PLATFORM_WEB", null); lib.defineCMacro("PLATFORM_WEB", null);
raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null); lib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
if (b.sysroot == null) { if (b.sysroot == null) {
@panic("Pass '--sysroot \"$EMSDK/upstream/emscripten\"'"); @panic("Pass '--sysroot \"$EMSDK/upstream/emscripten\"'");
@ -158,14 +221,12 @@ pub fn addRaylib(b: *std.Build, target: std.zig.CrossTarget, optimize: std.built
var dir = std.fs.openDirAbsolute(cache_include, std.fs.Dir.OpenDirOptions{ .access_sub_paths = true, .no_follow = true }) catch @panic("No emscripten cache. Generate it!"); var dir = std.fs.openDirAbsolute(cache_include, std.fs.Dir.OpenDirOptions{ .access_sub_paths = true, .no_follow = true }) catch @panic("No emscripten cache. Generate it!");
dir.close(); dir.close();
raylib.addIncludePath(.{ .path = cache_include }); lib.addIncludePath(.{ .path = cache_include });
}, },
else => { else => {
@panic("Unsupported OS"); @panic("Unsupported OS");
}, },
} }
return raylib;
} }
pub const Options = struct { pub const Options = struct {
@ -200,8 +261,10 @@ pub fn build(b: *std.Build) void {
.raygui = b.option(bool, "raygui", "Compile with raygui support") orelse defaults.raygui, .raygui = b.option(bool, "raygui", "Compile with raygui support") orelse defaults.raygui,
}; };
const lib = addRaylib(b, target, optimize, options); const static_lib = addStaticRaylib(b, target, optimize, options);
const shared_lib = addSharedRaylib(b, target, optimize, options);
inline for (&[_]*std.Build.CompileStep{ static_lib, shared_lib }) |lib| {
lib.installHeader("src/raylib.h", "raylib.h"); lib.installHeader("src/raylib.h", "raylib.h");
lib.installHeader("src/raymath.h", "raymath.h"); lib.installHeader("src/raymath.h", "raymath.h");
lib.installHeader("src/rlgl.h", "rlgl.h"); lib.installHeader("src/rlgl.h", "rlgl.h");
@ -212,6 +275,7 @@ pub fn build(b: *std.Build) void {
b.installArtifact(lib); b.installArtifact(lib);
} }
}
const srcdir = struct { const srcdir = struct {
fn getSrcDir() []const u8 { fn getSrcDir() []const u8 {