@@ -6,7 +6,8 @@ use crate::external_deps::llvm::llvm_ar;
66use crate :: path_helpers:: path;
77use 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]
8182pub 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+ }
0 commit comments