From b7650aaa77634e42d62ed6fc1a9081b10139618b Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sat, 28 Nov 2020 19:32:43 +0000 Subject: [PATCH] rust: Clean up target configs and test some more See the new docs for details. The difference is vis-a-vis older versions of this PR, not master. --- doc/languages-frameworks/rust.section.md | 14 ++--- pkgs/development/compilers/rust/default.nix | 6 +-- pkgs/test/rust-sysroot/default.nix | 60 +++++++++++++-------- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/doc/languages-frameworks/rust.section.md b/doc/languages-frameworks/rust.section.md index 28e488fe8a6..0230993d3f0 100644 --- a/doc/languages-frameworks/rust.section.md +++ b/doc/languages-frameworks/rust.section.md @@ -71,14 +71,14 @@ By default, it takes the `stdenv.hostPlatform.config` and replaces components where they are known to differ. But there are ways to customize the argument: - To choose a different target by name, define - `stdenv.hostPlatform.rustc.arch.config` as that name (a string), and that + `stdenv.hostPlatform.rustc.config` as that name (a string), and that name will be used instead. For example: ```nix import { crossSystem = (import ).systems.examples.armhf-embedded // { - rustc.arch.config = "thumbv7em-none-eabi"; + rustc.config = "thumbv7em-none-eabi"; }; } ``` @@ -88,18 +88,18 @@ where they are known to differ. But there are ways to customize the argument: ``` - To pass a completely custom target, define - `stdenv.hostPlatform.rustc.arch.config` with its name, and - `stdenv.hostPlatform.rustc.arch.custom` with the value. The value will be + `stdenv.hostPlatform.rustc.config` with its name, and + `stdenv.hostPlatform.rustc.platform` with the value. The value will be serialized to JSON in a file called - `${stdenv.hostPlatform.rustc.arch.config}.json`, and the path of that file + `${stdenv.hostPlatform.rustc.config}.json`, and the path of that file will be used instead. For example: ```nix import { crossSystem = (import ).systems.examples.armhf-embedded // { - rustc.arch.config = "thumb-crazy"; - rustc.arch.custom = { foo = ""; bar = ""; }; + rustc.config = "thumb-crazy"; + rustc.platform = { foo = ""; bar = ""; }; }; } will result in: diff --git a/pkgs/development/compilers/rust/default.nix b/pkgs/development/compilers/rust/default.nix index 95febd8945d..25876cc6380 100644 --- a/pkgs/development/compilers/rust/default.nix +++ b/pkgs/development/compilers/rust/default.nix @@ -27,7 +27,7 @@ # Returns the name of the rust target, even if it is custom. Adjustments are # because rust has slightly different naming conventions than we do. toRustTarget = platform: with platform.parsed; let - cpu_ = platform.rustc.arch or { + cpu_ = platform.rustc.platform.arch or { "armv7a" = "armv7"; "armv7l" = "armv7"; "armv6l" = "arm"; @@ -38,8 +38,8 @@ # Returns the name of the rust target if it is standard, or the json file # containing the custom target spec. toRustTargetSpec = platform: - if (platform.rustc.arch or {}) ? custom - then builtins.toFile (platform.rustc.config + ".json") (builtins.toJSON platform.rustc.arch.custom) + if (platform.rustc or {}) ? platform + then builtins.toFile (toRustTarget platform + ".json") (builtins.toJSON platform.rustc.platform) else toRustTarget platform; # This just contains tools for now. But it would conceivably contain diff --git a/pkgs/test/rust-sysroot/default.nix b/pkgs/test/rust-sysroot/default.nix index cd07ef53c32..3a786ad6f00 100644 --- a/pkgs/test/rust-sysroot/default.nix +++ b/pkgs/test/rust-sysroot/default.nix @@ -1,6 +1,7 @@ -{ lib, rustPlatform, fetchFromGitHub, writeText }: +{ lib, rust, rustPlatform, fetchFromGitHub }: -rustPlatform.buildRustPackage rec { +let + mkBlogOsTest = target: rustPlatform.buildRustPackage rec { name = "blog_os-sysroot-test"; src = fetchFromGitHub { @@ -12,27 +13,7 @@ rustPlatform.buildRustPackage rec { cargoSha256 = "1cbcplgz28yxshyrp2krp1jphbrcqdw6wxx3rry91p7hiqyibd30"; - # The book uses rust-lld for linking, but rust-lld is not currently packaged for NixOS. - # The justification in the book for using rust-lld suggests that gcc can still be used for testing: - # > Instead of using the platform's default linker (which might not support Linux targets), - # > we use the cross platform LLD linker that is shipped with Rust for linking our kernel. - # https://github.com/phil-opp/blog_os/blame/7212ffaa8383122b1eb07fe1854814f99d2e1af4/blog/content/second-edition/posts/02-minimal-rust-kernel/index.md#L157 - target = writeText "x86_64-blog_os.json" '' - { - "llvm-target": "x86_64-unknown-none", - "data-layout": "e-m:e-i64:64-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": "gcc", - "panic-strategy": "abort", - "disable-redzone": true, - "features": "-mmx,-sse,+soft-float" - } - ''; + inherit target; RUSTFLAGS = "-C link-arg=-nostartfiles"; @@ -42,5 +23,38 @@ rustPlatform.buildRustPackage rec { meta = with lib; { description = "Test for using custom sysroots with buildRustPackage"; maintainers = with maintainers; [ aaronjanse ]; + platforms = lib.platforms.x86_64; }; + }; + + # The book uses rust-lld for linking, but rust-lld is not currently packaged for NixOS. + # The justification in the book for using rust-lld suggests that gcc can still be used for testing: + # > Instead of using the platform's default linker (which might not support Linux targets), + # > we use the cross platform LLD linker that is shipped with Rust for linking our kernel. + # https://github.com/phil-opp/blog_os/blame/7212ffaa8383122b1eb07fe1854814f99d2e1af4/blog/content/second-edition/posts/02-minimal-rust-kernel/index.md#L157 + targetContents = { + "llvm-target" = "x86_64-unknown-none"; + "data-layout" = "e-m:e-i64:64-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" = "gcc"; + "panic-strategy" = "abort"; + "disable-redzone" = true; + "features" = "-mmx,-sse,+soft-float"; + }; + +in { + blogOS-targetByFile = mkBlogOsTest (builtins.toFile "x86_64-blog_os.json" (builtins.toJSON targetContents)); + blogOS-targetByNix = let + plat = lib.systems.elaborate { config = "x86_64-none"; } // { + rustc = { + config = "x86_64-blog_os"; + platform = targetContents; + }; + }; + in mkBlogOsTest (rust.toRustTargetSpec plat); }