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

Building on macos #7

Open
unorsk opened this issue Jan 22, 2025 · 0 comments
Open

Building on macos #7

unorsk opened this issue Jan 22, 2025 · 0 comments

Comments

@unorsk
Copy link

unorsk commented Jan 22, 2025

Hey! Nice work!

I managed to build and run it on MacOS! :)

Just in case someone's having troubles building the project on macos (or is willing to add macos support ;) ) here's my build.zig. And yeah, I couldn't build it with the latest compiler but it worked like a charm on 0.13.0!

const std = @import("std");
const GPA = std.heap.GeneralPurposeAllocator;

var gpa = GPA(.{}){};

pub fn build(b: *std.Build) void {
    var allocator = gpa.allocator();

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

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

    const imgui = b.addStaticLibrary(.{ .name = "imgui", .target = target, .optimize = optimize });
    imgui.linkLibCpp();

    imgui.addCSourceFiles(.{
        .files = &[_][]const u8{
            "libs/cimgui/cimgui.cpp",
            "libs/cimgui/imgui/imgui.cpp",
            "libs/cimgui/imgui/imgui_demo.cpp",
            "libs/cimgui/imgui/imgui_draw.cpp",
            "libs/cimgui/imgui/imgui_tables.cpp",
            "libs/cimgui/imgui/backends/imgui_impl_sdl2.cpp",
            "libs/cimgui/imgui/backends/imgui_impl_opengl3.cpp",
            "libs/cimgui/imgui/imgui_widgets.cpp",
        },
        .flags = &[_][]const u8{
            "-fdeclspec",
            "-fms-extensions",
            "-std=c++17",
            // "-DCIMGUI_DEFINE_ENUMS_AND_STRUCTS",
            "-DIMGUI_IMPL_API=extern \"C\" __declspec(dllexport)",
            "-DCIMGUI_USE_SDL2",
            "-DCIMGUI_USE_OPENGL3",
        },
    });

    const sdl2_path = std.process.getEnvVarOwned(allocator, "SDL2_PATH") catch {
        std.debug.print("Build Error: ENV variable 'SDL2_PATH' not found", .{});
        return;
    };
    defer allocator.free(sdl2_path);

    const sdl2_include_path = std.process.getEnvVarOwned(allocator, "SDL2_INCLUDE_PATH") catch {
        std.debug.print("Build Error: ENV variable 'SDL2_INCLUDE_PATH' not found", .{});
        return;
    };
    defer allocator.free(sdl2_include_path);

    const sdl2_lib = switch (target.result.os.tag) {
        .windows => "SDL2.dll",
        .macos => "libSDL2.dylib",
        else => unreachable, // unsupported target
    };
    const sdl2_dll_path = std.fs.path.join(allocator, &.{
        sdl2_path,
        sdl2_lib,
    }) catch |err| {
        // const sdl2_dll_path = std.fs.path.join(allocator, &.{ sdl2_path, "SDL2.dll" }) catch |err| {
        std.debug.print("{s}", .{@errorName(err)});
        return;
    };
    defer allocator.free(sdl2_dll_path);

    imgui.addIncludePath(b.path("./libs/cimgui/imgui"));
    imgui.addIncludePath(.{ .cwd_relative = sdl2_include_path });
    imgui.addLibraryPath(.{ .cwd_relative = sdl2_path });
    imgui.addIncludePath(.{ .cwd_relative = "/opt/homebrew/include" });
    imgui.addLibraryPath(.{ .cwd_relative = "/opt/homebrew/lib" });
    imgui.linkFramework("OpenGL");

    exe.addIncludePath(b.path("./libs/cimgui"));
    exe.addIncludePath(b.path("./libs/cimgui/generator/output"));
    exe.linkLibrary(imgui);

    exe.addIncludePath(.{ .cwd_relative = sdl2_include_path });
    exe.addLibraryPath(.{ .cwd_relative = sdl2_path });
    b.getInstallStep().dependOn(&b.addInstallFileWithDir(.{ .cwd_relative = sdl2_dll_path }, .bin, sdl2_lib).step);

    const glew_path = std.process.getEnvVarOwned(allocator, "GLEW_PATH") catch {
        std.debug.print("Build Error: ENV variable 'GLEW_PATH' not found", .{});
        return;
    };
    defer allocator.free(glew_path);

    const glew_include_path = std.process.getEnvVarOwned(allocator, "GLEW_INCLUDE_PATH") catch {
        std.debug.print("Build Error: ENV variable 'GLEW_INCLUDE_PATH' not found", .{});
        return;
    };
    defer allocator.free(glew_include_path);

    exe.addIncludePath(.{ .cwd_relative = glew_include_path });
    exe.addLibraryPath(.{ .cwd_relative = glew_path });

    switch (target.result.os.tag) {
        .windows => {
            exe.linkSystemLibrary("opengl32");
            exe.linkSystemLibrary("sdl2");
            exe.linkSystemLibrary("glew32");
        },
        .macos => {
            exe.linkSystemLibrary("sdl2");
            exe.linkSystemLibrary("GLEW");
        },
        else => unreachable, // unsupported target
    }
    exe.linkLibC();

    // Only output debug messages in debug build
    if (optimize == .Debug) {
        exe.subsystem = .Console;
    } else {
        exe.subsystem = .Windows;
    }

    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 ZigNES");
    run_step.dependOn(&run_cmd.step);

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

    const run_tests = b.addRunArtifact(tests);
    const test_step = b.step("test", "Run tests");
    test_step.dependOn(&run_tests.step);
}
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

No branches or pull requests

1 participant