From 0c9e24cccdbb8eb976c9a5b3b7e00ad3382f0f29 Mon Sep 17 00:00:00 2001 From: Alynx Zhou Date: Thu, 16 Sep 2021 01:19:57 +0800 Subject: [PATCH] Add Windows build support for libusb --- app/meson.build | 14 +++++++++++++- app/src/aoa_hid.c | 5 ++++- app/src/aoa_hid.h | 1 + cross_win32.txt | 1 + cross_win64.txt | 1 + prebuilt-deps/Makefile | 18 ++++++++++++++++-- prebuilt-deps/prepare-dep | 29 +++++++++++++++++++++++++---- release.mk | 2 ++ 8 files changed, 63 insertions(+), 8 deletions(-) diff --git a/app/meson.build b/app/meson.build index 538d5a40a9..9a7f3854dd 100644 --- a/app/meson.build +++ b/app/meson.build @@ -65,7 +65,7 @@ if not get_option('crossbuild_windows') endif else - # TODO: add libusb dependency for windows. + # cross-compile mingw32 build (from Linux to Windows) prebuilt_sdl2 = meson.get_cross_property('prebuilt_sdl2') sdl2_bin_dir = meson.current_source_dir() + '/../prebuilt-deps/' + prebuilt_sdl2 + '/bin' @@ -93,8 +93,20 @@ else include_directories: include_directories(ffmpeg_include_dir) ) + prebuilt_libusb = meson.get_cross_property('prebuilt_libusb') + libusb_bin_dir = meson.current_source_dir() + '/../prebuilt-deps/' + prebuilt_libusb + '/dll' + libusb_include_dir = '../prebuilt-deps/' + prebuilt_libusb + '/include' + + libusb = declare_dependency( + dependencies: [ + cc.find_library('libusb-1.0', dirs: libusb_bin_dir), + ], + include_directories: include_directories(libusb_include_dir) + ) + dependencies = [ ffmpeg, + libusb, sdl2, cc.find_library('mingw32') ] diff --git a/app/src/aoa_hid.c b/app/src/aoa_hid.c index e4990313d3..8012ecf1fc 100644 --- a/app/src/aoa_hid.c +++ b/app/src/aoa_hid.c @@ -1,6 +1,7 @@ #include "util/log.h" #include +#include #include "aoa_hid.h" @@ -108,6 +109,7 @@ aoa_open_usb_handle(libusb_device *device, libusb_device_handle **handle) { } bool aoa_init(struct aoa *aoa, const struct scrcpy_options *options) { + aoa->usb_context = NULL; aoa->usb_device = NULL; aoa->usb_handle = NULL; aoa->next_accessories_id = 0; @@ -123,7 +125,7 @@ bool aoa_init(struct aoa *aoa, const struct scrcpy_options *options) { return false; } - libusb_init(NULL); + libusb_init(&aoa->usb_context); aoa->usb_device = aoa_find_usb_device(options->serial); if (!aoa->usb_device) { @@ -313,6 +315,7 @@ void aoa_thread_join(struct aoa *aoa) { void aoa_destroy(struct aoa *aoa) { libusb_close(aoa->usb_handle); libusb_unref_device(aoa->usb_device); + libusb_exit(aoa->usb_context); sc_cond_destroy(&aoa->event_cond); sc_mutex_destroy(&aoa->mutex); } diff --git a/app/src/aoa_hid.h b/app/src/aoa_hid.h index fdf5ceb0b5..226210264e 100644 --- a/app/src/aoa_hid.h +++ b/app/src/aoa_hid.h @@ -25,6 +25,7 @@ struct hid_event { struct hid_event_queue CBUF(struct hid_event, 64); struct aoa { + libusb_context *usb_context; libusb_device *usb_device; libusb_device_handle *usb_handle; sc_thread thread; diff --git a/cross_win32.txt b/cross_win32.txt index 4db17be71d..cb4ac03ac1 100644 --- a/cross_win32.txt +++ b/cross_win32.txt @@ -18,3 +18,4 @@ endian = 'little' prebuilt_ffmpeg_shared = 'ffmpeg-4.3.1-win32-shared' prebuilt_ffmpeg_dev = 'ffmpeg-4.3.1-win32-dev' prebuilt_sdl2 = 'SDL2-2.0.16/i686-w64-mingw32' +prebuilt_libusb = 'libusb-1.0.24/MinGW32' diff --git a/cross_win64.txt b/cross_win64.txt index d03f02722a..13cba3df4d 100644 --- a/cross_win64.txt +++ b/cross_win64.txt @@ -18,3 +18,4 @@ endian = 'little' prebuilt_ffmpeg_shared = 'ffmpeg-4.3.1-win64-shared' prebuilt_ffmpeg_dev = 'ffmpeg-4.3.1-win64-dev' prebuilt_sdl2 = 'SDL2-2.0.16/x86_64-w64-mingw32' +prebuilt_libusb = 'libusb-1.0.24/MinGW64' diff --git a/prebuilt-deps/Makefile b/prebuilt-deps/Makefile index dced047cbd..04d517c0d8 100644 --- a/prebuilt-deps/Makefile +++ b/prebuilt-deps/Makefile @@ -3,11 +3,14 @@ prepare-ffmpeg-dev-win32 \ prepare-ffmpeg-shared-win64 \ prepare-ffmpeg-dev-win64 \ + prepare-libusb \ prepare-sdl2 \ prepare-adb -prepare-win32: prepare-sdl2 prepare-ffmpeg-shared-win32 prepare-ffmpeg-dev-win32 prepare-adb -prepare-win64: prepare-sdl2 prepare-ffmpeg-shared-win64 prepare-ffmpeg-dev-win64 prepare-adb +LIBUSB_DIR := libusb-1.0.24 + +prepare-win32: prepare-sdl2 prepare-ffmpeg-shared-win32 prepare-ffmpeg-dev-win32 prepare-libusb prepare-adb +prepare-win64: prepare-sdl2 prepare-ffmpeg-shared-win64 prepare-ffmpeg-dev-win64 prepare-libusb prepare-adb prepare-ffmpeg-shared-win32: @./prepare-dep https://github.com/Genymobile/scrcpy/releases/download/v1.16/ffmpeg-4.3.1-win32-shared.zip \ @@ -29,6 +32,17 @@ prepare-ffmpeg-dev-win64: 2e8038242cf8e1bd095c2978f196ff0462b122cc6ef7e74626a6af15459d8b81 \ ffmpeg-4.3.1-win64-dev +prepare-libusb: + # libusb put all things under the root of 7z file, so we pass the last + # argument which means creating extract dir manually. + @./prepare-dep https://github.com/libusb/libusb/releases/download/v1.0.24/libusb-1.0.24.7z \ + 620cec4dbe4868202949294157da5adb75c9fbb4f04266146fc833eef85f90fb \ + "$(LIBUSB_DIR)" \ + 1 + # Our code expects include dir under architechture dir. + cp -a "$(LIBUSB_DIR)"/include "$(LIBUSB_DIR)"/MinGW32 + cp -a "$(LIBUSB_DIR)"/include "$(LIBUSB_DIR)"/MinGW64 + prepare-sdl2: @./prepare-dep https://libsdl.org/release/SDL2-devel-2.0.16-mingw.tar.gz \ 2bfe48628aa9635c12eac7d421907e291525de1d0b04b3bca4a5bd6e6c881a6f \ diff --git a/prebuilt-deps/prepare-dep b/prebuilt-deps/prepare-dep index f152e6cf65..179d52ee70 100755 --- a/prebuilt-deps/prepare-dep +++ b/prebuilt-deps/prepare-dep @@ -3,6 +3,7 @@ set -e url="$1" sum="$2" dir="$3" +create_extract_dir="$4" checksum() { local file="$1" @@ -27,13 +28,32 @@ get_file() { extract() { local file="$1" + local create_extract_dir="$2" echo "Extracting $file..." if [[ "$file" == *.zip ]] then - unzip -q "$file" + if [[ -n "$create_extract_dir" ]] + then + unzip -q "$file" -d "$dir" + else + unzip -q "$file" + fi elif [[ "$file" == *.tar.gz ]] then - tar xf "$file" + if [[ -n "$create_extract_dir" ]] + then + tar xf "$file" --one-top-level="$dir" + else + tar xf "$file" + fi + elif [[ "$file" == *.7z ]] + then + if [[ -n "$create_extract_dir" ]] + then + 7z x "$file" -o"$dir" + else + 7z x "$file" + fi else echo "Unsupported file: $file" return 1 @@ -44,6 +64,7 @@ get_dep() { local url="$1" local sum="$2" local dir="$3" + local create_extract_dir="$4" local file="${url##*/}" if [[ -d "$dir" ]] then @@ -51,8 +72,8 @@ get_dep() { else echo "$dir: not found" get_file "$url" "$file" "$sum" - extract "$file" + extract "$file" "$create_extract_dir" fi } -get_dep "$url" "$sum" "$dir" +get_dep "$url" "$sum" "$dir" "$create_extract_dir" diff --git a/release.mk b/release.mk index e327654cfd..5359a5eebc 100644 --- a/release.mk +++ b/release.mk @@ -99,6 +99,7 @@ dist-win32: build-server build-win32 cp prebuilt-deps/ffmpeg-4.3.1-win32-shared/bin/avformat-58.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp prebuilt-deps/ffmpeg-4.3.1-win32-shared/bin/swresample-3.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp prebuilt-deps/ffmpeg-4.3.1-win32-shared/bin/swscale-5.dll "$(DIST)/$(WIN32_TARGET_DIR)/" + cp prebuilt-deps/libusb-1.0.24/MinGW32/dll/libusb-1.0.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp prebuilt-deps/platform-tools/adb.exe "$(DIST)/$(WIN32_TARGET_DIR)/" cp prebuilt-deps/platform-tools/AdbWinApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp prebuilt-deps/platform-tools/AdbWinUsbApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/" @@ -115,6 +116,7 @@ dist-win64: build-server build-win64 cp prebuilt-deps/ffmpeg-4.3.1-win64-shared/bin/avformat-58.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp prebuilt-deps/ffmpeg-4.3.1-win64-shared/bin/swresample-3.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp prebuilt-deps/ffmpeg-4.3.1-win64-shared/bin/swscale-5.dll "$(DIST)/$(WIN64_TARGET_DIR)/" + cp prebuilt-deps/libusb-1.0.24/MinGW64/dll/libusb-1.0.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp prebuilt-deps/platform-tools/adb.exe "$(DIST)/$(WIN64_TARGET_DIR)/" cp prebuilt-deps/platform-tools/AdbWinApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp prebuilt-deps/platform-tools/AdbWinUsbApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"