From 493c5c75328ebcdf81457af642578ce5a133ebba Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sun, 17 Nov 2024 00:36:56 -0500 Subject: [PATCH] properly expose all symbls in manganis without linker intercept (#3230) * force a volatile read of manganis symbols to preserve them in the final binary --- packages/cli/src/build/request.rs | 25 ------------------- packages/manganis/manganis-macro/src/asset.rs | 2 ++ .../manganis/manganis-macro/src/linker.rs | 4 +++ packages/manganis/manganis/src/builder.rs | 6 +++++ 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/packages/cli/src/build/request.rs b/packages/cli/src/build/request.rs index b8633da45..35b7f2471 100644 --- a/packages/cli/src/build/request.rs +++ b/packages/cli/src/build/request.rs @@ -7,7 +7,6 @@ use crate::{AppBundle, Platform}; use anyhow::Context; use serde::Deserialize; use std::{ - ffi::OsStr, path::{Path, PathBuf}, process::Stdio, time::Instant, @@ -225,30 +224,6 @@ impl BuildRequest { // walk every file in the incremental cache dir, reading and inserting items into the manifest. let mut manifest = AssetManifest::default(); - // Add from the deps folder where the rlibs are stored for dependencies - let deps_folder = exe.parent().unwrap().join("deps"); - tracing::trace!("Adding assets from deps folder: {deps_folder:?}"); - for entry in deps_folder.read_dir()?.flatten() { - if entry.path().extension() == Some(OsStr::new("rlib")) { - _ = manifest.add_from_object_path(&entry.path()); - } - } - - // Add from the incremental cache folder by recursively walking the folder - // it seems that this sticks around no matter what - and cargo doesn't clean it up since the .os are cached anyway - fn recursive_add(manifest: &mut AssetManifest, path: &Path) -> Result<()> { - if path.extension() == Some(OsStr::new("o")) { - _ = manifest.add_from_object_path(path); - } else if let Ok(dir) = path.read_dir() { - for entry in dir.flatten() { - recursive_add(manifest, &entry.path())?; - } - } - - Ok(()) - } - recursive_add(&mut manifest, &exe.parent().unwrap().join("incremental"))?; - // And then add from the exe directly, just in case it's LTO compiled and has no incremental cache _ = manifest.add_from_object_path(exe); diff --git a/packages/manganis/manganis-macro/src/asset.rs b/packages/manganis/manganis-macro/src/asset.rs index 7eebd799b..7d2b995be 100644 --- a/packages/manganis/manganis-macro/src/asset.rs +++ b/packages/manganis/manganis-macro/src/asset.rs @@ -104,6 +104,8 @@ impl ToTokens for AssetParser { // "/blahcss123.css" bundled: #bundled, + + metadata: __volatile_reader as fn() -> u8, } } ) #option_source diff --git a/packages/manganis/manganis-macro/src/linker.rs b/packages/manganis/manganis-macro/src/linker.rs index 8f3b7cfa5..745c472e5 100644 --- a/packages/manganis/manganis-macro/src/linker.rs +++ b/packages/manganis/manganis-macro/src/linker.rs @@ -17,5 +17,9 @@ pub fn generate_link_section(asset: &impl Serialize) -> TokenStream2 { #[link_section = #section_name] #[used] static ASSET: [u8; #len] = * #asset_bytes; + + fn __volatile_reader() -> u8 { + unsafe { std::ptr::read_volatile(ASSET.as_ptr()) } + } } } diff --git a/packages/manganis/manganis/src/builder.rs b/packages/manganis/manganis/src/builder.rs index 75513ac24..91d6ff0e4 100644 --- a/packages/manganis/manganis/src/builder.rs +++ b/packages/manganis/manganis/src/builder.rs @@ -14,6 +14,9 @@ pub struct Asset { /// /// `blah-123.css`` pub bundled: &'static str, + + /// The metadata for this asset plumbed by the compiler + pub metadata: fn() -> u8, } impl Asset { @@ -47,6 +50,9 @@ impl Asset { /// Attempts to resolve it against an `assets` folder in the current directory. /// If that doesn't exist, it will resolve against the cargo manifest dir pub fn resolve(&self) -> PathBuf { + // Force a volatile read of the metadata to ensure the symbol makes it into the binary + (self.metadata)(); + // If the asset is relative, we resolve the asset at the current directory if !dioxus_core_types::is_bundled_app() { return PathBuf::from(self.local);