architecture independent header files

Do endian detection in the header file instead of cmake.
This commit is contained in:
Andrew Kelley 2015-09-24 09:25:51 -07:00
parent 6a75dda407
commit 0a0715eb72
4 changed files with 99 additions and 12 deletions

View file

@ -146,6 +146,7 @@ set(LIBSOUNDIO_SOURCES
set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h") set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")
set(LIBSOUNDIO_HEADERS set(LIBSOUNDIO_HEADERS
"${CMAKE_SOURCE_DIR}/soundio/soundio.h" "${CMAKE_SOURCE_DIR}/soundio/soundio.h"
"${CMAKE_SOURCE_DIR}/soundio/endian.h"
${CONFIGURE_OUT_FILE} ${CONFIGURE_OUT_FILE}
) )
@ -207,15 +208,6 @@ set(EXAMPLE_CFLAGS "-std=c99 -Wall")
set(TEST_CFLAGS "${LIB_CFLAGS} -fprofile-arcs -ftest-coverage") set(TEST_CFLAGS "${LIB_CFLAGS} -fprofile-arcs -ftest-coverage")
set(TEST_LDFLAGS "-fprofile-arcs -ftest-coverage") set(TEST_LDFLAGS "-fprofile-arcs -ftest-coverage")
include(TestBigEndian)
test_big_endian(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
set(SOUNDIO_OS_BIG_ENDIAN true)
set(SOUNDIO_OS_LITTLE_ENDIAN false)
else()
set(SOUNDIO_OS_BIG_ENDIAN false)
set(SOUNDIO_OS_LITTLE_ENDIAN true)
endif()
configure_file( configure_file(
"${CMAKE_SOURCE_DIR}/src/config.h.in" "${CMAKE_SOURCE_DIR}/src/config.h.in"
${CONFIGURE_OUT_FILE} ${CONFIGURE_OUT_FILE}

97
soundio/endian.h Normal file
View file

@ -0,0 +1,97 @@
/*
* Copyright (c) 2015 Andrew Kelley
*
* This file is part of libsoundio, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#ifndef SOUNDIO_ENDIAN_H
#define SOUNDIO_ENDIAN_H
#if defined(__BIG_ENDIAN__)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__ARMEB__)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__THUMBEB__)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__AARCH64EB__)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(_MIPSEB)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__MIPSEB)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__MIPSEB__)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(_BIG_ENDIAN)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__sparc)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__sparc__)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(_POWER)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__powerpc__)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__ppc__)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__hpux)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__hppa)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(_POWER)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__s390__)
#define SOUNDIO_OS_BIG_ENDIAN
#elif defined(__LITTLE_ENDIAN__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__ARMEL__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__THUMBEL__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__AARCH64EL__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(_MIPSEL)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__MIPSEL)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__MIPSEL__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(_LITTLE_ENDIAN)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__i386__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__alpha__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__ia64)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__ia64__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(_M_IX86)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(_M_IA64)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(_M_ALPHA)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__amd64)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__amd64__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(_M_AMD64)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__x86_64)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__x86_64__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(_M_X64)
#define SOUNDIO_OS_LITTLE_ENDIAN
#elif defined(__bfin__)
#define SOUNDIO_OS_LITTLE_ENDIAN
#else
#error unable to detect endianness
#endif
#endif

View file

@ -9,6 +9,7 @@
#define SOUNDIO_SOUNDIO_H #define SOUNDIO_SOUNDIO_H
#include "config.h" #include "config.h"
#include "endian.h"
#include <stdbool.h> #include <stdbool.h>
/// \cond /// \cond

View file

@ -13,9 +13,6 @@
#define SOUNDIO_VERSION_PATCH @LIBSOUNDIO_VERSION_PATCH@ #define SOUNDIO_VERSION_PATCH @LIBSOUNDIO_VERSION_PATCH@
#define SOUNDIO_VERSION_STRING "@LIBSOUNDIO_VERSION@" #define SOUNDIO_VERSION_STRING "@LIBSOUNDIO_VERSION@"
#cmakedefine SOUNDIO_OS_BIG_ENDIAN
#cmakedefine SOUNDIO_OS_LITTLE_ENDIAN
#cmakedefine SOUNDIO_HAVE_JACK #cmakedefine SOUNDIO_HAVE_JACK
#cmakedefine SOUNDIO_HAVE_PULSEAUDIO #cmakedefine SOUNDIO_HAVE_PULSEAUDIO
#cmakedefine SOUNDIO_HAVE_ALSA #cmakedefine SOUNDIO_HAVE_ALSA