Skip to content

Commit 26db1f2

Browse files
committed
[GR-15770] [GR-15772] [GR-16311] [GR-15769] [GR-16445] [GR-14742] Use toolchain to build C API and extensions.
PullRequest: graalpython/553
2 parents cadf050 + 7d63eb0 commit 26db1f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1957
-2033
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ language
3434
/*.py
3535
*.o
3636
*.so
37+
*.dylib
3738
*.rej
3839
.checkstyle
3940
/graalpython/com.oracle.graal.python.cext/compile_flags.txt
@@ -57,6 +58,7 @@ Python3.g4.stamp
5758
*.orig
5859
/graalpython/com.oracle.graal.python.test/src/tests/cpyext/Py*.c
5960
/graalpython/com.oracle.graal.python.test/src/tests/cpyext/Test*.c
61+
/graalpython/com.oracle.graal.python.test/src/tests/cpyext/*.sha256
6062
/*.diff
6163
## generated from: pyhocon -i ci.hocon -f json -o ci.json
6264
/ci.json

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,41 @@ source <dir-to-venv>/bin/activate
3232

3333
In the venv multiple executables are available, like `python`, `python3` and `graalpython`.
3434

35+
### Using modules with C extensions
36+
37+
This Python implementation is able to load and run modules with C extensions.
38+
Supporting C extensions is one of the most difficult features for any Python
39+
implementation since it requires to be compatible to CPython's C API.
40+
41+
However, GraalVM's Python implementation is capable of executing C extensions and
42+
there is also no optimization boundary.
43+
44+
In order to be able to run C extensions, a user must first build the C API
45+
runtime library. It is recommended to build the C API in any case because
46+
it will only be used if necessary.
47+
The recommended way to do so is to create a venv (see [Create a virtual environment](#create-a-virtual-environment))
48+
and run everything within the venv.
49+
50+
If you don't want to create and use a venv, the C API can be built using
51+
following command:
52+
```
53+
mx python -m build_capi
54+
```
55+
56+
You can test if everything was built correctly by, for example, using
57+
a memoryvew object:
58+
```
59+
(your-venv) graalpython -c "print(repr(memoryview(b'')))"
60+
```
61+
62+
in the venv or
63+
64+
```
65+
mx python -c "print(repr(memoryview(b'')))"
66+
```
67+
68+
without a venv.
69+
3570
### Installing packages
3671

3772
At the moment not enough of the standard library is implemented to run the

graalpython/com.oracle.graal.python.cext/Makefile

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,68 +36,26 @@
3636
# SOFTWARE.
3737

3838
QUIETLY$(MX_VERBOSE) = @
39-
LIBDIR=../lib-graalpython
40-
TARGET_LIB=${LIBDIR}/capi.bc
41-
39+
.PHONY: default clean
4240

43-
SOURCE_FILES=$(wildcard src/*.c)
44-
OBJ_FILES=${SOURCE_FILES:%.c=%.o}
4541
INCLUDE_FILES=$(wildcard include/*.h)
46-
47-
MODULE_SOURCES=$(wildcard modules/*.c)
48-
MODULE_OBJ_FILES=$(MODULE_SOURCES:%.c=%.o)
49-
MODULE_TARGETS=$(MODULE_SOURCES:modules/%.c=${LIBDIR}/modules/%.bc)
42+
LEGACY_INCLUDES=$(wildcard $(TRUFFLE_H_INC)/*.h)
5043

5144
# These are just needed to allow compilation from source, because include needs
5245
# to be next to lib-graalpython
53-
HEADER_TARGETS=$(INCLUDE_FILES:include/%.h=../include/%.h)
54-
55-
.PHONY: default clean
56-
default: ${TARGET_LIB} ${MODULE_TARGETS} ${HEADER_TARGETS} compile_flags.txt
57-
58-
59-
CFLAGS=${LLVM_TARGET_FLAGS} -O1 -ggdb -emit-llvm
60-
OPT_FLAGS=-mem2reg -globalopt -simplifycfg -constprop -always-inline -instcombine -dse -loop-simplify -reassociate -licm -gvn
61-
WARNINGS=-Wno-int-to-pointer-cast -Wno-int-conversion -Wno-incompatible-pointer-types-discards-qualifiers -Wno-pointer-type-mismatch
62-
INCLUDES=-I${POLYGLOT_INC} -Iinclude
63-
46+
HEADER_TARGETS=$(INCLUDE_FILES:include/%.h=../include/%.h)
47+
LEGACY_TARGETS=$(LEGACY_INCLUDES:$(TRUFFLE_H_INC)/%.h=../include/%.h)
6448

65-
rebuild:
66-
$(MAKE) clean
67-
$(MAKE) default
49+
default: ${HEADER_TARGETS} ${LEGACY_TARGETS}
6850

69-
${LIBDIR}/modules/%.bc: modules/%.o
70-
$(QUIETLY) mkdir -p ${LIBDIR}/modules
71-
$(QUIETLY) llvm-link -o $@ $+
72-
73-
${TARGET_LIB}: ${OBJ_FILES}
74-
$(QUIETLY) llvm-link -o $@ $+
75-
76-
src/%.o: src/%.c Makefile src/capi.h ${INCLUDE_FILES}
77-
$(QUIETLY) clang ${CFLAGS} ${WARNINGS} ${INCLUDES} -o $@ -c $<
78-
$(QUIETLY) opt -o $@ $@ ${OPT_FLAGS}
79-
80-
modules/%.o: modules/%.c Makefile src/capi.h ${INCLUDE_FILES}
81-
$(QUIETLY) clang ${CFLAGS} ${WARNINGS} ${INCLUDES} -o $@ -c $<
82-
$(QUIETLY) opt -o $@ $@ ${OPT_FLAGS}
83-
84-
../include/%.h: ${INCLUDE_FILES}
51+
$(HEADER_TARGETS): ${INCLUDE_FILES}
8552
$(QUIETLY) mkdir -p ../include/
8653
$(QUIETLY) cp $(@:../include/%.h=include/%.h) $@
8754

55+
$(LEGACY_TARGETS): ${LEGACY_INCLUDES}
56+
$(QUIETLY) mkdir -p ../include/
57+
$(QUIETLY) cp $(@:../include/%.h=$(TRUFFLE_H_INC)/%.h) $@
58+
8859
clean:
89-
$(QUIETLY) rm -f ${TARGET_LIB}
90-
$(QUIETLY) rm -f ${OBJ_FILES}
91-
$(QUIETLY) rm -f ${MODULE_TARGETS}
92-
$(QUIETLY) rm -f ${MODULE_OBJ_FILES}
9360
$(QUIETLY) rm -f ${HEADER_TARGETS}
94-
$(QUIETLY) rm -f compile_flags.txt
95-
ifeq ($(wildcard ../com.oracle.graal.python.test/src/tests/cpyext/*.bc),)
96-
rm -f $(wildcard ../com.oracle.graal.python.test/src/tests/cpyext/*.bc)
97-
endif
98-
99-
# compile_flags.txt is useful with clangd as language server
100-
compile_flags.txt: Makefile
101-
$(QUIETLY) $(file > $@,-O1)
102-
$(QUIETLY) $(foreach var,$(WARNINGS),$(file >> $@,$(var)))
103-
$(QUIETLY) $(foreach var,$(INCLUDES),$(file >> $@,$(var)))
61+
$(QUIETLY) rm -f $(LEGACY_TARGETS)

0 commit comments

Comments
 (0)