[U-Boot] [PATCH v4] compiler.h: unify system ifdef cruft here

Mike Frysinger vapier at gentoo.org
Fri Jul 3 01:23:25 CEST 2009


Shove a lot of the HOSTCC and related #ifdef checking crap into the new
compiler.h header so that we can keep all other headers nice and clean.

Also introduce custom uswap functions so we don't have to rely on the non
standard implementations that a host may (or may not in the case of OS X)
provide.  This allows mkimage to finally build cleanly on an OS X system.

Signed-off-by: Mike Frysinger <vapier at gentoo.org>
---
this version has been tested with Blackfin targets on Linux, Darwin, and
now MinGW host systems.  needed a small fix in image.h to use the cpu
funcs rather than ntoh/hton.

 include/compiler.h    |  125 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/elf.h         |   10 +----
 include/environment.h |    6 +--
 include/image.h       |   13 ++---
 include/libfdt_env.h  |   53 ++-------------------
 include/u-boot/md5.h  |    2 +-
 lib_generic/md5.c     |    8 +--
 tools/bmp_logo.c      |   13 +-----
 tools/img2srec.c      |    3 +-
 tools/mingw_support.h |    3 -
 tools/mkimage.c       |   14 ++----
 tools/mkimage.h       |   33 -------------
 tools/os_support.c    |    1 +
 tools/os_support.h    |    2 +
 tools/ubsha1.c        |   13 -----
 15 files changed, 150 insertions(+), 149 deletions(-)
 create mode 100644 include/compiler.h

