Skip to content

Commit 97224e0

Browse files
Add run_make_support::build_native_static_lib function
1 parent 51e6271 commit 97224e0

File tree

1 file changed

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

1 file changed

+44
-0
lines changed

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

+44
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,50 @@ 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+
/// Returns the path for the given library 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+
if is_msvc() {
174+
let mut msvc_lib = Command::new(env::var("MSVC_LIB_PATH").unwrap());
175+
msvc_lib
176+
.current_dir(tmp_dir())
177+
.arg("-nologo")
178+
.arg(&format!("-out:{}", cygpath_windows(lib_path)))
179+
.arg(&obj_file);
180+
let output = msvc_lib.output().unwrap();
181+
if !output.status.success() {
182+
handle_failed_output(&msvc_lib, output, caller_line_number);
183+
}
184+
} else {
185+
ar(obj_file, lib_path, caller_line_number);
186+
}
187+
} else {
188+
let lib_path = format!("lib{lib_name}.a");
189+
// First compiling `.c` to `.o`.
190+
cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run();
191+
// Generating `.a` from `.o`.
192+
ar(obj_file, lib_path, caller_line_number);
193+
};
194+
}
195+
152196
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
153197
/// available on the platform!
154198
#[track_caller]

0 commit comments

Comments
 (0)