Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import lib generation under MinGW #438

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ anyhow = "1.0"
cc = "1.0"
glob = "0.3"
itertools = "0.14"
implib = "0.3.3"
implib = "0.3.4"
object = { version = "0.36.4", default-features = false, features = ["std", "read_core", "pe"] }

[features]
Expand Down
27 changes: 15 additions & 12 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,15 @@ fn build_def_file(
// Create the .def output file
let def_file = cargo_util::paths::create(targetdir.join(format!("{name}.def")))?;

write_def_file(name, dll_file, def_file)?;
write_def_file(dll_file, def_file)?;
}

Ok(())
}

fn write_def_file<W: std::io::Write>(
name: &str,
dll_file: object::File,
mut def_file: W,
) -> anyhow::Result<W> {
fn write_def_file<W: std::io::Write>(dll_file: object::File, mut def_file: W) -> anyhow::Result<W> {
use object::read::Object;

writeln!(def_file, "LIBRARY \"{name}.dll\"")?;
writeln!(def_file, "EXPORTS")?;

for export in dll_file.exports()? {
Expand All @@ -171,7 +166,7 @@ fn write_def_file<W: std::io::Write>(
Ok(def_file)
}

/// Build import library for windows-gnu
/// Build import library for windows
fn build_implib_file(
ws: &Workspace,
build_targets: &BuildTargets,
Expand Down Expand Up @@ -202,22 +197,30 @@ fn build_implib_file(
}
};

let lib_path = build_targets.impl_lib.as_ref().unwrap();
let lib_name = build_targets
.shared_output_file_name()
.unwrap()
.into_string()
.unwrap();
let implib_path = build_targets.impl_lib.as_ref().unwrap();

let lib_file = cargo_util::paths::create(lib_path)?;
write_implib(lib_file, machine_type, flavor, &def_contents)?;
let implib_file = cargo_util::paths::create(implib_path)?;
write_implib(implib_file, lib_name, machine_type, flavor, &def_contents)?;
}

Ok(())
}

fn write_implib<W: std::io::Write + std::io::Seek>(
mut w: W,
lib_name: String,
machine_type: MachineType,
flavor: Flavor,
def_contents: &str,
) -> anyhow::Result<W> {
let module_def = ModuleDef::parse(def_contents, machine_type)?;
let mut module_def = ModuleDef::parse(def_contents, machine_type)?;
module_def.import_name = lib_name;

let import_library = ImportLibrary::from_def(module_def, machine_type, flavor);

import_library.write_to(&mut w)?;
Expand Down
Loading