gpt4 book ai didi

How to objcopy a bin file as part of a Zig build script(如何将bin文件复制为Zig构建脚本的一部分)

转载 作者:bug小助手 更新时间:2023-10-24 18:43:24 30 4
gpt4 key购买 nike



How can I export a binary output from my elf output as part of my Zig build process?
This should happen as part of the default build command zig build.

作为Zig构建过程的一部分,如何从ELF输出中导出二进制输出?这应该作为默认构建命令Zig Build的一部分执行。


I am using Zig 0.11.

我正在使用Zig 0.11。


My build.zig contains the code below. I have added comments to describe what I think should be happening.

我的Build.zig包含以下代码。我添加了一些评论来描述我认为应该发生的事情。


const std = @import("std");
const Builder = std.build.Builder;

pub fn build(b: *Builder) void {
const optimization = std.builtin.OptimizeMode.ReleaseFast;
const cpu_model = &std.Target.arm.cpu.cortex_m0;

// elf should be a build step that emits output.elf
const elf = b.addExecutable(.{
.name = "output.elf",
.root_source_file = .{ .path = "src/startup.zig" },
.target = .{
.cpu_arch = .thumb,
.os_tag = .freestanding,
.abi = .none,
.cpu_model = .{ .explicit = cpu_model },
},
.optimize = optimization,
});

elf.setLinkerScript(.{ .path = "src/STM32Z/MCU/STM32L052.ld" });

// bin should be a build step that objcopy's output.bin from output.elf
const bin = b.addObjCopy(.{ elf.getEmittedBin(), .{
.format = .bin,
});
// This step requires output.elf to exist
bin.step.dependOn(&elf.step);

// This is the step that I want to be completed when I run `zig build`.
// I would expect zig build to generate the elf and then the bin file
b.default_step = &bin.step;
}

This builds with no error at all. Yet a zig-out directory is not created, so I cannot find either output.elf or output.bi.

这一构建完全没有错误。但是没有创建zig-out目录,所以我既找不到output.self也找不到output.bi。


I'm not really clear on:

我不是很清楚:



  1. If elf.getEmittedBin() is the correct thing to pass into the b.addObjCopy method

  2. How to debug what steps are getting run at all.

  3. How to declare what my output artifact is and where it should go. I assume I have to somehow specify that output.bin is the thing I want.


Sorry for the wall of text - I'm just feeling a bit stumped here.

很抱歉有这么多文字--我只是觉得有点被难住了。


更多回答
优秀答案推荐

Turns out I was actually pretty close to the solution.

事实证明,我实际上离解决方案很近了。


I just needed a step to copy the bin file to my zig-out directory. This step was b.addInstallBinFile(bin.getOutput(), "output.bin");

我只需要一个步骤就可以将bin文件复制到我的zig-out目录。此步骤为b.addInstallBinFile(bin.getOutput(),“output.bin”);


I was mostly confused with the nomenclature here - the build outputs seem to be referred to as "install artifacts" or "install files". This makes some sense in hindsight.

我主要混淆了这里的术语--构建输出似乎被称为“安装构件”或“安装文件”。事后看来,这是有一定道理的。


My full final build script here:

我的完整最终构建脚本如下:


const std = @import("std");
const Builder = std.build.Builder;

pub fn build(b: *Builder) void {
const optimization = std.builtin.OptimizeMode.ReleaseSmall;
const cpu_model = &std.Target.arm.cpu.cortex_m0;

// Build the elf
const elf = b.addExecutable(.{
.name = "output.elf",
.root_source_file = .{ .path = "src/startup.zig" },
.target = .{
.cpu_arch = .thumb,
.os_tag = .freestanding,
.abi = .none,
.cpu_model = .{ .explicit = cpu_model },
},
.optimize = optimization,
.single_threaded = true,
});
elf.setLinkerScript(.{ .path = "src/STM32Z/MCU/STM32L052.ld" });

// Copy the elf to the output directory.
const copy_elf = b.addInstallArtifact(elf, .{});
b.default_step.dependOn(&copy_elf.step);

// Copy the bin out of the elf
const bin = b.addObjCopy(elf.getEmittedBin(), .{
.format = .bin,
});
bin.step.dependOn(&elf.step);

// Copy the bin to the output directory
const copy_bin = b.addInstallBinFile(bin.getOutput(), "output.bin");
b.default_step.dependOn(&copy_bin.step);
}

更多回答

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com