Skip to content

Cross-compiling wasm32-unknown-unknown on 64bit debian #1221

Closed
@jruiss

Description

@jruiss

🐛 Bug Reports

When cross-compiling pyo3s build.rs fails on two places without any pointers on what lines, but rather just "Error: NotPresent",

The guilty lines in build.rs:
https://github.com/PyO3/pyo3/blob/master/build.rs#L157, env::var("CARGO_CFG_TARGET_FAMILY") is not set
https://github.com/PyO3/pyo3/blob/master/build.rs#L433, env::var("CARGO_CFG_TARGET_FAMILY") is not set

After that, a custom python 32bit compilation requires PYO3_CROSS_INCLUDE_DIR which is only set for windows in fn cross_compilings https://github.com/PyO3/pyo3/blob/master/build.rs#L150

And finally, Include/pyconfig.h doesn't exists in https://www.python.org/downloads/release/python-379/
Which is required at https://github.com/PyO3/pyo3/blob/master/build.rs#L414

pyconfig.h gets generated when using ./configure and ends up in the root dir.

Also, when python is configured without --enable-shared and such, options retrieved from config_map crashes instead of defaulting to false, dunno if there's a reason for that. https://github.com/PyO3/pyo3/blob/master/build.rs#L415 but unwrap_or(false) might be better than current solution?

Accidently posted this issue before successfully compiling, will update on progress

update:
Well, seem to have gotten rid of all errors in build.rs, it seems to find all neccessary files and and so on but now I get about 100 errors regarding libc.

Using wasm32-wasi instead pulls the errors down to only 3 regarding wchar_t in libc. Seems to be a problem with libc and wasm32 target from here on out.

cargo build --release --target wasm32-wasi
pyo3 git:(master) ✗ cargo build --release --target wasm32-wasi           
   ...
   Compiling libc v0.2.79
   Compiling pyo3 v0.12.1 (/home/cfr/workspace/clones/pyo3)
   ...
error[E0432]: unresolved import `libc::wchar_t`
 --> src/ffi/unicodeobject.rs:3:5
  |
3 | use libc::wchar_t;
  |     ^^^^^^^^^^^^^ no `wchar_t` in the root

error[E0432]: unresolved import `libc::wchar_t`
 --> src/ffi/pylifecycle.rs:2:5
  |
2 | use libc::wchar_t;
  |     ^^^^^^^^^^^^^ no `wchar_t` in the root

error[E0432]: unresolved import `libc::wchar_t`
 --> src/ffi/sysmodule.rs:3:5
  |
3 | use libc::wchar_t;
  |     ^^^^^^^^^^^^^ no `wchar_t` in the root

error: aborting due to 3 previous errors

🌍 Environment

  • Your operating system and version: Linux debian 4.19.0-11-amd64 Ideas and progress #1 SMP Debian 4.19.146-1 (2020-09-17) x86_64 GNU/Linu
  • Your python version: 3.7.9 (default, Oct 10 2020, 16:39:07) [GCC 8.3.0] ('32bit', 'ELF')
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: Compiled from source
  • Your Rust version (rustc --version): rustc 1.49.0-nightly (38d911dfc 2020-10-09)
  • Your PyO3 version: Cloned from git master today
  • Have you tried using latest PyO3 master (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?: yupp

💥 Reproducing

Please provide a minimal working example. This means both the Rust code and the Python.

Please also write what exact flags are required to reproduce your results.

Compiled Python 3.7.9 with

CFLAGS=-m32 LDFLAGS=-m32 ./configure --enable-shared
make

Setup PYO3_CROSS flags

export PYO3_CROSS_PYTHON_VERSION=3.7
export PYO3_PYTHON=/home/Python-3.7.9/python
export PYO3_CROSS_INCLUDE_DIR=/home/Python-3.7.9/Include
export PYO3_CROSS_LIB_DIR=/home/Python-3.7.9/build/lib.linux-x86_64-3.7

lib.rs

use wasm_bindgen::prelude::*;

use pyo3::prelude::*;
use pyo3::types::IntoPyDict;

// Called when the wasm module is instantiated
#[wasm_bindgen(start)]
pub fn main() -> Result<(), JsValue> {
    // Use `web_sys`'s global `window` function to get a handle on the global
    // window object.
    let window = web_sys::window().expect("no global `window` exists");
    let document = window.document().expect("should have a document on window");
    let body = document.body().expect("document should have a body");

    // Manufacture the element we're gonna append
    let val = document.create_element("p")?;

    Python::with_gil(|py| {
        main_(py).map_err(|e| {
          // We can't display Python exceptions via std::fmt::Display,
          // so print the error here manually.
          e.print_and_set_sys_last_vars(py);
    	  val.set_inner_html("Hello from Python!");
        })
    });

    body.append_child(&val)?;

    Ok(())
}

#[wasm_bindgen]
pub fn add(a: u32, b: u32) -> u32 {
    a + b
}

fn main_(py: Python) -> PyResult<()> {
    let sys = py.import("sys")?;
    let version: String = sys.get("version")?.extract()?;
    let locals = [("os", py.import("os")?)].into_py_dict(py);
    let code = "os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'";
    let user: String = py.eval(code, None, Some(&locals))?.extract()?;
    println!("Hello {}, I'm Python {}", user, version);
    Ok(())
}

Cargo.toml

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "*"
pyo3 = {path="/home/cfr/workspace/clones/pyo3"}

[dependencies.web-sys]
version = "0.3.4"
features = [
  'Document',
  'Element',
  'HtmlElement',
  'Node',
  'Window',
]

Build command

wasm-pack build --target web

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions