Subject: endian.mk proposal
To: None <tech-pkg@netbsd.org>
From: Johnny Lam <jlam@jgrind.org>
List: tech-pkg
Date: 04/12/2002 14:31:00
--BOKacYhQ+x31HxR3
Content-Type: text/plain; charset=us-ascii

I'd like to add the following file as /usr/pkgsrc/mk/endian.mk to assist
in the creation of some packages that need to determine the endianness of
the CPU.  It's taken from emulators/xmame/Makefile, and I believe it does
the right thing across several versions of NetBSD, Linux, and Solaris.
I'm not sure about Darwin, yet.  Does anyone have comments on the
implementation, or whether this is even worthwhile?

	Thanks,

	-- Johnny Lam <jlam@jgrind.org>

--BOKacYhQ+x31HxR3
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="endian.mk"

# $NetBSD$
#
# Determine the endianness of the CPU by checking header files.
# Yes, we cheat and assume that there are only "big" and "little"
# endian, disregarding the other ones.
#
# This file is used as follows:
#
# .include "../../mk/endian.mk"
#
# .if ${MACHINE_ENDIAN} == "big"
# # ...big endian stuff...
# .else
# # ...little endian stuff...
# .endif

.include "../../mk/bsd.prefs.mk"

.if !defined(MACHINE_ENDIAN)
_ENDIAN_H_FILES=	/usr/include/sys/endian.h
_ENDIAN_H_FILES+=	/usr/include/machine/endian.h
_ENDIAN_H_FILES+=	/usr/include/endian.h
_ENDIAN_H_FILES+=	/usr/include/sys/byteorder.h
_ENDIAN_H_FILES+=	/dev/null
.  for FILE in ${_ENDIAN_H_FILES}
.    if exists(${FILE})
_ENDIAN_H?=		${FILE:S/\/usr\/include\///}
.    endif
.  endfor
MACHINE_ENDIAN!=							\
	if (								\
		${ECHO} "\#include <${_ENDIAN_H}>";			\
		${ECHO} "\#ifndef BYTE_ORDER";				\
		${ECHO} "\#ifdef _BIG_ENDIAN";				\
		${ECHO} "\#define BYTE_ORDER 4321";			\
		${ECHO} "\#else";					\
		${ECHO} "\#define BYTE_ORDER 1234";			\
		${ECHO} "\#endif";					\
		${ECHO} "\#endif";					\
		${ECHO} "BYTE_ORDER";					\
	   ) | ${CC} -E - | ${GREP} "4321" >/dev/null 2>&1;		\
	then								\
		${ECHO} big;						\
	else								\
		${ECHO} little;						\
	fi
MAKEFLAGS+=	MACHINE_ENDIAN="${MACHINE_ENDIAN}"
.endif

--BOKacYhQ+x31HxR3--