bobot / ocaml-flint

GNU Lesser General Public License v2.1
5 stars 3 forks source link

Allow passing custom library prefix #2

Open mseri opened 2 years ago

mseri commented 2 years ago

Flint fails to compile for me with

- File "flint/flint/dune", line 5, characters 0-9578:
-   5 | (rule
-   6 |  (alias flint2_build)
-   7 |  (targets
- .....
- 271 |     (copy prefix/include/flint/thread_pool.h thread_pool.h)
- 272 |     (copy prefix/include/flint/thread_support.h thread_support.h)
- 273 |     (copy prefix/include/flint/ulong_extras.h ulong_extras.h)))))
- (cd _build/.sandbox/a98ebd6af55d85543f7c87e281f980d4/default/flint/flint/flint2 && ./configure --prefix=../prefix 'CFLAGS=-fPIC -D_GNU_SOURCE')
- Invalid GMP directory
[ERROR] The compilation of flint.0.1.5 failed at "dune build -p flint -j 7 @install".

This is because flint looks by default into /usr/local. On my system (macos M1 and x86_64 with macports) I need to call configure as ./configure --with-gmp=/opt/local --with-mpfr=/opt/local --with-blas=/opt/local/include/lapack to make flint configure.

I still get some weird failures on ARM after that change:

(cd _build/.sandbox/8d3842cec7d00cf7bfbddd396f2cb247/default/flint/flint/flint2 && ./configure --prefix=../prefix 'CFLAGS=-fPIC -D_GNU_SOURCE' --with-gmp=/opt/local --with-mpfr=/opt/local --with-blas=/opt/local)
./configure: line 125: config.log: Permission denied
./configure: line 126: config.log: Permission denied
cp: fmpz/fmpz.c: Permission denied
cp: fmpz-conversions.h: Permission denied
Configuring...unknown-Darwin
./configure: line 475: config.log: Permission denied
cp: fft_tuning.h: Permission denied
Testing __builtin_popcountl..../configure: line 574: config.log: Permission denied
./configure: line 581: build/test-popcnt.c: Permission denied
yes
./configure: line 585: config.log: Permission denied
Testing __thread..../configure: line 662: config.log: Permission denied
yes
Testing fenv..../configure: line 691: config.log: Permission denied
yes
./configure: line 698: config.log: Permission denied
Testing big endian..../configure: line 711: config.log: Permission denied
no
./configure: line 717: config.log: Permission denied
Testing cpu_set_t..../configure: line 736: config.log: Permission denied
no
./configure: line 749: config.log: Permission denied
./configure: line 798: flint-config.h: Permission denied
./configure: line 799: flint-config.h: Permission denied
./configure: line 800: flint-config.h: Permission denied
./configure: line 801: flint-config.h: Permission denied
./configure: line 802: flint-config.h: Permission denied
./configure: line 803: flint-config.h: Permission denied
./configure: line 804: flint-config.h: Permission denied
./configure: line 805: flint-config.h: Permission denied
./configure: line 806: flint-config.h: Permission denied
./configure: line 807: flint-config.h: Permission denied
./configure: line 808: flint-config.h: Permission denied
./configure: line 816: flint-config.h: Permission denied
./configure: line 821: Makefile: Permission denied
./configure: line 822: Makefile: Permission denied
./configure: line 823: Makefile: Permission denied
./configure: line 824: Makefile: Permission denied
./configure: line 825: Makefile: Permission denied
./configure: line 826: Makefile: Permission denied
./configure: line 827: Makefile: Permission denied
./configure: line 828: Makefile: Permission denied
./configure: line 829: Makefile: Permission denied
./configure: line 830: Makefile: Permission denied
./configure: line 831: Makefile: Permission denied
./configure: line 832: Makefile: Permission denied
./configure: line 833: Makefile: Permission denied
./configure: line 834: Makefile: Permission denied
./configure: line 835: Makefile: Permission denied
./configure: line 836: Makefile: Permission denied
./configure: line 837: Makefile: Permission denied
./configure: line 838: Makefile: Permission denied
./configure: line 839: Makefile: Permission denied
./configure: line 840: Makefile: Permission denied
./configure: line 841: Makefile: Permission denied
./configure: line 842: Makefile: Permission denied
./configure: line 843: Makefile: Permission denied
./configure: line 844: Makefile: Permission denied
./configure: line 845: Makefile: Permission denied
./configure: line 846: Makefile: Permission denied
./configure: line 847: Makefile: Permission denied
./configure: line 848: Makefile: Permission denied
./configure: line 849: Makefile: Permission denied
./configure: line 850: Makefile: Permission denied
./configure: line 851: Makefile: Permission denied
./configure: line 852: Makefile: Permission denied
./configure: line 853: Makefile: Permission denied
./configure: line 854: Makefile: Permission denied
./configure: line 855: Makefile: Permission denied
./configure: line 856: Makefile: Permission denied
./configure: line 857: Makefile: Permission denied
./configure: line 858: Makefile: Permission denied
./configure: line 859: Makefile: Permission denied
./configure: line 861: Makefile: Permission denied
FLINT was successfully configured.
./configure: line 864: config.log: Permission denied

