Add a configure script and rely on local definitions of derived constants
authorDuncan Coutts <duncan@well-typed.com>
Sat, 13 Jun 2009 14:46:10 +0000 (14:46 +0000)
committerDuncan Coutts <duncan@well-typed.com>
Sat, 13 Jun 2009 14:46:10 +0000 (14:46 +0000)
cbits/gmp-wrappers.cmm
cbits/mkGmpDerivedConstants.c [new file with mode: 0644]
configure.ac [new file with mode: 0644]
integer.buildinfo.in [new file with mode: 0644]
integer.cabal

index fd1e9c4..17f7aba 100644 (file)
@@ -26,7 +26,7 @@
  * ---------------------------------------------------------------------------*/
 
 #include "Cmm.h"
-#include "DerivedConstants.h"
+#include "GmpDerivedConstants.h"
 
 #ifdef __PIC__
 #ifndef mingw32_HOST_OS
diff --git a/cbits/mkGmpDerivedConstants.c b/cbits/mkGmpDerivedConstants.c
new file mode 100644 (file)
index 0000000..f3d551d
--- /dev/null
@@ -0,0 +1,81 @@
+/* --------------------------------------------------------------------------
+ *
+ * (c) The GHC Team, 1992-2004
+ *
+ * mkDerivedConstants.c
+ *
+ * Basically this is a C program that extracts information from the C
+ * declarations in the header files (primarily struct field offsets)
+ * and generates a header file that can be #included into non-C source
+ * containing this information.
+ *
+ * ------------------------------------------------------------------------*/
+
+#include <stdio.h>
+#include "gmp.h"
+
+
+#define str(a,b) #a "_" #b
+
+#define OFFSET(s_type, field) ((size_t)&(((s_type*)0)->field))
+
+#ifdef mingw32_HOST_OS
+#define SIZET_FMT "d"
+#else
+#define SIZET_FMT "zd"
+#endif
+
+
+/* struct_size(TYPE)
+ *
+ */
+#define def_size(str, size) \
+    printf("#define SIZEOF_" str " %lu\n", (unsigned long)size);
+
+#define struct_size(s_type) \
+    def_size(#s_type, sizeof(s_type));
+
+
+
+/* struct_field(TYPE, FIELD)
+ *
+ */
+#define def_offset(str, offset) \
+    printf("#define OFFSET_" str " %" SIZET_FMT "\n", offset);
+
+#define field_offset_(str, s_type, field) \
+    def_offset(str, OFFSET(s_type,field));
+
+#define field_offset(s_type, field) \
+    field_offset_(str(s_type,field),s_type,field);
+
+#define field_type_(str, s_type, field) \
+    printf("#define REP_" str " b"); \
+    printf("%lu\n", (unsigned long)sizeof (__typeof__(((((s_type*)0)->field)))) * 8);
+
+#define field_type(s_type, field) \
+    field_type_(str(s_type,field),s_type,field);
+
+/* An access macro for use in C-- sources. */
+#define struct_field_macro(str) \
+    printf("#define " str "(__ptr__)  REP_" str "[__ptr__+OFFSET_" str "]\n");
+
+/* Outputs the byte offset and MachRep for a field */
+#define struct_field(s_type, field)            \
+    field_offset(s_type, field);               \
+    field_type(s_type, field);                 \
+    struct_field_macro(str(s_type,field))
+
+
+int
+main(int argc, char *argv[])
+{
+    printf("/* This file is created automatically.  Do not edit by hand.*/\n\n");
+
+    struct_size(MP_INT);
+    struct_field(MP_INT,_mp_alloc);
+    struct_field(MP_INT,_mp_size);
+    struct_field(MP_INT,_mp_d);
+
+    return 0;
+}
diff --git a/configure.ac b/configure.ac
new file mode 100644 (file)
index 0000000..1feae2c
--- /dev/null
@@ -0,0 +1,88 @@
+AC_INIT([Haskell integer (GMP)], [0.1], [libraries@haskell.org], [integer])
+
+# Safety check: Ensure that we are in the correct source directory.
+AC_CONFIG_SRCDIR([cbits/mkGmpDerivedConstants.c])
+
+AC_CANONICAL_TARGET
+
+AC_ARG_WITH([cc],
+            [C compiler],
+            [CC=$withval])
+AC_PROG_CC()
+
+
+dnl--------------------------------------------------------------------
+dnl * Deal with arguments telling us gmp is somewhere odd
+dnl--------------------------------------------------------------------
+
+AC_ARG_WITH([gmp-includes],
+  [AC_HELP_STRING([--with-gmp-includes],
+    [directory containing gmp.h])],
+    [GMP_INCLUDE_DIRS=$withval; CPPFLAGS="-I$withval"],
+    [GMP_INCLUDE_DIRS=])
+
+AC_ARG_WITH([gmp-libraries],
+  [AC_HELP_STRING([--with-gmp-libraries],
+    [directory containing gmp library])],
+    [GMP_LIB_DIRS=$withval; LDFLAGS="-L$withval"],
+    [GMP_LIB_DIRS=])
+
+
+dnl--------------------------------------------------------------------
+dnl * Check whether this machine has gmp/gmp3 installed
+dnl--------------------------------------------------------------------
+
+AC_CHECK_LIB([gmp],  [__gmpz_fdiv_qr],
+             [HAVE_GMP=YES; GMP_LIBS=gmp],
+             [HAVE_GMP=NO;  GMP_LIBS=])
+if test "$HAVE_GMP" = "NO"; then
+AC_CHECK_LIB([gmp3], [__gmpz_fdiv_qr],
+             [HAVE_GMP=YES; GMP_LIBS=gmp3],
+             [HAVE_GMP=NO;  GMP_LIBS=])
+fi
+
+dnl--------------------------------------------------------------------
+dnl * Mac OS X only: check for GMP.framework
+dnl--------------------------------------------------------------------
+
+case $target_os in
+  darwin*)
+    AC_MSG_CHECKING([for GMP.framework])
+    save_libs="$LIBS"
+    LIBS="-framework GMP"
+    AC_TRY_LINK_FUNC(__gmpz_fdiv_qr,
+      [HAVE_GMP_FRAMEWORK=yes; GMP_FRAMEWORK=GMP; GMP_LIBS=; HAVE_GMP=YES],
+      [HAVE_GMP_FRAMEWORK=no])
+    LIBS="$save_libs"
+    AC_MSG_RESULT([$HAVE_GMP_FRAMEWORK])
+    ;;
+esac
+
+dnl--------------------------------------------------------------------
+dnl * Make sure we got some form of gmp
+dnl--------------------------------------------------------------------
+
+if test "$HAVE_GMP" = "NO"; then
+  AC_MSG_ERROR([cannot find the gmp library on the system.]
+    [If you have gmp installed in a non-standard location re-run ./configure]
+    [and specify the flags --with-gmp-includes= and/or --with-gmp-libraries=])
+fi
+
+
+AC_SUBST(GMP_INCLUDE_DIRS)
+AC_SUBST(GMP_LIBS)
+AC_SUBST(GMP_LIB_DIRS)
+AC_SUBST(GMP_FRAMEWORK)
+
+AC_CONFIG_FILES([integer.buildinfo])
+
+dnl--------------------------------------------------------------------
+dnl * Generate the header cbits/GmpDerivedConstants.h
+dnl--------------------------------------------------------------------
+
+AC_MSG_NOTICE([generating GmpDerivedConstants.h])
+${CC} cbits/mkGmpDerivedConstants.c -o cbits/mkGmpDerivedConstants
+cbits/mkGmpDerivedConstants > cbits/GmpDerivedConstants.h
+rm cbits/mkGmpDerivedConstants
+
+AC_OUTPUT
diff --git a/integer.buildinfo.in b/integer.buildinfo.in
new file mode 100644 (file)
index 0000000..2f5130d
--- /dev/null
@@ -0,0 +1,4 @@
+include-dirs: @GMP_INCLUDE_DIRS@
+extra-lib-dirs: @GMP_LIB_DIRS@
+extra-libraries: @GMP_LIBS@
+frameworks: @GMP_FRAMEWORK@
index ca323a4..71d569a 100644 (file)
@@ -8,7 +8,7 @@ synopsis:       Integer library based on GMP
 description:
     This package contains an Integer library based on GMP.
 cabal-version:  >=1.6
-build-type: Simple
+build-type: Configure
 
 extra-source-files:
   cbits/float.c