-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
libusb-cmake as libusb provider and added MSVC Support #1440
base: testing
Are you sure you want to change the base?
Changes from all commits
3770f59
af996f8
7a89e4a
7cbfc34
0d26a91
1f31439
41fdb1d
7bfd3a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
# LIBUSB_DEFINITIONS compiler switches required for using libusb | ||
|
||
include(FindPackageHandleStandardArgs) | ||
include(FetchContent) | ||
|
||
if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") # FreeBSD; libusb is integrated into the system | ||
# libusb header file | ||
|
@@ -51,75 +52,42 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") # OpenBSD; libus | |
message(FATAL_ERROR "No libusb-1.0 library found on your system! Install libusb-1.0 from ports or packages.") | ||
endif() | ||
|
||
elseif (WIN32 OR (MINGW AND EXISTS "/etc/debian_version")) # Windows OR cross-build with MinGW-toolchain on Debian | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to leave including the following associated code block for now, until cross building via MVSC on Linux has also been tested and verified? The idea is to drop this part as well later on, if everything works well. I'd just prefer to keep at least one working approach for now ... |
||
# MinGW: 64-bit or 32-bit? | ||
if (CMAKE_SIZEOF_VOID_P EQUAL 8) | ||
message(STATUS "=== Building for Windows (x86-64) ===") | ||
set(ARCH 64) | ||
else () | ||
message(STATUS "=== Building for Windows (i686) ===") | ||
set(ARCH 32) | ||
endif() | ||
elseif (MSVC) # Native Windows MSVC | ||
|
||
if (NOT LIBUSB_FOUND) | ||
# Preparations for installing libusb library | ||
set(LIBUSB_WIN_VERSION 1.0.27) # set libusb version | ||
set(LIBUSB_WIN_ARCHIVE_PATH ${CMAKE_SOURCE_DIR}/3rdparty/libusb-${LIBUSB_WIN_VERSION}.7z) | ||
set(LIBUSB_WIN_OUTPUT_FOLDER ${CMAKE_SOURCE_DIR}/3rdparty/libusb-${LIBUSB_WIN_VERSION}) | ||
|
||
# Get libusb package | ||
if (EXISTS ${LIBUSB_WIN_ARCHIVE_PATH}) # ... should the package be already there | ||
message(STATUS "libusb archive already in build folder") | ||
else () # ... download the package | ||
message(STATUS "downloading libusb ${LIBUSB_WIN_VERSION}") | ||
file(DOWNLOAD | ||
https://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-${LIBUSB_WIN_VERSION}/libusb-${LIBUSB_WIN_VERSION}.7z/download | ||
${LIBUSB_WIN_ARCHIVE_PATH} EXPECTED_MD5 c72153fc5a32f3b942427b0671897a1a | ||
) | ||
endif() | ||
|
||
file(MAKE_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER}) | ||
|
||
# Extract libusb package with cmake | ||
execute_process( | ||
COMMAND ${CMAKE_COMMAND} -E tar xv ${LIBUSB_WIN_ARCHIVE_PATH} | ||
WORKING_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER} | ||
) | ||
|
||
# libusb header file | ||
FIND_PATH(LIBUSB_INCLUDE_DIR | ||
set(libusb_FIND_REQUIRED OFF) # Will either find it or download it, there's no missing it. | ||
set(LIBUSB_DEFINITIONS "-D_SSIZE_T_DEFINED" "-Dssize_t=int64_t") # fix for ill-defined ssize-t | ||
|
||
# libusb header file | ||
FIND_PATH(LIBUSB_INCLUDE_DIR | ||
NAMES libusb.h | ||
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/include | ||
PATH_SUFFIXES libusb-1.0 | ||
NO_DEFAULT_PATH | ||
NO_CMAKE_FIND_ROOT_PATH | ||
) | ||
|
||
if (MINGW OR MSYS) | ||
# libusb library (static) | ||
set(LIBUSB_NAME libusb-1.0) | ||
find_library(LIBUSB_LIBRARY | ||
NAMES ${LIBUSB_NAME} | ||
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MinGW${ARCH}/static | ||
NO_DEFAULT_PATH | ||
NO_CMAKE_FIND_ROOT_PATH | ||
) | ||
else (MSVC) | ||
# libusb library | ||
set(LIBUSB_NAME libusb-1.0) | ||
find_library(LIBUSB_LIBRARY | ||
NAMES ${LIBUSB_NAME} | ||
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MinGW${ARCH}/dll | ||
NO_DEFAULT_PATH | ||
NO_CMAKE_FIND_ROOT_PATH | ||
) | ||
endif() | ||
endif() | ||
HINTS "C:/Program Files/libusb-1.0/include" "C:/Program Files (x86)/libusb-1.0/include" | ||
PATH_SUFFIXES "libusb-1.0" | ||
) | ||
|
||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(libusb DEFAULT_MSG LIBUSB_INCLUDE_DIR LIBUSB_LIBRARY) | ||
# libusb library | ||
set(LIBUSB_NAME usb-1.0) | ||
find_library(LIBUSB_LIBRARY | ||
NAMES ${LIBUSB_NAME} | ||
HINTS "C:/Program Files/libusb-1.0" "C:/Program Files (x86)/libusb-1.0" | ||
) | ||
|
||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(libusb DEFAULT_MSG LIBUSB_LIBRARY LIBUSB_INCLUDE_DIR) | ||
mark_as_advanced(LIBUSB_INCLUDE_DIR LIBUSB_LIBRARY) | ||
message(STATUS "Missing libusb library has been installed") | ||
if (NOT LIBUSB_FOUND) | ||
message(STATUS "No libusb-1.0 not installed into your system. Downloading and building it from source") | ||
|
||
FetchContent_Declare( | ||
${LIBUSB_NAME} | ||
GIT_REPOSITORY https://github.com/libusb/libusb-cmake | ||
GIT_TAG v1.0.27-0 | ||
) | ||
|
||
FetchContent_MakeAvailable(${LIBUSB_NAME}) | ||
set(LIBUSB_FOUND ON) | ||
set(LIBUSB_INCLUDE_DIR "") | ||
set(LIBUSB_LIBRARY ${LIBUSB_NAME}) | ||
mark_as_advanced(LIBUSB_FOUND LIBUSB_INCLUDE_DIR LIBUSB_LIBRARY) | ||
endif() | ||
else () # all other OS (unix-based) | ||
# libusb header file | ||
FIND_PATH(LIBUSB_INCLUDE_DIR | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentaton: Replace "implibs" with "imported libraries (implibs)" in comment for clarification.