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

ENH Add Ctypes support #1656

Merged
merged 32 commits into from Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d4d1b8d
Some work on ctypes
Jun 20, 2021
6f68214
Partially set up libffi installation
Jun 23, 2021
83f19a5
Remove ctypes patches
Jun 23, 2021
3983eb3
Get ctypes CI ready
Jun 23, 2021
4d047ff
Fix makefile
Jun 24, 2021
6a33d48
Add libffi to all build rule in Makefile
Jun 24, 2021
c3c5b8e
FIx libffi build
Jun 24, 2021
66400d0
Install texinfo too
Jun 24, 2021
3f1fb66
Patch emsdk to remove failing terser call
Jun 24, 2021
ba398f4
Fix path in patch
Jun 24, 2021
426f5b2
typo eemcc => emcc
Jun 24, 2021
df19ff5
Remove WASM_BIGINT
Jun 24, 2021
9d19afb
Make sure to copy Setup.local
Jun 24, 2021
02a5099
Fix Makefile more
Jun 24, 2021
bde1acc
Try to fix libffi include path
Jun 24, 2021
501d9eb
Try another change to cpython Makefile
Jun 24, 2021
95a8a19
Another Makefile fix
Jun 24, 2021
27df231
Another small fix
Jun 24, 2021
d76638c
More Makefile adjustments
Jun 24, 2021
479ed51
Update configure flags
Jun 24, 2021
558cd84
Update Makefile
Jun 24, 2021
0b00cc5
Add _ctypes_test
Jun 24, 2021
8aa4fa0
Fix _ctypes_test path
Jun 24, 2021
3f2baf4
Patch out CoExtra test
Jun 24, 2021
ece2506
Remove CLAPACK and dependencies
Jun 24, 2021
259fe94
xfail core ctypes tests that have problems with the C APIs
Jun 25, 2021
054fbac
Revert "Remove CLAPACK and dependencies"
Jun 25, 2021
32ba5a6
Remove emscripten patch
Jun 25, 2021
44821df
Try to fix terser error again
Jun 25, 2021
281c721
Restore scipy elliptic harm patch
Jun 25, 2021
c6a526c
Use pyodide/pyodide-env:18 instead of hoodmane/pyodide-env:18
Jun 25, 2021
7e50510
Update changelog
Jun 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -3,7 +3,7 @@ version: 2
defaults: &defaults
working_directory: ~/repo
docker:
- image: pyodide/pyodide-env:17
- image: hoodmane/pyodide-env:18
environment:
- EMSDK_NUM_CORES: 3
EMCC_CORES: 3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -19,6 +19,7 @@ build
ccache
cpython/downloads
cpython/installs
cpython/build.log
docs/_build/
emsdk/emsdk
geckodriver.log
Expand Down
12 changes: 7 additions & 5 deletions Dockerfile
Expand Up @@ -3,11 +3,13 @@ FROM python:3.9.5-slim-buster

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
# building packages
bzip2 ccache clang-format-6.0 cmake f2c g++ gfortran git make \
patch pkg-config swig unzip wget xz-utils \
# testing packages: libgconf-2-4 is necessary for running chromium
libgconf-2-4 "chromium=90.*" \
# building packages
bzip2 ccache clang-format-6.0 cmake f2c g++ gfortran git make \
patch pkg-config swig unzip wget xz-utils \
autoconf autotools-dev automake texinfo \
build-essential prelink autoconf libtool libltdl-dev \
# testing packages: libgconf-2-4 is necessary for running chromium
libgconf-2-4 "chromium=90.*" \
&& rm -rf /var/lib/apt/lists/*

RUN pip3 --no-cache-dir install \
Expand Down
7 changes: 6 additions & 1 deletion Makefile.envs
Expand Up @@ -42,14 +42,17 @@ export LDFLAGS_BASE=\
-s WASM=1 \
-std=c++14 \
-s LZ4=1 \
-L $(CPYTHONROOT)/installs/python-$(PYVERSION)/lib/ \
$(EXTRA_LDFLAGS)

export CXXFLAGS_BASE=

export SIDE_MODULE_LDFLAGS= $(LDFLAGS_BASE) -s SIDE_MODULE=1
export MAIN_MODULE_LDFLAGS= $(LDFLAGS_BASE) -s MAIN_MODULE=1 \
-s EXPORTED_FUNCTIONS='["___cxa_guard_acquire", "__ZNSt3__28ios_base4initEPv", "_main"]' \
$(CPYTHONROOT)/installs/python-$(PYVERSION)/lib/libpython$(PYMINOR).a \
-lpython$(PYMINOR) \
-lffi \
-s LLD_REPORT_UNDEFINED \
-s TOTAL_MEMORY=20971520 \
-s ALLOW_MEMORY_GROWTH=1 \
--use-preload-plugins \
Expand All @@ -59,6 +62,7 @@ export MAIN_MODULE_LDFLAGS= $(LDFLAGS_BASE) -s MAIN_MODULE=1 \
$(wildcard $(CPYTHONROOT)/build/bzip2*/libbz2.a) \
-lstdc++ \
--memory-init-file 0 \
-s WASM_BIGINT \
hoodmane marked this conversation as resolved.
Show resolved Hide resolved

