Improve Web example building

This commit is contained in:
Not-Nik 2023-01-31 16:45:14 +01:00
parent d1733e60cc
commit 1e1108be1d
No known key found for this signature in database
GPG Key ID: 08BB71E672DB3BFD
4 changed files with 35 additions and 28 deletions

View File

@ -67,8 +67,8 @@ ifeq ($(PLATFORM),PLATFORM_RPI)
endif
endif
# Determine PLATFORM_OS in case PLATFORM_DESKTOP selected
ifeq ($(PLATFORM),PLATFORM_DESKTOP)
# Determine PLATFORM_OS in case PLATFORM_DESKTOP or PLATFORM_WEB selected
ifeq ($(PLATFORM),$(filter $(PLATFORM),PLATFORM_DESKTOP PLATFORM_WEB))
# No uname.exe on MinGW!, but OS=Windows_NT on Windows!
# ifeq ($(UNAME),Msys) -> Windows
ifeq ($(OS),Windows_NT)
@ -128,16 +128,6 @@ endif
# Define raylib release directory for compiled library
RAYLIB_RELEASE_PATH ?= $(RAYLIB_PATH)/src
ifeq ($(PLATFORM),PLATFORM_WEB)
# Emscripten required variables
EMSDK_PATH ?= C:/emsdk
EMSCRIPTEN_PATH ?= $(EMSDK_PATH)/upstream/emscripten
CLANG_PATH = $(EMSDK_PATH)/upstream/bin
PYTHON_PATH = $(EMSDK_PATH)/python/3.9.2-1_64bit
NODE_PATH = $(EMSDK_PATH)/node/14.15.5_64bit/bin
export PATH = $(EMSDK_PATH);$(EMSCRIPTEN_PATH);$(CLANG_PATH);$(NODE_PATH);$(PYTHON_PATH):$$(PATH)
endif
# Define default C compiler: CC
#------------------------------------------------------------------------------------------------
CC = gcc
@ -557,7 +547,7 @@ endif
# Clean everything
clean:
ifeq ($(PLATFORM),PLATFORM_DESKTOP)
ifeq ($(PLATFORM),$(filter $(PLATFORM),PLATFORM_DESKTOP PLATFORM_WEB))
ifeq ($(PLATFORM_OS),WINDOWS)
del *.o *.exe /s
endif
@ -577,9 +567,6 @@ endif
ifeq ($(PLATFORM),PLATFORM_DRM)
find . -type f -executable -delete
rm -fv *.o
endif
ifeq ($(PLATFORM),PLATFORM_WEB)
del *.o *.html *.js
endif
@echo Cleaning done

View File

@ -18,13 +18,8 @@ fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zi
// zig's mingw headers do not include pthread.h
if (std.mem.eql(u8, "core_loading_thread", name) and target.getOsTag() == .windows) continue;
const exe = b.addExecutable(name, null);
exe.addCSourceFile(path, switch (target.getOsTag()) {
.windows => &[_][]const u8{},
.linux => &[_][]const u8{},
.macos => &[_][]const u8{"-DPLATFORM_DESKTOP"},
else => @panic("Unsupported OS"),
});
const exe = b.addSharedLibrary(name, null, std.build.LibExeObjStep.SharedLibKind.unversioned);
exe.addCSourceFile(path, &[_][]const u8{});
exe.setTarget(target);
exe.setBuildMode(mode);
exe.linkLibC();
@ -32,6 +27,7 @@ fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zi
.windows => "../src/raylib.lib",
.linux => "../src/libraylib.a",
.macos => "../src/libraylib.a",
.emscripten => "../src/libraylib.a",
else => @panic("Unsupported OS"),
});
@ -45,6 +41,8 @@ fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zi
exe.linkSystemLibrary("gdi32");
exe.linkSystemLibrary("opengl32");
exe.addIncludePath("external/glfw/deps/mingw");
exe.defineCMacro("PLATFORM_DESKTOP", null);
},
.linux => {
exe.linkSystemLibrary("GL");
@ -52,6 +50,8 @@ fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zi
exe.linkSystemLibrary("dl");
exe.linkSystemLibrary("m");
exe.linkSystemLibrary("X11");
exe.defineCMacro("PLATFORM_DESKTOP", null);
},
.macos => {
exe.linkFramework("Foundation");
@ -60,6 +60,24 @@ fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zi
exe.linkFramework("CoreAudio");
exe.linkFramework("CoreVideo");
exe.linkFramework("IOKit");
exe.defineCMacro("PLATFORM_DESKTOP", null);
},
.emscripten => {
exe.defineCMacro("PLATFORM_WEB", null);
exe.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
if (b.sysroot == null) {
@panic("Pass '--sysroot \"$EMSDK/upstream/emscripten\"'S");
}
const cache_include = std.fs.path.join(b.allocator, &.{ b.sysroot.?, "cache", "sysroot", "include" }) catch @panic("Out of memory");
defer b.allocator.free(cache_include);
var d = std.fs.openDirAbsolute(cache_include, std.fs.Dir.OpenDirOptions{.access_sub_paths = true, .no_follow = true}) catch @panic("No emscripten cache. Generate it!");
d.close();
exe.addIncludePath(cache_include);
},
else => {
@panic("Unsupported OS");
@ -68,10 +86,12 @@ fn add_module(comptime module: []const u8, b: *std.build.Builder, target: std.zi
exe.setOutputDir(module);
if (exe.target.toTarget().os.tag != std.Target.Os.Tag.emscripten) {
var run = exe.run();
run.step.dependOn(&b.addInstallArtifact(exe).step);
run.cwd = module;
b.step(name, name).dependOn(&run.step);
}
all.dependOn(&exe.step);
}
return all;

View File

View File

@ -81,7 +81,7 @@ pub fn addRaylib(b: *std.build.Builder, target: std.zig.CrossTarget) *std.build.
raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
if (b.sysroot == null) {
@panic("Pass '--sysroot \"$EMSDK/upstream/emscripten\"' or '--sysroot \"/usr/local/opt/emscripten/libexec\"' on macOS");
@panic("Pass '--sysroot \"$EMSDK/upstream/emscripten\"'");
}
const cache_include = std.fs.path.join(b.allocator, &.{ b.sysroot.?, "cache", "sysroot", "include" }) catch @panic("Out of memory");