diff --git a/include/compiler.h b/include/compiler.h
new file mode 100644
index 0000000..272fd3c
--- /dev/null
+++ b/include/compiler.h
@@ -0,0 +1,125 @@
+/*
+ * Keep all the ugly #ifdef for system stuff here
+ */
+
+#ifndef __COMPILER_H__
+#define __COMPILER_H__
+
+#include <stddef.h>
+
+#ifdef USE_HOSTCC
+
+#if defined(__BEOS__)	 || \
+    defined(__NetBSD__)  || \
+    defined(__FreeBSD__) || \
+    defined(__sun__)	 || \
+    defined(__APPLE__)
+# include <inttypes.h>
+#elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__)
+# include <stdint.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+extern int errno;
+
+#if !defined(__WIN32__) && !defined(__MINGW32__)
+# include <sys/mman.h>
+#endif
+
+/* Not all systems (like Windows) has this define, and yes
+ * we do replace/emulate mmap() on those systems ...
+ */
+#ifndef MAP_FAILED
+# define MAP_FAILED ((void *)-1)
+#endif
+
+#include <fcntl.h>
+#ifndef O_BINARY		/* should be define'd on __WIN32__ */
+#define O_BINARY	0
+#endif
+
+#ifdef __linux__
+# include <endian.h>
+# include <byteswap.h>
+#elif defined(__MACH__)
+# include <machine/endian.h>
+typedef unsigned long ulong;
+typedef unsigned int  uint;
+#endif
+
+typedef uint8_t __u8;
+typedef uint16_t __u16;
+typedef uint32_t __u32;
+
+#define uswap_16(x) \
+	((((x) & 0xff00) >> 8) | \
+	 (((x) & 0x00ff) << 8))
+#define uswap_32(x) \
+	((((x) & 0xff000000) >> 24) | \
+	 (((x) & 0x00ff0000) >>  8) | \
+	 (((x) & 0x0000ff00) <<  8) | \
+	 (((x) & 0x000000ff) << 24))
+#define _uswap_64(x, sfx) \
+	((((x) & 0xff00000000000000##sfx) >> 56) | \
+	 (((x) & 0x00ff000000000000##sfx) >> 40) | \
+	 (((x) & 0x0000ff0000000000##sfx) >> 24) | \
+	 (((x) & 0x000000ff00000000##sfx) >>  8) | \
+	 (((x) & 0x00000000ff000000##sfx) <<  8) | \
+	 (((x) & 0x0000000000ff0000##sfx) << 24) | \
+	 (((x) & 0x000000000000ff00##sfx) << 40) | \
+	 (((x) & 0x00000000000000ff##sfx) << 56))
+#if defined(__GNUC__)
+# define uswap_64(x) _uswap_64(x, ull)
+#else
+# define uswap_64(x) _uswap_64(x, )
+#endif
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+# define cpu_to_le16(x)		(x)
+# define cpu_to_le32(x)		(x)
+# define cpu_to_le64(x)		(x)
+# define le16_to_cpu(x)		(x)
+# define le32_to_cpu(x)		(x)
+# define le64_to_cpu(x)		(x)
+# define cpu_to_be16(x)		uswap_16(x)
+# define cpu_to_be32(x)		uswap_32(x)
+# define cpu_to_be64(x)		uswap_64(x)
+# define be16_to_cpu(x)		uswap_16(x)
+# define be32_to_cpu(x)		uswap_32(x)
+# define be64_to_cpu(x)		uswap_64(x)
+#else
+# define cpu_to_le16(x)		uswap_16(x)
+# define cpu_to_le32(x)		uswap_32(x)
+# define cpu_to_le64(x)		uswap_64(x)
+# define le16_to_cpu(x)		uswap_16(x)
+# define le32_to_cpu(x)		uswap_32(x)
+# define le64_to_cpu(x)		uswap_64(x)
+# define cpu_to_be16(x)		(x)
+# define cpu_to_be32(x)		(x)
+# define cpu_to_be64(x)		(x)
+# define be16_to_cpu(x)		(x)
+# define be32_to_cpu(x)		(x)
+# define be64_to_cpu(x)		(x)
+#endif
+
+#else /* !USE_HOSTCC */
+
+#include <linux/string.h>
+#include <linux/types.h>
+#include <asm/byteorder.h>
+
+/* Types for `void *' pointers. */
+#if __WORDSIZE == 64
+typedef unsigned long int       uintptr_t;
+#else
+typedef unsigned int            uintptr_t;
+#endif
+
+#endif
+
+#endif
diff --git a/include/elf.h b/include/elf.h
index f640388..29f276d 100644
--- a/include/elf.h
+++ b/include/elf.h
@@ -33,15 +33,7 @@
 #ifndef _ELF_H
 #define _ELF_H
 
-#if defined(__BEOS__)	 || \
-    defined(__NetBSD__)  || \
-    defined(__FreeBSD__) || \
-    defined(__sun__)	 || \
-    defined(__APPLE__)
-#include <inttypes.h>
-#elif (defined(__linux__) && defined(USE_HOSTCC)) || defined(__WIN32__)
-#include <stdint.h>
-#endif
+#include "compiler.h"
 
 /*
  *  This version doesn't work for 64-bit ABIs - Erik.
diff --git a/include/environment.h b/include/environment.h
index 507e832..5bed32f 100644
--- a/include/environment.h
+++ b/include/environment.h
@@ -96,11 +96,7 @@
 # endif
 #endif /* CONFIG_ENV_IS_IN_MG_DISK */
 
-#ifdef USE_HOSTCC
-# include <stdint.h>
-#else
-# include <linux/types.h>
-#endif
+#include "compiler.h"
 
 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
 # define ENV_HEADER_SIZE	(sizeof(uint32_t) + 1)
diff --git a/include/image.h b/include/image.h
index f183757..beb3a16 100644
--- a/include/image.h
+++ b/include/image.h
@@ -33,10 +33,9 @@
 #ifndef __IMAGE_H__
 #define __IMAGE_H__
 
-#if USE_HOSTCC
-#ifndef __MINGW32__
-#include <endian.h>
-#endif
+#include "compiler.h"
+
+#ifdef USE_HOSTCC
 
 /* new uImage format support enabled on host */
 #define CONFIG_FIT		1
@@ -46,9 +45,7 @@
 #else
 
 #include <lmb.h>
-#include <linux/string.h>
 #include <asm/u-boot.h>
-#include <asm/byteorder.h>
 
 #endif /* USE_HOSTCC */
 
@@ -284,8 +281,8 @@ typedef struct bootm_headers {
 #define CHUNKSZ_SHA1 (64 * 1024)
 #endif
 
-#define uimage_to_cpu(x)		ntohl(x)
-#define cpu_to_uimage(x)		htonl(x)
+#define uimage_to_cpu(x)		be32_to_cpu(x)
+#define cpu_to_uimage(x)		cpu_to_be32(x)
 
 const char *genimg_get_os_name (uint8_t os);
 const char *genimg_get_arch_name (uint8_t arch);
diff --git a/include/libfdt_env.h b/include/libfdt_env.h
index 1c67015..bf63583 100644
--- a/include/libfdt_env.h
+++ b/include/libfdt_env.h
@@ -21,56 +21,13 @@
 #ifndef _LIBFDT_ENV_H
 #define _LIBFDT_ENV_H
 
-#ifdef USE_HOSTCC
-#include <stdint.h>
-#include <string.h>
-#ifdef __MINGW32__
-#include <linux/types.h>
-#include <linux/byteorder/swab.h>
-#else
-#include <endian.h>
-#include <byteswap.h>
-#endif /* __MINGW32__ */
-#else
-#include <linux/string.h>
-#include <linux/types.h>
-#include <asm/byteorder.h>
-#endif /* USE_HOSTCC */
+#include "compiler.h"
 
-#include <stddef.h>
 extern struct fdt_header *working_fdt;  /* Pointer to the working fdt */
 
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-#ifdef __MINGW32__
-#define fdt32_to_cpu(x)		___swab32(x)
-#define cpu_to_fdt32(x)		___swab32(x)
-#define fdt64_to_cpu(x)		___swab64(x)
-#define cpu_to_fdt64(x)		___swab64(x)
-#else
-#define fdt32_to_cpu(x)		bswap_32(x)
-#define cpu_to_fdt32(x)		bswap_32(x)
-#define fdt64_to_cpu(x)		bswap_64(x)
-#define cpu_to_fdt64(x)		bswap_64(x)
-#endif
-#else
-#define fdt32_to_cpu(x)		(x)
-#define cpu_to_fdt32(x)		(x)
-#define fdt64_to_cpu(x)		(x)
-#define cpu_to_fdt64(x)		(x)
-#endif
-
-#ifndef USE_HOSTCC
-/*
- * Types for `void *' pointers.
- *
- * Note: libfdt uses this definition from /usr/include/stdint.h.
- * Define it here rather than pulling in all of stdint.h.
- */
-#if __WORDSIZE == 64
-typedef unsigned long int       uintptr_t;
-#else
-typedef unsigned int            uintptr_t;
-#endif
-#endif /* not USE_HOSTCC */
+#define fdt32_to_cpu(x)		be32_to_cpu(x)
+#define cpu_to_fdt32(x)		cpu_to_be32(x)
+#define fdt64_to_cpu(x)		be64_to_cpu(x)
+#define cpu_to_fdt64(x)		cpu_to_be64(x)
 
 #endif /* _LIBFDT_ENV_H */
diff --git a/include/u-boot/md5.h b/include/u-boot/md5.h
index 8b44a7f..08924cc 100644
--- a/include/u-boot/md5.h
+++ b/include/u-boot/md5.h
@@ -6,7 +6,7 @@
 #ifndef _MD5_H
 #define _MD5_H
 
-#include <linux/types.h>
+#include "compiler.h"
 
 struct MD5Context {
 	__u32 buf[4];
diff --git a/lib_generic/md5.c b/lib_generic/md5.c
index 9150510..81a09e3 100644
--- a/lib_generic/md5.c
+++ b/lib_generic/md5.c
@@ -25,14 +25,12 @@
    and to fit the cifs vfs by
    Steve French sfrench at us.ibm.com */
 
+#include "compiler.h"
+
 #ifndef USE_HOSTCC
 #include <common.h>
-#include <linux/string.h>
-#else
-#include <string.h>
-#endif /* USE_HOSTCC */
 #include <watchdog.h>
-#include <linux/types.h>
+#endif /* USE_HOSTCC */
 #include <u-boot/md5.h>
 
 static void
diff --git a/tools/bmp_logo.c b/tools/bmp_logo.c
index e8dd8c8..47228d2 100644
--- a/tools/bmp_logo.c
+++ b/tools/bmp_logo.c
@@ -1,15 +1,4 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#if defined(__linux__)
-#include <stdint.h>
-#else
-#ifdef __CYGWIN__
-#include "elf.h"
-#else
-#include <inttypes.h>
-#endif
-#endif
+#include "compiler.h"
 
 typedef struct bitmap_s {		/* bitmap description */
 	uint16_t width;
diff --git a/tools/img2srec.c b/tools/img2srec.c
index b04abbd..f10379f 100644
--- a/tools/img2srec.c
+++ b/tools/img2srec.c
@@ -52,6 +52,7 @@
 |  INCLUDES
 |*************************************************************************/
 
+#include "os_support.h"
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -61,8 +62,6 @@
 #include <unistd.h>
 #include <errno.h>
 
-extern int errno;
-
 /*************************************************************************
 |  DEFINES
 |*************************************************************************/
diff --git a/tools/mingw_support.h b/tools/mingw_support.h
index 1fb6c93..9e45e64 100644
--- a/tools/mingw_support.h
+++ b/tools/mingw_support.h
@@ -34,9 +34,6 @@
 #define MAP_SHARED	0x01		/* Share changes */
 #define MAP_PRIVATE	0x02		/* Changes are private */
 
-/* Return value of `mmap' in case of an error */
-#define MAP_FAILED	((void *) -1)
-
 /* Windows 64-bit access macros */
 #define LODWORD(x) ((DWORD)((DWORDLONG)(x)))
 #define HIDWORD(x) ((DWORD)(((DWORDLONG)(x) >> 32) & 0xffffffff))
diff --git a/tools/mkimage.c b/tools/mkimage.c
index 967fe9a..02cdb95 100644
--- a/tools/mkimage.c
+++ b/tools/mkimage.c
@@ -25,12 +25,6 @@
 #include "mkimage.h"
 #include <image.h>
 
-extern int errno;
-
-#ifndef MAP_FAILED
-#define MAP_FAILED (void *)(-1)
-#endif
-
 extern	unsigned long	crc32 (unsigned long crc, const char *buf, unsigned int len);
 static	void		copy_file (int, const char *, int);
 static	void		usage (void);
@@ -502,7 +496,7 @@ image_verify_header (char *ptr, int image_size)
 	 */
 	memcpy (hdr, ptr, sizeof(image_header_t));
 
-	if (ntohl(hdr->ih_magic) != IH_MAGIC) {
+	if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
 		fprintf (stderr,
 			"%s: Bad Magic Number: \"%s\" is no valid image\n",
 			cmdname, imagefile);
@@ -512,8 +506,8 @@ image_verify_header (char *ptr, int image_size)
 	data = (char *)hdr;
 	len  = sizeof(image_header_t);
 
-	checksum = ntohl(hdr->ih_hcrc);
-	hdr->ih_hcrc = htonl(0);	/* clear for re-calculation */
+	checksum = be32_to_cpu(hdr->ih_hcrc);
+	hdr->ih_hcrc = cpu_to_be32(0);	/* clear for re-calculation */
 
 	if (crc32 (0, data, len) != checksum) {
 		fprintf (stderr,
@@ -525,7 +519,7 @@ image_verify_header (char *ptr, int image_size)
 	data = ptr + sizeof(image_header_t);
 	len  = image_size - sizeof(image_header_t) ;
 
-	if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
+	if (crc32 (0, data, len) != be32_to_cpu(hdr->ih_dcrc)) {
 		fprintf (stderr,
 			"%s: ERROR: \"%s\" has corrupted data!\n",
 			cmdname, imagefile);
diff --git a/tools/mkimage.h b/tools/mkimage.h
index c8df6e1..70c53ad 100644
--- a/tools/mkimage.h
+++ b/tools/mkimage.h
@@ -26,14 +26,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#ifndef __WIN32__
-#include <netinet/in.h>		/* for host / network byte order conversions	*/
-#endif
-#ifdef __MINGW32__
-#include <stdint.h>
-#else
-#include <sys/mman.h>
-#endif
 #include <sys/stat.h>
 #include <time.h>
 #include <unistd.h>
@@ -53,28 +45,3 @@
 #define MKIMAGE_DEFAULT_DTC_OPTIONS	"-I dts -O dtb -p 500"
 #define MKIMAGE_MAX_DTC_CMDLINE_LEN	512
 #define MKIMAGE_DTC			"dtc"   /* assume dtc is in $PATH */
-
-#if defined(__BEOS__) || defined(__NetBSD__) || defined(__APPLE__)
-#include <inttypes.h>
-#endif
-
-#ifdef __WIN32__
-typedef unsigned int __u32;
-
-#define SWAP_LONG(x) \
-	((__u32)( \
-		(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
-		(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
-		(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
-		(((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
-typedef		unsigned char	uint8_t;
-typedef		unsigned short	uint16_t;
-typedef		unsigned int	uint32_t;
-
-#define     ntohl(a)	SWAP_LONG(a)
-#define     htonl(a)	SWAP_LONG(a)
-#endif	/* __WIN32__ */
-
-#ifndef	O_BINARY		/* should be define'd on __WIN32__ */
-#define O_BINARY	0
-#endif
diff --git a/tools/os_support.c b/tools/os_support.c
index 001fe64..5b919aa 100644
--- a/tools/os_support.c
+++ b/tools/os_support.c
@@ -19,6 +19,7 @@
 /*
  * Include additional files required for supporting different operating systems
  */
+#include "compiler.h"
 #ifdef __MINGW32__
 #include "mingw_support.c"
 #endif
diff --git a/tools/os_support.h b/tools/os_support.h
index f6f86b0..7bf930e 100644
--- a/tools/os_support.h
+++ b/tools/os_support.h
@@ -19,6 +19,8 @@
 #ifndef __OS_SUPPORT_H_
 #define __OS_SUPPORT_H_
 
+#include "compiler.h"
+
 /*
  * Include additional files required for supporting different operating systems
  */
diff --git a/tools/ubsha1.c b/tools/ubsha1.c
index c4203ed..9774eea 100644
--- a/tools/ubsha1.c
+++ b/tools/ubsha1.c
@@ -28,9 +28,6 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
-#ifndef __MINGW32__
-#include <sys/mman.h>
-#endif
 #include <sys/stat.h>
 #include "sha1.h"
 
@@ -40,16 +37,6 @@
 #include <config.h>
 #undef	__ASSEMBLY__
 
-#ifndef	O_BINARY		/* should be define'd on __WIN32__ */
-#define O_BINARY	0
-#endif
-
-#ifndef MAP_FAILED
-#define MAP_FAILED (-1)
-#endif
-
-extern int errno;
-
 extern void sha1_csum (unsigned char *input, int ilen, unsigned char output[20]);
 
 int main (int argc, char **argv)
-- 
1.6.3.3



More information about the U-Boot mailing list