export SIDE_MODULE_CXXFLAGS = $(CXXFLAGS_BASE)

Expand All @@ -73,5 +77,6 @@ export MAIN_MODULE_CFLAGS= $(CFLAGS_BASE) \
-I$(PYTHONINCLUDE)



.output_vars:
set
20 changes: 17 additions & 3 deletions cpython/Makefile
Expand Up @@ -25,8 +25,10 @@ BZIP2TARBALL=$(ROOT)/downloads/bzip2-1.0.2.tar.gz
BZIP2BUILD=$(ROOT)/build/bzip2-1.0.2
BZIP2URL=https://sourceware.org/pub/bzip2/bzip2-1.0.2.tar.gz

FFIBUILD=$(ROOT)/build/libffi
LIBFFIREPO=https://github.com/hoodmane/libffi-emscripten

all: $(INSTALL)/lib/$(LIB)
all: $(INSTALL)/lib/$(LIB) $(INSTALL)/lib/libffi.a


$(INSTALL)/lib/$(LIB): $(BUILD)/$(LIB) remove_modules.txt
Expand Down Expand Up @@ -106,6 +108,14 @@ $(BZIP2BUILD)/libbz2.a: $(BZIP2TARBALL)
emmake make -j $${PYODIDE_JOBS:-3} CC=emcc CFLAGS="$(PYTHON_CFLAGS) -D_FILE_OFFSET_BITS=64" AR=emar RANLIB=emranlib libbz2.a; \
)

$(FFIBUILD)/target/lib/libffi.a :
rm -rf $(FFIBUILD)
git clone $(LIBFFIREPO) $(FFIBUILD) --depth 1
source $(PYODIDE_ROOT)/emsdk/emsdk/emsdk_env.sh
cd $(FFIBUILD) ; bash ./build.sh ; make install

$(INSTALL)/lib/libffi.a : $(INSTALL)/lib/$(LIB) $(FFIBUILD)/target/lib/libffi.a
cp $(FFIBUILD)/target/lib/libffi.a $(INSTALL)/lib/

$(BUILD)/Makefile: $(BUILD)/.patched $(ZLIBBUILD)/.configured $(SQLITEBUILD)/libsqlite3.la $(BZIP2BUILD)/libbz2.a
cp config.site $(BUILD)/
Expand All @@ -125,9 +135,13 @@ $(BUILD)/Makefile: $(BUILD)/.patched $(ZLIBBUILD)/.configured $(SQLITEBUILD)/lib
)


$(BUILD)/$(LIB): $(BUILD)/Makefile Setup.local
$(BUILD)/Modules/Setup.local : Setup.local
cp Setup.local $(BUILD)/Modules/
cat pyconfig.undefs.h >> $(BUILD)/pyconfig.h

$(BUILD)/pyconfig.h : pyconfig.undefs.h
cp pyconfig.undefs.h $(BUILD)/pyconfig.h

