[PATCH 3/4] scripts: unify libusb.h inclusion

Ahmad Fatoum a.fatoum at pengutronix.de
Tue Sep 14 06:20:15 PDT 2021


We have three host tools using libusb:
imx-usb-loader omap3-usb-loader and omap4_usbboot

All three link against it using `pkg-config --libs libusb-1.0`. All
three also do `pkg-config --cflags libusb-1.0`, but omap3-usb-loader
doesn't actually use it and assumes that <libusb-1.0/libusb.h> will resolve
to something that's correct.

I've also run into downstream patches around broken toolchains that
change <libusb.h> to <libusb-1.0/libusb.h>, so the header is found.

Fix the first issue and workaround the broken toolchains by using GCC 5.0+
__has_include to fallback to <libusb-1.0/libusb.h> if it doesn't exist
or if GCC is older than 5.0.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 scripts/imx/imx-usb-loader.c |  2 +-
 scripts/libusb.h             | 17 +++++++++++++++++
 scripts/omap3-usb-loader.c   |  2 +-
 scripts/omap4_usbboot.c      |  2 +-
 4 files changed, 20 insertions(+), 3 deletions(-)
 create mode 100644 scripts/libusb.h

diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index cff77f27f264..78af2b61bb06 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -29,12 +29,12 @@
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
-#include <libusb.h>
 #include <getopt.h>
 #include <arpa/inet.h>
 #include <linux/kernel.h>
 
 #include "../compiler.h"
+#include "../libusb.h"
 #include "imx.h"
 
 #define get_min(a, b) (((a) < (b)) ? (a) : (b))
diff --git a/scripts/libusb.h b/scripts/libusb.h
new file mode 100644
index 000000000000..892d5422c451
--- /dev/null
+++ b/scripts/libusb.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef SCRIPTS_LIBUSB_H_
+#define SCRIPTS_LIBUSB_H_
+
+#ifdef __has_include
+/* not using && to keep compatibility with older compilers */
+#if __has_include("<libusb.h>")
+#include <libusb.h>
+#define HAS_LIBUSB_H
+#endif
+#endif
+
+#ifndef HAS_LIBUSB_H
+#include <libusb-1.0/libusb.h>
+#endif
+
+#endif
diff --git a/scripts/omap3-usb-loader.c b/scripts/omap3-usb-loader.c
index 599a93856ac3..edc46938d9ba 100644
--- a/scripts/omap3-usb-loader.c
+++ b/scripts/omap3-usb-loader.c
@@ -28,7 +28,7 @@
 #include <errno.h>
 #include <libgen.h>		/* for basename */
 
-#include <libusb-1.0/libusb.h>		/* the main event */
+#include "libusb.h"
 
 /* Device specific defines (OMAP)
  * Primary source: http://www.ti.com/lit/pdf/sprugn4
diff --git a/scripts/omap4_usbboot.c b/scripts/omap4_usbboot.c
index 342efd0c9a40..71e4f0acd1fd 100644
--- a/scripts/omap4_usbboot.c
+++ b/scripts/omap4_usbboot.c
@@ -10,7 +10,7 @@
 #include <fcntl.h>
 #include <string.h>
 #include <sys/mman.h>
-#include <libusb.h>
+#include "libusb.h"
 #include <pthread.h>
 #include <termios.h>
 
-- 
2.30.2




More information about the barebox mailing list