Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake: Add option to use bundled libusb library #156

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -97,15 +98,15 @@ 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
if: startsWith(matrix.os, 'archlinux')
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')
Expand All @@ -114,20 +115,20 @@ 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
if: startsWith(matrix.os, 'fedora')
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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unordered_map>
#include <vector>

-#include <libusb/libusb.h>
+#ifdef ANDROID_TOOLS_USE_BUNDLED_LIBUSB
#include <libusb/libusb.h>
+#else
+#include <libusb-1.0/libusb.h>
+#endif

#include <android-base/file.h>
#include <android-base/logging.h>
Expand Down
12 changes: 11 additions & 1 deletion vendor/CMakeLists.adb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
48 changes: 48 additions & 0 deletions vendor/CMakeLists.libusb.txt
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 5 additions & 1 deletion vendor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions vendor/libusb
Submodule libusb added at c96048
Loading