$(BUILD)/$(LIB): $(BUILD)/Makefile $(BUILD)/pyconfig.h $(BUILD)/Modules/Setup.local
( \
cd $(BUILD); \
emmake make CROSS_COMPILE=yes $(LIB) -j $${PYODIDE_JOBS:-3} \
Expand Down
4 changes: 4 additions & 0 deletions cpython/Setup.local
Expand Up @@ -18,6 +18,10 @@ _datetime _datetimemodule.c
_heapq _heapqmodule.c
_json _json.c
_csv _csv.c

CTYPES_FLAGS=-DPy_BUILD_CORE_BUILTIN -DPy_BUILD_CORE_MODULE -DHAVE_FFI_PREP_CIF_VAR=1 -DHAVE_FFI_PREP_CLOSURE_LOC=1 -DHAVE_FFI_CLOSURE_ALLOC=1 -I/src/cpython/build/libffi/target/include
_ctypes _ctypes/_ctypes.c _ctypes/callbacks.c _ctypes/callproc.c _ctypes/cfield.c _ctypes/stgdict.c $(CTYPES_FLAGS)

unicodedata unicodedata.c
_pickle _pickle.c
parser parsermodule.c
Expand Down
48 changes: 48 additions & 0 deletions cpython/patches/0001-Remove-duplicate-symbols-from-cfield.c.patch
@@ -0,0 +1,48 @@
From fc3b69f9afa779185a60834cf1817c22706edcd1 Mon Sep 17 00:00:00 2001
From: Hood <hood@mit.edu>
Date: Tue, 22 Jun 2021 20:12:45 -0700
Subject: [PATCH] Remove duplicate symbols from cfield.c

---
Modules/_ctypes/cfield.c | 26 --------------------------
1 file changed, 26 deletions(-)

diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index 7ebd4ba..7a63ab7 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1635,31 +1635,5 @@ typedef struct _ffi_type
} ffi_type;
*/

-/* align and size are bogus for void, but they must not be zero */
-ffi_type ffi_type_void = { 1, 1, FFI_TYPE_VOID };
-
-ffi_type ffi_type_uint8 = { 1, 1, FFI_TYPE_UINT8 };
-ffi_type ffi_type_sint8 = { 1, 1, FFI_TYPE_SINT8 };
-
-ffi_type ffi_type_uint16 = { 2, 2, FFI_TYPE_UINT16 };
-ffi_type ffi_type_sint16 = { 2, 2, FFI_TYPE_SINT16 };
-
-ffi_type ffi_type_uint32 = { 4, INT_ALIGN, FFI_TYPE_UINT32 };
-ffi_type ffi_type_sint32 = { 4, INT_ALIGN, FFI_TYPE_SINT32 };
-
-ffi_type ffi_type_uint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_UINT64 };
-ffi_type ffi_type_sint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_SINT64 };
-
-ffi_type ffi_type_float = { sizeof(float), FLOAT_ALIGN, FFI_TYPE_FLOAT };
-ffi_type ffi_type_double = { sizeof(double), DOUBLE_ALIGN, FFI_TYPE_DOUBLE };
-
-#ifdef ffi_type_longdouble
-#undef ffi_type_longdouble
-#endif
- /* This is already defined on OSX */
-ffi_type ffi_type_longdouble = { sizeof(long double), LONGDOUBLE_ALIGN,
- FFI_TYPE_LONGDOUBLE };
-
-ffi_type ffi_type_pointer = { sizeof(void *), VOID_P_ALIGN, FFI_TYPE_POINTER };

/*---------------- EOF ----------------*/
--
2.17.1

35 changes: 35 additions & 0 deletions cpython/patches/ctypes-dont-deref-function-pointer.patch
@@ -0,0 +1,35 @@
From 3f11694d7587e782530118d176306eeb6eebf5d1 Mon Sep 17 00:00:00 2001
From: Hood <hood@mit.edu>
Date: Wed, 23 Jun 2021 13:47:30 -0700
Subject: [PATCH] Don't dereference function pointer

