caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Stefan Hellermann <stefan@the2masters.de>
To: caml-list@inria.fr
Subject: [Caml-list] [PATCH 2/2] configure: Check endianess by using the preprocessor
Date: Fri,  9 Oct 2015 14:03:14 +0200	[thread overview]
Message-ID: <1444392194-18276-3-git-send-email-stefan@the2masters.de> (raw)
In-Reply-To: <1444392194-18276-1-git-send-email-stefan@the2masters.de>

Signed-off-by: Stefan Hellermann <stefan@the2masters.de>
---
 config/auto-aux/endian.c | 60 +++++++++++++++++++-----------------------------
 configure                | 26 ++++++++++++---------
 2 files changed, 38 insertions(+), 48 deletions(-)

diff --git a/config/auto-aux/endian.c b/config/auto-aux/endian.c
index 5dc623a..eee48c8 100644
--- a/config/auto-aux/endian.c
+++ b/config/auto-aux/endian.c
@@ -1,40 +1,26 @@
-/***********************************************************************/
-/*                                                                     */
-/*                                OCaml                                */
-/*                                                                     */
-/*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         */
-/*                                                                     */
-/*  Copyright 1996 Institut National de Recherche en Informatique et   */
-/*  en Automatique.  All rights reserved.  This file is distributed    */
-/*  under the terms of the GNU Library General Public License, with    */
-/*  the special exception on linking described in file ../../LICENSE.  */
-/*                                                                     */
-/***********************************************************************/
-
-#include <string.h>
-#include "m.h"
+// Try to find the right includes
+#if defined(__linux__) || defined(__CYGWIN__)
+#  define _BSD_SOURCE
+#  include <endian.h>
+#elif defined(__APPLE__)
+#  include <libkern/OSByteOrder.h>
+#elif defined(BSD)
+#   include <sys/endian.h>
+#elif defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
+#  include <winsock2.h>
+#  include <sys/param.h>
+#endif
 
-#ifndef ARCH_SIXTYFOUR
-long intval = 0x41424344L;
-char * bigendian = "ABCD";
-char * littleendian = "DCBA";
+#if defined(__BIG_ENDIAN__) || \
+  defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
+  defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
+  defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN
+0
+#elif defined(__LITTLE_ENDIAN__) || \
+  defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
+  defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
+  defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
+1
 #else
-long intval = 0x4142434445464748L;
-char * bigendian = "ABCDEFGH";
-char * littleendian = "HGFEDCBA";
+#  error Cannot detect endianess of target
 #endif
-
-int main(void)
-{
-  long n[2];
-  char * p;
-
-  n[0] = intval;
-  n[1] = 0;
-  p = (char *) n;
-  if (strcmp(p, bigendian) == 0)
-    return 0;
-  if (strcmp(p, littleendian) == 0)
-    return 1;
-  return 2;
-}
diff --git a/configure b/configure
index e3ea7eb..b7c3348 100755
--- a/configure
+++ b/configure
@@ -552,22 +552,26 @@ echo "#define SIZEOF_LONGLONG $5" >> m.h
 
 # Determine endianness
 
-sh ./runtest endian.c
-case $? in
-  0) inf "This is a big-endian architecture."
-     echo "#define ARCH_BIG_ENDIAN" >> m.h;;
-  1) inf "This is a little-endian architecture."
-     echo "#undef ARCH_BIG_ENDIAN" >> m.h;;
-  2) err "This architecture seems to be neither big endian nor little" \
-         "endian.\n OCaml won't run on this architecture.";;
-  *) case $target in
+ret=`sh ./preprocesstest endian.c`
+if test "$?" -eq 0; then
+  set $ret
+  case "$1" in
+    0) inf "This is a big-endian architecture."
+       echo "#define ARCH_BIG_ENDIAN" >> m.h;;
+    1) inf "This is a little-endian architecture."
+       echo "#undef ARCH_BIG_ENDIAN" >> m.h;;
+    *) err "This architecture seems to be neither big endian nor little" \
+           "endian.\n OCaml won't run on this architecture.";;
+  esac
+else
+  case $target in
        *-*-mingw*) inf "This is a little-endian architecture."
                    echo "#undef ARCH_BIG_ENDIAN" >> m.h;;
        *) wrn "Something went wrong during endianness determination.\n" \
               "You will have to figure out endianness yourself\n" \
               "(option ARCH_BIG_ENDIAN in m.h).";;
-     esac;;
-esac
+     esac
+fi
 
 # Determine alignment constraints
 
-- 
2.1.4


  parent reply	other threads:[~2015-10-09 12:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-09 12:03 [Caml-list] [PATCH 0/2] use preprocessor to get infos about target arch Stefan Hellermann
2015-10-09 12:03 ` [Caml-list] [PATCH 1/2] configure: Check sizes of integers and pointers by using the preprocessor Stefan Hellermann
2015-10-09 12:03 ` Stefan Hellermann [this message]
2015-10-09 12:24 ` [Caml-list] [PATCH 0/2] use preprocessor to get infos about target arch Daniel Bünzli
2015-10-09 12:31   ` Stefan Hellermann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1444392194-18276-3-git-send-email-stefan@the2masters.de \
    --to=stefan@the2masters.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).