summaryrefslogtreecommitdiff
path: root/build/find_normal.rs
blob: 0edc22c520a3c4dfab5fa5a30acaabf2341ec549 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader, Result};
use std::ops::Bound;
use std::path::{Path, PathBuf};

pub fn probe_lua() -> PathBuf {
    let include_dir = env::var_os("LUA_INC").unwrap_or_default();
    let lib_dir = env::var_os("LUA_LIB").unwrap_or_default();
    let lua_lib = env::var_os("LUA_LIB_NAME").unwrap_or_default();

    println!("cargo:rerun-if-env-changed=LUA_INC");
    println!("cargo:rerun-if-env-changed=LUA_LIB");
    println!("cargo:rerun-if-env-changed=LUA_LIB_NAME");
    println!("cargo:rerun-if-env-changed=LUA_LINK");

    let need_lua_lib = cfg!(any(not(feature = "module"), target_os = "windows"));

    if include_dir != "" && (!need_lua_lib || lib_dir != "") {
        if lua_lib == "" {
            panic!("LUA_LIB_NAME is not set");
        }
        let _version = use_custom_lua(&include_dir, &lib_dir, &lua_lib).unwrap();
        return PathBuf::from(include_dir);
    }

    // Find using `pkg-config`

    #[cfg(feature = "lua54")]
    {
        let mut lua = pkg_config::Config::new()
            .range_version((Bound::Included("5.4"), Bound::Excluded("5.5")))
            .cargo_metadata(need_lua_lib)
            .probe("lua");

        if lua.is_err() {
            lua = pkg_config::Config::new()
                .cargo_metadata(need_lua_lib)
                .probe("lua5.4");
        }

        lua.unwrap().include_paths[0].clone()
    }

    #[cfg(feature = "lua53")]
    {
        let mut lua = pkg_config::Config::new()
            .range_version((Bound::Included("5.3"), Bound::Excluded("5.4")))
            .cargo_metadata(need_lua_lib)
            .probe("lua");

        if lua.is_err() {
            lua = pkg_config::Config::new()
                .cargo_metadata(need_lua_lib)
                .probe("lua5.3");
        }

        lua.unwrap().include_paths[0].clone()
    }

    #[cfg(feature = "lua52")]
    {
        let mut lua = pkg_config::Config::new()
            .range_version((Bound::Included("5.2"), Bound::Excluded("5.3")))
            .cargo_metadata(need_lua_lib)
            .probe("lua");

        if lua.is_err() {
            lua = pkg_config::Config::new()
                .cargo_metadata(need_lua_lib)
                .probe("lua5.2");
        }

        lua.unwrap().include_paths[0].clone()
    }

    #[cfg(feature = "lua51")]
    {
        let mut lua = pkg_config::Config::new()
            .range_version((Bound::Included("5.1"), Bound::Excluded("5.2")))
            .cargo_metadata(need_lua_lib)
            .probe("lua");

        if lua.is_err() {
            lua = pkg_config::Config::new()
                .cargo_metadata(need_lua_lib)
                .probe("lua5.1");
        }

        lua.unwrap().include_paths[0].clone()
    }

    #[cfg(feature = "luajit")]
    {
        let lua = pkg_config::Config::new()
            .range_version((Bound::Included("2.0.5"), Bound::Unbounded))
            .cargo_metadata(need_lua_lib)
            .probe("luajit");

        lua.unwrap().include_paths[0].clone()
    }
}

fn use_custom_lua<S: AsRef<Path>>(include_dir: &S, lib_dir: &S, lua_lib: &S) -> Result<String> {
    let mut version_found = String::new();

    // Find LUA_VERSION_NUM
    let mut lua_h_path = include_dir.as_ref().to_owned();
    lua_h_path.push("lua.h");
    let f = File::open(lua_h_path)?;
    let reader = BufReader::new(f);
    for line in reader.lines() {
        let line = line?;
        let parts = line.split_whitespace().collect::<Vec<_>>();
        if parts.len() == 3 && parts[1] == "LUA_VERSION_NUM" {
            version_found = parts[2].to_string();
        }
    }

    let link_lib = match env::var("LUA_LINK") {
        Ok(s) if s == "static" => "static=",
        _ => "",
    };

    if cfg!(any(not(feature = "module"), target_os = "windows")) {
        println!(
            "cargo:rustc-link-search=native={}",
            lib_dir.as_ref().display()
        );
        println!(
            "cargo:rustc-link-lib={}{}",
            link_lib,
            lua_lib.as_ref().display()
        );
    }

    Ok(version_found)
}