Skip to content

Commit 091bd33

Browse files
committed
test case with shared library libs
1 parent deb8934 commit 091bd33

File tree

8 files changed

+135
-0
lines changed

8 files changed

+135
-0
lines changed

tests/support/copy_file.bzl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Copies a file to a directory."""
2+
3+
def _copy_file_to_dir_impl(ctx):
4+
out_file = ctx.actions.declare_file(
5+
"{}/{}".format(ctx.attr.out_dir, ctx.file.src.basename),
6+
)
7+
ctx.actions.run_shell(
8+
inputs = [ctx.file.src],
9+
outputs = [out_file],
10+
arguments = [ctx.file.src.path, out_file.path],
11+
command = 'cp -f "$1" "$2"',
12+
progress_message = "Copying %{input} to %{output}",
13+
)
14+
return [DefaultInfo(files = depset([out_file]))]
15+
16+
copy_file_to_dir = rule(
17+
implementation = _copy_file_to_dir_impl,
18+
attrs = {
19+
"src": attr.label(
20+
allow_single_file = True,
21+
mandatory = True,
22+
),
23+
"out_dir": attr.string(mandatory = True),
24+
},
25+
)

tests/venv_site_packages_libs/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,15 @@ py_reconfig_test(
3434
"@other//with_external_data",
3535
],
3636
)
37+
38+
py_reconfig_test(
39+
name = "shared_lib_loading_test",
40+
srcs = ["shared_lib_loading_test.py"],
41+
bootstrap_impl = "script",
42+
main = "shared_lib_loading_test.py",
43+
target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT,
44+
venvs_site_packages = "yes",
45+
deps = [
46+
"//tests/venv_site_packages_libs/ext_with_libs",
47+
],
48+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
2+
load("@rules_cc//cc:cc_shared_library.bzl", "cc_shared_library")
3+
load("//python:py_library.bzl", "py_library")
4+
load("//tests/support:copy_file.bzl", "copy_file_to_dir")
5+
6+
package(
7+
default_visibility = ["//visibility:public"],
8+
licenses = ["notice"],
9+
)
10+
11+
cc_library(
12+
name = "increment_impl",
13+
srcs = ["increment.c"],
14+
deps = [":increment_headers"],
15+
)
16+
17+
cc_library(
18+
name = "increment_headers",
19+
hdrs = ["increment.h"],
20+
)
21+
22+
cc_shared_library(
23+
name = "increment",
24+
deps = [":increment_impl"],
25+
)
26+
27+
cc_library(
28+
name = "adder_impl",
29+
srcs = ["adder.c"],
30+
deps = [
31+
":increment_headers",
32+
"@rules_python//python/cc:current_py_cc_headers",
33+
],
34+
)
35+
36+
# todo: copy more from py_extension in local_toolchains
37+
cc_shared_library(
38+
name = "adder",
39+
dynamic_deps = [":increment"],
40+
shared_lib_name = select({
41+
"//conditions:default": "adder.so",
42+
}),
43+
user_link_flags = [
44+
"-Wl,-rpath,$ORIGIN/libs",
45+
],
46+
deps = [":adder_impl"],
47+
)
48+
49+
copy_file_to_dir(
50+
name = "relocate_adder",
51+
src = ":adder",
52+
out_dir = "site-packages/ext_with_libs",
53+
)
54+
55+
copy_file_to_dir(
56+
name = "relocate_increment",
57+
src = ":increment",
58+
out_dir = "site-packages/ext_with_libs/libs",
59+
)
60+
61+
py_library(
62+
name = "ext_with_libs",
63+
srcs = glob(["site-packages/**/*.py"]),
64+
data = [
65+
":relocate_adder",
66+
":relocate_increment",
67+
],
68+
experimental_venvs_site_packages = "//python/config_settings:venvs_site_packages",
69+
imports = [package_name() + "/site-packages"],
70+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <Python.h>
2+
3+
#include "increment.h"
4+
5+
static PyObject *do_add(PyObject *self, PyObject *Py_UNUSED(args)) {
6+
return PyLong_FromLong(increment(1));
7+
}
8+
9+
static PyMethodDef AdderMethods[] = {
10+
{"do_add", do_add, METH_NOARGS, "Add one"}, {NULL, NULL, 0, NULL}};
11+
12+
static struct PyModuleDef addermodule = {PyModuleDef_HEAD_INIT, "adder", NULL,
13+
-1, AdderMethods};
14+
15+
PyMODINIT_FUNC PyInit_adder(void) { return PyModule_Create(&addermodule); }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "increment.h"
2+
3+
int increment(int val) { return val + 1; }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef TESTS_VENV_SITE_PACKAGES_LIBS_EXT_WITH_LIBS_INCREMENT_H_
2+
#define TESTS_VENV_SITE_PACKAGES_LIBS_EXT_WITH_LIBS_INCREMENT_H_
3+
4+
int increment(int);
5+
6+
#endif // TESTS_VVENV_SITE_PACKAGES_LIBS_EXT_WITH_LIBS_INCREMENT_H_

tests/venv_site_packages_libs/ext_with_libs/site-packages/ext_with_libs/__init__.py

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
import ext_with_libs
4+
import ext_with_libs.adder

0 commit comments

Comments
 (0)