File: build.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (260 lines) | stat: -rw-r--r-- 7,860 bytes parent folder | download | duplicates (3)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use pkg_config::Config;
use std::env;
use std::fmt;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

/// # Link Type Enumeration
///
/// Holds the different types of linking we support in this
/// script. Used to keep track of what the default link type is and
/// what override has been specified, if any, in the environment.
#[derive(Eq, PartialEq)]
enum LinkType {
    /// Static linking. This corresponds to the `static` type in Cargo.
    Static,
    /// Dynamic linking. This corresponds to the `dylib` type in Cargo.
    Dynamic,
}

impl fmt::Display for LinkType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                &LinkType::Static => "static",
                &LinkType::Dynamic => "dylib",
            }
        )
    }
}

fn env_var_bool(name: &str) -> Option<bool> {
    if name.starts_with("RUSTONIG") {
        println!("cargo:rerun-if-env-changed={}", name);
    }
    env::var(name)
        .ok()
        .map(|s| match &s.to_string().to_lowercase()[..] {
            "0" | "no" | "false" => false,
            _ => true,
        })
}

/// # Link Type Override
///
/// Retuns the override from the environment, if any is set.
fn link_type_override() -> Option<LinkType> {
    let dynamic_env = env_var_bool("RUSTONIG_DYNAMIC_LIBONIG").map(|b| match b {
        true => LinkType::Dynamic,
        false => LinkType::Static,
    });
    let static_env = env_var_bool("RUSTONIG_STATIC_LIBONIG").map(|b| match b {
        true => LinkType::Static,
        false => LinkType::Dynamic,
    });

    dynamic_env.or(static_env)
}

