180 lines
3.8 KiB
CMake
180 lines
3.8 KiB
CMake
|
|
cmake_minimum_required(VERSION 3.10)
|
|||
|
|
project(USB2CAN)
|
|||
|
|
|
|||
|
|
# 设置C++标准
|
|||
|
|
set(CMAKE_CXX_STANDARD 17)
|
|||
|
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|||
|
|
|
|||
|
|
# 添加编译选项
|
|||
|
|
if(WIN32)
|
|||
|
|
add_definitions(-DWIN32_LEAN_AND_MEAN)
|
|||
|
|
add_definitions(-DNOMINMAX)
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# 包含目录
|
|||
|
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|||
|
|
|
|||
|
|
# 查找所需的包
|
|||
|
|
find_package(Threads REQUIRED)
|
|||
|
|
|
|||
|
|
# Windows特定的设置
|
|||
|
|
if(WIN32)
|
|||
|
|
# 添加WinUSB库
|
|||
|
|
find_library(WINUSB_LIBRARY winusb)
|
|||
|
|
if(NOT WINUSB_LIBRARY)
|
|||
|
|
message(FATAL_ERROR "WinUSB library not found")
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# 添加SetupAPI库
|
|||
|
|
find_library(SETUPAPI_LIBRARY setupapi)
|
|||
|
|
if(NOT SETUPAPI_LIBRARY)
|
|||
|
|
message(FATAL_ERROR "SetupAPI library not found")
|
|||
|
|
endif()
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# Linux特定的设置
|
|||
|
|
if(UNIX AND NOT APPLE)
|
|||
|
|
# 查找libusb
|
|||
|
|
find_package(PkgConfig REQUIRED)
|
|||
|
|
pkg_check_modules(LIBUSB REQUIRED libusb-1.0)
|
|||
|
|
|
|||
|
|
# 查找libsocketcan
|
|||
|
|
find_library(SOCKETCAN_LIBRARY socketcan)
|
|||
|
|
if(NOT SOCKETCAN_LIBRARY)
|
|||
|
|
message(WARNING "socketcan library not found, using direct socket access")
|
|||
|
|
endif()
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# macOS特定的设置
|
|||
|
|
if(APPLE)
|
|||
|
|
# 查找libusb
|
|||
|
|
find_package(PkgConfig REQUIRED)
|
|||
|
|
pkg_check_modules(LIBUSB REQUIRED libusb-1.0)
|
|||
|
|
|
|||
|
|
# 设置包含路径
|
|||
|
|
include_directories(${LIBUSB_INCLUDE_DIRS})
|
|||
|
|
|
|||
|
|
# 在macOS上,我们可能需要使用其他方式访问CAN设备
|
|||
|
|
# 这里暂时使用Linux的实现作为基础
|
|||
|
|
message(STATUS "Building for macOS, using Linux platform implementation as base")
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# 源文件
|
|||
|
|
set(PLATFORM_SOURCES
|
|||
|
|
src/platform/Platform.cpp
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# Windows特定的源文件
|
|||
|
|
if(WIN32)
|
|||
|
|
list(APPEND PLATFORM_SOURCES src/platform/WindowsPlatform.cpp)
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# Linux特定的源文件
|
|||
|
|
if(UNIX AND NOT APPLE)
|
|||
|
|
list(APPEND PLATFORM_SOURCES src/platform/LinuxPlatform.cpp)
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# macOS特定的源文件
|
|||
|
|
if(APPLE)
|
|||
|
|
# 在macOS上,我们可以使用Linux平台的实现作为基础
|
|||
|
|
list(APPEND PLATFORM_SOURCES src/platform/LinuxPlatform.cpp)
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
set(USB_SOURCES
|
|||
|
|
src/usb/UsbDevice.cpp
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# Windows特定的USB源文件
|
|||
|
|
if(WIN32)
|
|||
|
|
list(APPEND USB_SOURCES src/usb/WinUsbDevice.cpp)
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# Linux特定的USB源文件
|
|||
|
|
if(UNIX AND NOT APPLE)
|
|||
|
|
list(APPEND USB_SOURCES src/usb/LinuxUsbDevice.cpp)
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# macOS特定的USB源文件
|
|||
|
|
if(APPLE)
|
|||
|
|
# 在macOS上,我们可以使用Linux平台的实现作为基础
|
|||
|
|
list(APPEND USB_SOURCES src/usb/LinuxUsbDevice.cpp)
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
set(CAN_SOURCES
|
|||
|
|
src/can/CanController.cpp
|
|||
|
|
src/can/CanMessage.cpp
|
|||
|
|
src/can/CanFdController.cpp
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
set(DEVICE_SOURCES
|
|||
|
|
src/device/Usb2CanDevice.cpp
|
|||
|
|
src/device/DeviceManager.cpp
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
set(API_SOURCES
|
|||
|
|
src/api/Usb2Can.cpp
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 创建静态库
|
|||
|
|
add_library(usb2can STATIC
|
|||
|
|
${PLATFORM_SOURCES}
|
|||
|
|
${USB_SOURCES}
|
|||
|
|
${CAN_SOURCES}
|
|||
|
|
${DEVICE_SOURCES}
|
|||
|
|
${API_SOURCES}
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 链接库
|
|||
|
|
target_link_libraries(usb2can
|
|||
|
|
Threads::Threads
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# Windows特定的链接库
|
|||
|
|
if(WIN32)
|
|||
|
|
target_link_libraries(usb2can
|
|||
|
|
${WINUSB_LIBRARY}
|
|||
|
|
${SETUPAPI_LIBRARY}
|
|||
|
|
)
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# Linux特定的链接库
|
|||
|
|
if(UNIX AND NOT APPLE)
|
|||
|
|
target_link_libraries(usb2can
|
|||
|
|
${LIBUSB_LIBRARIES}
|
|||
|
|
)
|
|||
|
|
if(SOCKETCAN_LIBRARY)
|
|||
|
|
target_link_libraries(usb2can
|
|||
|
|
${SOCKETCAN_LIBRARY}
|
|||
|
|
)
|
|||
|
|
endif()
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# macOS特定的链接库
|
|||
|
|
if(APPLE)
|
|||
|
|
target_link_libraries(usb2can
|
|||
|
|
${LIBUSB_LIBRARIES}
|
|||
|
|
)
|
|||
|
|
# 在macOS上,我们可能需要链接其他库
|
|||
|
|
# 这里暂时使用Linux的实现作为基础
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# 添加示例
|
|||
|
|
add_subdirectory(examples)
|
|||
|
|
|
|||
|
|
# 添加测试
|
|||
|
|
option(BUILD_TESTS "Build tests" OFF)
|
|||
|
|
if(BUILD_TESTS)
|
|||
|
|
enable_testing()
|
|||
|
|
add_subdirectory(tests)
|
|||
|
|
endif()
|
|||
|
|
|
|||
|
|
# 安装规则
|
|||
|
|
install(TARGETS usb2can
|
|||
|
|
ARCHIVE DESTINATION lib
|
|||
|
|
LIBRARY DESTINATION lib
|
|||
|
|
RUNTIME DESTINATION bin
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
install(DIRECTORY include/
|
|||
|
|
DESTINATION include
|
|||
|
|
)
|