Copy build system from master (1/?)

This commit is contained in:
Not-Nik 2020-12-22 15:58:39 +01:00
parent 08536a981d
commit e47623000d
No known key found for this signature in database
GPG Key ID: 08BB71E672DB3BFD
3 changed files with 217 additions and 48 deletions

112
build.zig
View File

@ -7,43 +7,87 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder; const Builder = std.build.Builder;
const raylib = @import("lib.zig").Pkg(".");
pub fn createExe(b: *Builder, name: []const u8, source: []const u8, desc: []const u8) *std.build.LibExeObjStep const Program = struct {
{ name: []const u8,
path: []const u8,
desc: []const u8,
};
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{});
const examples = [_]Program{
.{
.name = "basic_window",
.path = "examples/core/basic_window.zig",
.desc = "Creates a basic window with text",
},
.{
.name = "input_keys",
.path = "examples/core/input_keys.zig",
.desc = "Simple keyboard input",
},
.{
.name = "input_mouse",
.path = "examples/core/input_mouse.zig",
.desc = "Simple mouse input",
},
.{
.name = "input_mouse_wheel",
.path = "examples/core/input_mouse_wheel.zig",
.desc = "Mouse wheel input",
},
.{
.name = "input_multitouch",
.path = "examples/core/input_multitouch.zig",
.desc = "Multitouch input",
},
.{
.name = "2d_camera",
.path = "examples/core/2d_camera.zig",
.desc = "Shows the functionality of a 2D camera",
},
.{
.name = "models_loading",
.path = "examples/models/models_loading.zig",
.desc = "Loads a model and renders it",
},
.{
.name = "draw_2d",
.path = "tests/draw_2d.zig",
.desc = "Testing workaround on 2D drawing functions",
},
.{
.name = "draw_sphere",
.path = "tests/draw_sphere.zig",
.desc = "Testing workaround on DrawSphere",
},
.{
.name = "del_exit_key",
.path = "tests/del_exit_key.zig",
.desc = "Testing disabling of exit key",
},
};
const examples_step = b.step("examples", "Builds all the examples and tests");
const system_lib = b.option(bool, "system-raylib", "link to preinstalled raylib libraries") orelse false;
for (examples) |ex| {
const exe = b.addExecutable(ex.name, ex.path);
var exe = b.addExecutable(name, source);
exe.setBuildMode(mode); exe.setBuildMode(mode);
exe.linkSystemLibrary("raylib"); exe.setTarget(target);
exe.addCSourceFile("lib/workaround.c", &[_][]const u8{});
exe.addPackagePath("raylib", "lib/raylib-zig.zig");
exe.addPackagePath("raylib-math", "lib/raylib-zig-math.zig");
const runExe = exe.run(); raylib.link(exe, system_lib);
const exeStep = b.step(name, desc); raylib.addAsPackage("raylib", exe);
exeStep.dependOn(&runExe.step); raylib.math.addAsPackage("raylib-math", exe);
return exe;
} const run_cmd = exe.run();
const run_step = b.step(ex.name, ex.desc);
pub fn build(b: *Builder) void run_step.dependOn(&run_cmd.step);
{ examples_step.dependOn(&exe.step);
var basicWindow = createExe(b, "basic_window" , "examples/core/basic_window.zig" , "Creates a basic window with text"); }
var inputKeys = createExe(b, "input_keys" , "examples/core/input_keys.zig" , "Simple keyboard input");
var inputMouse = createExe(b, "input_mouse" , "examples/core/input_mouse.zig" , "Simple mouse input");
var inputMouseWheel = createExe(b, "input_mouse_wheel", "examples/core/input_mouse_wheel.zig", "Mouse wheel input");
var inputMultitouch = createExe(b, "input_multitouch" , "examples/core/input_multitouch.zig" , "Multitouch input");
var twoDCamera = createExe(b, "2d_camera" , "examples/core/2d_camera.zig" , "Shows the functionality of a 2D camera");
var modelsLoading = createExe(b, "models_loading" , "examples/models/models_loading.zig" , "Loads a model and renders it");
var mouseRay = createExe(b, "draw_2d" , "tests/draw_2d.zig" , "Testing workaround on 2D drawing functions");
var drawSphere = createExe(b, "draw_sphere" , "tests/draw_sphere.zig" , "Testing workaround on DrawSphere");
var delExitKey = createExe(b, "del_exit_key" , "tests/del_exit_key.zig" , "Testing disabling of exit key");
const examplesStep = b.step("examples", "Builds all the examples");
examplesStep.dependOn(&basicWindow.step);
examplesStep.dependOn(&inputKeys.step);
examplesStep.dependOn(&inputMouse.step);
examplesStep.dependOn(&inputMouseWheel.step);
examplesStep.dependOn(&inputMultitouch.step);
examplesStep.dependOn(&twoDCamera.step);
examplesStep.dependOn(&modelsLoading.step);
} }

116
lib.zig Normal file
View File

