fn do_link(sess: &Session, objects: &[PathBuf], rlibs: &[PathBuf], legalize: bool) -> Vec<u32> {
    fn load(bytes: &[u8]) -> rspirv::dr::Module {
        let mut loader = rspirv::dr::Loader::new();
        rspirv::binary::parse_bytes(&bytes, &mut loader).unwrap();
        loader.module()
    }

    let load_modules_timer = sess.timer("link_load_modules");
    let mut modules = Vec::new();
    // `objects` are the plain obj files we need to link - usually produced by the final crate.
    for obj in objects {
        let mut bytes = Vec::new();
        File::open(obj).unwrap().read_to_end(&mut bytes).unwrap();
        modules.push(load(&bytes));
    }
    // `rlibs` are archive files we've created in `create_archive`, usually produced by crates that are being
    // referenced. We need to unpack them and add the modules inside.
    for rlib in rlibs {
        for entry in Archive::new(File::open(rlib).unwrap()).entries().unwrap() {
            let mut entry = entry.unwrap();
            if entry.path().unwrap() != Path::new(".metadata") {
                let mut bytes = Vec::new();
                entry.read_to_end(&mut bytes).unwrap();
                modules.push(load(&bytes));
            }
        }
    }

    if let Ok(ref path) = env::var("DUMP_PRE_LINK") {
        let path = Path::new(path);
        if path.is_file() {
            std::fs::remove_file(path).unwrap();
        }
        std::fs::create_dir_all(path).unwrap();
        for (num, module) in modules.iter().enumerate() {
            File::create(path.join(format!("mod_{}.spv", num)))
                .unwrap()
                .write_all(spirv_tools::binary::from_binary(&module.assemble()))
                .unwrap();
        }
    }
    drop(load_modules_timer);

    // Do the link...
    let options = linker::Options {
        dce: env::var("NO_DCE").is_err(),
        compact_ids: env::var("NO_COMPACT_IDS").is_err(),
        inline: legalize,
        mem2reg: legalize,
        structurize: env::var("NO_STRUCTURIZE").is_err(),
        use_new_structurizer: env::var("OLD_STRUCTURIZER").is_err(),
    };

    let link_result = linker::link(sess, modules, &options);

    let assembled = match link_result {
        Ok(v) => v,
        Err(rustc_errors::ErrorReported) => {
            sess.abort_if_errors();
            bug!("Linker errored, but no error reported")
        }
    };

    // And finally write out the linked binary.
    use rspirv::binary::Assemble;
    assembled.assemble()
}
/// This is the actual guts of linking: the rest of the link-related functions are just digging through rustc's
/// shenanigans to collect all the object files we need to link.
This is the actual guts of linking: the rest of the link-related functions are just digging through rustc's
shenanigans to collect all the object files we need to link.
This is the actual guts of linking : the rest of the link - related functions are just digging through rustc ' s shenanigans to collect all the object files we need to link .
fn do_link(sess: &Session, objects: &[PathBuf], rlibs: &[PathBuf], legalize: bool) -> Vec<u32> {
    fn load(bytes: &[u8]) -> rspirv::dr::Module {
        let mut loader = rspirv::dr::Loader::new();
        rspirv::binary::parse_bytes(&bytes, &mut loader).unwrap();
        loader.module()
    }
    let load_modules_timer = sess.timer("link_load_modules");
    let mut modules = Vec::new();
    // `objects` are the plain obj files we need to link - usually produced by the final crate.
    for obj in objects {
        let mut bytes = Vec::new();
        File::open(obj).unwrap().read_to_end(&mut bytes).unwrap();
        modules.push(load(&bytes));
    }
    // `rlibs` are archive files we've created in `create_archive`, usually produced by crates that are being
    // referenced. We need to unpack them and add the modules inside.
    for rlib in rlibs {
        for entry in Archive::new(File::open(rlib).unwrap()).entries().unwrap() {
            let mut entry = entry.unwrap();
            if entry.path().unwrap() != Path::new(".metadata") {
                let mut bytes = Vec::new();
                entry.read_to_end(&mut bytes).unwrap();
                modules.push(load(&bytes));
            }
        }
    }
    if let Ok(ref path) = env::var("DUMP_PRE_LINK") {
        let path = Path::new(path);
        if path.is_file() {
            std::fs::remove_file(path).unwrap();
        }
        std::fs::create_dir_all(path).unwrap();
        for (num, module) in modules.iter().enumerate() {
            File::create(path.join(format!("mod_{}.spv", num)))
                .unwrap()
                .write_all(spirv_tools::binary::from_binary(&module.assemble()))
                .unwrap();
        }
    }
    drop(load_modules_timer);
    // Do the link...
    let options = linker::Options {
        dce: env::var("NO_DCE").is_err(),
        compact_ids: env::var("NO_COMPACT_IDS").is_err(),
        inline: legalize,
        mem2reg: legalize,
        structurize: env::var("NO_STRUCTURIZE").is_err(),
        use_new_structurizer: env::var("OLD_STRUCTURIZER").is_err(),
    };
    let link_result = linker::link(sess, modules, &options);
    let assembled = match link_result {
        Ok(v) => v,
        Err(rustc_errors::ErrorReported) => {
            sess.abort_if_errors();
            bug!("Linker errored, but no error reported")
        }
    };
    // And finally write out the linked binary.
    use rspirv::binary::Assemble;
    assembled.assemble()
}
fn do_link ( sess : & Session , objects : & [ PathBuf ] , rlibs : & [ PathBuf ] , legalize : bool ) -> Vec < u32 > { fn load ( bytes : & [ u8 ] ) -> rspirv :: dr :: Module { let mut loader = rspirv :: dr :: Loader :: new ( ) ; rspirv :: binary :: parse_bytes ( & bytes , & mut loader ) . unwrap ( ) ; loader . module ( ) } let load_modules_timer = sess . timer ( " " ) ; let mut modules = Vec :: new ( ) ; // `objects` are the plain obj files we need to link - usually produced by the final crate. for obj in objects { let mut bytes = Vec :: new ( ) ; File :: open ( obj ) . unwrap ( ) . read_to_end ( & mut bytes ) . unwrap ( ) ; modules . push ( load ( & bytes ) ) ; } // `rlibs` are archive files we've created in `create_archive`, usually produced by crates that are being // referenced. We need to unpack them and add the modules inside. for rlib in rlibs { for entry in Archive :: new ( File :: open ( rlib ) . unwrap ( ) ) . entries ( ) . unwrap ( ) { let mut entry = entry . unwrap ( ) ; if entry . path ( ) . unwrap ( ) != Path :: new ( " " ) { let mut bytes = Vec :: new ( ) ; entry . read_to_end ( & mut bytes ) . unwrap ( ) ; modules . push ( load ( & bytes ) ) ; } } } if let Ok ( ref path ) = env :: var ( " " ) { let path = Path :: new ( path ) ; if path . is_file ( ) { std :: fs :: remove_file ( path ) . unwrap ( ) ; } std :: fs :: create_dir_all ( path ) . unwrap ( ) ; for ( num , module ) in modules . iter ( ) . enumerate ( ) { File :: create ( path . join ( format ! ( " " num ) ) ) . unwrap ( ) . write_all ( spirv_tools :: binary :: from_binary ( & module . assemble ( ) ) ) . unwrap ( ) ; } } drop ( load_modules_timer ) ; // Do the link... let options = linker :: Options { dce : env :: var ( " " ) . is_err ( ) , compact_ids : env :: var ( " " ) . is_err ( ) , inline : legalize , mem2reg : legalize , structurize : env :: var ( " " ) . is_err ( ) , use_new_structurizer : env :: var ( " " ) . is_err ( ) , } ; let link_result = linker :: link ( sess , modules , & options ) ; let assembled = match link_result { Ok ( v ) => v , Err ( rustc_errors :: ErrorReported ) => { sess . abort_if_errors ( ) ; bug ! ( " " ) } } ; // And finally write out the linked binary. use rspirv :: binary :: Assemble ; assembled . assemble ( ) }
This is the actual guts of linking: the rest of the link-related functions are just digging through rustc's
shenanigans to collect all the object files we need to link.
This is the actual guts of linking : the rest of the link - related functions are just digging through rustc ' s shenanigans to collect all the object files we need to link .
sess
&Session
objects
&[PathBuf]
rlibs
&[PathBuf]
legalize
bool
sess
&Session
objects
&[PathBuf]
rlibs
&[PathBuf]
legalize
bool