recp / cglm

📽 Highly Optimized 2D / 3D Graphics Math (glm) for C
MIT License
2.34k stars 231 forks source link

Add a build.zig to support building with zig #418

Open ali-shahwali opened 6 months ago

ali-shahwali commented 6 months ago

Hi,

I think it would be nice to have a build.zig file to support C/C++ projects that are built using Zig. I have created a simple one that is by no means perfect, just to give an idea of what it could look like.

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addStaticLibrary(.{
        .name = "cglm",
        .target = target,
        .optimize = optimize,
    });

    lib.installHeadersDirectory(b.path("include/"), ".", .{});

    lib.addCSourceFiles(.{
        .files = &sources,
        .flags = &flags,
    });

    lib.linkLibC();

    b.installArtifact(lib);
}

const flags = [_][]const u8{
    "-std=c99",
};

const sources = [_][]const u8{
    "src/aabb2d.c",
    "src/affine.c",
    "src/affine2d.c",
    "src/bezier.c",
    "src/box.c",
    "src/cam.c",
    "src/clipspace/ortho_lh_no.c",
    "src/clipspace/ortho_lh_zo.c",
    "src/clipspace/ortho_rh_no.c",
    "src/clipspace/ortho_rh_zo.c",
    "src/clipspace/persp_lh_no.c",
    "src/clipspace/persp_lh_zo.c",
    "src/clipspace/persp_rh_no.c",
    "src/clipspace/persp_rh_zo.c",
    "src/clipspace/project_no.c",
    "src/clipspace/project_zo.c",
    "src/clipspace/view_lh_no.c",
    "src/clipspace/view_lh_zo.c",
    "src/clipspace/view_rh_no.c",
    "src/clipspace/view_rh_zo.c",
    "src/config.h",
    "src/curve.c",
    "src/ease.c",
    "src/euler.c",
    "src/frustum.c",
    "src/io.c",
    "src/ivec2.c",
    "src/ivec3.c",
    "src/ivec4.c",
    "src/mat2.c",
    "src/mat2x3.c",
    "src/mat2x4.c",
    "src/mat3.c",
    "src/mat3x2.c",
    "src/mat3x4.c",
    "src/mat4.c",
    "src/mat4x2.c",
    "src/mat4x3.c",
    "src/plane.c",
    "src/project.c",
    "src/quat.c",
    "src/ray.c",
    "src/sphere.c",
    "src/swift/empty.c",
    "src/vec2.c",
    "src/vec3.c",
    "src/vec4.c",
};
recp commented 6 months ago

Hi @ali-shahwali,

Thanks for the feedback,

Since we maintain a few build types e.g. Autotools, CMake, VisualStudio, meson... I'm not sure for new one, also we need to keep this updated and compatible with new ZIG versions...

Im not opposite but let's get more feedbacks

ant1fact commented 6 months ago

As much as I love Zig, I agree with recp that this would incur a lot of extra work before Zig goes 1.0 as the language (and the build system) is still evolving a lot.

On a different note: You don't need to manually list out all of the sources. You can recursively search the src directory for any .c files like this:

var src_files = std.ArrayList([]const u8).init(b.allocator);
defer src_files.deinit();
// Open directory, assuming cglm was added into project root via git submodule
var src_dir = try std.fs.cwd().openDir("cglm/src", .{ .iterate = true });
defer src_dir.close();
// Create walker
var src_walker = try src_dir.walk(b.allocator);
defer src_walker.deinit();
// Iterate
while (try src_walker.next()) |entry| {
    if (entry.kind != .file or !std.mem.eql(u8, std.fs.path.extension(entry.basename), ".c")) {
        continue;
    }
    try src_files.append(b.dupePath(entry.path));
}