11use std:: env;
2- use std:: fs:: File ;
3- use std:: io:: { BufRead , BufReader , Result } ;
42use std:: ops:: Bound ;
5- use std:: path:: { Path , PathBuf } ;
3+ use std:: path:: PathBuf ;
4+
5+ fn get_env_var ( name : & str ) -> String {
6+ match env:: var ( name) {
7+ Ok ( val) => val,
8+ Err ( env:: VarError :: NotPresent ) => String :: new ( ) ,
9+ Err ( err) => panic ! ( "cannot get {}: {}" , name, err) ,
10+ }
11+ }
612
713pub fn probe_lua ( ) -> PathBuf {
8- let include_dir = env :: var_os ( "LUA_INC" ) . unwrap_or_default ( ) ;
9- let lib_dir = env :: var_os ( "LUA_LIB" ) . unwrap_or_default ( ) ;
10- let lua_lib = env :: var_os ( "LUA_LIB_NAME" ) . unwrap_or_default ( ) ;
14+ let include_dir = get_env_var ( "LUA_INC" ) ;
15+ let lib_dir = get_env_var ( "LUA_LIB" ) ;
16+ let lua_lib = get_env_var ( "LUA_LIB_NAME" ) ;
1117
1218 println ! ( "cargo:rerun-if-env-changed=LUA_INC" ) ;
1319 println ! ( "cargo:rerun-if-env-changed=LUA_LIB" ) ;
@@ -16,11 +22,22 @@ pub fn probe_lua() -> PathBuf {
1622
1723 let need_lua_lib = cfg ! ( any( not( feature = "module" ) , target_os = "windows" ) ) ;
1824
19- if include_dir != "" && ( !need_lua_lib || lib_dir != "" ) {
20- if need_lua_lib && lua_lib == "" {
21- panic ! ( "LUA_LIB_NAME is not set" ) ;
25+ if include_dir != "" {
26+ if need_lua_lib {
27+ if lib_dir == "" {
28+ panic ! ( "LUA_LIB is not set" ) ;
29+ }
30+ if lua_lib == "" {
31+ panic ! ( "LUA_LIB_NAME is not set" ) ;
32+ }
33+
34+ let mut link_lib = "" ;
35+ if get_env_var ( "LUA_LINK" ) == "static" {
36+ link_lib = "static=" ;
37+ } ;
38+ println ! ( "cargo:rustc-link-search=native={}" , lib_dir) ;
39+ println ! ( "cargo:rustc-link-lib={}{}" , link_lib, lua_lib) ;
2240 }
23- let _version = use_custom_lua ( & include_dir, & lib_dir, & lua_lib) . unwrap ( ) ;
2441 return PathBuf :: from ( include_dir) ;
2542 }
2643
@@ -100,39 +117,3 @@ pub fn probe_lua() -> PathBuf {
100117 lua. unwrap ( ) . include_paths [ 0 ] . clone ( )
101118 }
102119}
103-
104- fn use_custom_lua < S : AsRef < Path > > ( include_dir : & S , lib_dir : & S , lua_lib : & S ) -> Result < String > {
105- let mut version_found = String :: new ( ) ;
106-
107- // Find LUA_VERSION_NUM
108- let mut lua_h_path = include_dir. as_ref ( ) . to_owned ( ) ;
109- lua_h_path. push ( "lua.h" ) ;
110- let f = File :: open ( lua_h_path) ?;
111- let reader = BufReader :: new ( f) ;
112- for line in reader. lines ( ) {
113- let line = line?;
114- let parts = line. split_whitespace ( ) . collect :: < Vec < _ > > ( ) ;
115- if parts. len ( ) == 3 && parts[ 1 ] == "LUA_VERSION_NUM" {
116- version_found = parts[ 2 ] . to_string ( ) ;
117- }
118- }
119-
120- let link_lib = match env:: var ( "LUA_LINK" ) {
121- Ok ( s) if s == "static" => "static=" ,
122- _ => "" ,
123- } ;
124-
125- if cfg ! ( any( not( feature = "module" ) , target_os = "windows" ) ) {
126- println ! (
127- "cargo:rustc-link-search=native={}" ,
128- lib_dir. as_ref( ) . display( )
129- ) ;
130- println ! (
131- "cargo:rustc-link-lib={}{}" ,
132- link_lib,
133- lua_lib. as_ref( ) . display( )
134- ) ;
135- }
136-
137- Ok ( version_found)
138- }
0 commit comments