From f82264b346997903eeebc3b47d7484c963bcf725 Mon Sep 17 00:00:00 2001 From: Biswapriyo Nath Date: Sat, 10 Aug 2024 14:13:23 +0000 Subject: [PATCH] CMake: Add option to use bundled libusb library --- .github/workflows/build.yml | 17 ++++--- .gitmodules | 4 ++ CMakeLists.txt | 1 + ....0-libusb.h-instead-of-libusb-libusb.patch | 7 ++- vendor/CMakeLists.adb.txt | 12 ++++- vendor/CMakeLists.libusb.txt | 48 +++++++++++++++++++ vendor/CMakeLists.txt | 6 ++- vendor/libusb | 1 + 8 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 vendor/CMakeLists.libusb.txt create mode 160000 vendor/libusb diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0499572..819e365 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,7 +13,7 @@ jobs: DEBIAN_FRONTEND=noninteractive sudo apt-get install -yq \ ${{ matrix.pkgs }} cmake git golang libbrotli-dev \ libgtest-dev liblz4-dev libpcre2-dev libprotobuf-dev libunwind-dev \ - libusb-1.0-0-dev libzstd-dev make pandoc pkg-config + libzstd-dev make pandoc pkg-config - name: checkout uses: actions/checkout@v4 with: @@ -35,6 +35,7 @@ jobs: mkdir build && cd build cmake \ -DANDROID_TOOLS_USE_BUNDLED_FMT=ON \ + -DANDROID_TOOLS_USE_BUNDLED_LIBUSB=ON \ .. make package_source for x in sha1sum sha256sum sha512sum b2sum; do @@ -97,7 +98,7 @@ jobs: zypper -n ref zypper -n in ${{ matrix.pkgs }} cmake git go gtest pcre2-devel pkgconfig \ 'pkgconfig(libbrotlicommon)' 'pkgconfig(liblz4)' \ - 'pkgconfig(libunwind-generic)' 'pkgconfig(libusb-1.0)' \ + 'pkgconfig(libunwind-generic)' \ 'pkgconfig(libzstd)' 'pkgconfig(protobuf)' ninja tar xz - name: prep archlinux @@ -105,7 +106,7 @@ jobs: run: | pacman -Syu --needed --noconfirm \ ${{ matrix.pkgs }} brotli cmake git go gtest libunwind \ - libusb lz4 pcre2 pkgconfig protobuf zstd ninja + lz4 pcre2 pkgconfig protobuf zstd ninja - name: prep ubuntu if: startsWith(matrix.os, 'ubuntu') @@ -114,12 +115,12 @@ jobs: DEBIAN_FRONTEND=noninteractive apt-get install -yq \ ${{ matrix.pkgs }} cmake git golang libbrotli-dev \ libgtest-dev liblz4-dev libpcre2-dev libprotobuf-dev libunwind-dev \ - libusb-1.0-0-dev libzstd-dev pkg-config protobuf-compiler ninja-build + libzstd-dev pkg-config protobuf-compiler ninja-build - name: prep alpine if: startsWith(matrix.os, 'alpine') run: | - apk add build-base pcre2-dev linux-headers libusb-dev gtest-dev samurai \ + apk add build-base pcre2-dev linux-headers gtest-dev samurai \ go git perl cmake protobuf-dev brotli-dev zstd-dev lz4-dev - name: prep fedora @@ -127,7 +128,7 @@ jobs: run: | dnf install -y ${{ matrix.pkgs }} cmake ninja-build perl git golang \ brotli-devel gtest-devel lz4-devel pcre2-devel protobuf-devel \ - libusbx-devel libzstd-devel tar xz + libzstd-devel tar xz - name: download source uses: actions/download-artifact@v4 @@ -147,6 +148,7 @@ jobs: -DCMAKE_CXX_FLAGS="$CXXFLAGS" \ -DCMAKE_BUILD_TYPE=Release \ -DANDROID_TOOLS_USE_BUNDLED_FMT=ON \ + -DANDROID_TOOLS_USE_BUNDLED_LIBUSB=ON \ -GNinja \ .. ninja --verbose @@ -179,7 +181,7 @@ jobs: - name: prep macos run: | rm -rf /opt/homebrew/include/openssl /usr/local/opt/openssl /usr/local/include/openssl - brew install --overwrite --quiet brotli cmake go googletest libusb lz4 \ + brew install --overwrite --quiet brotli cmake go googletest lz4 \ ninja pcre2 protobuf zstd - name: download source @@ -198,6 +200,7 @@ jobs: -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX=$HOME/android-tools \ -DANDROID_TOOLS_USE_BUNDLED_FMT=ON \ + -DANDROID_TOOLS_USE_BUNDLED_LIBUSB=ON \ -GNinja \ .. ninja --verbose diff --git a/.gitmodules b/.gitmodules index 5d0a1a2..782ca65 100644 --- a/.gitmodules +++ b/.gitmodules @@ -54,3 +54,7 @@ shallow = true path = vendor/libufdt url = https://android.googlesource.com/platform/system/libufdt.git +[submodule "vendor/libusb"] + shallow = true + path = vendor/libusb + url = https://android.googlesource.com/platform/external/libusb.git diff --git a/CMakeLists.txt b/CMakeLists.txt index f71b872..e7d947b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ include(GNUInstallDirs) # files are changed in vendor projects those can be retained with this option. option(ANDROID_TOOLS_PATCH_VENDOR "Patch vendor projects using patches directory" ON) option(ANDROID_TOOLS_USE_BUNDLED_FMT "Use bundled fmt library instead of system provided one" OFF) +option(ANDROID_TOOLS_USE_BUNDLED_LIBUSB "Use bundled libusb library instead of system provided one" OFF) # Install bash/zsh completion files. set(COMPLETION_COMMON_DIR "${CMAKE_INSTALL_FULL_DATADIR}/android-tools/completions") diff --git a/patches/adb/0005-Include-libusb-1.0-libusb.h-instead-of-libusb-libusb.patch b/patches/adb/0005-Include-libusb-1.0-libusb.h-instead-of-libusb-libusb.patch index f490dc1..8dd6cc5 100644 --- a/patches/adb/0005-Include-libusb-1.0-libusb.h-instead-of-libusb-libusb.patch +++ b/patches/adb/0005-Include-libusb-1.0-libusb.h-instead-of-libusb-libusb.patch @@ -11,12 +11,15 @@ diff --git a/client/usb_libusb.cpp b/client/usb_libusb.cpp index e0b0104a..bcfb03af 100644 --- a/client/usb_libusb.cpp +++ b/client/usb_libusb.cpp -@@ -36,7 +36,7 @@ +@@ -36,7 +36,11 @@ #include #include --#include ++#ifdef ANDROID_TOOLS_USE_BUNDLED_LIBUSB + #include ++#else +#include ++#endif #include #include diff --git a/vendor/CMakeLists.adb.txt b/vendor/CMakeLists.adb.txt index 4196bc0..a9bff22 100644 --- a/vendor/CMakeLists.adb.txt +++ b/vendor/CMakeLists.adb.txt @@ -64,6 +64,11 @@ target_include_directories(libadb PUBLIC libbase/include libziparchive/include) +if(ANDROID_TOOLS_USE_BUNDLED_LIBUSB) + target_compile_definitions(libadb PUBLIC ANDROID_TOOLS_USE_BUNDLED_LIBUSB) + target_include_directories(libadb PUBLIC libusb) +endif() + add_library(libadb_crypto_defaults STATIC adb/crypto/key.cpp adb/crypto/rsa_2048_key.cpp @@ -202,10 +207,15 @@ target_link_libraries(adb PkgConfig::libbrotlienc PkgConfig::liblz4 Threads::Threads - PkgConfig::libusb-1.0 z PkgConfig::libzstd) +if(ANDROID_TOOLS_USE_BUNDLED_LIBUSB) + target_link_libraries(adb android_tools_libusb) +else() + target_link_libraries(adb PkgConfig::libusb-1.0) +endif() + if(APPLE) target_link_libraries(adb "-framework CoreFoundation" diff --git a/vendor/CMakeLists.libusb.txt b/vendor/CMakeLists.libusb.txt new file mode 100644 index 0000000..ab13c00 --- /dev/null +++ b/vendor/CMakeLists.libusb.txt @@ -0,0 +1,48 @@ +# SPDX-License-Identifier: Apache-2.0 +# CMakeLists.libusb.txt: CMake file for libusb directory + +set(android_tools_libusb_SOURCES + libusb/libusb/core.c + libusb/libusb/descriptor.c + libusb/libusb/hotplug.c + libusb/libusb/io.c + libusb/libusb/sync.c + libusb/libusb/strerror.c) + +if(WIN32) + list(APPEND android_tools_libusb_SOURCES + libusb/libusb/os/events_windows.c + libusb/libusb/os/threads_windows.c + libusb/libusb/os/windows_common.c + libusb/libusb/os/windows_usbdk.c + libusb/libusb/os/windows_winusb.c) +elseif(APPLE) + list(APPEND android_tools_libusb_SOURCES + libusb/libusb/os/events_posix.c + libusb/libusb/os/darwin_usb.c + libusb/libusb/os/threads_posix.c) +else() + list(APPEND android_tools_libusb_SOURCES + libusb/libusb/os/events_posix.c + libusb/libusb/os/linux_usbfs.c + libusb/libusb/os/threads_posix.c + libusb/libusb/os/linux_netlink.c) +endif() + +add_library(android_tools_libusb STATIC ${android_tools_libusb_SOURCES}) + +target_include_directories(android_tools_libusb PRIVATE libusb/libusb libusb/libusb/os) + +if(WIN32) + target_include_directories(android_tools_libusb PRIVATE libusb/windows) +elseif(APPLE) + target_include_directories(android_tools_libusb PRIVATE libusb/darwin) + + target_link_libraries(android_tools_libusb PRIVATE + "-framework CoreFoundation" + "-framework IOKit" + "-framework Security" + "-lobjc") +else() + target_include_directories(android_tools_libusb PRIVATE libusb/linux) +endif() diff --git a/vendor/CMakeLists.txt b/vendor/CMakeLists.txt index d4281cc..081c30d 100644 --- a/vendor/CMakeLists.txt +++ b/vendor/CMakeLists.txt @@ -89,7 +89,11 @@ pkg_check_modules(libbrotlidec REQUIRED IMPORTED_TARGET libbrotlidec) pkg_check_modules(libbrotlienc REQUIRED IMPORTED_TARGET libbrotlienc) pkg_check_modules(liblz4 REQUIRED IMPORTED_TARGET liblz4) pkg_check_modules(libpcre2-8 REQUIRED IMPORTED_TARGET libpcre2-8) -pkg_check_modules(libusb-1.0 REQUIRED IMPORTED_TARGET libusb-1.0) +if(ANDROID_TOOLS_USE_BUNDLED_LIBUSB) + include(CMakeLists.libusb.txt) +else() + pkg_check_modules(libusb-1.0 REQUIRED IMPORTED_TARGET libusb-1.0) +endif() pkg_check_modules(libzstd REQUIRED IMPORTED_TARGET libzstd) find_package(Protobuf CONFIG) diff --git a/vendor/libusb b/vendor/libusb new file mode 160000 index 0000000..c960485 --- /dev/null +++ b/vendor/libusb @@ -0,0 +1 @@ +Subproject commit c9604857c0a5928ac8c47f6dc67e4f4fb699d675