Do you by chance know why these happen? If I enter the flint2 folder and configure and compile by hand it works fine.

albinahlback commented 1 year ago

The latter problem should be because it is creating dummies and configuration files, and you do not appear to have permission to edit files there.

The former should be expected (I believe). It is my understanding that even standard libraries such as GMP and MPFR cannot find things there unless some of your environment variables to point there.

bobot commented 1 year ago

So now the version of the library use pkg-config, which allow it to be compiled in particular on macosx. @mseri can you test the new master version? It should land in opam soon, https://github.com/ocaml/opam-repository/pull/23244

mseri commented 1 year ago

It still fails to me, but now it is because the ocaml code is not using pkg-config:

mkdir -p "/Users/mseri/code/ocaml/ocaml-flint/_build/.sandbox/5b075a58457560a58a9094a9156149aa/default/flint/flint/flint2/../prefix/include/flint"
cp  aprcl.h  ulong_extras.h  long_extras.h  perm.h  fmpz.h  fmpz_vec.h  fmpz_poly.h  fmpq_poly.h  fmpz_mat.h  fmpz_lll.h  mpfr_vec.h  mpfr_mat.h  mpf_vec.h  mpf_mat.h  nmod.h  nmod_vec.h  nmod_poly.h  nmod_poly_factor.h  arith.h  mpn_extras.h  nmod_mat.h  fmpq.h  fmpq_vec.h  fmpq_mat.h  padic.h  fmpz_poly_q.h  fmpz_poly_mat.h  nmod_poly_mat.h  fmpz_mod_poly.h  fmpz_mod_poly_factor.h  fmpz_factor.h  fmpz_poly_factor.h  fft.h  qsieve.h  double_extras.h  d_vec.h  d_mat.h  padic_poly.h  padic_mat.h  qadic.h  fq.h  fq_vec.h  fq_mat.h  fq_poly.h  fq_poly_factor.h  fq_embed.h  fq_nmod.h  fq_nmod_vec.h  fq_nmod_mat.h  fq_nmod_poly.h  fq_nmod_poly_factor.h  fq_nmod_embed.h  fq_zech.h  fq_zech_vec.h  fq_zech_mat.h  fq_zech_poly.h  fq_zech_poly_factor.h  fq_zech_embed.h  fmpz_mod_mat.h  mpoly.h  fmpz_mpoly.h  fmpq_mpoly.h  nmod_mpoly.h  fq_nmod_mpoly.h  thread_pool.h  thread_support.h  fmpz_mod.h  fmpz_mod_vec.h  fmpz_mod_mpoly.h  fmpz_mod_mpoly_factor.h  n_poly.h  fmpz_mpoly_factor.h  fmpq_mpoly_factor.h  nmod_mpoly_factor.h  fq_nmod_mpoly_factor.h  fq_zech_mpoly.h  fq_zech_mpoly_factor.h  fq_default.h  fq_default_poly.h  fq_default_poly_factor.h  fq_default_mat.h NTL-interface.h flint.h longlong.h flint-config.h gmpcompat.h fft_tuning.h fmpz-conversions.h profiler.h templates.h exception.h hashmap.h  fq_vec_templates.h  fq_mat_templates.h  fq_poly_templates.h  fq_poly_factor_templates.h  fq_embed_templates.h  fq_templates.h "/Users/mseri/code/ocaml/ocaml-flint/_build/.sandbox/5b075a58457560a58a9094a9156149aa/default/flint/flint/flint2/../prefix/include/flint"
mkdir -p "/Users/mseri/code/ocaml/ocaml-flint/_build/.sandbox/5b075a58457560a58a9094a9156149aa/default/flint/flint/flint2/../prefix/include/flint/flintxx"
cp flintxx/*.h "/Users/mseri/code/ocaml/ocaml-flint/_build/.sandbox/5b075a58457560a58a9094a9156149aa/default/flint/flint/flint2/../prefix/include/flint/flintxx"
cp *xx.h "/Users/mseri/code/ocaml/ocaml-flint/_build/.sandbox/5b075a58457560a58a9094a9156149aa/default/flint/flint/flint2/../prefix/include/flint"
File "flint/dune", line 8, characters 9-26:
8 |   (names ocaml_flint_utils)
             ^^^^^^^^^^^^^^^^^
(cd _build/default/flint && /usr/bin/cc -O2 -fno-strict-aliasing -fwrapv -pthread -D_FILE_OFFSET_BITS=64 -fdiagnostics-color=always -g -I /Users/mseri/.opam/5.0.0/lib/ocaml -I /Users/mseri/.opam/5.0.0/lib/bigarray-compat -I /Users/mseri/.opam/5.0.0/lib/bytes -I /Users/mseri/.opam/5.0.0/lib/ctypes -I /Users/mseri/.opam/5.0.0/lib/integers -I /Users/mseri/.opam/5.0.0/lib/ocaml/str -I /Users/mseri/.opam/5.0.0/lib/ocaml/threads -I /Users/mseri/.opam/5.0.0/lib/ocaml/unix -I /Users/mseri/.opam/5.0.0/lib/stdlib-shims -I /Users/mseri/.opam/5.0.0/lib/zarith -I flint -I /Users/mseri/.opam/5.0.0/lib/zarith -o ocaml_flint_utils.o -c ocaml_flint_utils.c)
In file included from ocaml_flint_utils.c:1:
./flint/fmpz.h:27:10: fatal error: 'gmp.h' file not found
#include <gmp.h>
         ^~~~~~~
1 error generated.
File "flint/_unknown_", line 1, characters 0-0:
(cd _build/default && /usr/bin/cc -O2 -fno-strict-aliasing -fwrapv -pthread -D_FILE_OFFSET_BITS=64 -fdiagnostics-color=always -g -I/Users/mseri/code/ocaml/ocaml-flint/_build/default/flint/flint -I/Users/mseri/code/ocaml/ocaml-flint/_build/default/flint/. -Werror=implicit-function-declaration -O2 -fno-strict-aliasing -fwrapv -pthread -D_FILE_OFFSET_BITS=64 -fdiagnostics-color=always -g -I /Users/mseri/.opam/5.0.0/lib/ocaml -I /Users/mseri/.opam/5.0.0/lib/ctypes /Users/mseri/code/ocaml/ocaml-flint/_build/default/flint/flint__c_cout_generated_types.c -o /Users/mseri/code/ocaml/ocaml-flint/_build/default/flint/flint__c_cout_generated_types.exe)
In file included from /Users/mseri/code/ocaml/ocaml-flint/_build/default/flint/flint__c_cout_generated_types.c:1:
/Users/mseri/code/ocaml/ocaml-flint/_build/default/flint/./flint/fmpz.h:27:10: fatal error: 'gmp.h' file not found
#include <gmp.h>
         ^~~~~~~
1 error generated.

I would suggest to do that via dune-configurator, akin to what I did here: https://github.com/thvnx/mlmpfr/pull/27 (you just need a small modification of that code, I can send a PR next week if you want)

bobot commented 1 year ago

My bad, I pushed the tag 0.2.1 with dune-release but forgot to push the branch. main is updated now. In particular, https://github.com/bobot/ocaml-flint/commit/221ac6e6eceead2ebf6e5f2968dc570d6ee19cde

mseri commented 1 year ago

There is only one error left now:

Running[1]: (cd _build/default/flint && /usr/bin/cc -O2 -fno-strict-aliasing -fwrapv -pthread -D_FILE_OFFSET_BITS=64 -fdiagnostics-color=always -g -I /Users/mseri/.opam/5.0.0/lib/ocaml -I /Users/mseri/.opam/5.0.0/lib/bigarray-compat -I /Users/mseri/.opam/5.0.0/lib/bytes -I /Users/mseri/.opam/5.0.0/lib/ctypes -I /Users/mseri/.opam/5.0.0/lib/integers -I /Users/mseri/.opam/5.0.0/lib/ocaml/str -I /Users/mseri/.opam/5.0.0/lib/ocaml/threads -I /Users/mseri/.opam/5.0.0/lib/ocaml/unix -I /Users/mseri/.opam/5.0.0/lib/stdlib-shims -I /Users/mseri/.opam/5.0.0/lib/zarith -I flint -I /Users/mseri/.opam/5.0.0/lib/zarith -o ocaml_flint_utils.o -c ocaml_flint_utils.c)
File "flint/dune", line 8, characters 9-26:
8 |   (names ocaml_flint_utils)
             ^^^^^^^^^^^^^^^^^
Command [1] exited with code 1:
$ (cd _build/default/flint && /usr/bin/cc -O2 -fno-strict-aliasing -fwrapv -pthread -D_FILE_OFFSET_BITS=64 -fdiagnostics-color=always -g -I /Users/mseri/.opam/5.0.0/lib/ocaml -I /Users/mseri/.opam/5.0.0/lib/bigarray-compat -I /Users/mseri/.opam/5.0.0/lib/bytes -I /Users/mseri/.opam/5.0.0/lib/ctypes -I /Users/mseri/.opam/5.0.0/lib/integers -I /Users/mseri/.opam/5.0.0/lib/ocaml/str -I /Users/mseri/.opam/5.0.0/lib/ocaml/threads -I /Users/mseri/.opam/5.0.0/lib/ocaml/unix -I /Users/mseri/.opam/5.0.0/lib/stdlib-shims -I /Users/mseri/.opam/5.0.0/lib/zarith -I flint -I /Users/mseri/.opam/5.0.0/lib/zarith -o ocaml_flint_utils.o -c ocaml_flint_utils.c)
In file included from ocaml_flint_utils.c:1:
./flint/fmpz.h:27:10: fatal error: 'gmp.h' file not found
#include <gmp.h>
         ^~~~~~~
1 error generated.

I think the flags need to be added also to https://github.com/bobot/ocaml-flint/blob/4c3e97cf77adcb0183057735ed277f3ac1b0461f/flint/dune#L6

I tried

diff --git a/flint/dune b/flint/dune
index 06a7e04..969ca24 100644
--- a/flint/dune
+++ b/flint/dune
@@ -6,6 +6,9 @@
  (foreign_stubs
   (language c)
   (names ocaml_flint_utils)
+  (flags
+    (:include ../configure/c_flags.sexp)
+    :standard)
   (include_dirs
    flint
    (lib zarith)))

on the fly but it then fails at the final linking stage

Running[5]: (cd _build/default && /Users/mseri/.opam/5.0.0/bin/ocamlopt.opt -w -9-27 -g -shared -linkall -I flint -I flint/flint -o flint/flint.cmxs flint/flint.cmxa)
File "flint/dune", line 1, characters 0-993:
 1 | (library
 2 |  (name flint)
 3 |  (public_name flint)
....
37 |    (functor Function_description))
38 |   (generated_types Types_generated)
39 |   (generated_entry_point C)))
Command [5] exited with code 2:
$ (cd _build/default && /Users/mseri/.opam/5.0.0/bin/ocamlopt.opt -w -9-27 -g -shared -linkall -I flint -I flint/flint -o flint/flint.cmxs flint/flint.cmxa)
ld: library not found for -lgmp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
File "caml_startup", line 1:
Error: Error during linking (exit code 1)
mseri commented 1 year ago

@bobot I think I figured it out. With the following the libraries work fine and passes the tests:

diff --git a/arb/dune b/arb/dune
index 68925d4..ba0769d 100644
--- a/arb/dune
+++ b/arb/dune
@@ -5,7 +5,10 @@
  (foreign_archives arb)
  (foreign_stubs
   (language c)
-  (names ocaml_acb_utils))
+  (names ocaml_acb_utils)
+  (flags
+    (:include ../configure/c_flags.sexp)
+    :standard))
  (libraries zarith flint threads)
  (flags -w -9-27)
  (ctypes
@@ -18,7 +21,9 @@
      "-Werror=implicit-function-declaration"
      (:include ../configure/c_flags.sexp)
      :standard)
-    (c_library_flags "-lgmp -lmpfr")))
+    (c_library_flags
+     (:include ../configure/libs.sexp)
+     "-lgmp -lmpfr")))
   (deps arb.h acb.h)
   (headers
    (include "acb.h" "string.h"))
diff --git a/calcium/dune b/calcium/dune
index cc84cbb..a2942e9 100644
--- a/calcium/dune
+++ b/calcium/dune
@@ -6,6 +6,9 @@
  (foreign_stubs
   (language c)
   (names ocaml_calcium_utils)
+  (flags
+    (:include ../configure/c_flags.sexp)
+    :standard)
   (include_dirs
    lib
    (lib flint)
@@ -25,7 +28,9 @@
      "-Werror=implicit-function-declaration"
      (:include ../configure/c_flags.sexp)
      :standard)
-    (c_library_flags "-lgmp -lmpfr")))
+    (c_library_flags
+     (:include ../configure/libs.sexp)
+     "-lgmp -lmpfr")))
   (deps lib/ca.h)
   (headers
    (include "lib/ca.h" "string.h"))
diff --git a/configure/discover.ml b/configure/discover.ml
index ef7b777..4ca46c2 100644
--- a/configure/discover.ml
+++ b/configure/discover.ml
@@ -2,19 +2,21 @@ module C = Configurator.V1

 let () =
   C.main ~name:"discover" (fun c ->
-      let conf =
+      let cflags, libs =
         match C.Pkg_config.get c with
-        | None -> []
+        | None -> [], []
         | Some pc ->
-            let cflags ~package =
+            let flags ~package =
               match C.Pkg_config.query pc ~package with
-              | None -> []
-              | Some info -> info.cflags
+              | None -> [], []
+              | Some info -> info.cflags, info.libs
             in
-            let gmp = cflags ~package:"gmp" in
-            let mpfr = cflags ~package:"mpfr" in
+            let gmp = flags ~package:"gmp" in
+            let mpfr = flags ~package:"mpfr" in

-            gmp @ mpfr
+            (fst gmp) @ (fst mpfr),
+            (snd gmp) @ (snd mpfr)
       in

-      C.Flags.write_sexp "c_flags.sexp" conf)
+      C.Flags.write_sexp "c_flags.sexp" cflags;
+      C.Flags.write_sexp "libs.sexp" libs)
diff --git a/configure/dune b/configure/dune
index 617bc69..5bbdeb2 100644
--- a/configure/dune
+++ b/configure/dune
@@ -3,5 +3,5 @@
  (libraries dune-configurator))

 (rule
- (targets c_flags.sexp)
+ (targets c_flags.sexp libs.sexp)
  (action (run ./discover.exe)))
diff --git a/flint/dune b/flint/dune
index 06a7e04..7f83395 100644
--- a/flint/dune
+++ b/flint/dune
@@ -6,6 +6,9 @@
  (foreign_stubs
   (language c)
   (names ocaml_flint_utils)
+  (flags
+    (:include ../configure/c_flags.sexp)
+    :standard)
   (include_dirs
    flint
    (lib zarith)))
@@ -21,7 +24,9 @@
      "-Werror=implicit-function-declaration"
      (:include ../configure/c_flags.sexp)
      :standard)
-    (c_library_flags "-lgmp -lmpfr")))
+    (c_library_flags
+      (:include ../configure/libs.sexp)
+      "-lgmp -lmpfr")))
   (deps flint/flint.h flint/fmpz.h)
   (headers
    (include "flint/fmpz.h" "flint/fmpq.h" "string.h"))