Show More
@@ -1,128 +1,127 b'' | |||
|
1 | 1 | // build.rs -- Configure build environment for `hgcli` Rust package. |
|
2 | 2 | // |
|
3 | 3 | // Copyright 2017 Gregory Szorc <gregory.szorc@gmail.com> |
|
4 | 4 | // |
|
5 | 5 | // This software may be used and distributed according to the terms of the |
|
6 | 6 | // GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | use std::collections::HashMap; |
|
9 | 9 | use std::env; |
|
10 | 10 | use std::path::Path; |
|
11 | #[cfg(target_os = "windows")] | |
|
12 | use std::path::PathBuf; | |
|
13 | ||
|
14 | 11 | use std::process::Command; |
|
15 | 12 | |
|
16 | 13 | struct PythonConfig { |
|
17 | 14 | python: String, |
|
18 | 15 | config: HashMap<String, String>, |
|
19 | 16 | } |
|
20 | 17 | |
|
21 | 18 | fn get_python_config() -> PythonConfig { |
|
22 | 19 | // The python27-sys crate exports a Cargo variable defining the full |
|
23 | 20 | // path to the interpreter being used. |
|
24 | 21 | let python = env::var("DEP_PYTHON27_PYTHON_INTERPRETER").expect( |
|
25 | 22 | "Missing DEP_PYTHON27_PYTHON_INTERPRETER; bad python27-sys crate?", |
|
26 | 23 | ); |
|
27 | 24 | |
|
28 | 25 | if !Path::new(&python).exists() { |
|
29 | 26 | panic!( |
|
30 | 27 | "Python interpreter {} does not exist; this should never happen", |
|
31 | 28 | python |
|
32 | 29 | ); |
|
33 | 30 | } |
|
34 | 31 | |
|
35 | 32 | // This is a bit hacky but it gets the job done. |
|
36 | 33 | let separator = "SEPARATOR STRING"; |
|
37 | 34 | |
|
38 | 35 | let script = "import sysconfig; \ |
|
39 | 36 | c = sysconfig.get_config_vars(); \ |
|
40 | 37 | print('SEPARATOR STRING'.join('%s=%s' % i for i in c.items()))"; |
|
41 | 38 | |
|
42 | 39 | let mut command = Command::new(&python); |
|
43 | 40 | command.arg("-c").arg(script); |
|
44 | 41 | |
|
45 | 42 | let out = command.output().unwrap(); |
|
46 | 43 | |
|
47 | 44 | if !out.status.success() { |
|
48 | 45 | panic!( |
|
49 | 46 | "python script failed: {}", |
|
50 | 47 | String::from_utf8_lossy(&out.stderr) |
|
51 | 48 | ); |
|
52 | 49 | } |
|
53 | 50 | |
|
54 | 51 | let stdout = String::from_utf8_lossy(&out.stdout); |
|
55 | 52 | let mut m = HashMap::new(); |
|
56 | 53 | |
|
57 | 54 | for entry in stdout.split(separator) { |
|
58 | 55 | let mut parts = entry.splitn(2, "="); |
|
59 | 56 | let key = parts.next().unwrap(); |
|
60 | 57 | let value = parts.next().unwrap(); |
|
61 | 58 | m.insert(String::from(key), String::from(value)); |
|
62 | 59 | } |
|
63 | 60 | |
|
64 | 61 | PythonConfig { |
|
65 | 62 | python: python, |
|
66 | 63 | config: m, |
|
67 | 64 | } |
|
68 | 65 | } |
|
69 | 66 | |
|
70 | 67 | #[cfg(not(target_os = "windows"))] |
|
71 | 68 | fn have_shared(config: &PythonConfig) -> bool { |
|
72 | 69 | match config.config.get("Py_ENABLE_SHARED") { |
|
73 | 70 | Some(value) => value == "1", |
|
74 | 71 | None => false, |
|
75 | 72 | } |
|
76 | 73 | } |
|
77 | 74 | |
|
78 | 75 | #[cfg(target_os = "windows")] |
|
79 | 76 | fn have_shared(config: &PythonConfig) -> bool { |
|
77 | use std::path::PathBuf; | |
|
78 | ||
|
80 | 79 | // python27.dll should exist next to python2.7.exe. |
|
81 | 80 | let mut dll = PathBuf::from(&config.python); |
|
82 | 81 | dll.pop(); |
|
83 | 82 | dll.push("python27.dll"); |
|
84 | 83 | |
|
85 | 84 | return dll.exists(); |
|
86 | 85 | } |
|
87 | 86 | |
|
88 | 87 | const REQUIRED_CONFIG_FLAGS: [&'static str; 2] = ["Py_USING_UNICODE", "WITH_THREAD"]; |
|
89 | 88 | |
|
90 | 89 | fn main() { |
|
91 | 90 | let config = get_python_config(); |
|
92 | 91 | |
|
93 | 92 | println!("Using Python: {}", config.python); |
|
94 | 93 | println!("cargo:rustc-env=PYTHON_INTERPRETER={}", config.python); |
|
95 | 94 | |
|
96 | 95 | let prefix = config.config.get("prefix").unwrap(); |
|
97 | 96 | |
|
98 | 97 | println!("Prefix: {}", prefix); |
|
99 | 98 | |
|
100 | 99 | // TODO Windows builds don't expose these config flags. Figure out another |
|
101 | 100 | // way. |
|
102 | 101 | #[cfg(not(target_os = "windows"))] |
|
103 | 102 | for key in REQUIRED_CONFIG_FLAGS.iter() { |
|
104 | 103 | let result = match config.config.get(*key) { |
|
105 | 104 | Some(value) => value == "1", |
|
106 | 105 | None => false, |
|
107 | 106 | }; |
|
108 | 107 | |
|
109 | 108 | if !result { |
|
110 | 109 | panic!("Detected Python requires feature {}", key); |
|
111 | 110 | } |
|
112 | 111 | } |
|
113 | 112 | |
|
114 | 113 | // We need a Python shared library. |
|
115 | 114 | if !have_shared(&config) { |
|
116 | 115 | panic!("Detected Python lacks a shared library, which is required"); |
|
117 | 116 | } |
|
118 | 117 | |
|
119 | 118 | let ucs4 = match config.config.get("Py_UNICODE_SIZE") { |
|
120 | 119 | Some(value) => value == "4", |
|
121 | 120 | None => false, |
|
122 | 121 | }; |
|
123 | 122 | |
|
124 | 123 | if !ucs4 { |
|
125 | 124 | #[cfg(not(target_os = "windows"))] |
|
126 | 125 | panic!("Detected Python doesn't support UCS-4 code points"); |
|
127 | 126 | } |
|
128 | 127 | } |
General Comments 0
You need to be logged in to leave comments.
Login now