fix: windows shard library builds

This commit is contained in:
Kai Angulo 2024-10-24 12:57:44 -07:00
parent aa0a58d186
commit aeee736b0f
2 changed files with 94 additions and 80 deletions

View file

@ -0,0 +1,15 @@
#include "glslang/Include/glslang_c_interface.h"
#include "glslang/Public/resource_limits_c.h"
#include <iostream>
int main(void)
{
// Weird issue, proobably a bad config glslang won't export the right definitions
// unless we explicitly reference them when creating this shared library
// Seems like it's 'per-module' though (whatever that means), since we don't need to define every function and just core SPIRV/resource/glslang functions
glslang_initialize_process();
glslang_default_resource();
glslang_SPIRV_disassemble(nullptr, 0);
glslang_finalize_process();
}

103
build.zig
View file

@ -46,8 +46,6 @@ pub fn build(b: *Build) !void {
"-Wno-unused-variable",
"-fPIC",
"-static",
"-static-libgcc",
"-static-libstdc++"
};
try cppflags.appendSlice(base_flags);
@ -61,16 +59,7 @@ pub fn build(b: *Build) !void {
const path: []const u8 = "external/spirv-headers";
if (b.lazyDependency("SPIRV-Tools", .{
.target = target,
.optimize = optimize,
.debug = debug,
.shared = shared_tools,
.header_path = path,
.no_link = true,
.no_reduce = true,
.rebuild_headers = regenerate_headers
})) |dep| {
if (b.lazyDependency("SPIRV-Tools", .{ .target = target, .optimize = optimize, .debug = debug, .shared = shared_tools, .header_path = path, .no_link = true, .no_reduce = true, .rebuild_headers = regenerate_headers })) |dep| {
tools_lib = dep.artifact("SPIRV-Tools");
tools_opt = dep.artifact("SPIRV-Tools-opt");
tools_val = dep.artifact("SPIRV-Tools-val");
@ -85,27 +74,13 @@ pub fn build(b: *Build) !void {
sources_resource_limits ++
sources_c_interface;
var glslang_lib: *std.Build.Step.Compile = undefined;
if (shared) {
glslang_lib = b.addSharedLibrary(.{
var glslang_lib: *std.Build.Step.Compile = b.addStaticLibrary(.{
.name = "glslang",
.root_source_file = b.addWriteFiles().add("empty.c", ""),
.optimize = optimize,
.target = target,
});
glslang_lib.defineCMacro("GLSLANG_IS_SHARED_LIBRARY", "");
glslang_lib.defineCMacro("GLSLANG_EXPORTING", "");
} else {
glslang_lib = b.addStaticLibrary(.{
.name = "glslang",
.root_source_file = b.addWriteFiles().add("empty.c", ""),
.optimize = optimize,
.target = target,
});
}
if (regenerate_headers) {
glslang_lib.step.dependOn(generateHeaders(b));
}
@ -126,17 +101,14 @@ pub fn build(b: *Build) !void {
.files = &sources_win,
.flags = cppflags.items,
});
glslang_lib.defineCMacro("GLSLANG_OSINCLUDE_WIN32", "");
} else {
glslang_lib.addCSourceFiles(.{
.files = &sources_unix,
.flags = cppflags.items,
});
glslang_lib.defineCMacro("GLSLANG_OSINCLUDE_UNIX", "");
}
glslang_lib.defineCMacro("ENABLE_SPIRV", "1");
// Add HLSL sources
if (enable_hlsl) {
@ -150,7 +122,6 @@ pub fn build(b: *Build) !void {
glslang_lib.defineCMacro("ENABLE_HLSL", "0");
}
// Add SPIRV-Tools-opt sources
if (enable_opt) {
glslang_lib.addCSourceFiles(.{
@ -166,12 +137,38 @@ pub fn build(b: *Build) !void {
glslang_lib.defineCMacro("ENABLE_OPT", "0");
}
const build_step = b.step("glslang-library", "Build the glslang library");
build_step.dependOn(&b.addInstallArtifact(glslang_lib, .{}).step);
b.installArtifact(glslang_lib);
// Core glslang sources CANNOT be built as a standalone shared library or segfaults happen in c std::
// no idea if this is a zig or glslang problem, but it seems like zig
// the workaround is to build core glslang as a static library, then link it into a stub shared library
var shared_glslang: *std.Build.Step.Compile = undefined;
if (shared) {
shared_glslang = b.addSharedLibrary(.{
.name = "glslang",
.optimize = optimize,
.target = target,
});
const shared_glslang_step = b.step("glslang", "Build and install shared glslang.exe");
shared_glslang_step.dependOn(&b.addInstallArtifact(shared_glslang, .{}).step);
shared_glslang.addCSourceFile(.{
.file = b.path("StandAlone/shared_glslang.cpp"),
.flags = &.{"-std=c++17"},
});
addIncludes(b, shared_glslang);
b.installArtifact(shared_glslang);
shared_glslang.linkLibrary(glslang_lib);
glslang_lib.defineCMacro("GLSLANG_IS_SHARED_LIBRARY", "1");
glslang_lib.defineCMacro("GLSLANG_EXPORTING", "1");
}
if (standalone_glslang) {
const glslang_exe = b.addExecutable(.{
@ -192,7 +189,14 @@ pub fn build(b: *Build) !void {
addIncludes(b, glslang_exe);
b.installArtifact(glslang_exe);
if (shared) {
glslang_exe.defineCMacro("GLSLANG_IS_SHARED_LIBRARY", "");
glslang_exe.linkLibrary(shared_glslang);
}
else {
glslang_exe.linkLibrary(glslang_lib);
}
if (enable_hlsl) {
glslang_exe.defineCMacro("ENABLE_HLSL", "1");
@ -207,7 +211,6 @@ pub fn build(b: *Build) !void {
}
}
if (standalone_spvremap) {
const spirv_remap = b.addExecutable(.{
.name = "spirv-remap",
@ -219,6 +222,10 @@ pub fn build(b: *Build) !void {
if (shared) {
spirv_remap.defineCMacro("GLSLANG_IS_SHARED_LIBRARY", "");
spirv_remap.linkLibrary(shared_glslang);
}
else {
spirv_remap.linkLibrary(glslang_lib);
}
const install_remap_step = b.step("spirv-remap", "Build and install spirv-remap.exe");
@ -234,7 +241,6 @@ pub fn build(b: *Build) !void {
spirv_remap.linkLibrary(glslang_lib);
}
if (minimal_test_exe) {
const min_test = b.addExecutable(.{
.name = "minimal-test",
@ -246,6 +252,10 @@ pub fn build(b: *Build) !void {
if (shared) {
min_test.defineCMacro("GLSLANG_IS_SHARED_LIBRARY", "");
min_test.linkLibrary(shared_glslang);
}
else {
min_test.linkLibrary(glslang_lib);
}
const min_test_step = b.step("minimal-test", "Build and install minimal-test.exe");
@ -258,7 +268,7 @@ pub fn build(b: *Build) !void {
addIncludes(b, min_test);
b.installArtifact(min_test);
min_test.linkLibrary(glslang_lib);
}
}
@ -304,9 +314,7 @@ fn downloadOrPull(b: *Build, comptime user: []const u8, comptime repo: []const u
_ = std.fs.openDirAbsolute(out_path.getPath(b), .{}) catch |err| {
if (err == error.FileNotFound) {
_ = b.run(&.{
"git", "clone", "https://github.com/" ++ user ++ "/" ++ repo, out_path.getPath(b)
});
_ = b.run(&.{ "git", "clone", "https://github.com/" ++ user ++ "/" ++ repo, out_path.getPath(b) });
return;
}
@ -367,7 +375,6 @@ fn generateHeaders(b: *Build) *std.Build.Step {
return headers_step;
}
const sources_spirv = [_][]const u8{
"SPIRV/GlslangToSpv.cpp",
"SPIRV/InReadableOrder.cpp",
@ -425,13 +432,9 @@ const sources_machine_independent = [_][]const u8{
"glslang/MachineIndependent/reflection.cpp",
};
const sources_win = [_][]const u8{
"glslang/OSDependent/Windows/ossource.cpp"
};
const sources_win = [_][]const u8{"glslang/OSDependent/Windows/ossource.cpp"};
const sources_unix = [_][]const u8{
"glslang/OSDependent/Unix/ossource.cpp"
};
const sources_unix = [_][]const u8{"glslang/OSDependent/Unix/ossource.cpp"};
const sources_hlsl = [_][]const u8{
"glslang/HLSL/hlslAttributes.cpp",
@ -447,10 +450,6 @@ const sources_opt = [_][]const u8{
"SPIRV/SpvTools.cpp",
};
const sources_standalone_glslang = [_][]const u8{
"StandAlone/StandAlone.cpp"
};
const sources_standalone_glslang = [_][]const u8{"StandAlone/StandAlone.cpp"};
const sources_standalone_remap = [_][]const u8{
"StandAlone/spirv-remap.cpp"
};
const sources_standalone_remap = [_][]const u8{"StandAlone/spirv-remap.cpp"};