@@ -6,7 +6,8 @@ use crate::external_deps::llvm::llvm_ar;
6
6
use crate :: path_helpers:: path;
7
7
use crate :: targets:: { is_darwin, is_msvc, is_windows} ;
8
8
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.
10
11
11
12
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
12
13
/// Built from a C file.
@@ -75,8 +76,8 @@ pub fn build_native_dynamic_lib(lib_name: &str) -> PathBuf {
75
76
path ( lib_path)
76
77
}
77
78
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 .
80
81
#[ track_caller]
81
82
pub fn build_native_static_lib_cxx ( lib_name : & str ) -> PathBuf {
82
83
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 {
95
96
llvm_ar ( ) . obj_to_ar ( ) . output_input ( & lib_path, & obj_file) . run ( ) ;
96
97
path ( lib_path)
97
98
}
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
+ }
0 commit comments