Build error - "error: no field named 'cpu_arch' in struct 'Build.ResolvedTarget' "

Hi all -

I think I’m very close to getting a successful build but I’m getting this error -

andy@obsidian:~/Zig-DOS$ zig build
/home/andy/Zig-DOS/build.zig:20:19: error: no field named 'cpu_arch' in struct 'Build.ResolvedTarget'
    kernel_target.cpu_arch = .x86_64;
                          ^~~~~~~~  

I did a web search - not much joy there. Looked at the docs and found this -

host: ResolvedTarget
Deprecated. Use b.graph.host.

Ok, fine, I’ll try that. So, I put this in my build script -

kernel_target.graph.host = .x86_64;

After that, I got this -

andy@obsidian:~/Zig-DOS$ zig build
/home/andy/Zig-DOS/build.zig:21:19: error: no field named 'graph' in struct 'Build.ResolvedTarget'
    kernel_target.graph.host = .x86_64;
                          ^~~~~  

Arrgh!

I tried to do the right thing, searching around and looking at the docs before coming here, but doing that didn’t help in this instance.
So, I’m hoping someone may be able to help.

Btw - here is my build script -


//  build.zig  
//  The OS build file.  
//  This code is released to the public domain. 
//  "Share and enjoy....."   :)  

const std = @import("std");

pub fn build(b: *std.Build) void {
    // Standard target options allow the user to specify target via command line
    const optimize = b.standardOptimizeOption(.{});

    // Get the user-provided target options (optional)
    const base_target = b.standardTargetOptions(.{});
      
    // Create a mutable copy for the kernel target
    var kernel_target = base_target ;    // orelse std.build.UserTarget.init();
    // kernel_target.cpu_arch = .x86_64;
    kernel_target.graph.host = .x86_64;
    kernel_target.os_tag = .freestanding;

    // Create a mutable copy for the bootloader target
    var bootloader_target = base_target ;  // orelse std.build.UserTarget.init();
    bootloader_target.cpu_arch = .x86_64;
    bootloader_target.os_tag = .uefi;

    // Create an executable for the kernel
    const kernel = b.addExecutable(.{
        .name = "Zig-DOS-Kernel",
        .root_source_file = b.path("src/kernel/main.zig"),
        .target = kernel_target,
        .optimize = optimize,
    });

    // Install the kernel artifact
    b.installArtifact(kernel);

    // Build the bootloader with the correct entry point
    const bootloader = b.addExecutable(.{
        .name = "Zig-DOS-Bootloader",
        .root_source_file = b.path("src/boot/main.zig"),
        .target = bootloader_target,
        .optimize = optimize,
        .entry_symbol = "efiMain",
    });

    // Install the bootloader artifact
    b.installArtifact(bootloader);

}

Many thanks in advance! Bye for now -

  • mooseman

To find the usage: start from ziglang.org, select Download, click Standard Library Documentation and search for standardTargetOptions, click on the std.Build.standardTargetOptions then click on the StandardTargetOptionsArgs and finally on Target.Query

const kernel_target = b.standardTargetOptions(.{
    .default_target = .{
        .cpu_arch = .x86_64,
        .os_tag = .freestanding,
    },
});

Hi dimdin -

Thanks very much for that - that’s awesome!

I’ve bookmarked each of the solutions you’ve given me - they’ll be a big help in future. So will how to use the docs (which you’ve shown me here).

I have to give credit to the Zig team - there is a huge amount of documentation - it’s a matter of knowing how to find your way through it, as you’ve shown me here.

Many thanks again! Cheers -

  • mooseman
1 Like