Ctypes thinks that the result of dlsym is a pointer to the function pointer, so
it should call it like `result = (*f)(args)`. Probably this is true for the
native dlsym, but our dlsym returns an index into the indirect call table
"wasmTable", in particular it isn't even aligned like a pointer should be.
This patch fixes it so that it calls it like `result = f(args)` instead.

---
Modules/_ctypes/_ctypes.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index ceae67e..44f2d76 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -771,7 +771,11 @@ CDataType_in_dll(PyObject *type, PyObject *args)
return NULL;
}
#endif
- return PyCData_AtAddress(type, address);
+ CDataObject *ob = (CDataObject *)GenericPyCData_new(type, NULL, NULL);
+ if (ob == NULL)
+ return NULL;
+ *(void **)ob->b_ptr = address;
+ return (PyObject*)ob;
}

static const char from_param_doc[] =
--
2.17.1

1 change: 0 additions & 1 deletion cpython/remove_modules.txt
@@ -1,5 +1,4 @@
_osx_support.py
ctypes
curses
dbm
ensurepip
Expand Down
33 changes: 33 additions & 0 deletions emsdk/patches/0001-Dummy-out-failing-terser-call.patch
@@ -0,0 +1,33 @@
From 2d6b0f7986afabcf17b049a199b0af795faf5db2 Mon Sep 17 00:00:00 2001
From: Hood <hood@mit.edu>
Date: Thu, 24 Jun 2021 04:08:02 -0700
Subject: [PATCH] Dummy out failing terser call

---
emcc.py | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/emsdk/upstream/emscripten/eemcc.py b/emsdk/upstream/emscripten/emcc.py
index 839f791b3..b3eef03ef 100755
--- a/emsdk/upstream/emscripten/eemcc.py
+++ b/emsdk/upstream/emscripten/eemcc.py
@@ -2901,11 +2901,11 @@ def do_binaryen(target, options, wasm_target):
# Closure can print out readable error messages (Closure will then
# minify whitespace afterwards)
save_intermediate_with_wasm('preclean', wasm_target)
- final_js = building.minify_wasm_js(js_file=final_js,
- wasm_file=wasm_target,
- expensive_optimizations=will_metadce(),
- minify_whitespace=minify_whitespace() and not options.use_closure_compiler,
- debug_info=intermediate_debug_info)
+ # final_js = building.minify_wasm_js(js_file=final_js,
+ # wasm_file=wasm_target,
+ # expensive_optimizations=will_metadce(),
+ # minify_whitespace=minify_whitespace() and not options.use_closure_compiler,
+ # debug_info=intermediate_debug_info)
save_intermediate_with_wasm('postclean', wasm_target)

if shared.Settings.ASYNCIFY_LAZY_LOAD_CODE:
--
2.17.1

2 changes: 0 additions & 2 deletions packages/imageio/meta.yaml
Expand Up @@ -4,8 +4,6 @@ package:
source:
sha256: 52ddbaeca2dccf53ba2d6dec5676ca7bc3b2403ef8b37f7da78b7654bb3e10f0
url: https://files.pythonhosted.org/packages/c3/73/f37f428748c4f10a7991ac5bff00f113a34bcc0d0a78957d6e1cdc29a94e/imageio-2.9.0.tar.gz
patches:
- patches/setitup.patch
requirements:
run:
- numpy
Expand Down
35 changes: 0 additions & 35 deletions packages/imageio/patches/setitup.patch

This file was deleted.

1 change: 0 additions & 1 deletion packages/joblib/meta.yaml
Expand Up @@ -12,7 +12,6 @@ source:

patches:
- patches/use-setuptools.patch
- patches/ctypes-import.patch

test:
imports:
Expand Down
21 changes: 0 additions & 21 deletions packages/joblib/patches/ctypes-import.patch

This file was deleted.

1 change: 0 additions & 1 deletion packages/numpy/meta.yaml
Expand Up @@ -20,7 +20,6 @@ source:
- patches/make-int-return-values.patch
- patches/fix-ieee754.patch
- patches/rm-duplicate-symbols-link.patch
- patches/delay-ctypes-import.patch

build:
skip_host: False
Expand Down