Install JSON target in rustup

I have a x86_64-unknown-none target description in JSON for an operating system so that I can prevent packaging the machine object code inside a more complex Windows portable executable (.exe) in my case. I have followed this tutorial to some extent and then read the bootloader crate README.

{
    "llvm-target": "x86_64-unknown-none",
    "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": "64",
    "target-c-int-width": "32",
    "os": "none",
    "executables": true,
    "linker-flavor": "ld.lld",
    "linker": "rust-lld",
    "panic-strategy": "abort",
    "disable-redzone": true,
    "features": "-mmx,-sse,+soft-float"
}

I would like to install that target in my local environment so that I can prevent having path errors (actually I was referring to the JSON as x64.json and it hasn't been found during build somewhere, even though I have prepended the due ../ segments).

I have read Adding a new target in rustc-dev-guide.rust-lang.org, but I do not know where to place the rustc_target crate. I am inside a workspace after reading Template: Create a Disk Image which is outside of the first two blog articles I read in the tutorial.

  • There is no supported_targets! macro in the standard library.
  • There is no rustc-target package in crates.io that I would fork into my workspace.

rustc-dev-guide is about how to make modifications to the compiler. the rustc_target crate is part of the compiler internals, and supported_targets! is also part of the compiler internals.

if you're doing osdev, you generally don't need to recompile rustc unless you want to port rustc to your new OS (but that's way down the line, you need a mostly functional kernel and a decent userspace for that)

is there a reason you can't just do cargo build --target x86_64-unknown-none?

2 Likes

I'm unsure as to whether rustup target add x86_64-unknown-none will download a target that matches my specification.

C:\Users\hydro>rustup target add x86_64-unknown-none
info: downloading component 'rust-std' for 'x86_64-unknown-none'
info: installing component 'rust-std' for 'x86_64-unknown-none'
 11.2 MiB /  11.2 MiB (100 %)   4.7 MiB/s in  2s ETA:  0s

The built-in targets live in the compiler-internal rustc_target crate. Looking specifically at x86_64-unknown-none, it's very similar to your target, but the built-in uses ELF as the final stage, which you'd then have to objcopy to a non-ELF version.

2 Likes

Thanks for the details!

I have just run cargo run and got an EFI file (containing the ELF).

Now I just have the issue that cargo run should also emulate the operating system in QEMU, but maybe I should open an issue somewhere else.

1 Like

You can actually pass path to your-target-spec.json to cargo build --target.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.