Skip to content

Commit a7131ee

Browse files
committed
Add _sysconfigdata_*
1 parent 260d6f2 commit a7131ee

File tree

6 files changed

+71
-15
lines changed

6 files changed

+71
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ crossbeam-utils = "0.7"
7373
generational-arena = "0.2"
7474
parking_lot = { git = "https://github.com/Amanieu/parking_lot" } # TODO: use published version
7575
thread_local = "1.0"
76+
cfg-if = "0.1.10"
7677

7778
## unicode stuff
7879
unicode_names2 = "0.4"
@@ -128,3 +129,6 @@ features = [
128129

129130
[target.'cfg(target_arch = "wasm32")'.dependencies]
130131
wasm-bindgen = "0.2"
132+
133+
[build-dependencies]
134+
itertools = "0.8"

vm/build.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use itertools::Itertools;
2+
use std::env;
3+
use std::io::prelude::*;
4+
use std::path::PathBuf;
15
use std::process::Command;
26

37
fn main() {
@@ -8,6 +12,21 @@ fn main() {
812
);
913
println!("cargo:rustc-env=RUSTPYTHON_GIT_TAG={}", git_tag());
1014
println!("cargo:rustc-env=RUSTPYTHON_GIT_BRANCH={}", git_branch());
15+
16+
println!(
17+
"cargo:rustc-env=RUSTPYTHON_TARGET_TRIPLE={}",
18+
env::var("TARGET").unwrap()
19+
);
20+
21+
let mut env_path = PathBuf::from(env::var_os("OUT_DIR").unwrap());
22+
env_path.push("env_vars.rs");
23+
let mut f = std::fs::File::create(env_path).unwrap();
24+
write!(
25+
f,
26+
"hashmap! {{ {} }}",
27+
std::env::vars_os().format_with(", ", |(k, v), f| f(&format_args!("{:?} => {:?}", k, v)))
28+
)
29+
.unwrap();
1130
}
1231

1332
fn git_hash() -> String {

vm/src/stdlib/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub mod socket;
3030
mod string;
3131
#[cfg(feature = "rustpython-compiler")]
3232
mod symtable;
33+
mod sysconfigdata;
3334
#[cfg(not(target_arch = "wasm32"))]
3435
mod thread;
3536
mod time_module;
@@ -95,6 +96,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
9596
"_imp".to_owned() => Box::new(imp::make_module),
9697
"unicodedata".to_owned() => Box::new(unicodedata::make_module),
9798
"_warnings".to_owned() => Box::new(warnings::make_module),
99+
crate::sysmodule::sysconfigdata_name() => Box::new(sysconfigdata::make_module),
98100
};
99101

100102
// Insert parser related modules:

vm/src/stdlib/sysconfigdata.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::pyobject::{ItemProtocol, PyObjectRef};
2+
use crate::VirtualMachine;
3+
4+
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
5+
let vars = vm.ctx.new_dict();
6+
macro_rules! hashmap {
7+
($($key:literal => $value:literal),*) => {{
8+
$(vars.set_item($key, vm.new_str($value.to_owned()), vm).unwrap();)*
9+
}};
10+
}
11+
include!(concat!(env!("OUT_DIR"), "/env_vars.rs"));
12+
13+
py_module!(vm, "_sysconfigdata", {
14+
"build_time_vars" => vars,
15+
})
16+
}

vm/src/sysmodule.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,32 @@ fn sys_displayhook(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
222222
Ok(())
223223
}
224224

225+
const PLATFORM: &str = {
226+
cfg_if::cfg_if! {
227+
if #[cfg(any(target_os = "linux", target_os = "android"))] {
228+
// Android is linux as well. see https://bugs.python.org/issue32637
229+
"linux"
230+
} else if #[cfg(target_os = "macos")] {
231+
"darwin"
232+
} else if #[cfg(windows)] {
233+
"win32"
234+
} else {
235+
"unknown"
236+
}
237+
}
238+
};
239+
240+
const ABIFLAGS: &str = "";
241+
242+
// not the same as CPython (e.g. rust's x86_x64-unknown-linux-gnu is just x86_64-linux-gnu)
243+
// but hopefully that's just an implementation detail? TODO: copy CPython's multiarch exactly,
244+
// https://github.com/python/cpython/blob/3.8/configure.ac#L725
245+
const MULTIARCH: &str = env!("RUSTPYTHON_TARGET_TRIPLE");
246+
247+
pub fn sysconfigdata_name() -> String {
248+
format!("_sysconfigdata_{}_{}_{}", ABIFLAGS, PLATFORM, MULTIARCH)
249+
}
250+
225251
pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectRef) {
226252
let ctx = &vm.ctx;
227253

@@ -244,6 +270,7 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectR
244270
let implementation = py_namespace!(vm, {
245271
"name" => ctx.new_str("rustpython".to_owned()),
246272
"cache_tag" => ctx.new_str("rustpython-01".to_owned()),
273+
"_multiarch" => ctx.new_str(MULTIARCH.to_owned()),
247274
});
248275

249276
let path = ctx.new_list(
@@ -255,19 +282,6 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectR
255282
.collect(),
256283
);
257284

258-
let platform = if cfg!(target_os = "linux") {
259-
"linux".to_owned()
260-
} else if cfg!(target_os = "macos") {
261-
"darwin".to_owned()
262-
} else if cfg!(target_os = "windows") {
263-
"win32".to_owned()
264-
} else if cfg!(target_os = "android") {
265-
// Linux as well. see https://bugs.python.org/issue32637
266-
"linux".to_owned()
267-
} else {
268-
"unknown".to_owned()
269-
};
270-
271285
let framework = "".to_owned();
272286

273287
// https://doc.rust-lang.org/reference/conditional-compilation.html#target_endian
@@ -395,7 +409,7 @@ settrace() -- set the global debug tracing function
395409
"_getframe" => ctx.new_function(getframe),
396410
"modules" => modules.clone(),
397411
"warnoptions" => ctx.new_list(vec![]),
398-
"platform" => ctx.new_str(platform),
412+
"platform" => ctx.new_str(PLATFORM.to_owned()),
399413
"_framework" => ctx.new_str(framework),
400414
"meta_path" => ctx.new_list(vec![]),
401415
"path_hooks" => ctx.new_list(vec![]),
@@ -414,7 +428,7 @@ settrace() -- set the global debug tracing function
414428
"exec_prefix" => ctx.new_str(exec_prefix.to_owned()),
415429
"base_exec_prefix" => ctx.new_str(base_exec_prefix.to_owned()),
416430
"exit" => ctx.new_function(sys_exit),
417-
"abiflags" => ctx.new_str("".to_owned()),
431+
"abiflags" => ctx.new_str(ABIFLAGS.to_owned()),
418432
"audit" => ctx.new_function(sys_audit),
419433
"displayhook" => ctx.new_function(sys_displayhook),
420434
"__displayhook__" => ctx.new_function(sys_displayhook),

0 commit comments

Comments
 (0)