mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Johannes Schneider <johannes.schneider@leica-geosystems.com>,
	Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/3] scripts: compiler.h: add Windows support
Date: Tue, 11 Apr 2023 11:38:43 +0200	[thread overview]
Message-ID: <20230411093844.1297004-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230411093844.1297004-1-a.fatoum@pengutronix.de>

We lack endianness conversion functions for Windows. Import them from
the public-domain portable_endian.h[1]. We skip the 64-bit XBox support
though as it's unlikely we'll need to run imx-usb-loader on that
particular game console.

While at it, only define min when it's undefined. This works around one
of the winapi headers indirectly included defining it.

[1]: https://gist.github.com/panzi/6856583

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 scripts/compiler.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/scripts/compiler.h b/scripts/compiler.h
index c932f715c551..925cad21b6cf 100644
--- a/scripts/compiler.h
+++ b/scripts/compiler.h
@@ -61,6 +61,37 @@ typedef unsigned int  uint;
 #elif defined(__OpenBSD__) || defined(__FreeBSD__) || \
       defined(__NetBSD__) || defined(__DragonFly__)
 # include <sys/endian.h>
+#elif defined _WIN32
+# if defined(_MSC_VER)
+#  include <stdlib.h>
+#  define htobe16(x) _byteswap_ushort(x)
+#  define htole16(x) (x)
+#  define be16toh(x) _byteswap_ushort(x)
+#  define le16toh(x) (x)
+#  define htobe32(x) _byteswap_ulong(x)
+#  define htole32(x) (x)
+#  define be32toh(x) _byteswap_ulong(x)
+#  define le32toh(x) (x)
+#  define htobe64(x) _byteswap_uint64(x)
+#  define htole64(x) (x)
+#  define be64toh(x) _byteswap_uint64(x)
+#  define le64toh(x) (x)
+# elif defined(__GNUC__) || defined(__clang__)
+#  define htobe16(x) __builtin_bswap16(x)
+#  define htole16(x) (x)
+#  define be16toh(x) __builtin_bswap16(x)
+#  define le16toh(x) (x)
+#  define htobe32(x) __builtin_bswap32(x)
+#  define htole32(x) (x)
+#  define be32toh(x) __builtin_bswap32(x)
+#  define le32toh(x) (x)
+#  define htobe64(x) __builtin_bswap64(x)
+#  define htole64(x) (x)
+#  define be64toh(x) __builtin_bswap64(x)
+#  define le64toh(x) (x)
+#else
+#  error platform not supported
+#endif
 #else /* assume Linux */
 # include <sys/types.h>
 # include <endian.h>
@@ -128,11 +159,13 @@ typedef uint32_t __u32;
 # define be64_to_cpu(x)		(x)
 #endif
 
+#ifndef min
 #define min(x, y) ({                            \
 	typeof(x) _min1 = (x);                  \
 	typeof(y) _min2 = (y);                  \
 	(void) (&_min1 == &_min2);              \
 	_min1 < _min2 ? _min1 : _min2; })
+#endif
 
 static inline void *xmalloc(size_t size)
 {
-- 
2.39.2




  parent reply	other threads:[~2023-04-11  9:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-11  9:38 [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW Ahmad Fatoum
2023-04-11  9:38 ` [PATCH 1/3] imx-usb-loader: don't depend on arpa/inet.h for endianness conversion Ahmad Fatoum
2023-04-11  9:38 ` Ahmad Fatoum [this message]
2023-04-11  9:38 ` [PATCH 3/3] scripts: common: drop unused mman.h include Ahmad Fatoum
2023-04-11 12:30 ` [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW Sascha Hauer

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=20230411093844.1297004-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=johannes.schneider@leica-geosystems.com \
    /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