diff --git a/Makefile b/Makefile index e6e378fe0..95bc4b914 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,8 @@ # make qemu the library as a plain library (cargo xtask dist), then # vendor/qemu's qemu-system-x86_64 with reims-graphics-pci on # its C API, linked statically through pkg-config, in -# build/qemu (docs/qemu.md) +# build/qemu (docs/qemu.md), with a copy of the dist tree's +# stage helper, reims-vgpu-compile, beside it # make vulkan-links # on a Mac, links in build/lib to Homebrew's Vulkan loader and # Mesa's KosmicKrisp driver, which the library's rt platform @@ -32,6 +33,10 @@ RUST_TARGET_REL := $(or $(CARGO_TARGET_DIR),target) RUST_TARGET := $(if $(filter /%,$(RUST_TARGET_REL)),$(RUST_TARGET_REL),$(CURDIR)/$(RUST_TARGET_REL)) # The dist tree cargo xtask dist writes (xtask/src/dist.rs). RUST_DIST := $(RUST_TARGET)/dist-vulkan +# The library's stage helper, in the dist tree's bin, which the library starts +# from beside the process's executable +# (crates/reims-vgpu-vulkan/src/rt_platform/stage.rs). +HELPER := reims-vgpu-compile$(if $(filter Windows_NT,$(OS)),.exe) .PHONY: all qemu vulkan-links clean @@ -91,6 +96,7 @@ qemu: all vendor/qemu/configure vulkan-links fi @git -C "$(QEMU_SRC)" rev-parse HEAD >"$(QEMU_BUILD)/qemu-source" 2>/dev/null || rm -f "$(QEMU_BUILD)/qemu-source" ninja -C "$(QEMU_BUILD)" $(filter-out -j,$(filter -j%,$(MAKEFLAGS))) qemu-system-x86_64 + cp -p "$(RUST_DIST)/bin/$(HELPER)" "$(QEMU_BUILD)/$(HELPER)" # On a Mac, the library's rt platform loads the Vulkan loader and Mesa's # KosmicKrisp driver only from beside the process's executable or from ../lib diff --git a/docs/qemu.md b/docs/qemu.md index a8e8012b2..f03736884 100644 --- a/docs/qemu.md +++ b/docs/qemu.md @@ -33,6 +33,12 @@ guest. Anything not measured is marked **unknown**. make qemu # cargo xtask dist, then build/qemu/qemu-system-x86_64 ``` +Beside the binary it copies the dist tree's `bin/reims-vgpu-compile`, the +library's stage helper, which the library starts from beside the process's +executable to compile a kernel's pipeline past rt's stage bound +(`crates/reims-vgpu-vulkan/src/rt_platform/stage.rs`). A QEMU without it +beside it refuses those pipelines. + `make qemu` runs `cargo xtask dist`, which writes the library into the dist tree, `/dist-vulkan` (`target`, or `CARGO_TARGET_DIR`), on Vulkan: on a Mac on Mesa's KosmicKrisp (below). Then it configures `vendor/qemu` out of tree in @@ -2295,6 +2301,7 @@ PKG_CONFIG_PATH=$PWD/../../target/dist-vulkan/lib/pkgconfig ../../vendor/qemu/co --target-list=aarch64-softmmu --enable-hvf --enable-reims-vgpu --enable-gnutls \ --disable-docs --disable-bsd-user --disable-linux-user --disable-tools make qemu-system-aarch64 +cp -p ../../target/dist-vulkan/bin/reims-vgpu-compile . # the stage helper, above ``` configure's summary says `reims-vgpu support: YES` and `HVF support: YES`, diff --git a/scripts/install-ctl.sh b/scripts/install-ctl.sh index c87f55c15..884a4a55d 100755 --- a/scripts/install-ctl.sh +++ b/scripts/install-ctl.sh @@ -25,7 +25,8 @@ # build fetches what it needs); cargo fetches metal2vulkan itself, at the # commit the library's Cargo.toml names; # 3. builds the library (cargo xtask dist) on Vulkan and QEMU on it, in -# ~/.cache/reims-ctl, and installs QEMU into +# ~/.cache/reims-ctl, and installs QEMU, with the library's stage helper +# beside its binaries, into # ~/.local/share/reims-ctl/qemu/, linking qemu/current to it. A # build never replaces another: a running VM keeps the binary it started # with; @@ -674,6 +675,12 @@ mac_vulkan_links() { COMMIT="$(git -C "$ROOT" rev-parse --short HEAD)" TARGET="${CARGO_TARGET_DIR:-$ROOT/target}" DIST="$TARGET/dist-vulkan" +# The library's stage helper, which compiles a kernel past rt's stage bound in +# a process of its own: the library starts it from beside QEMU's binary +# (crates/reims-vgpu-vulkan/src/rt_platform/stage.rs), and the dist tree has it +# in its bin. +HELPER=reims-vgpu-compile +[[ $WINDOWS == 1 ]] && HELPER+=.exe # Where QEMU builds and is staged: the cache directory, else, where its path # holds a space (a Windows user's folder: "paths" above), the target directory. QEMU_BUILDS="$CACHE" @@ -683,6 +690,7 @@ if [[ $SKIP_QEMU == 0 ]]; then say "Building the library (cargo xtask dist --platform vulkan)" (cd "$ROOT" && CARGO_TARGET_DIR="$TARGET" cargo xtask dist --platform vulkan) [[ -f "$DIST/lib/pkgconfig/reims-vgpu.pc" ]] || die "cargo xtask dist wrote no $(shown "$DIST/lib/pkgconfig/reims-vgpu.pc")" + [[ -x "$DIST/bin/$HELPER" ]] || die "cargo xtask dist wrote no $(shown "$DIST/bin/$HELPER")" BUILD="$QEMU_BUILDS/qemu-build-$PLATFORM" PC="$DIST/lib/pkgconfig" @@ -732,13 +740,15 @@ if [[ $SKIP_QEMU == 0 ]]; then staged="$(find "$STAGE" -path "*/bin/$QEMU_BINARY" -print -quit)" [[ -n $staged ]] || die "QEMU's install has no bin/$QEMU_BINARY" PREFIX="${staged%/bin/*}" PREFIX="${PREFIX#"$STAGE"}" - # The CLANG64 DLLs QEMU and qemu-img load, copied beside them: reims-ctl - # starts QEMU outside MSYS2, and an update of MSYS2's packages then changes - # no build a VM runs. libva's and its vaon12 driver too, with what they - # load, which the library opens by name when the device's video starts - # (so ldd lists none of them): libva looks for the driver beside itself. + cp "$DIST/bin/$HELPER" "$STAGE$PREFIX/bin/" + # The CLANG64 DLLs QEMU, qemu-img and the stage helper load, copied beside + # them: reims-ctl starts QEMU outside MSYS2, and an update of MSYS2's + # packages then changes no build a VM runs. libva's and its vaon12 driver + # too, with what they load, which the library opens by name when the + # device's video starts (so ldd lists none of them): libva looks for the + # driver beside itself. { - for exe in "$QEMU_BINARY" qemu-img.exe; do + for exe in "$QEMU_BINARY" qemu-img.exe "$HELPER"; do ldd "$STAGE$PREFIX/bin/$exe" | awk '$3 ~ "^/clang64/" { print $3 }' done for dll in libva libva_win32 vaon12_drv_video; do @@ -748,6 +758,7 @@ if [[ $SKIP_QEMU == 0 ]]; then } | sort -u | while read -r dll; do cp "$dll" "$STAGE$PREFIX/bin/"; done else DESTDIR="$STAGE" ninja -C "$BUILD" install >/dev/null + cp "$DIST/bin/$HELPER" "$STAGE$PREFIX/bin/" fi if [[ $OS == Darwin ]]; then mac_vulkan_links "$STAGE$PREFIX/lib" @@ -790,6 +801,8 @@ else for bin in "${QEMU_BINARIES[@]}"; do [[ -x $QEMU_DIR/bin/$bin ]] || warn "the installed QEMU has no $bin, which $([[ $bin == qemu-system-aarch64* ]] && echo arm64 || echo x86-64) VMs run on: run this without --skip-qemu to build it" done + # A QEMU installed before the library had a stage helper has none beside it. + [[ -x $QEMU_DIR/bin/$HELPER ]] || warn "the installed QEMU has no $HELPER beside it, so its VMs' heaviest GPU programs are refused: run this without --skip-qemu to build it" fi # ------------------------------------------------------------------ reims-ctl diff --git a/xtask/src/dist.rs b/xtask/src/dist.rs index 5e5eafbeb..9b4bc8c5f 100644 --- a/xtask/src/dist.rs +++ b/xtask/src/dist.rs @@ -7,6 +7,9 @@ //! `lib/libreims_vgpu.dll` on Windows. How each OS builds and names them is its own module's: //! `macos`, `linux`, `windows`. //! - `include/reims_vgpu.h`, the committed header. +//! - `bin/reims-vgpu-compile` (`.exe` on Windows), the program the library starts beside its +//! host's executable to compile a kernel past rt's stage bound ([`HELPER`]). A host's install +//! puts it beside the host's executable. //! - `lib/pkgconfig/reims-vgpu.pc`. Its `Libs.private` names every library a static link of the //! archive needs. //! - `share/doc/reims-vgpu/`: the license, `LICENSE` and the texts in `LICENSES/` it points at, @@ -54,6 +57,9 @@ pub const LIB: &str = "reims_vgpu"; pub const PACKAGE: &str = "reims-vgpu"; /// The header, under the name it is committed and installed with. pub const HEADER: &str = "reims_vgpu.h"; +/// The stage helper's program (`reims_vgpu_vulkan::rt_platform::stage`), `reims-vgpu-vulkan`'s, +/// in the tree's `bin` with the OS's executable suffix ([`helper_file`]). +pub const HELPER: &str = "reims-vgpu-compile"; /// The file that states the license, in the repository and in the tree's [`doc_dir`]. pub const LICENSE: &str = "LICENSE"; /// The directory of the license's texts, `.txt` each, in the repository and in @@ -159,6 +165,36 @@ pub fn c_api_args(o: &Options) -> Vec { ]) } +/// The helper's file name, in the build and in the tree: [`HELPER`] with the OS's executable +/// suffix. +pub fn helper_file() -> String { + format!("{HELPER}{}", std::env::consts::EXE_SUFFIX) +} + +/// The Cargo arguments every OS's build of the helper starts its own with, after the subcommand: +/// [`HELPER`] in the profile, with `reims-vgpu-c` selected beside its package, so the crates the +/// two share are resolved with the library's build's features, and built once. +pub fn helper_args(o: &Options) -> Vec { + strs(&[ + "--profile", + o.profile.0.as_str(), + "-p", + "reims-vgpu-c", + "-p", + "reims-vgpu-vulkan", + "--bin", + HELPER, + ]) +} + +/// Builds the helper with Cargo's `args` and installs it from `built`, the profile's directory of +/// the build, to `to`, with mode 0755. +fn install_helper(tools: &mut dyn Tools, args: &[String], built: &Path, to: &Path) -> Res<()> { + let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_owned()); + tools.run(&cargo, args)?; + install(&built.join(helper_file()), to, 0o755) +} + /// `reims-vgpu.pc`. Its prefix is the file's grandparent directory (`${pcfiledir}`), so the tree /// can be moved. pub fn pc_file(version: &str, libs_private: &[String]) -> String { @@ -179,13 +215,14 @@ pub fn pc_file(version: &str, libs_private: &[String]) -> String { } /// The files `dist` writes under its output directory, relative to it: the archive, the shared -/// library (`native::SHARED_LIBRARY_EXTENSION`), the package and the header. -pub fn layout() -> [String; 4] { +/// library (`native::SHARED_LIBRARY_EXTENSION`), the package, the header and the helper. +pub fn layout() -> [String; 5] { [ format!("lib/lib{LIB}.a"), format!("lib/lib{LIB}.{}", native::SHARED_LIBRARY_EXTENSION), format!("lib/pkgconfig/{PACKAGE}.pc"), format!("include/{HEADER}"), + format!("bin/{}", helper_file()), ] } @@ -628,10 +665,12 @@ mod tests { #[test] fn layout_matches_what_the_pc_file_names() { let pc = pc_file("0", &[]); - let [archive, _shared, pcpath, header] = layout(); + let [archive, _shared, pcpath, header, helper] = layout(); assert_eq!(archive, "lib/libreims_vgpu.a"); assert_eq!(pcpath, "lib/pkgconfig/reims-vgpu.pc"); assert_eq!(header, "include/reims_vgpu.h"); + let suffix = if cfg!(windows) { ".exe" } else { "" }; + assert_eq!(helper, format!("bin/reims-vgpu-compile{suffix}")); assert!( pc.contains("libdir=${prefix}/lib\n") && pc.contains("includedir=${prefix}/include\n") ); diff --git a/xtask/src/dist/linux.rs b/xtask/src/dist/linux.rs index a5ec0aa61..239ca69c4 100644 --- a/xtask/src/dist/linux.rs +++ b/xtask/src/dist/linux.rs @@ -29,7 +29,10 @@ use super::archive::{ LOCALIZED, api_functions, check_globals, defined_globals, native_static_libs, nm_args, }; use super::order; -use super::{LIB, Options, c_api_args, install, layout, pc_file, strs, strs_with, write}; +use super::{ + LIB, Options, c_api_args, helper_args, install, install_helper, layout, pc_file, strs, + strs_with, write, +}; use crate::tools::Tools; use crate::util::Res; @@ -75,7 +78,7 @@ pub fn dist(tools: &mut dyn Tools, o: &Options, target_dir: &Path, header: &Path let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_owned()); tools.run(&cargo, &cargo_args(o))?; let built = target_dir.join(o.profile.dir()); - let [archive, shared, pc, include] = layout().map(|p| o.out.join(p)); + let [archive, shared, pc, include, helper] = layout().map(|p| o.out.join(p)); localize( tools, @@ -90,9 +93,17 @@ pub fn dist(tools: &mut dyn Tools, o: &Options, target_dir: &Path, header: &Path // in the target dir, not the one installed. let libs = native_static_libs(&tools.output(&cargo, &static_libs_args(o))?)?; write(&pc, &pc_file(env!("CARGO_PKG_VERSION"), &libs))?; + install_helper(tools, &helper_build_args(o), &built, &helper)?; Ok(o.out.clone()) } +/// The `cargo build` arguments that build the helper ([`super::HELPER`]) in the profile. +pub fn helper_build_args(o: &Options) -> Vec { + let mut args = vec!["build".to_owned()]; + args.extend(helper_args(o)); + args +} + /// Writes `archive`, the archive installed, from `built`, Cargo's, in `work` (module doc): its /// members linked into one object, every global symbol but the C API's made local and the /// functions `link_order` lists laid out first ([`order_renames`]), archived alone in `work` @@ -223,6 +234,7 @@ mod tests { let target = root.join("target"); put(&target.join("release/libreims_vgpu_c.a"), "archive"); put(&target.join("release/libreims_vgpu_c.so"), "shared"); + put(&target.join("release/reims-vgpu-compile"), "helper"); let header = root.join("reims_vgpu.h"); put(&header, "header"); (root.clone(), target, header) @@ -298,8 +310,10 @@ mod tests { read("lib/pkgconfig/reims-vgpu.pc") .contains("Libs.private: -lgcc_s -lutil -lrt -lpthread -lm -ldl -lc\n") ); + assert_eq!(read("bin/reims-vgpu-compile"), "helper"); assert_eq!(mode(&out.join("lib/libreims_vgpu.so")), 0o755); assert_eq!(mode(&out.join("lib/libreims_vgpu.a")), 0o644); + assert_eq!(mode(&out.join("bin/reims-vgpu-compile")), 0o755); let work = target.join("dist-localized"); let keep = work.join("keep-global-symbols"); assert_eq!( @@ -338,7 +352,15 @@ mod tests { "{}", calls[6] ); - assert_eq!(calls.len(), 7, "{calls:?}"); + assert!( + calls[7].ends_with( + " build --profile release -p reims-vgpu-c -p reims-vgpu-vulkan \ + --bin reims-vgpu-compile" + ), + "{}", + calls[7] + ); + assert_eq!(calls.len(), 8, "{calls:?}"); std::fs::remove_dir_all(&root).unwrap(); } diff --git a/xtask/src/dist/macos.rs b/xtask/src/dist/macos.rs index a74a00638..9cf877c70 100644 --- a/xtask/src/dist/macos.rs +++ b/xtask/src/dist/macos.rs @@ -38,7 +38,10 @@ use std::path::{Path, PathBuf}; -use super::{LIB, Options, c_api_args, install, layout, pc_file, strs, strs_with, write}; +use super::{ + LIB, Options, c_api_args, helper_args, install, install_helper, layout, pc_file, strs, + strs_with, write, +}; use crate::tools::Tools; use crate::util::Res; @@ -98,17 +101,31 @@ fn shadowing_cflags(is_set: impl Fn(&str) -> bool) -> Option<&'static str> { pub fn cargo_args(o: &Options) -> Vec { let mut args = vec!["build".to_owned()]; args.extend(c_api_args(o)); - args.push("--config".to_owned()); - args.push(format!( - "env.MACOSX_DEPLOYMENT_TARGET=\"{DEPLOYMENT_TARGET}\"" - )); - args.push("--config".to_owned()); - args.push(format!("env.{TARGET_CFLAGS}=\"{}\"", sdl_cflags())); - args.push("--config".to_owned()); - args.push(format!("env.{KACHE_BUILD_SCRIPT_CACHE}=\"0\"")); + args.extend(configs()); args } +/// The `cargo build` arguments that build the helper ([`super::HELPER`]) in the profile, with +/// the library's `[env]` entries ([`cargo_args`]), so the crates the two share are built once. +pub fn helper_build_args(o: &Options) -> Vec { + let mut args = vec!["build".to_owned()]; + args.extend(helper_args(o)); + args.extend(configs()); + args +} + +/// The `--config` arguments of [`cargo_args`]: its `[env]` entries. +fn configs() -> Vec { + vec![ + "--config".to_owned(), + format!("env.MACOSX_DEPLOYMENT_TARGET=\"{DEPLOYMENT_TARGET}\""), + "--config".to_owned(), + format!("env.{TARGET_CFLAGS}=\"{}\"", sdl_cflags()), + "--config".to_owned(), + format!("env.{KACHE_BUILD_SCRIPT_CACHE}=\"0\""), + ] +} + /// The variable that, `0`, turns kache's build-script cache off (module doc): kache then installs /// no launcher in front of a script it compiles, and runs a script it installed one for as Cargo /// would. Unread where the build does not run through kache. @@ -204,7 +221,7 @@ pub fn dist(tools: &mut dyn Tools, o: &Options, target_dir: &Path, header: &Path } tools.run(&cargo, &cargo_args(o))?; let built = target_dir.join(o.profile.dir()); - let [archive, dylib, pc, include] = layout().map(|p| o.out.join(p)); + let [archive, dylib, pc, include, helper] = layout().map(|p| o.out.join(p)); localize( tools, @@ -226,6 +243,7 @@ pub fn dist(tools: &mut dyn Tools, o: &Options, target_dir: &Path, header: &Path let otool = tools.output("xcrun", &strs_with(&["otool", "-L"], &dylib))?; let libs = libs_private(&otool, INSTALL_NAME)?; write(&pc, &pc_file(env!("CARGO_PKG_VERSION"), &libs))?; + install_helper(tools, &helper_build_args(o), &built, &helper)?; Ok(o.out.clone()) } @@ -507,6 +525,7 @@ mod tests { let target = root.join("target"); put(&target.join("release/libreims_vgpu_c.a"), "archive"); put(&target.join("release/libreims_vgpu_c.dylib"), "dylib"); + put(&target.join("release/reims-vgpu-compile"), "helper"); let header = root.join("reims_vgpu.h"); put(&header, "header"); (root.clone(), target, header) @@ -528,8 +547,10 @@ mod tests { assert!(read("lib/pkgconfig/reims-vgpu.pc").contains( "Libs.private: -weak_framework AppKit -framework Metal -lSystem -liconv -lobjc\n" )); + assert_eq!(read("bin/reims-vgpu-compile"), "helper"); assert_eq!(mode(&out.join("lib/libreims_vgpu.dylib")), 0o755); assert_eq!(mode(&out.join("lib/libreims_vgpu.a")), 0o644); + assert_eq!(mode(&out.join("bin/reims-vgpu-compile")), 0o755); let dylib = out.join("lib/libreims_vgpu.dylib"); let d = dylib.to_str().unwrap(); let work = target.join("dist-localized"); @@ -556,8 +577,13 @@ mod tests { std::fs::read_to_string(work.join("sdl-cflags-release")).unwrap(), sdl_cflags() ); + assert!(calls[8].ends_with(&format!( + " build --profile release -p reims-vgpu-c -p reims-vgpu-vulkan \ + --bin reims-vgpu-compile --config env.MACOSX_DEPLOYMENT_TARGET=\"11.0\" {}", + renames() + ))); assert_eq!( - calls[2..], + calls[2..8], [ format!( "xcrun ld -r -arch arm64 -platform_version macos 11.0 11.0 \ @@ -577,6 +603,7 @@ mod tests { format!("xcrun otool -L {d}"), ] ); + assert_eq!(calls.len(), 9, "{calls:?}"); std::fs::remove_dir_all(&root).unwrap(); } diff --git a/xtask/src/dist/windows.rs b/xtask/src/dist/windows.rs index 99df5cd89..322aac11e 100644 --- a/xtask/src/dist/windows.rs +++ b/xtask/src/dist/windows.rs @@ -39,7 +39,10 @@ use super::archive::{ API_PREFIX, LOCALIZED, api_functions, check_globals, defined_globals, native_static_libs, nm_args, }; -use super::{LIB, Options, c_api_args, install, layout, pc_file, strs, strs_with, write}; +use super::{ + LIB, Options, c_api_args, helper_args, install, install_helper, layout, pc_file, strs, + strs_with, write, +}; use crate::tools::Tools; use crate::util::Res; @@ -93,7 +96,7 @@ pub fn dist(tools: &mut dyn Tools, o: &Options, target_dir: &Path, header: &Path let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_owned()); tools.run(&cargo, &cargo_args(o))?; let built = target_dir.join(TARGET).join(o.profile.dir()); - let [archive, shared, pc, include] = layout().map(|p| o.out.join(p)); + let [archive, shared, pc, include, helper] = layout().map(|p| o.out.join(p)); let work = target_dir.join(LOCALIZED); let api = localize(tools, &built.join("libreims_vgpu_c.a"), &work, &archive)?; @@ -101,9 +104,19 @@ pub fn dist(tools: &mut dyn Tools, o: &Options, target_dir: &Path, header: &Path link_shared(tools, &archive, &api, &libs, &work, &shared)?; install(header, &include, 0o644)?; write(&pc, &pc_file(env!("CARGO_PKG_VERSION"), &libs))?; + install_helper(tools, &helper_build_args(o), &built, &helper)?; Ok(o.out.clone()) } +/// The `cargo build` arguments that build the helper ([`super::HELPER`]) in the profile, for +/// [`TARGET`]. +pub fn helper_build_args(o: &Options) -> Vec { + let mut args = vec!["build".to_owned()]; + args.extend(helper_args(o)); + args.extend(strs(&["--target", TARGET])); + args +} + /// Whether an archive's member is one of rustc's raw-dylib import members, which it names after /// their DLL. fn is_import(member: &str) -> bool { @@ -292,6 +305,10 @@ mod tests { &target.join("x86_64-pc-windows-gnullvm/release/libreims_vgpu_c.a"), "archive", ); + put( + &target.join("x86_64-pc-windows-gnullvm/release/reims-vgpu-compile.exe"), + "helper", + ); let header = root.join("reims_vgpu.h"); put(&header, "header"); (root.clone(), target, header) @@ -379,6 +396,7 @@ mod tests { -lws2_32 -ldbghelp -lsynchronization\n" )); assert!(!out.join("lib/libreims_vgpu.dll.a").exists()); + assert_eq!(read("bin/reims-vgpu-compile.exe"), "helper"); let work = target.join("dist-localized"); let work_file = |name: &str| std::fs::read_to_string(work.join(name)).unwrap(); @@ -456,7 +474,15 @@ mod tests { archive.display() ) ); - assert_eq!(calls.len(), 10, "{calls:?}"); + assert!( + calls[10].ends_with( + " build --profile release -p reims-vgpu-c -p reims-vgpu-vulkan \ + --bin reims-vgpu-compile --target x86_64-pc-windows-gnullvm" + ), + "{}", + calls[10] + ); + assert_eq!(calls.len(), 11, "{calls:?}"); std::fs::remove_dir_all(&root).unwrap(); }