ocaml-cross / opam-cross-android

An OCaml cross-toolchain for Android and several useful libraries
105 stars 15 forks source link

ctypes-android 0.6.1 fails to compile #23

Closed holocronweaver closed 8 years ago

holocronweaver commented 8 years ago

Ok, may have an actual bug this time. ctypes 0.6.1 fails to compile. See log.

=-=- Processing actions -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[ERROR] The compilation of ctypes-android failed at "make OCAMLFIND=ocamlfind -toolchain android HOSTOCAMLFIND=ocamlfind".
Processing  1/3: [ctypes-android: ocamlfind android]
#=== ERROR while installing ctypes-android.0.6.1 ==============================#
# opam-version 1.2.2
# os           linux
# command      make OCAMLFIND=ocamlfind -toolchain android HOSTOCAMLFIND=ocamlfind
# path         /home/jesse/.opam/4.02.3+32bit/build/ctypes-android.0.6.1
# compiler     4.02.3+32bit
# exit-code    2
# env-file     /home/jesse/.opam/4.02.3+32bit/build/ctypes-android.0.6.1/ctypes-android-6990-d0f245.env
# stdout-file  /home/jesse/.opam/4.02.3+32bit/build/ctypes-android.0.6.1/ctypes-android-6990-d0f245.out
# stderr-file  /home/jesse/.opam/4.02.3+32bit/build/ctypes-android.0.6.1/ctypes-android-6990-d0f245.err
### stdout ###
# [...]
# ocamlfind -toolchain android ocamlc -package str -package bigarray -package bytes   -principal -short-paths   -c -o _build/src/ctypes/unsigned.cmo -I _build/src/ctypes  src/ctypes/unsigned.ml
# ocamlfind -toolchain android ocamlc -bin-annot -c -o _build/src/ctypes/signed.cmi -package str -package bigarray -package bytes   -principal -short-paths -I _build/src/ctypes  src/ctypes/signed.mli
# ocamlfind -toolchain android ocamlc -package str -package bigarray -package bytes   -principal -short-paths   -c -o _build/src/ctypes/signed.cmo -I _build/src/ctypes  src/ctypes/signed.ml
# ocamlfind -toolchain android ocamlc -package str -package bigarray -package bytes   -principal -short-paths   -c -o _build/src/ctypes/ctypes_ptr.cmo -I _build/src/ctypes  src/ctypes/ctypes_ptr.ml
# ocamlfind -toolchain android ocamlc -bin-annot -c -o _build/src/ctypes/ctypes_primitive_types.cmi -package str -package bigarray -package bytes   -principal -short-paths -I _build/src/ctypes  src/ctypes/ctypes_primitive_types.mli
# ocamlfind -toolchain android ocamlc -package str -package bigarray -package bytes   -principal -short-paths   -c -o _build/src/ctypes/ctypes_primitive_types.cmo -I _build/src/ctypes  src/ctypes/ctypes_primitive_types.ml
# ocamlfind -toolchain android ocamlc -package str -package bigarray -package bytes   -principal -short-paths   -c -o _build/src/ctypes/ctypes_memory_stubs.cmo -I _build/src/ctypes  src/ctypes/ctypes_memory_stubs.ml
# ocamlfind -toolchain android ocamlc -package str -package bigarray -package bytes   -principal -short-paths   -c -o _build/src/ctypes/ctypes_bigarray_stubs.cmo -I _build/src/ctypes  src/ctypes/ctypes_bigarray_stubs.ml
# ocamlfind -toolchain android ocamlc -package str -package bigarray -package bytes   -principal -short-paths   -c -o _build/src/ctypes/ctypes_primitives.cmo -I _build/src/ctypes  src/ctypes/ctypes_primitives.ml
# Makefile.rules:94: recipe for target '_build/src/ctypes/ctypes_primitives.cmo' failed
### stderr ###
# File "src/ctypes/ctypes_primitives.ml", line 1:
# Error: I/O error: src/ctypes/ctypes_primitives.ml: No such file or directory
# make: *** [_build/src/ctypes/ctypes_primitives.cmo] Error 2

Oddly running make all in the build directory seems to complete successfully.

whitequark commented 8 years ago

Oddly running make all in the build directory seems to complete successfully.

That's not a cross-build.

I will take a look shortly.

whitequark commented 8 years ago

