Skip to content

Commit

Permalink
Fix bundled library and generated cffi module paths for nested packag…
Browse files Browse the repository at this point in the history
…es (#16)
  • Loading branch information
daa authored and mitsuhiko committed Aug 29, 2018
1 parent 1f3cf00 commit ef0723e
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 2 deletions.
4 changes: 2 additions & 2 deletions milksnake/setuptools_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def __init__(self, spec, module_path, dylib=None, header_filename=None,
self.header_strip_directives = header_strip_directives
self.rtld_flags = get_rtld_flags(rtld_flags)

parts = self.module_path.rsplit('.', 2)
parts = self.module_path.rsplit('.', 1)
self.module_base = parts[0]
self.name = parts[-1]

Expand Down Expand Up @@ -276,7 +276,7 @@ def build_cffi(base_path, **extra):
ffi = make_ffi()
log.info('generating cffi module for %r' % self.module_path)
py_file = os.path.join(
base_path, *self.cffi_module_path.split('.')[1:]) + '.py'
base_path, self.cffi_module_path.split('.')[-1]) + '.py'
updated = cffi_recompiler.make_py_source(
ffi, self.cffi_module_path, py_file)
if not updated:
Expand Down
1 change: 1 addition & 0 deletions tests/res/nested/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example/_native*
Empty file.
6 changes: 6 additions & 0 deletions tests/res/nested/example/nested/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from . import _native


def test():
point = _native.lib.example_get_origin()
return (point.x, point.y)
2 changes: 2 additions & 0 deletions tests/res/nested/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
Cargo.lock
8 changes: 8 additions & 0 deletions tests/res/nested/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "example"
version = "0.1.0"
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]

[lib]
name = "example"
crate-type = ["cdylib"]
13 changes: 13 additions & 0 deletions tests/res/nested/rust/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
typedef struct {
float x;
float y;
} Point;

typedef enum {
FOO_A = 1,
FOO_B = 2,
FOO_C = 3
} Foo;

Point example_get_origin(void);
void example_print_foo(Foo *);
26 changes: 26 additions & 0 deletions tests/res/nested/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[repr(C)]
pub struct Point {
pub x: f32,
pub y: f32,
}

#[repr(u32)]
pub enum Foo {
A = 1,
B,
C
}

#[no_mangle]
pub unsafe extern "C" fn example_get_origin() -> Point {
Point { x: 0.0, y: 0.0 }
}

#[no_mangle]
pub unsafe extern "C" fn example_print_foo(foo: *const Foo) {
println!("{}", match *foo {
Foo::A => "a",
Foo::B => "b",
Foo::C => "c",
});
}
35 changes: 35 additions & 0 deletions tests/res/nested/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from setuptools import setup, find_packages

def build_native(spec):
# Step 1: build the rust library
build = spec.add_external_build(
cmd=['cargo', 'build', '--release'],
path='./rust'
)

# Step 2: add a cffi module based on the dylib we built
#
# We use lambdas here for dylib and header_filename so that those are
# only called after the external build finished.
spec.add_cffi_module(
module_path='example.nested._native',
dylib=lambda: build.find_dylib('example', in_path='target/release'),
header_filename=lambda: build.find_header('example.h', in_path='.'),
rtld_flags=['NOW', 'NODELETE']
)


setup(
name='example',
version='0.0.1',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
platforms='any',
install_requires=[
'milksnake',
],
milksnake_tasks=[
build_native,
]
)
11 changes: 11 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ def test_example_dev_run(virtualenv):
def execute():
assert test() == (0.0, 0.0)
''')


def test_example_nested_dev_run(virtualenv):
pkg = os.path.abspath(os.path.join(os.path.dirname(__file__),
'res', 'nested'))
virtualenv.run('pip', ['install', '-v', '--editable', pkg])
virtualenv.eval('''if 1:
from example.nested import test
def execute():
assert test() == (0.0, 0.0)
''')

0 comments on commit ef0723e

Please sign in to comment.