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

libusb-cmake as libusb provider and added MSVC Support #1440

Open
wants to merge 8 commits into
base: testing
Choose a base branch
from
27 changes: 27 additions & 0 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@ jobs:
run: sudo make package
- name: sudo make uninstall
run: sudo make uninstall && sudo make clean

job_windows_msvc:
name: windows MSVC
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: debug
run: |
mkdir build
mkdir build\\debug
cd build\\debug
cmake ..\\.. -G "Visual Studio 17 2022" -DCMAKE_BUILD_TYPE="Debug"
cmake --build . --target ALL_BUILD
- name: make release
run: |
mkdir build\\release
cd build\\release
cmake ..\\.. -G "Visual Studio 17 2022" -DCMAKE_BUILD_TYPE="Release"
cmake --build . --target ALL_BUILD
- name: make install
run: |
cd build\\release
cmake --build . --target INSTALL
- name: sudo make uninstall
run: |
cd build\\release
cmake --build . --target uninstall
# Linux MinGW cross compliation

# job_linux_22_04_cross:
Expand Down
22 changes: 20 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ endif()
###

find_package(libusb REQUIRED)
add_definitions(${LIBUSB_DEFINITIONS}) #only affects MSVC, For ssize-t

## Check for system-specific additional header files and libraries

Expand Down Expand Up @@ -262,15 +263,32 @@ set_target_properties(${STLINK_LIB_SHARED} PROPERTIES
OUTPUT_NAME ${PROJECT_NAME}
)

# Since we're not using __declspec(dllexport), WINDOWS_EXPORT_ALL_SYMBOLS is mandatory for MSVC.
# This call will only affect MSVC, compilers/OSes ignore it.
# TODO: Use dllexport on the desired functions
set_target_properties(${STLINK_LIB_SHARED} PROPERTIES
WINDOWS_EXPORT_ALL_SYMBOLS ON
)

# Link shared library
if (WIN32)
target_link_libraries(${STLINK_LIB_SHARED} ${LIBUSB_LIBRARY} ${SSP_LIB} wsock32 ws2_32)
else ()
target_link_libraries(${STLINK_LIB_SHARED} ${LIBUSB_LIBRARY} ${SSP_LIB})
endif()

install(TARGETS ${STLINK_LIB_SHARED} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

# For windows, shared libraries go to `bin` and their implibs go to `lib`
Copy link
Member

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.

if (MSVC)
install(TARGETS ${STLINK_LIB_SHARED}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
else()
install(TARGETS ${STLINK_LIB_SHARED}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

###
# Static library
Expand Down
96 changes: 32 additions & 64 deletions cmake/modules/Findlibusb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Member

@Nightwalker-87 Nightwalker-87 Nov 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to leave
elseif (MINGW AND EXISTS "/etc/debian_version") # Cross-build with MinGW-toolchain on Debian

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
Expand Down
Loading