Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example program #64

Closed
likeafox opened this issue Oct 29, 2016 · 8 comments
Closed

Example program #64

likeafox opened this issue Oct 29, 2016 · 8 comments

Comments

@likeafox
Copy link

likeafox commented Oct 29, 2016

I'm running into trouble trying to get the first example program on this project's main page to work for me. It builds OK, but running it does nothing but crash. I don't even know where to begin troubleshooting this:

cargo run
Running `target\debug\yay_python.exe`
error: Process didn't exit successfully: `target\debug\yay_python.exe` (exit code: 3221225477)

I think it is failing at let sys = py.import("sys").unwrap(); because the result is identical when everything after that line is commented out.

My environment:
Windows
Rust 1.12.1 gnu 32 bit
Python 3.4.4 32 bit

What could I be doing wrong?

@dgrunwald
Copy link
Owner

rust-cpython currently only works on Linux and OSX. I don't know how to correctly link with the python DLL on Windows.
windows-gnu produces weirdly corrupt binaries (which might explain the crash you are seeing); and windows-msvc doesn't link at all (see #10).

@AraHaan
Copy link

AraHaan commented Dec 10, 2016

With a quick google search I found this:
https://users.rust-lang.org/t/help-needed-linking-external-libraries-on-windows/7440
Basically if you know where they installed python to you can allow them to compile it using the pythionxx.dll for windows.

For example I have Python 3.6 installed at %SystemDrive%\Python360\ and that lib file can be found at %SystemDrive%\Python360\libs\python36.lib (for 32 bit python)
And for 64 bit 3.6 I have it at %SystemDrive%\Python360x64\ and so the lib file would be at %SystemDrive%\Python360x64\libs\python36.lib.

With that in mind one can compile for Windows.

Also dont forget that 32 bit pyd files would have to be named (since 3.5) [module name].cp3#-win32.pyd. And for 64 bit pyd's (for Windows, Linux, and all other python distributions [module name].cp3#-win_amd64.pyd

Maybe it is possible to allow people who are using this crate to define it in their Cargo.toml themself as it would need to be customized to where they have python installed at.

@AraHaan
Copy link

AraHaan commented Dec 10, 2016

Another idea is to look for the python libs in %SystemDrive%\Pythonlibs and then have the end user copy the pythonxx.lib files to that folder.

@dgrunwald
Copy link
Owner

Finding the .lib is not the problem.

@AraHaan
Copy link

AraHaan commented Dec 10, 2016

hmm
When what part on the linking that lib file is?

@dgrunwald
Copy link
Owner

See my previous comment in this thread.
Or in short: windows-gnu produces corrupt binaries; windows-msvc fails due to missing __declspec(dllimport).

@AraHaan
Copy link

AraHaan commented Dec 10, 2016

ok, I think I made a somewhat better rewrite on the build script:

extern crate target_build_utils;
use std::env;
use std::io::Write;
use target_build_utils::TargetInfo;

const CFG_KEY: &'static str = "py_sys_config";

#[cfg(feature="python27-sys")]
const PYTHONSYS_ENV_VAR: &'static str = "DEP_PYTHON27_PYTHON_FLAGS";

#[cfg(feature="python3-sys")]
const PYTHONSYS_ENV_VAR: &'static str = "DEP_PYTHON3_PYTHON_FLAGS";

fn main() {
    // python{27,3.x}-sys/build.rs passes python interpreter compile flags via 
    // environment variable (using the 'links' mechanism in the cargo.toml).
    let flags = match env::var(PYTHONSYS_ENV_VAR) {
        Ok(flags) => flags,
        Err(_) => {
            writeln!(std::io::stderr(),
                "Environment variable {} not found - this is supposed to be \
                 exported from the pythonXX-sys dependency, so the build chain is broken",
                PYTHONSYS_ENV_VAR).unwrap();
            std::process::exit(1);
        }
    };
    let target = TargetInfo::new().expect("could not get target");
    if target.target_vendor() == "x86_64-pc-windows-msvc" {
        // MSVC Building here.
        // This is where I think __declspec(dllimport) can be defined.
    }
    if target.target_vendor() == "i686-pc-windows-msvc" {
        // MSVC Building here.
        // This is where I think __declspec(dllimport) can be defined.
    }
    if target.target_vendor() == "x86_64-pc-windows-gnu" {
        // gnu Building here.
    }
    if target.target_vendor() == "i686-pc-windows-gnu" {
        // gnu Building here.
    }

    if flags.len() > 0 {
        for f in flags.split(",") {
            // write out flags as --cfg so that the same #cfg blocks can be used
            // in rust-cpython as in the -sys libs
            let key_and_val: Vec<&str> = f.split("=").collect();
            let key = key_and_val[0];
            let val = key_and_val[1];
            if key.starts_with("FLAG") {
                println!("cargo:rustc-cfg={}=\"{}\"", CFG_KEY, &key[5..])
            } else {
                println!("cargo:rustc-cfg={}=\"{}_{}\"", CFG_KEY, &key[4..], val);
            }
        }
    }
}

With this it would mean also adding this to Cargo.toml:

[build-dependencies]
target_build_utils = "*"

edit: now I see this on issue: rust-lang/rust#27438

@dgrunwald
Copy link
Owner

Linking on windows-msvc should be working now, assuming you are using the latest nightly -- the Rust issues with dllimport were just fixed in rustc 1.15.0-nightly (8f02c429a 2016-12-15)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants