Skip to content

Commit

Permalink
WIP add libusb-download
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Oct 29, 2023
1 parent 4242be5 commit 3308487
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 49 deletions.
78 changes: 39 additions & 39 deletions CMake/external_libusb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,45 @@ include(ExternalProject)

message( STATUS "Fetching libusb..." )


ExternalProject_Add(
libusb

# Work-around for libusb master broken on Nov 26' 2020 with introduction of v1.0.24
# the issue has been reported in https://github.com/libusb/libusb/issues/812
GIT_REPOSITORY "https://github.com/ev-mp/libusb.git"
GIT_TAG "2a7372db54094a406a755f0b8548b614ba8c78ec" # "v1.0.22" + Mac get_device_list hang fix

SOURCE_DIR "${CMAKE_BINARY_DIR}/third-party/libusb/"
BINARY_DIR "${CMAKE_BINARY_DIR}/third-party/libusb/build"

UPDATE_COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/third-party/libusb/CMakeLists.txt
${CMAKE_BINARY_DIR}/third-party/libusb/CMakeLists.txt
PATCH_COMMAND ""
TEST_COMMAND ""

CMAKE_ARGS -DCMAKE_CXX_STANDARD_LIBRARIES=${CMAKE_CXX_STANDARD_LIBRARIES}
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DANDROID_ABI=${ANDROID_ABI}
-DANDROID_STL=${ANDROID_STL}
)

add_library(usb INTERFACE)
target_include_directories( usb INTERFACE
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/third-party/libusb/libusb>
)
target_link_libraries( usb INTERFACE
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/third-party/libusb/build/$<CONFIG>/${CMAKE_STATIC_LIBRARY_PREFIX}usb${CMAKE_STATIC_LIBRARY_SUFFIX}>
)
set(USE_EXTERNAL_USB ON) # INTERFACE libraries can't have real deps, so targets that link with usb need to also depend on libusb

if (APPLE)
find_library(corefoundation_lib CoreFoundation)
find_library(iokit_lib IOKit)
target_link_libraries(usb INTERFACE objc ${corefoundation_lib} ${iokit_lib})
endif()
function( get_libusb )

configure_file( ${CMAKE_SOURCE_DIR}/CMake/libusb-download.cmake.in
${CMAKE_BINARY_DIR}/external-projects/libusb-download/CMakeLists.txt )
execute_process( COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/external-projects/libusb-download"
#OUTPUT_QUIET
RESULT_VARIABLE configure_ret )
execute_process( COMMAND "${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/external-projects/libusb-download"
#OUTPUT_QUIET
RESULT_VARIABLE build_ret )

if( configure_ret OR build_ret )
message( FATAL_ERROR "Failed to download libusb" )
endif()

add_subdirectory( "${CMAKE_BINARY_DIR}/third-party/libusb/" "${CMAKE_BINARY_DIR}/third-party/libusb/build" )

add_library(usb INTERFACE)
target_include_directories( usb INTERFACE
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/third-party/libusb/libusb>
)
target_link_libraries( usb INTERFACE
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/third-party/libusb/build/$<CONFIG>/${CMAKE_STATIC_LIBRARY_PREFIX}usb${CMAKE_STATIC_LIBRARY_SUFFIX}>
)
set(USE_EXTERNAL_USB ON) # INTERFACE libraries can't have real deps, so targets that link with usb need to also depend on libusb

if (APPLE)
find_library(corefoundation_lib CoreFoundation)
find_library(iokit_lib IOKit)
target_link_libraries(usb INTERFACE objc ${corefoundation_lib} ${iokit_lib})
endif()

endfunction()
get_libusb()

message( STATUS "Fetching libusb - Done" )

32 changes: 32 additions & 0 deletions CMake/libusb-download.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required( VERSION 3.6 )
project( libusb-download NONE )

include( ExternalProject )

ExternalProject_Add(
libusb
PREFIX .

# Work-around for libusb master broken on Nov 26' 2020 with introduction of v1.0.24
# the issue has been reported in https://github.com/libusb/libusb/issues/812
GIT_REPOSITORY "https://github.com/ev-mp/libusb.git"
GIT_TAG "2a7372db54094a406a755f0b8548b614ba8c78ec" # "v1.0.22" + Mac get_device_list hang fix
GIT_CONFIG advice.detachedHead=false # otherwise we'll get "You are in 'detached HEAD' state..."
GIT_SHALLOW 1 # No history needed (requires cmake 3.6)

SOURCE_DIR "${CMAKE_BINARY_DIR}/third-party/libusb/"
BINARY_DIR "${CMAKE_BINARY_DIR}/third-party/libusb/build"

UPDATE_COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/third-party/libusb/CMakeLists.txt
${CMAKE_BINARY_DIR}/third-party/libusb/CMakeLists.txt
PATCH_COMMAND ""
TEST_COMMAND ""

CMAKE_ARGS -DCMAKE_CXX_STANDARD_LIBRARIES=${CMAKE_CXX_STANDARD_LIBRARIES}
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DANDROID_ABI=${ANDROID_ABI}
-DANDROID_STL=${ANDROID_STL}
)

17 changes: 7 additions & 10 deletions third-party/libusb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ include_directories(
add_library(usb STATIC ${LIBUSB_C} ${LIBUSB_H})

if(WIN32)
set_target_properties (usb PROPERTIES
set_target_properties( ${PROJECT_NAME} PROPERTIES
FOLDER "3rd Party"
)
include_directories(msvc)
)
target_include_directories( ${PROJECT_NAME} PRIVATE msvc )
foreach(flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
Expand All @@ -69,27 +69,24 @@ if(WIN32)
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MD")
endforeach(flag_var)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /utf-8")
target_compile_definitions( ${PROJECT_NAME} PRIVATE /utf-8 )
endif()

if(ANDROID)
include_directories(android)
target_include_directories( ${PROJECT_NAME} PRIVATE android )
endif()

if(APPLE)
find_library(corefoundation_lib CoreFoundation)
find_library(iokit_lib IOKit)
target_include_directories(usb PRIVATE XCode)
TARGET_LINK_LIBRARIES(usb objc ${corefoundation_lib} ${iokit_lib})
target_link_libraries(usb PRIVATE objc ${corefoundation_lib} ${iokit_lib})
endif()

if((NOT APPLE) AND (NOT ANDROID) AND (NOT WIN32))
TARGET_LINK_LIBRARIES(usb udev)
target_link_libraries(usb PRIVATE udev)
endif()

#set_target_properties(usb PROPERTIES PREFIX "")

install(TARGETS ${PROJECT_NAME}
EXPORT realsense2Targets
ARCHIVE DESTINATION lib
Expand Down

0 comments on commit 3308487

Please sign in to comment.