Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[raygui] raygui.c leftover from zig build #3413

Closed
bluesillybeard opened this issue Oct 14, 2023 · 2 comments · Fixed by #3417
Closed

[raygui] raygui.c leftover from zig build #3413

bluesillybeard opened this issue Oct 14, 2023 · 2 comments · Fixed by #3417

Comments

@bluesillybeard
Copy link
Contributor

Issue description

When building with the Zig build system and raygui is enabled, a file named 'raygui.c' is leftover from the build process, and git counts it as a new file to add.

Environment

X86_64, Archlinux, OpenGL 4.6, NVidia 3060 mobile

Issue Screenshot

The extra file in the VSCode file explorer
image

Code Example

I am using Raylib as part of a project written in Zig, so here is my build.zig

const std = @import("std");
const raylibBuild = @import("raylib/src/build.zig");

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

    const exe = b.addExecutable(.{
        .name = "Celestial",
        .root_source_file = .{ .path = "Celestial/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    b.installArtifact(exe);

    // Add zig-ecs as a module
    const ecs_module = b.addModule("zig-ecs", .{
        .source_file = std.build.FileSource{ .path = "zig-ecs/src/ecs.zig" },
    });
    exe.addModule("ecs", ecs_module);

    // Add raylib as a module
    const raylib = raylibBuild.addRaylib(b, target, optimize, .{
        .raudio = true,
        .rmodels = true,
        .rshapes = true,
        .rtext = true,
        .rtextures = true,
        // Enable raygui
        .raygui = true,
        .platform_drm = false,
    });
    raylib.installHeader("raylib/src/raylib.h", "raylib.h");
    raylib.installHeader("raylib/src/raymath.h", "raymath.h");
    raylib.installHeader("raylib/src/rlgl.h", "rlgl.h");

    // I could have just copied the header, but using a git submodule is more convenient if I ever need a more recent version
    raylib.installHeader("raygui/src/raygui.h", "raygui.h");
    exe.linkLibrary(raylib);

    // the exe needs the headers too
    exe.installHeader("raylib/src/raylib.h", "raylib.h");
    exe.installHeader("raygui/src/raygui.h", "raygui.h");

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    // pass through arguments to the application
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }
    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);

    // Unit tests are important
    const unit_tests = b.addTest(.{
        .root_source_file = .{ .path = "Celestial/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    const run_unit_tests = b.addRunArtifact(unit_tests);

    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_unit_tests.step);
}

The issue happens around line 56 of raylib/src/build.zig

var gen_step = std.build.Step.WriteFile.create(b);
raylib.step.dependOn(&gen_step.step);

if (options.raygui) {
    _ = gen_step.add(srcdir ++ "/raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
    raylib.addCSourceFile(.{ .file = .{ .path = srcdir ++ "/raygui.c" }, .flags = raylib_flags });
    raylib.addIncludePath(.{ .path = srcdir });
    raylib.addIncludePath(.{ .path = srcdir ++ "/../../raygui/src" });
}

To fix the issue, the code could be changed to:

// For one, using the create() method directly is generally not a good idea, so addWriteFiles is used instead.
var gen_step = b.addWriteFiles();
raylib.step.dependOn(&gen_step.step);

if (options.raygui) {
    //Instead of hardcoding the path into the source directory, the raygui.c file is put into the zig cache, as it should.
    const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
    raylib.addCSourceFile(.{ .file = raygui_c_path, .flags = raylib_flags });
    raylib.addIncludePath(.{ .path = srcdir });
    raylib.addIncludePath(.{ .path = srcdir ++ "/../../raygui/src" });
}

I decided to apply the change, and as far as I can tell it works as it should.

@raysan5
Copy link
Owner

raysan5 commented Oct 14, 2023

@bluesillybeard thanks for the review, feel free to send a PR

@bluesillybeard
Copy link
Contributor Author

Yeah I'll go ahead and make a PR.

@bluesillybeard bluesillybeard changed the title [raygui] raygui.c levtover from zig build [raygui] raygui.c leftover from zig build Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants