Skip to content

Commit 8f41572

Browse files
authored
tests(cclibs): Basic test that the current_py_cc_libs target works (#1749)
This is just a basic to to ensure the `@rules_python//python/cc:current_py_cc_libs` target is making the Python C libraries available; that the compiler and linker are both happy enough to resolve symbols. The test program itself isn't actually runnable, as that requires more configuration than I can figure out in telling Python where its embedded runtime information is. Fow now, the test is restricted to Linux. The Mac and Windows CC building isn't happy with it, but I'm out of my depth about how to make those happy. Work towards #824
1 parent a5e17e6 commit 8f41572

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

tests/cc/current_py_cc_libs/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,19 @@
1515
load(":current_py_cc_libs_tests.bzl", "current_py_cc_libs_test_suite")
1616

1717
current_py_cc_libs_test_suite(name = "current_py_cc_libs_tests")
18+
19+
# buildifier: disable=native-cc
20+
cc_test(
21+
name = "python_libs_linking_test",
22+
srcs = ["python_libs_linking_test.cc"],
23+
# Mac and Windows fail with linking errors, but its not clear why; someone
24+
# with more C + Mac/Windows experience will have to figure it out.
25+
# - rickeylev@
26+
target_compatible_with = [
27+
"@platforms//os:linux",
28+
],
29+
deps = [
30+
"@rules_python//python/cc:current_py_cc_headers",
31+
"@rules_python//python/cc:current_py_cc_libs",
32+
],
33+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <Python.h>
2+
3+
int main(int argc, char** argv) {
4+
// Early return to prevent the broken code below from running.
5+
if (argc >= 1) {
6+
return 0;
7+
}
8+
9+
// The below code won't actually run. We just reference some Python
10+
// symbols so the compiler and linker do some work to verify they are
11+
// able to resolve the symbols.
12+
// To make it actually run, more custom initialization is necessary.
13+
// See https://docs.python.org/3/c-api/intro.html#embedding-python
14+
Py_Initialize();
15+
PyRun_SimpleString("print('Hello, world')\n");
16+
Py_Finalize();
17+
return 0;
18+
}

0 commit comments

Comments
 (0)