From e9c08bcb1c7f1e6140bcc4e8867f54febebcc5d1 Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Tue, 8 Apr 2025 13:33:50 +0300 Subject: [PATCH 1/4] Major update of CMake configuration * Support Windows (MSVC and MinGW), Linux, and macOS * `libusb` installation can be specified explicitly by defining `LIBUSB_INCLUDE_DIRS` and `LIBUSB_LIBRARIES` CMake variables * Support the current structure of Windows `libusb` package, version 1.0.27 and newer * Select proper `libusb` MSVC libraries for various versions of Visual Studio * Remove duplicated and redundant command line options * Include `libusb.h` without `libusb-1.0` directory * `pkg-config` returns include string with this directory added * Windows packages don't have this directory at all * Normalize all CMake commands to lower case * Normalize indents by using 4 spaces * Install udev rules on Linux only --- CMakeLists.txt | 196 +++++++++++++++++++++++++------------------------ fobos/fobos.c | 3 +- 2 files changed, 100 insertions(+), 99 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 903931e..fc5b885 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,15 +24,12 @@ ######################################################################## cmake_minimum_required(VERSION 3.7.2) -if(${CMAKE_VERSION} VERSION_LESS "3.12.0") - project(fobos) -else() - project(fobos C) -endif() +project(fobos LANGUAGES C) +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_EXTENSIONS OFF) include(GNUInstallDirs) message(>>> "CMAKE_INSTALL_LIBDIR: " ${CMAKE_INSTALL_LIBDIR}) -set(CMAKE_C_FLAGS, "${CMAKE_C_FLAGS} -std=c99") ######################################################################## ######################################################################## @@ -47,50 +44,81 @@ endif(NOT CMAKE_BUILD_TYPE) ######################################################################## # Compiler specific setup ######################################################################## -if(CMAKE_COMPILER_IS_GNUCC AND NOT WIN32) - ADD_DEFINITIONS(-Wall) - ADD_DEFINITIONS(-Wextra) - ADD_DEFINITIONS(-Wno-unused-parameter) - ADD_DEFINITIONS(-Wno-unused) - ADD_DEFINITIONS(-Wsign-compare) - ADD_DEFINITIONS(-ansi) -elseif(MSVC14 OR MSVC14) - ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS) - ADD_DEFINITIONS(-D_TIMESPEC_DEFINED) +if(MSVC) + add_definitions( + -D_CRT_SECURE_NO_WARNINGS + -D_TIMESPEC_DEFINED + ) +else() + add_compile_options( + -Wall + -Wextra + -Wno-unused-parameter + -Wno-unused + -Wsign-compare + ) endif() ######################################################################## ######################################################################## # Find build dependencies ######################################################################## -find_package(PkgConfig) -if(PKG_CONFIG_FOUND) - pkg_check_modules(LIBUSB libusb-1.0 IMPORTED_TARGET) - if(LIBUSB_LINK_LIBRARIES) - set(LIBUSB_LIBRARIES "${LIBUSB_LINK_LIBRARIES}") +if(NOT DEFINED LIBUSB_INCLUDE_DIRS OR NOT DEFINED LIBUSB_LIBRARIES) + find_package(PkgConfig) + if(PKG_CONFIG_FOUND) + pkg_check_modules(LIBUSB libusb-1.0 IMPORTED_TARGET) + if(NOT LIBUSB_FOUND) + message(FATAL_ERROR "LibUSB 1.0 required") + else() + set(LIBUSB_LIBRARIES "${LIBUSB_LINK_LIBRARIES}") + endif() + else() + if(MSVC) + if(MSVC_VERSION EQUAL 1800) + set(LIBUSB_SUFFIX VS2013) + elseif(MSVC_VERSION EQUAL 1900) + set(LIBUSB_SUFFIX VS2015) + elseif((MSVC_VERSION GREATER_EQUAL 1910) AND (MSVC_VERSION LESS_EQUAL 1919)) + set(LIBUSB_SUFFIX VS2017) + elseif((MSVC_VERSION GREATER_EQUAL 1920) AND (MSVC_VERSION LESS_EQUAL 1929)) + set(LIBUSB_SUFFIX VS2019) + else() + set(LIBUSB_SUFFIX VS2022) + endif() + + if(CMAKE_SIZEOF_VOID_P EQUAL 4) + set(LIBUSB_SUFFIX "${LIBUSB_SUFFIX}/MS32") + else() + set(LIBUSB_SUFFIX "${LIBUSB_SUFFIX}/MS64") + endif() + + set(LIBUSB_SUFFIX "${LIBUSB_SUFFIX}/dll") + elseif(MINGW) + if(CMAKE_SIZEOF_VOID_P EQUAL 4) + set(LIBUSB_SUFFIX "MinGW32") + else() + set(LIBUSB_SUFFIX "MinGW64") + endif() + + set(LIBUSB_SUFFIX "${LIBUSB_SUFFIX}/dll/libusb-1.0.dll") + endif() + + set(LIBUSB_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/libusb/include) + set(LIBUSB_LIBRARIES ${CMAKE_SOURCE_DIR}/libusb/${LIBUSB_SUFFIX}) endif() -else() -if (CMAKE_VS_PLATFORM_NAME STREQUAL Win32) - set(LIBUSB_LIBRARIES ${CMAKE_SOURCE_DIR}/libusb/MS32/dll) -endif() -if (CMAKE_VS_PLATFORM_NAME STREQUAL x64) - set(LIBUSB_LIBRARIES ${CMAKE_SOURCE_DIR}/libusb/MS64/dll) -endif() - set(LIBUSB_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/libusb/include) -endif() -if(PKG_CONFIG_FOUND AND NOT LIBUSB_FOUND) - message(FATAL_ERROR "LibUSB 1.0 required") endif() ######################################################################## ######################################################################## # Install udev rules ######################################################################## -install( - FILES fobos-sdr.rules - DESTINATION "/etc/udev/rules.d" - COMPONENT "udev" +if (CMAKE_SYSTEM_NAME STREQUAL "Linux") + install( + FILES fobos-sdr.rules + DESTINATION "/etc/udev/rules.d" + COMPONENT "udev" ) +endif() ######################################################################## ######################################################################## @@ -108,36 +136,38 @@ message(>>> "PROJECT_BINARY_DIR: " ${PROJECT_BINARY_DIR}) ######################################################################## add_library(libfobos SHARED fobos/fobos.c) -target_compile_options(libfobos PUBLIC -std=c99) - if(MSVC) -target_compile_options(libfobos PUBLIC "/MT") -ADD_CUSTOM_COMMAND(TARGET libfobos - POST_BUILD + target_compile_options(libfobos PUBLIC "/MT") + add_custom_command(TARGET libfobos + POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/Debug - COMMAND ${CMAKE_COMMAND} -E copy ${LIBUSB_LIBRARIES}/libusb-1.0.dll ${PROJECT_BINARY_DIR}/Debug -) -ADD_CUSTOM_COMMAND(TARGET libfobos - POST_BUILD - COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/Release - COMMAND ${CMAKE_COMMAND} -E copy ${LIBUSB_LIBRARIES}/libusb-1.0.dll ${PROJECT_BINARY_DIR}/Release -) -else() + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${LIBUSB_LIBRARIES}/libusb-1.0.dll ${PROJECT_BINARY_DIR}/Debug + ) + add_custom_command(TARGET libfobos + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/Release + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${LIBUSB_LIBRARIES}/libusb-1.0.dll ${PROJECT_BINARY_DIR}/Release + ) +elseif(MINGW) + add_custom_command(TARGET libfobos + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${LIBUSB_LIBRARIES} ${PROJECT_BINARY_DIR}/msys-usb-1.0.dll + ) endif() -if(CMAKE_COMPILER_IS_GNUCC AND NOT WIN32) - target_link_libraries(libfobos PRIVATE ${LIBUSB_LIBRARIES}) -else() +if(MSVC) target_link_directories(libfobos PRIVATE ${LIBUSB_LIBRARIES}) +else() + target_link_libraries(libfobos PRIVATE ${LIBUSB_LIBRARIES}) endif() target_include_directories(libfobos PUBLIC - $ - $ - $/fobos - ${LIBUSB_INCLUDE_DIRS} - ) - + $ + $ + $/fobos + ${LIBUSB_INCLUDE_DIRS} +) + set_target_properties(libfobos PROPERTIES DEFINE_SYMBOL "FOBOS_EXPORTS") set_target_properties(libfobos PROPERTIES OUTPUT_NAME fobos) ######################################################################## @@ -145,7 +175,7 @@ set_target_properties(libfobos PROPERTIES OUTPUT_NAME fobos) ######################################################################## # Install public header files ######################################################################## -INSTALL( +install( FILES fobos/fobos.h DESTINATION include ) @@ -154,7 +184,7 @@ INSTALL( ######################################################################## # Install built library files & utilities ######################################################################## -INSTALL( +install( TARGETS libfobos LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ) @@ -163,29 +193,29 @@ INSTALL( ######################################################################## # Create Pkg Config File ######################################################################## -FOREACH(inc ${LIBUSB_INCLUDEDIR}) - LIST(APPEND LIBFOBOS_PC_CFLAGS "-I${inc}") -ENDFOREACH(inc) +foreach(inc ${LIBUSB_INCLUDEDIR}) + list(APPEND LIBFOBOS_PC_CFLAGS "-I${inc}") +endforeach(inc) FOREACH(lib ${LIBUSB_LIBRARY_DIRS}) - LIST(APPEND LIBFOBOS_PC_LIBS "-L${lib}") -ENDFOREACH(lib) + list(APPEND LIBFOBOS_PC_LIBS "-L${lib}") +endforeach(lib) # use space-separation format for the pc file -STRING(REPLACE ";" " " LIBFOBOS_PC_CFLAGS "${LIBFOBOS_PC_CFLAGS}") -STRING(REPLACE ";" " " LIBFOBOS_PC_LIBS "${LIBFOBOS_PC_LIBS}") +string(REPLACE ";" " " LIBFOBOS_PC_CFLAGS "${LIBFOBOS_PC_CFLAGS}") +string(REPLACE ";" " " LIBFOBOS_PC_LIBS "${LIBFOBOS_PC_LIBS}") set(prefix "${CMAKE_INSTALL_PREFIX}") set(exec_prefix \${prefix}) set(includedir \${prefix}/include) set(libdir \${exec_prefix}/lib) -CONFIGURE_FILE( +configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/libfobos.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libfobos.pc @ONLY) -INSTALL( +install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libfobos.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig ) @@ -210,18 +240,8 @@ add_custom_target(uninstall add_executable(fobos_devinfo eval/fobos_devinfo_main.c) -target_compile_options(fobos_devinfo PUBLIC -std=c99) - target_link_libraries(fobos_devinfo PRIVATE libfobos) -if(CMAKE_COMPILER_IS_GNUCC AND NOT WIN32) - target_link_libraries(fobos_devinfo PRIVATE ${LIBUSB_LIBRARIES}) -else() - target_link_directories(fobos_devinfo PRIVATE ${LIBUSB_LIBRARIES}) -endif() - -target_include_directories(fobos_devinfo PRIVATE ${CMAKE_SOURCE_DIR}) - target_include_directories(fobos_devinfo PRIVATE ${CMAKE_SOURCE_DIR}/fobos) ######################################################################## @@ -232,16 +252,8 @@ add_executable(fobos_recorder eval/fobos_recorder_main.c wav/wav_file.c) -target_compile_options(fobos_recorder PUBLIC -std=c99) - target_link_libraries(fobos_recorder PRIVATE libfobos) -if(CMAKE_COMPILER_IS_GNUCC AND NOT WIN32) - target_link_libraries(fobos_recorder PRIVATE ${LIBUSB_LIBRARIES}) -else() - target_link_directories(fobos_recorder PRIVATE ${LIBUSB_LIBRARIES}) -endif() - target_include_directories(fobos_recorder PRIVATE ${CMAKE_SOURCE_DIR}) target_include_directories(fobos_recorder PRIVATE ${CMAKE_SOURCE_DIR}/fobos) @@ -253,18 +265,8 @@ target_include_directories(fobos_recorder PRIVATE ${CMAKE_SOURCE_DIR}/fobos) add_executable(fobos_fwloader eval/fobos_fwloader_main.c) -target_compile_options(fobos_fwloader PUBLIC -std=c99) - target_link_libraries(fobos_fwloader PRIVATE libfobos) -if(CMAKE_COMPILER_IS_GNUCC AND NOT WIN32) - target_link_libraries(fobos_fwloader PRIVATE ${LIBUSB_LIBRARIES}) -else() - target_link_directories(fobos_fwloader PRIVATE ${LIBUSB_LIBRARIES}) -endif() - -target_include_directories(fobos_fwloader PRIVATE ${CMAKE_SOURCE_DIR}) - target_include_directories(fobos_fwloader PRIVATE ${CMAKE_SOURCE_DIR}/fobos) ######################################################################## diff --git a/fobos/fobos.c b/fobos/fobos.c index bf1a8d4..57dda45 100644 --- a/fobos/fobos.c +++ b/fobos/fobos.c @@ -22,14 +22,13 @@ #include #include #include "fobos.h" +#include #ifdef _WIN32 -#include #include #include #pragma comment(lib, "libusb-1.0.lib") #define printf_internal _cprintf #else -#include #include #endif #ifndef printf_internal From 8066c19c2c5e80152dcb2ea012b7aaf7607bb9e6 Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Tue, 8 Apr 2025 13:39:17 +0300 Subject: [PATCH 2/4] Add continuous integration workflow using GitHub Actions Build and test the following targets * Linux 64-bit using Clang and GCC * macOS 64-bit for ARM and Intel * Windows 64-bit via MinGW * Windows 32- and 64-bit via MSVC --- .github/workflows/ci.yml | 94 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f5ac155 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,94 @@ +name: CI + +on: [push, pull_request] + +jobs: + build: + name: ${{ matrix.toolchain.name }} + runs-on: ${{ matrix.toolchain.os }} + + defaults: + run: + shell: bash + + strategy: + fail-fast: false + matrix: + toolchain: + - name: Linux Clang + os: ubuntu-24.04 + cmake_args: -DCMAKE_C_COMPILER=clang + + - name: Linux GCC + os: ubuntu-24.04 + + - name: macOS ARM + os: macos-14 + + - name: macOS Intel + os: macos-13 + + - name: MinGW + os: windows-2022 + cmake_args: -G "MinGW Makefiles" + + - name: MSVC 32-bit + os: windows-2022 + cmake_args: -A Win32 + + - name: MSVC 64-bit + os: windows-2022 + + steps: + - uses: actions/checkout@v4 + + - name: Install Linux Dependencies + if: runner.os == 'Linux' + run: | + sudo apt-get update && sudo apt-get install libusb-1.0-0-dev + + - name: Install Windows Dependencies + if: runner.os == 'Windows' + run: | + LIBUSB_VERSION=1.0.28 + LIBUSB_PACKAGE=libusb-$LIBUSB_VERSION.7z + aria2c -q https://github.com/libusb/libusb/releases/download/v$LIBUSB_VERSION/$LIBUSB_PACKAGE + 7z x $LIBUSB_PACKAGE -olibusb + + - name: Install MinGW + if: startsWith(matrix.toolchain.name, 'MinGW') + uses: msys2/setup-msys2@v2 + + - name: Configure + run: | + cmake -B build ${{ matrix.toolchain.cmake_args }} . + + - name: Get Number of CPU Cores + uses: SimenB/github-actions-cpu-cores@v2 + id: cpu-cores + + - name: Build + run: | + export MAKEFLAGS=--keep-going + cmake --build build --config Release --parallel ${{ steps.cpu-cores.outputs.count }} + + - name: Install Linux/macOS + if: runner.os != 'Windows' + run: | + sudo cmake --install build + + - name: Test POSIX + if: ${{ !startsWith(matrix.toolchain.name, 'MSVC') }} + run: | + build/fobos_devinfo + + - name: Test MSVC + if: startsWith(matrix.toolchain.name, 'MSVC') + run: | + build/Release/fobos_devinfo.exe + + - name: List Build Directory + if: always() + run: | + git status + ls -lR build From 85be32288170bd299b2abf1ce56f528f7a7bbf5c Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Tue, 8 Apr 2025 13:43:07 +0300 Subject: [PATCH 3/4] Remove obsolete macOS build script * It produced unusable library because of name mangling, C code was compiled as C++ * It supported ARM only, Homebrew on Intel Macs uses different prefix --- build-mac.sh | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100755 build-mac.sh diff --git a/build-mac.sh b/build-mac.sh deleted file mode 100755 index 479b80d..0000000 --- a/build-mac.sh +++ /dev/null @@ -1,21 +0,0 @@ -mkdir -p build/mac - -g++ -w -fpermissive -Wno-permissive \ - -Ifobos/ \ - -L/opt/homebrew/lib -lusb-1.0 \ - -o build/mac/fobos_devinfo eval/fobos_devinfo_main.c fobos/fobos.c - -g++ -w -fpermissive -Wno-permissive \ - -Ifobos/ \ - -L/opt/homebrew/lib -lusb-1.0 \ - -o build/mac/fobos_fwloader eval/fobos_fwloader_main.c fobos/fobos.c - -g++ -w -fpermissive -Wno-permissive \ - -Ifobos/ -I./ \ - -L/opt/homebrew/lib -lusb-1.0 \ - -o build/mac/fobos_recorder eval/fobos_recorder_main.c wav/wav_file.c fobos/fobos.c - -g++ -w -fpermissive -Wno-permissive -shared -fPIC \ - -L/opt/homebrew/lib -lusb-1.0 \ - -o build/mac/fobos.dylib fobos/fobos.c - From 9dc3c5b7681c1a4eb101d9a596d23f50e836e2e3 Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Tue, 8 Apr 2025 13:43:40 +0300 Subject: [PATCH 4/4] Update macOS section of readme according to recent changes --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index abfc88b..efcb0a7 100644 --- a/README.md +++ b/README.md @@ -57,12 +57,15 @@ cmake --build . --config Release or
open **fobos.sln** in your favorite **MS VisualStudio IDE**, build, debug, trace, evaluate.
-### MacOS (if cmake fails) +### MacOS Make sure you have libusb installed: `brew install libusb` ``` git clone [this repo] cd libfobos -./build-mac.sh +mkdir build +cd build +cmake .. +make ``` Verify the device is recognized: `build/mac/fobos_devinfo`