Skip to content

Commit fb663e6

Browse files
committed
run-make-support: add build_native_static_lib_cxx_optimized helper
These should eventually be cleaned up, but let's add a helper for the immediate need of porting the `cpp_smoke_test` test.
1 parent 64e06c0 commit fb663e6

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/tools/run-make-support/src/external_deps/c_build.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use crate::external_deps::llvm::llvm_ar;
66
use crate::path_helpers::path;
77
use crate::targets::{is_darwin, is_msvc, is_windows};
88

9-
// FIXME(Oneirical): These native build functions should take a Path-based generic.
9+
// FIXME(jieyouxu): figure out a way to improve the usability of these helpers... They are really
10+
// messy.
1011

1112
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
1213
/// Built from a C file.
@@ -75,8 +76,8 @@ pub fn build_native_dynamic_lib(lib_name: &str) -> PathBuf {
7576
path(lib_path)
7677
}
7778

78-
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
79-
/// Built from a C++ file.
79+
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name. Built
80+
/// from a C++ file. Unoptimized.
8081
#[track_caller]
8182
pub fn build_native_static_lib_cxx(lib_name: &str) -> PathBuf {
8283
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
@@ -95,3 +96,35 @@ pub fn build_native_static_lib_cxx(lib_name: &str) -> PathBuf {
9596
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
9697
path(lib_path)
9798
}
99+
100+
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name. Built
101+
/// from a C++ file. Optimized with the provided `opt_level` flag. Note that the `opt_level_flag`
102+
/// can differ between different C++ compilers! Consult relevant docs for `g++`/`clang++`/MSVC.
103+
#[track_caller]
104+
pub fn build_native_static_lib_cxx_optimized(lib_name: &str, opt_level_flag: &str) -> PathBuf {
105+
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
106+
let src = format!("{lib_name}.cpp");
107+
let lib_path = static_lib_name(lib_name);
108+
109+
// NOTE: generate debuginfo
110+
if is_msvc() {
111+
cxx()
112+
.arg(opt_level_flag)
113+
.arg("-Zi")
114+
.arg("-debug")
115+
.arg("-EHs")
116+
.arg("-c")
117+
.out_exe(&obj_file)
118+
.input(src)
119+
.run();
120+
} else {
121+
cxx().arg(opt_level_flag).arg("-g").arg("-c").out_exe(&obj_file).input(src).run();
122+
};
123+
let obj_file = if is_msvc() {
124+
PathBuf::from(format!("{lib_name}.obj"))
125+
} else {
126+
PathBuf::from(format!("{lib_name}.o"))
127+
};
128+
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
129+
path(lib_path)
130+
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub mod rfs {
3737
// Re-exports of third-party library crates.
3838
// tidy-alphabetical-start
3939
pub use bstr;
40+
pub use cc as crate_cc; // NOTE: aliased to avoid being confused with `c_cxx_compiler::cc`.
4041
pub use gimli;
4142
pub use libc;
4243
pub use object;
@@ -55,7 +56,7 @@ pub use external_deps::{
5556
pub use c_cxx_compiler::{Cc, Gcc, cc, cxx, extra_c_flags, extra_cxx_flags, gcc};
5657
pub use c_build::{
5758
build_native_dynamic_lib, build_native_static_lib, build_native_static_lib_cxx,
58-
build_native_static_lib_optimized,
59+
build_native_static_lib_optimized, build_native_static_lib_cxx_optimized,
5960
};
6061
pub use cargo::cargo;
6162
pub use clang::{clang, Clang};

0 commit comments

Comments
 (0)