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