fn compile() {
    bindgen_headers("oniguruma/src/oniguruma.h");

    let mut cc = cc::Build::new();
    let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR"));
    let ref src = Path::new("oniguruma").join("src");
    let config_h = out_dir.join("config.h");

    if env_var_bool("CARGO_FEATURE_PRINT_DEBUG").unwrap_or(false) {
        cc.define("ONIG_DEBUG_PARSE", Some("1"));
        cc.define("ONIG_DEBUG_COMPILE", Some("1"));
        cc.define("ONIG_DEBUG_SEARCH", Some("1"));
        cc.define("ONIG_DEBUG_MATCH", Some("1"));
    }

    if !src.exists() {
        panic!(
            "Unable to find source files in {}. Is oniguruma submodule checked out?\n\
             Try git submodule init; git submodule update",
            src.display()
        );
    }

    let arch = env::var("CARGO_CFG_TARGET_ARCH");
    let os = env::var("CARGO_CFG_TARGET_OS");
    let bits = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap();
    if let Ok("windows") = os.as_ref().map(String::as_str) {
        fs::copy(src.join(format!("config.h.win{}", bits)), config_h)
            .expect("Can't copy config.h.win??");
    } else {
        let family = env::var("CARGO_CFG_TARGET_FAMILY");
        if let Ok("unix") = family.as_ref().map(String::as_str) {
            cc.define("HAVE_UNISTD_H", Some("1"));
            cc.define("HAVE_SYS_TYPES_H", Some("1"));
            cc.define("HAVE_SYS_TIME_H", Some("1"));
        }

        // Can't use size_of::<c_long>(), because it'd refer to build arch, not target arch.
        // so instead assume it's a non-exotic target (LP32/LP64).
        fs::write(
            config_h,
            format!(
                "
            #define HAVE_PROTOTYPES 1
            #define STDC_HEADERS 1
            #define HAVE_STRING_H 1
            #define HAVE_STDARG_H 1
            #define HAVE_STDLIB_H 1
            #define HAVE_LIMITS_H 1
            #define HAVE_INTTYPES_H 1
            #define SIZEOF_INT 4
            #define SIZEOF_SHORT 2
            #define SIZEOF_LONG {0}
            #define SIZEOF_VOIDP {0}
            #define SIZEOF_LONG_LONG 8
        ",
                if bits == "64" { "8" } else { "4" }
            ),
        )
        .expect("Can't write config.h to OUT_DIR");
    }
    if let Ok("wasm32") = arch.as_ref().map(String::as_str) {
        cc.define("ONIG_DISABLE_DIRECT_THREADING", Some("1"));
        if let Ok("unknown") = os.as_ref().map(String::as_str) {
            cc.define(
                "ONIG_EXTERN",
                Some(r#"__attribute__((visibility("default")))"#),
            );
        }
    }

    cc.include(out_dir); // Read config.h from there
    cc.include(src);

    let files = [
        "regexec.c",
        "regerror.c",
        "regparse.c",
        "regext.c",
        "regcomp.c",
        "reggnu.c",
        "regenc.c",
        "regsyntax.c",
        "regtrav.c",
        "regversion.c",
        "st.c",
        "onig_init.c",
        "unicode.c",
        "ascii.c",
        "utf8.c",
        "utf16_be.c",
        "utf16_le.c",
        "utf32_be.c",
        "utf32_le.c",
        "euc_jp.c",
        "sjis.c",
        "iso8859_1.c",
        "iso8859_2.c",
        "iso8859_3.c",
        "iso8859_4.c",
        "iso8859_5.c",
        "iso8859_6.c",
        "iso8859_7.c",
        "iso8859_8.c",
        "iso8859_9.c",
        "iso8859_10.c",
        "iso8859_11.c",
        "iso8859_13.c",
        "iso8859_14.c",
        "iso8859_15.c",
        "iso8859_16.c",
        "euc_tw.c",
        "euc_kr.c",
        "big5.c",
        "gb18030.c",
        "koi8_r.c",
        "cp1251.c",
        "euc_jp_prop.c",
        "sjis_prop.c",
        "unicode_unfold_key.c",
        "unicode_fold1_key.c",
        "unicode_fold2_key.c",
        "unicode_fold3_key.c",
    ];
    for file in files.iter() {
        cc.file(src.join(file));
    }

    if cfg!(feature = "posix-api") {
        cc.file(src.join("regposix.c"));
        cc.file(src.join("regposerr.c"));
    }

    cc.warnings(false); // not actionable by the end user
    cc.compile("onig");
}

#[cfg(not(feature = "generate"))]
fn bindgen_headers(_path: &str) {}

#[cfg(feature = "generate")]
fn bindgen_headers(path: &str) {
    let arch = env::var("CARGO_CFG_TARGET_ARCH");
    let mut bindgen = bindgen::Builder::default()
        .header(path)
        .derive_eq(true)
        .layout_tests(false);
    if let Ok("wasm32") = arch.as_ref().map(String::as_str) {
        bindgen = bindgen.clang_arg("-fvisibility=default");
    }
    let bindings = bindgen.generate().expect("bindgen");
    let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR");
    let out_path = Path::new(&out_dir);
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

pub fn main() {
    let link_type = link_type_override();
    let require_pkg_config = env_var_bool("RUSTONIG_SYSTEM_LIBONIG").unwrap_or(true);

    if require_pkg_config || link_type == Some(LinkType::Dynamic) {
        let mut conf = Config::new();
        // dynamically-generated headers can work with an older version
        // pre-generated headers are for the latest
        conf.atleast_version(if cfg!(feature = "generate") {
            "6.8.0"
        } else {
            "6.9.3"
        });
        if link_type == Some(LinkType::Static) {
            conf.statik(true);
        }
        match conf.probe("oniguruma") {
            Ok(lib) => {
                for path in &lib.include_paths {
                    let header = path.join("oniguruma.h");
                    if header.exists() {
                        bindgen_headers(&header.display().to_string());
                        return;
                    }
                }
                if require_pkg_config {
                    panic!(
                        "Unable to find oniguruma.h in include paths from pkg-config: {:?}",
                        lib.include_paths
                    );
                }
            }
            Err(ref err) if require_pkg_config => {
                panic!("Unable to find oniguruma in pkg-config, and RUSTONIG_SYSTEM_LIBONIG is set: {}", err);
            }
            _ => {}
        }
    }

    compile();
}