@ -0,0 +1,116 @@
const std = @import("std");
const Builder = std.build.Builder;
const LibExeObjStep = std.build.LibExeObjStep;
pub fn Pkg(pkgdir: comptime []const u8) type {
return struct {
var ran_git = false;
pub fn link(exe: *LibExeObjStep, system_lib: bool) void {
if (system_lib) {
exe.linkSystemLibrary("raylib");
return;
}
const raylibFlags = &[_][]const u8{
"-std=c99",
"-DPLATFORM_DESKTOP",
"-D_POSIX_C_SOURCE",
"-DGL_SILENCE_DEPRECATION",
};
const target_os = exe.target.toTarget().os.tag;
switch (target_os) {
.windows => {
exe.linkSystemLibrary("winmm");
exe.linkSystemLibrary("gdi32");
exe.linkSystemLibrary("opengl32");
// build vendored glfw as well
exe.addIncludeDir(pkgdir ++ "/raylib/src/external/glfw/include");
exe.addIncludeDir(pkgdir ++ "/raylib/src/external/glfw/deps/mingw");
exe.addCSourceFile(pkgdir ++ "/raylib/src/rglfw.c", raylibFlags);
},
.macos => {
std.debug.warn("Compiling raylib is unsupported on macOS. Please add '-Dsystem-raylib=true' to your build command to use your system raylib.\n", .{});
std.os.exit(1);
},
.freebsd, .openbsd, .netbsd, .dragonfly => {
exe.linkSystemLibrary("glfw");
exe.linkSystemLibrary("GL");
exe.linkSystemLibrary("rt");
exe.linkSystemLibrary("dl");
exe.linkSystemLibrary("m");
exe.linkSystemLibrary("X11");
exe.linkSystemLibrary("Xrandr");
exe.linkSystemLibrary("Xinerama");
exe.linkSystemLibrary("Xi");
exe.linkSystemLibrary("Xxf86vm");
exe.linkSystemLibrary("Xcursor");
},
else => { // linux and possibly others
exe.linkSystemLibrary("glfw");
exe.linkSystemLibrary("GL");
exe.linkSystemLibrary("rt");
exe.linkSystemLibrary("dl");
exe.linkSystemLibrary("m");
exe.linkSystemLibrary("X11");
},
}
exe.linkLibC();
fetchSubmodules(exe.builder) catch
std.debug.warn(
\\Warning:
\\Unable to fetch git submodule(s) Assuming package folder is not under
\\version control. If build fails, this is probably why.
, .{});
exe.addIncludeDir(pkgdir ++ "/raylib/src");
exe.addCSourceFile(pkgdir ++ "/raylib/src/core.c", raylibFlags);
exe.addCSourceFile(pkgdir ++ "/raylib/src/models.c", raylibFlags);
exe.addCSourceFile(pkgdir ++ "/raylib/src/raudio.c", raylibFlags);
exe.addCSourceFile(pkgdir ++ "/raylib/src/shapes.c", raylibFlags);
exe.addCSourceFile(pkgdir ++ "/raylib/src/text.c", raylibFlags);
exe.addCSourceFile(pkgdir ++ "/raylib/src/textures.c", raylibFlags);
exe.addCSourceFile(pkgdir ++ "/raylib/src/utils.c", raylibFlags);
}
fn fetchSubmodules(b: *Builder) !void {
if (ran_git) return;
ran_git = true;
std.debug.warn("attempting to fetch submodule(s)...\n", .{});
const git_proc = std.ChildProcess.init(
&[_][]const u8{ "git", "submodule", "update", "--init" },
b.allocator,
) catch {
std.debug.warn("unable to create child process for git. build interrupted\n", .{});
std.os.exit(1);
};
git_proc.cwd = pkgdir;
const term = git_proc.spawnAndWait() catch {
std.debug.warn("unable to spawn child process for git. build interrupted\n", .{});
std.os.exit(1);
};
switch (term) {
.Exited => |code| if (code != 0) return error.GitFail,
else => {
std.debug.warn("git terminated unexpectedly. build interrupted\n", .{});
std.os.exit(1);
},
}
}
pub fn addAsPackage(name: comptime []const u8, to: *LibExeObjStep) void {
to.addPackagePath(name, pkgdir ++ "/lib/raylib-zig.zig");
to.addCSourceFile("lib/workaround.c", &[_][]const u8{});
}
pub const math = struct {
pub fn addAsPackage(name: comptime []const u8, to: *LibExeObjStep) void {
to.addPackagePath(name, pkgdir ++ "/lib/raylib-zig-math.zig");
}
};
};
}

View File

@ -6,26 +6,35 @@ fi
mkdir "$PROJECT_NAME" && cd "$PROJECT_NAME" || exit mkdir "$PROJECT_NAME" && cd "$PROJECT_NAME" || exit
touch build.zig touch build.zig
echo "generating project files..."
echo 'const std = @import("std");
const Builder = std.build.Builder;
const raylib = @import("raylib-zig/lib.zig").Pkg("raylib-zig"); //call .Pkg() with the folder raylib-zig is in relative to project build.zig
echo 'const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
const exe = b.addExecutable("'"$PROJECT_NAME"'", "src/main.zig"); const target = b.standardTargetOptions(.{});
exe.setBuildMode(mode);
exe.linkSystemLibrary("raylib");
exe.addCSourceFile("raylib-zig/workaround.c", &[_][]const u8{});
exe.addPackagePath("raylib", "raylib-zig/raylib-zig.zig");
exe.addPackagePath("raylib-math", "lib/raylib-zig-math.zig");
const runExe = exe.run(); const system_lib = b.option(bool, "system-raylib", "link to preinstalled raylib libraries") orelse false;
const exeStep = b.step("run", "Runs the app");
exeStep.dependOn(&runExe.step); const exe = b.addExecutable("'$PROJECT_NAME'", "src/main.zig");
exe.setBuildMode(mode);
exe.setTarget(target);
raylib.link(exe, system_lib);
raylib.addAsPackage("raylib", exe);
raylib.math.addAsPackage("raylib-math", exe);
const run_cmd = exe.run();
const run_step = b.step("run", "run '$PROJECT_NAME'");
run_step.dependOn(&run_cmd.step);
exe.install();
} }
' >> build.zig ' >> build.zig
mkdir src mkdir src
mkdir raylib-zig cp ../examples/core/basic_window.zig src/main.zig
cp ../lib/** raylib-zig echo "cloning raylib-zig inside of project..."
cp ../examples/core/basic_window.zig src git clone ../ raylib-zig
mv src/basic_window.zig src/main.zig