From 4d8cd6bee549931bd81302d7b1bc8665c917c793 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Sat, 10 Feb 2024 11:49:40 +0000 Subject: [PATCH] nix: Use Rust in sel4test --- hacking/nix/scope/default.nix | 1 + hacking/nix/scope/sel4test/default.nix | 81 +++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/hacking/nix/scope/default.nix b/hacking/nix/scope/default.nix index 907181d45..00cb28fd5 100644 --- a/hacking/nix/scope/default.nix +++ b/hacking/nix/scope/default.nix @@ -184,6 +184,7 @@ superCallPackage ../rust-utils {} self // # TODO name more configurations sel4test = makeOverridable' mkSeL4Test { + rust = hostPlatform.isAarch || hostPlatform.isRiscV || hostPlatform.isx86_64; # mcs = true; }; diff --git a/hacking/nix/scope/sel4test/default.nix b/hacking/nix/scope/sel4test/default.nix index c762d7656..a50760546 100644 --- a/hacking/nix/scope/sel4test/default.nix +++ b/hacking/nix/scope/sel4test/default.nix @@ -14,6 +14,16 @@ , qemuForSeL4 , git , sources +, crateUtils +, defaultRustToolchain +, bareMetalRustTargetInfo +, libclangPath +, vendoredTopLevelLockfile +, buildSysroot +, crates +, pruneLockfile +, topLevelLockfile +, vendorLockfile , runCommandCC , seL4Arch }: @@ -21,12 +31,17 @@ { mcs ? false , smp ? false , virtualization ? false +, rust ? true +, release ? false , filter ? ".*" }: with lib; let + rustToolchain = defaultRustToolchain; + rustTargetInfo = bareMetalRustTargetInfo; + initBuildArgs = let bool = v: if v then "TRUE" else "FALSE"; @@ -36,7 +51,14 @@ let "-DMCS=${bool mcs}" "-DSMP=${bool smp}" "-DSIMULATION=TRUE" + "-DLibSel4UseRust=${bool rust}" "-DLibSel4TestPrinterRegex='${filter}'" + ] ++ lib.optionals rust [ + "-DHACK_RUST_TARGET=${rustTargetInfo.name}" + "-DHACK_CARGO_MANIFEST_PATH=${workspace}/Cargo.toml" + "-DHACK_CARGO_CONFIG=${cargoConfig}" + "-DHACK_CARGO_NO_BUILD_SYSROOT=TRUE" + "-DHACK_CARGO_RELEASE=${bool release}" ] ++ lib.optionals hostPlatform.isi686 [ "-DPLATFORM=pc99" ] ++ lib.optionals hostPlatform.isRiscV [ @@ -50,7 +72,60 @@ let "-DARM_CPU=cortex-a15" ]; - kernelSrc = sources.seL4.rust; + kernelSrc = sources.seL4.rust-sel4test; + + cratesSrc = crateUtils.collectReals (lib.attrValues (crateUtils.getClosureOfCrate rootCrate)); + + rootCrate = crates.sel4-sys-wrappers; + + lockfile = builtins.toFile "Cargo.lock" lockfileContents; + lockfileContents = builtins.readFile lockfileDrv; + lockfileDrv = pruneLockfile { + superLockfile = topLevelLockfile; + superLockfileVendoringConfig = vendoredTopLevelLockfile.configFragment; + rootCrates = [ rootCrate ]; + }; + + cargoConfig = crateUtils.toTOMLFile "config" (crateUtils.clobber [ + (crateUtils.baseConfig { + inherit rustToolchain; + rustTargetName = rustTargetInfo.name; + }) + { + target.${rustTargetInfo.name}.rustflags = [ + "--sysroot" sysroot + ]; + } + (vendorLockfile { inherit lockfileContents; }).configFragment + ]); + + profiles = { + profile.release = { + debug = 0; + opt-level = "z"; + }; + }; + + manifest = crateUtils.toTOMLFile "Cargo.toml" (crateUtils.clobber [ + { + workspace.resolver = "2"; + workspace.members = [ "src/${rootCrate.name}" ]; + } + profiles + ]); + + workspace = linkFarm "workspace" [ + { name = "Cargo.toml"; path = manifest; } + { name = "Cargo.lock"; path = lockfile; } + { name = "src"; path = cratesSrc; } + ]; + + sysroot = buildSysroot { + release = false; # TODO why? + inherit rustTargetInfo; + extraManifest = profiles; + compilerBuiltinsWeakIntrinsics = true; + }; tests = stdenv.mkDerivation { name = "sel4test"; @@ -62,6 +137,8 @@ let sha256 = "sha256-1Gmbksgh2VTUggM6qcawRC9b+g/bwB8tWGfUzCg1A0U="; }; + LIBCLANG_PATH = libclangPath; + depsBuildBuild = lib.optionals (buildPlatform != hostPlatform) [ buildPackages.stdenv.cc # NOTE: cause drv.__spliced.buildBuild to be used to work around splicing issue @@ -72,6 +149,7 @@ let cmake ninja libxml2 dtc cpio protobuf git + defaultRustToolchain ] ++ (with python3Packages; [ aenum plyplus pyelftools simpleeval sel4-deps @@ -126,6 +204,7 @@ let installPhase = '' cd .. mv build $out + rm -rf $out/libsel4/rust/target ''; dontFixup = true;