Skip to content

Commit 968a8f9

Browse files
Add run_make_support::build_native_static_lib function
1 parent 5a00105 commit 968a8f9

File tree

1 file changed

+40
-0
lines changed
  • src/tools/run-make-support/src

1 file changed

+40
-0
lines changed

src/tools/run-make-support/src/lib.rs

+40
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,46 @@ pub fn bin_name(name: &str) -> String {
149149
if is_windows() { format!("{name}.exe") } else { name.to_string() }
150150
}
151151

152+
fn ar<P: AsRef<str>, P2: AsRef<str>>(obj_path: P, lib_path: P2, caller_line_number: u32) {
153+
let mut ar = Command::new(env::var("AR").unwrap());
154+
ar.current_dir(tmp_dir()).arg("crus").arg(lib_path.as_ref()).arg(obj_path.as_ref());
155+
let output = ar.output().unwrap();
156+
if !output.status.success() {
157+
handle_failed_output(&ar, output, caller_line_number);
158+
}
159+
}
160+
161+
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
162+
pub fn build_native_static_lib(lib_name: &str) {
163+
let caller_location = std::panic::Location::caller();
164+
let caller_line_number = caller_location.line();
165+
166+
let obj_file = format!("{lib_name}.o");
167+
let src = format!("{lib_name}.c");
168+
if is_msvc() {
169+
let lib_path = format!("lib{lib_name}.lib");
170+
// First compiling `.c` to `.o`.
171+
cc().arg("-c").out_exe(lib_name).input(src).run();
172+
// Generating `.lib` from `.o`.
173+
let mut msvc_lib = Command::new(env::var("MSVC_LIB_PATH").unwrap());
174+
msvc_lib
175+
.current_dir(tmp_dir())
176+
.arg("-nologo")
177+
.arg(&format!("-out:{}", cygpath_windows(lib_path)))
178+
.arg(&obj_file);
179+
let output = msvc_lib.output().unwrap();
180+
if !output.status.success() {
181+
handle_failed_output(&msvc_lib, output, caller_line_number);
182+
}
183+
} else {
184+
let lib_path = format!("lib{lib_name}.a");
185+
// First compiling `.c` to `.o`.
186+
cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run();
187+
// Generating `.a` from `.o`.
188+
ar(obj_file, lib_path, caller_line_number);
189+
};
190+
}
191+
152192
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
153193
/// available on the platform!
154194
#[track_caller]

0 commit comments

Comments
 (0)