What is the exact compiler configuration you are using? (the conf-android installation command)

holocronweaver commented 8 years ago

ARCH=arm SUBARCH=armv7 SYSTEM=linux_eabi CCARCH=arm TOOLCHAIN=arm-linux-androideabi-4.9 TRIPLE=arm-linux-androideabi LEVEL=19 STLVER=4.9 STLARCH=armeabi opam reinstall conf-android

whitequark commented 8 years ago

OK so here's your problem: API level 19 is not supported with ctypes.

- In file included from /tmp/ctypes_libffi_configd73136.c:3:0:
- /home/whitequark/.opam/4.02.3+32bit/android-ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/complex.h:42:46: warning: missing terminating ' character
-  # error Cannot include both <complex> and C99's <complex.h>
-                                               ^
- /home/whitequark/.opam/4.02.3+32bit/android-ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/complex.h:47:26: fatal error: complex.h: No such file or directory
-  #include_next <complex.h>
-                           ^
- compilation terminated.
whitequark commented 8 years ago

You can apply the following patch to ctypes to make it compile. This will require you to create an OPAM overlay (copy the packages/ctypes-android.0.6.1 somewhere then opam repo add overlay . there), put this patch into files/patches/backport-complex.patch, then add a patches: ["patches/backport-complex.patch"] to the opam file).

--- a/src/configure/extract_from_c.ml  Wed Jul 13 10:02:47 2016
+++ b/src/configure/extract_from_c.ml  Wed Jul 13 10:02:49 2016
@@ -54,7 +54,7 @@
 let headers = "\
 #include <stdint.h>
 #include <stdbool.h>
-#include <complex.h>
+//#include <complex.h>
 #include <inttypes.h>
 #include <caml/mlvalues.h>
 "
--- a/src/configure/gen_c_primitives.ml  Wed Jul 13 10:02:58 2016
+++ b/src/configure/gen_c_primitives.ml  Wed Jul 13 10:03:00 2016
@@ -51,8 +51,8 @@
   c_primitive "Uint64_t"  "uint64_t"           (Defined_format "PRIu64");
   c_primitive "Float"     "float"              (Known_format ".12g");
   c_primitive "Double"    "double"             (Known_format ".12g");
-  c_primitive "Complex32" "float complex"      (No_format);
-  c_primitive "Complex64" "double complex"     (No_format);
+  c_primitive "Complex32" "float _Complex"      (No_format);
+  c_primitive "Complex64" "double _Complex"     (No_format);
   c_primitive "Nativeint" "intnat"             (Defined_format "REAL_ARCH_INTNAT_PRINTF_FORMAT \"d\"");
   { constructor = "Camlint";
     typ         = "camlint";
holocronweaver commented 8 years ago

I followed your instructions, and the patch seems to be applied correctly, but I get a similar error from a different file:

# In file included from /home/jesse/.opam/4.02.3+32bit/build/ctypes-android.0.6.1/src/ctypes/complex_stubs.c:8:0:
# /home/jesse/.opam/4.02.3+32bit/android-ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/complex.h:42:46: warning: missing terminating ' character
#  # error Cannot include both <complex> and C99's <complex.h>
#                                               ^
# /home/jesse/.opam/4.02.3+32bit/android-ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/complex.h:47:26: fatal error: complex.h: No such file or directory
#  #include_next <complex.h>
#                           ^

Do these files c files need to be patched as well, or is there another ocaml generator file that needs patching?

whitequark commented 8 years ago

Hm, weird, I recall compiling it successfully... in any case complex stubs should be handwritten, so just another patch.

lingz commented 8 years ago

Sorry to revive this issue but what's the status with using ctypes. Do we need to cross compile the C code separately for ARM and then link it after the fact? Or can this tool chain compile the actual C as well.

whitequark commented 8 years ago

@lingz It can. Compile the C code with ocamlfind -toolchain android ocamlc foo.c.

lingz commented 8 years ago

And I guess I can also use ocamlopt and directly use linker flags and c flags as usual to link to both static and dynamic libraries?

whitequark commented 8 years ago

No. ocamlc only knows the flags for compiling a C file to an object file.

To link something, you need to make an opam package for your project, and then use the opam variables from the conf-android package. E.g. see how bzip2-sys-android does it: https://github.com/whitequark/opam-cross-android/blob/master/packages/bzip2-sys-android.1.0.6/opam