-
Notifications
You must be signed in to change notification settings - Fork 13
/
build.rs
44 lines (39 loc) · 1.79 KB
/
build.rs
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
use std::env;
use std::process::Command;
const PHP_API_VERSION_COMMAND: &str = "php -i | grep 'PHP API' | sed -e 's/PHP API => //'";
const PHP_API_VERSION_ERROR: &str = "Error trying to get PHP API version from your machine";
const PHP_EXTENSION_BUILD_COMMAND: &str = "php -i | grep 'PHP Extension Build' | sed -e 's/PHP Extension Build => //'";
const PHP_EXTENSION_BUILD_ERROR: &str = "Error trying to get PHP EXTENSION BUILD version from your machine";
fn main() {
let api_version = match env::var("PHP_API_VERSION") {
Ok(api_version) => api_version,
Err(_) => execute_command(PHP_API_VERSION_COMMAND, PHP_API_VERSION_ERROR)
};
let zend_extension_build = match env::var("PHP_EXTENSION_BUILD") {
Ok(zend_extension_build) => zend_extension_build,
Err(_) => execute_command(PHP_EXTENSION_BUILD_COMMAND, PHP_EXTENSION_BUILD_ERROR)
};
println!("cargo:rustc-env=PHP_API_VERSION={}", api_version);
println!("cargo:rerun-if-env-changed=PHP_API_VERSION");
println!("cargo:rustc-env=PHP_EXTENSION_BUILD={}", zend_extension_build);
println!("cargo:rerun-if-env-changed=PHP_EXTENSION_BUILD");
set_version_features(api_version);
}
fn execute_command(command: &str, error_message: &str) -> String {
let output = Command::new("sh")
.arg("-c")
.arg(command)
.output()
.expect(error_message)
.stdout;
String::from_utf8(output).unwrap()
}
fn set_version_features(api_version: String) {
let api_version = api_version.trim();
if !api_version.is_empty() && api_version.parse::<i64>().unwrap() >= 20170718 {
println!("cargo:rustc-cfg=feature=\"php72\"");
}
if !api_version.is_empty() && api_version.trim().parse::<i64>().unwrap() >= 20180731 {
println!("cargo:rustc-cfg=feature=\"php73\"");
}
}