Associating cpu and target with C source file in my build.zig

i just moved to 0.12.0-dev.3489, where i updated my current use of zig build-exe as follows (scraping some lines from my makefile):

	--name main \
	-fno-lto \
	-mcpu cortex_m0plus \
	-target thumb-freestanding-eabi \
	-O ReleaseSmall \
	-fentry=__em_program_start \
	--script etc/linkcmd.ld \

	zig build-exe $(ZIGOPTS) $(ZIGEXE) src/main.zig etc/startup.c

i’m in the process of moving to a build.zig that does the same thing, but have run into some problems… here’s what i started with:

pub fn build(b: *std.Build) void {
    b.verbose = true;
    const target = b.resolveTargetQuery(.{
        .cpu_arch = .thumb,
        .cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m0plus },
        .os_tag = .freestanding,
        .abi = .eabi,
    const optimize = std.builtin.OptimizeMode.ReleaseSmall;
    const exe = b.addExecutable(.{
        .name = "main",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
        .file = .{ .path = "etc/startup.c" },

        // not sure if i need this???

        //.flags = &.{
        //    "-mcpu cortex_m0plus",
        //    "--target thumb-freestanding-eabi",
    exe.setLinkerScript(.{ .path = "etc/linkcmd.ld" });
    exe.entry = .{ .symbol_name = "__em_program_start" };

when i build, i get the following errors from the linker:

C:\tools\zig-dev\zig.exe build-exe -fentry=__em_program_start C:\Users\biosb\gitrepos\zigem\etc\startup.c -OReleaseSmall -target thumb-freestanding-eabi -mcpu cortex_m0plus -Mroot=C:\Users\biosb\gitrepos\zigem\src\main.zig --cache-dir C:\Users\biosb\gitrepos\zigem\zig-cache --global-cache-dir C:\Users\biosb\AppData\Local\zig --name main --script C:\Users\biosb\gitrepos\zigem\etc\linkcmd.ld --listen=-
+- install main
   +- zig build-exe main ReleaseSmall thumb-freestanding-eabi failure
error: warning(link): unexpected LLD stderr:
ld.lld: warning: Linking two modules of different target triples: 'C:\Users\biosb\gitrepos\zigem\zig-cache\o\c9f1fc0feff79cdfb0dfcf745fd4a745\main.o' 
is 'thumb-unknown-unknown-eabi' whereas 'ld-temp.o' is 'thumbv4t-unknown-unknown-eabi'

i would have thought my specification of the target would apply to etc/startup.c as well… note that in my hand-crafted call to zig build-exe in my makefile that etc/startup.c appears at the END of the command-line…

reading about the .flags field when adding my C source file, i fiddled around with various options here without success… it appears these are passed through to clang via -cflags but i could never seem to match my orignal `zig build-exe -mcpu and -target’ options…

FWIW, i passed the --verbose-cc option to zig build-exe (hoping to learn how it invokes clang) but nothing came out…

given that my original makefile “works” (i can actually build/load executables on my bare-metal MCU), i could use a little guidance in creating an equivalent build.zig

just skimmed through this on some recent changes to cross-compilation)…

not sure i fully grok this yet, but should i be creating a separate module that encapsulates my C file – and presumably reuse the same target therein ???

as another approach, could i @cInclude my C code in some other .zig module that presumably IS built consistently with my main.zig ???

There is an open issue for that:


    exe.want_lto = false;