raysan5 / raylib

A simple and easy-to-use library to enjoy videogames programming
http://www.raylib.com
zlib License
21.99k stars 2.22k forks source link

[build.zig] Zig build fails with `error: file exists in multiple modules` on master with Zig >0.12 #4138

Closed jpiechowka closed 2 months ago

jpiechowka commented 3 months ago

Issue description

On the latest Zig version, build fails with error: file exists in multiple modules on master branch. I have added Raylib as git submodule from master inside my repository:

D:\GitHub\ps1z\raylib\build.zig:2:24: note: imported from module root.@dependencies.12202097d817adcf30a420dfb63266dd3402de94a1012736204749fdd76f41d37bd0
const raylib = @import("src/build.zig");
                       ^~~~~~~~~~~~~~~
D:\GitHub\ps1z\raylib\build.zig:1:1: error: file exists in multiple modules
D:\GitHub\ps1z\raylib\build.zig:1:1: note: root of module root.@dependencies.12202097d817adcf30a420dfb63266dd3402de94a1012736204749fdd76f41d37bd0
D:\GitHub\ps1z\raylib\src\build.zig:15:29: note: imported from module root.@build    
const BuildScript = @import("../build.zig");

image

Environment

OS: Windows 11 Arch: x86-64 ZIG: 0.14.0-dev.185+c40708a2c

Code Example

build.zig:

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

pub fn build(b: *std.Build) void {
    b.verbose = true;

    comptime {
        if (builtin.zig_version.minor < 13) @compileError("ps1z requires zig version >= 0.13.0");
    }

    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

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

    const raylib = try raySdk.addRaylib(b, target, optimize, .{});
    exe.addIncludePath(b.path("raylib/src"));
    exe.linkLibrary(raylib);

    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);

    const exe_unit_tests = b.addTest(.{
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

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

build.zig.zon:

.{
    .name = "ps1z",
    .version = "0.0.1",
    .minimum_zig_version = "0.13.0",

    .dependencies = .{
        .raylib = .{
            .path = "raylib",
        },
    },

    .paths = .{
        "build.zig",
        "build.zig.zon",
        "src",
        "LICENSE",
        "README.md",
    },
}
hitakiri commented 3 months ago
const std = @import("std");
const builtin = @import("builtin");

pub fn build(b: *std.Build) void {
    b.verbose = true;

    comptime {
        if (builtin.zig_version.minor < 13) @compileError("ps1z requires zig version >= 0.13.0");
    }

    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

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

   const rl = b.dependency("raylib", .{
        .target = target,
        .optimize = optimize,
    });

    // you need to link to the output of the build process
    // that was done by the raylib package
    // in this case, duck is outputting a library
    // to which your project need to link as well
    exe.linkLibrary(rl.artifact("raylib"));

    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);

    const exe_unit_tests = b.addTest(.{
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

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

Here more info.

jpiechowka commented 3 months ago

Thank you, it works!

image

There is another problem that I lose all of the configuration options from addRaylib()

pub fn addRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, options: Options) !*std.Build.Step.Compile {
    const raylib_dep = b.dependencyFromBuildZig(BuildScript, .{
        .target = target,
        .optimize = optimize,
        .raudio = options.raudio,
        .rmodels = options.rmodels,
        .rshapes = options.rshapes,
        .rtext = options.rtext,
        .rtextures = options.rtextures,
        .platform_drm = options.platform_drm,
        .shared = options.shared,
        .linux_display_backend = options.linux_display_backend,
        .opengl_version = options.opengl_version,
    });
raysan5 commented 2 months ago

@jpiechowka Is this issue addressed? Does it require some change? Please, send a PR if required.

jpiechowka commented 2 months ago

@jpiechowka Is this issue addressed? Does it require some change? Please, send a PR if required.

The main problem was addressed. I think I can figure the config later when I have some time.