mach/libs/basisu/build.zig
Stephen Gutekanst 0645429df9 all: move standalone libraries to libs/ subdirectory
The root dir of our repository has grown quite a lot the past few months.

I'd like to make it more clear where the bulk of the engine lives (`src/`) and
also make it more clear which Mach libraries are consumable as standalone projects.

As for the name of this directory, `libs` was my first choice but there's a bit of
a convention of that being external libraries in Zig projects _today_, while these
are libraries maintained as part of Mach in this repository - not external ones.

We will name this directory `libs`, and if we have a need for external libraries
we will use `external` or `deps` for that directory name. I considered other names
such as `components`, `systems`, `modules` (which are bad as they overlap with
major ECS / engine concepts), and it seems likely the official Zig package manager
will break the convention of using a `libs` dir anyway.

Performed via:

```sh
mkdir libs/
git mv freetype libs/
git mv basisu libs/
git mv gamemode libs/
git mv glfw libs/
git mv gpu libs/
git mv gpu-dawn libs/
git mv sysaudio libs/
git mv sysjs libs/
git mv ecs libs/
```

git-subtree-dir: glfw
git-subtree-mainline: 0d5b853443
git-subtree-split: 572d1144f11b353abdb64fff828b25a4f0fbb7ca

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>

git mv ecs libs/

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-26 15:12:04 -07:00

124 lines
4.2 KiB
Zig

const std = @import("std");
const Builder = std.build.Builder;
const basisu_root = thisDir() ++ "/upstream/basisu";
pub const pkg = std.build.Pkg{
.name = "basisu",
.source = .{
.path = "src/main.zig",
},
};
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{});
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&testStep(b, mode, target).step);
}
pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTestExe("basisu-tests", comptime thisDir() ++ "/src/main.zig");
main_tests.setBuildMode(mode);
main_tests.setTarget(target);
main_tests.main_pkg_path = thisDir();
link(b, main_tests, .{
.encoder = true,
.transcoder = true,
});
main_tests.install();
return main_tests.run();
}
pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
if (options.encoder) {
step.linkLibrary(buildEncoder(b));
step.addCSourceFile(comptime thisDir() ++ "/src/encoder/wrapper.cpp", &.{});
step.addIncludeDir(basisu_root ++ "/encoder");
}
if (options.transcoder) {
step.linkLibrary(buildTranscoder(b));
step.addCSourceFile(comptime thisDir() ++ "/src/transcoder/wrapper.cpp", &.{});
step.addIncludeDir(basisu_root ++ "/transcoder");
}
}
pub fn buildEncoder(b: *Builder) *std.build.LibExeObjStep {
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
ensureDependencySubmodule(b.allocator, "upstream") catch unreachable;
const encoder = b.addStaticLibrary("basisu-encoder", null);
encoder.linkLibCpp();
encoder.addCSourceFiles(
encoder_sources,
&.{},
);
encoder.defineCMacro("BASISU_FORCE_DEVEL_MESSAGES", "0");
encoder.defineCMacro("BASISD_SUPPORT_KTX2_ZSTD", "0");
encoder.install();
return encoder;
}
pub fn buildTranscoder(b: *Builder) *std.build.LibExeObjStep {
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
ensureDependencySubmodule(b.allocator, "upstream") catch unreachable;
const transcoder = b.addStaticLibrary("basisu-transcoder", null);
transcoder.linkLibCpp();
transcoder.addCSourceFiles(
transcoder_sources,
&.{},
);
transcoder.defineCMacro("BASISU_FORCE_DEVEL_MESSAGES", "0");
transcoder.defineCMacro("BASISD_SUPPORT_KTX2_ZSTD", "0");
transcoder.install();
return transcoder;
}
pub const Options = struct {
encoder: bool,
transcoder: bool,
};
fn thisDir() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}
fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !void {
if (std.process.getEnvVarOwned(allocator, "NO_ENSURE_SUBMODULES")) |no_ensure_submodules| {
if (std.mem.eql(u8, no_ensure_submodules, "true")) return;
} else |_| {}
var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator);
child.cwd = (comptime thisDir());
child.stderr = std.io.getStdErr();
child.stdout = std.io.getStdOut();
_ = try child.spawnAndWait();
}
const transcoder_sources = &[_][]const u8{
basisu_root ++ "/transcoder/basisu_transcoder.cpp",
};
const encoder_sources = &[_][]const u8{
basisu_root ++ "/encoder/basisu_backend.cpp",
basisu_root ++ "/encoder/basisu_basis_file.cpp",
basisu_root ++ "/encoder/basisu_bc7enc.cpp",
basisu_root ++ "/encoder/basisu_comp.cpp",
basisu_root ++ "/encoder/basisu_enc.cpp",
basisu_root ++ "/encoder/basisu_etc.cpp",
basisu_root ++ "/encoder/basisu_frontend.cpp",
basisu_root ++ "/encoder/basisu_gpu_texture.cpp",
basisu_root ++ "/encoder/basisu_kernels_sse.cpp",
basisu_root ++ "/encoder/basisu_opencl.cpp",
basisu_root ++ "/encoder/basisu_pvrtc1_4.cpp",
basisu_root ++ "/encoder/basisu_resample_filters.cpp",
basisu_root ++ "/encoder/basisu_resampler.cpp",
basisu_root ++ "/encoder/basisu_ssim.cpp",
basisu_root ++ "/encoder/basisu_uastc_enc.cpp",
basisu_root ++ "/encoder/jpgd.cpp",
basisu_root ++ "/encoder/pvpngreader.cpp",
};