From 9b3a9624b84a5f68b9798462189b5aed765349a6 Mon Sep 17 00:00:00 2001 From: mcuee Date: Wed, 27 May 2026 21:33:29 +0800 Subject: [PATCH 1/4] Cmake msvc static only for avrdude-libserialport (#7) * Create CMakeLists.txt for MSVC static build * Create msvc-build.yml --- .github/workflows/msvc-build.yml | 32 ++++++++++++++++++++++++++++++++ CMakeLists.txt | 31 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 .github/workflows/msvc-build.yml create mode 100644 CMakeLists.txt diff --git a/.github/workflows/msvc-build.yml b/.github/workflows/msvc-build.yml new file mode 100644 index 0000000..429176b --- /dev/null +++ b/.github/workflows/msvc-build.yml @@ -0,0 +1,32 @@ +name: MSVC Static Build + +on: [push, pull_request] + +jobs: + build: + runs-on: windows-latest + strategy: + matrix: + include: + - { arch: x86, platform: Win32 } + - { arch: x64, platform: x64 } + - { arch: arm64, platform: ARM64 } + steps: + - uses: actions/checkout@v6 + + - name: Configure + run: >- + cmake + -A ${{ matrix.platform }} + -DBUILD_SHARED_LIBS=OFF + -DLIBSERIALPORT_BUILD_EXAMPLES=OFF + -B build + + - name: Build + run: cmake --build build --config Release + + - name: Archive + uses: actions/upload-artifact@v7 + with: + name: libserialport-${{ matrix.arch }} + path: build/Release/*.lib # Adjust path based on your output diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7d04ac8 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 3.12) +project(libserialport C) + +add_compile_definitions( + _CRT_SECURE_NO_WARNINGS=1 + SP_PRIV= + SP_API= + LIBSERIALPORT_MSBUILD=1 +) + +# List your sources +set(SERIALPORT_SOURCES + serialport.c + timing.c + # Add your OS-specific files here (e.g., windows.c) +) + +# Build as a static library +add_library(serialport STATIC ${SERIALPORT_SOURCES}) + +# Define public include directory +target_include_directories(serialport PUBLIC include) + +# MSVC specific compiler options for a clean build +if(MSVC) + target_compile_options(serialport PRIVATE + /W4 # High warning level + /wd4996 # Suppress deprecation warnings + /wd5105 # Suppress macro expansion warnings + ) +endif() From f8a83a7d9bd4254072399c13515c063db0732046 Mon Sep 17 00:00:00 2001 From: mcuee Date: Sat, 30 May 2026 09:10:50 +0800 Subject: [PATCH 2/4] Enhance CMake configuration for cross-platform support Refactor CMakeLists.txt for platform-specific configurations and visibility definitions. --- CMakeLists.txt | 70 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d04ac8..359c2e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,31 +1,63 @@ cmake_minimum_required(VERSION 3.12) project(libserialport C) -add_compile_definitions( - _CRT_SECURE_NO_WARNINGS=1 - SP_PRIV= - SP_API= - LIBSERIALPORT_MSBUILD=1 -) +# --- 1. Autotools Compatibility & Visibility Definitions --- +if(NOT WIN32) + # Generate an empty config.h in the binary directory to satisfy #include + if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/config.h") + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/config.h" "/* Generated by CMake */\n") + endif() + include_directories(${CMAKE_CURRENT_BINARY_DIR}) +endif() -# List your sources +# Gather common implementation source files set(SERIALPORT_SOURCES serialport.c timing.c - # Add your OS-specific files here (e.g., windows.c) ) -# Build as a static library -add_library(serialport STATIC ${SERIALPORT_SOURCES}) +# Platform-specific backend mapping +if(WIN32) + list(APPEND SERIALPORT_SOURCES windows.c) + set(WINDOWS_LIBS setupapi cfgmgr32) +elseif(APPLE) + list(APPEND SERIALPORT_SOURCES macosx.c) + find_library(FRAMEWORK_IOKIT IOKit REQUIRED) + find_library(FRAMEWORK_COREFOUNDATION CoreFoundation REQUIRED) + set(APPLE_LIBS ${FRAMEWORK_IOKIT} ${FRAMEWORK_COREFOUNDATION}) +else() + list(APPEND SERIALPORT_SOURCES linux.c) +endif() + +# --- 2. Core Library Target --- +add_library(serialport ${SERIALPORT_SOURCES}) -# Define public include directory -target_include_directories(serialport PUBLIC include) +# Use root directory for headers, set as SYSTEM so works everywhere +target_include_directories(serialport SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +# Ensure internal visibility macro SP_PRIV is defined across all platforms +target_compile_definitions(serialport PUBLIC SP_PRIV=) + +if(WIN32) + target_link_libraries(serialport PRIVATE ${WINDOWS_LIBS}) + + if(BUILD_SHARED_LIBS) + # Shared Build: Library defines MSBUILD privately so it exports symbols, + # but examples do NOT inherit it so they use dllimport. + target_compile_definitions(serialport PRIVATE LIBSERIALPORT_MSBUILD=1) + else() + # Static Build: Both the library AND the examples MUST see these definitions PUBLICLY. + # This prevents the header from falling back to __declspec(dllimport). + target_compile_definitions(serialport PUBLIC LIBSERIALPORT_MSBUILD=1 SP_API=) + endif() + +elseif(APPLE) + target_link_libraries(serialport PRIVATE ${APPLE_LIBS}) +endif() -# MSVC specific compiler options for a clean build -if(MSVC) - target_compile_options(serialport PRIVATE - /W4 # High warning level - /wd4996 # Suppress deprecation warnings - /wd5105 # Suppress macro expansion warnings - ) +# --- 3. Examples Build Configuration --- +if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/examples") + add_executable(list_ports examples/list_ports.c) + target_link_libraries(list_ports PRIVATE serialport) + target_include_directories(list_ports PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) endif() From 07add9a2c12e12cc9a65b9cf406c03c46ee642e6 Mon Sep 17 00:00:00 2001 From: mcuee Date: Sat, 30 May 2026 09:11:38 +0800 Subject: [PATCH 3/4] Update msvc-build.yml --- .github/workflows/msvc-build.yml | 40 +++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/.github/workflows/msvc-build.yml b/.github/workflows/msvc-build.yml index 429176b..62c156e 100644 --- a/.github/workflows/msvc-build.yml +++ b/.github/workflows/msvc-build.yml @@ -1,32 +1,44 @@ -name: MSVC Static Build +name: MSVC Build All Archs -on: [push, pull_request] +on: + push: + branches: ['**'] + pull_request: + branches: ['**'] jobs: build: + name: Build ${{ matrix.arch }} runs-on: windows-latest strategy: + fail-fast: false matrix: include: - { arch: x86, platform: Win32 } - { arch: x64, platform: x64 } - { arch: arm64, platform: ARM64 } + steps: - - uses: actions/checkout@v6 + - name: Checkout Code + uses: actions/checkout@v6 - - name: Configure - run: >- - cmake - -A ${{ matrix.platform }} - -DBUILD_SHARED_LIBS=OFF - -DLIBSERIALPORT_BUILD_EXAMPLES=OFF - -B build + - name: Build Shared Library + run: | + cmake -B build-shared -A ${{ matrix.platform }} -DBUILD_SHARED_LIBS=ON + cmake --build build-shared --config Release - - name: Build - run: cmake --build build --config Release + - name: Build Static Library + run: | + cmake -B build-static -A ${{ matrix.platform }} -DBUILD_SHARED_LIBS=OFF + cmake --build build-static --config Release - - name: Archive + - name: Archive Artifacts uses: actions/upload-artifact@v7 with: name: libserialport-${{ matrix.arch }} - path: build/Release/*.lib # Adjust path based on your output + path: | + build-shared/Release/serialport.dll + build-shared/Release/serialport.lib + build-shared/Release/list_ports.exe + build-static/Release/serialport.lib + build-static/Release/list_ports.exe From cf30cc997935b799fb759dc504ab03c7be37eb53 Mon Sep 17 00:00:00 2001 From: mcuee Date: Sat, 30 May 2026 09:12:23 +0800 Subject: [PATCH 4/4] Add cross-platform CI workflow for building libraries --- .github/workflows/build.yml | 151 ++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..6468909 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,151 @@ +name: Cross-Platform Build CI + +on: + push: + branches: + - '**' # Matches every branch push + pull_request: + branches: + - '**' # Matches every pull request target branch + workflow_dispatch: # Allows manual trigger from the GitHub Actions tab + +jobs: + # --------------------------------------------------------------------------- + # Linux Builds (Ubuntu) + # --------------------------------------------------------------------------- + linux: + name: Linux (Ubuntu) + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v6 + + - name: Install Dependencies + run: | + sudo apt-get update + sudo apt-get install -y libudev-dev ninja-build + + - name: Build Shared Library + run: | + cmake -G Ninja -B build-shared \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=ON \ + -DLIBSERIALPORT_BUILD_EXAMPLES=ON \ + -DCMAKE_INSTALL_PREFIX=stage/shared + cmake --build build-shared + cmake --install build-shared + + - name: Build Static Library + run: | + cmake -G Ninja -B build-static \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=OFF \ + -DLIBSERIALPORT_BUILD_EXAMPLES=OFF \ + -DCMAKE_INSTALL_PREFIX=stage/static + cmake --build build-static + cmake --install build-static + + - name: Upload Linux Artifacts + uses: actions/upload-artifact@v7 + with: + name: libserialport-linux-x86_64 + path: stage/ + + # --------------------------------------------------------------------------- + # macOS Builds (Apple Silicon & Intel via Universal Binary layout) + # --------------------------------------------------------------------------- + macos: + name: macOS (Apple) + runs-on: macos-latest + steps: + - name: Checkout Code + uses: actions/checkout@v6 + + - name: Build Shared Library + run: | + cmake -G Ninja -B build-shared \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=ON \ + -DLIBSERIALPORT_BUILD_EXAMPLES=ON \ + -DCMAKE_INSTALL_PREFIX=stage/shared + cmake --build build-shared + cmake --install build-shared + + - name: Build Static Library + run: | + cmake -G Ninja -B build-static \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=OFF \ + -DLIBSERIALPORT_BUILD_EXAMPLES=OFF \ + -DCMAKE_INSTALL_PREFIX=stage/static + cmake --build build-static + cmake --install build-static + + - name: Upload macOS Artifacts + uses: actions/upload-artifact@v7 + with: + name: libserialport-macos-universal + path: stage/ + + # --------------------------------------------------------------------------- + # Windows Builds (MSYS2 Environments) + # --------------------------------------------------------------------------- + windows-msys2: + name: Windows (${{ matrix.msys_env }}) + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + include: + - msys_env: mingw64 + sys_prefix: mingw-w64-x86_64 + - msys_env: ucrt64 + sys_prefix: mingw-w64-ucrt-x86_64 + - msys_env: clang64 + sys_prefix: mingw-w64-clang-x86_64 + + defaults: + run: + shell: msys2 {0} + + steps: + - name: Checkout Code + uses: actions/checkout@v6 + + - name: Setup MSYS2 Subsystem + uses: msys2/setup-msys2@v2 + with: + msystem: ${{ matrix.msys_env }} + update: true + install: >- + git + make + ${{ matrix.sys_prefix }}-cc + ${{ matrix.sys_prefix }}-cmake + ${{ matrix.sys_prefix }}-ninja + + - name: Build Shared Library + run: | + cmake -G Ninja -B build-shared \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=ON \ + -DLIBSERIALPORT_BUILD_EXAMPLES=ON \ + -DCMAKE_INSTALL_PREFIX=stage/shared + cmake --build build-shared + cmake --install build-shared + + - name: Build Static Library + run: | + cmake -G Ninja -B build-static \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=OFF \ + -DLIBSERIALPORT_BUILD_EXAMPLES=OFF \ + -DCMAKE_INSTALL_PREFIX=stage/static + cmake --build build-static + cmake --install build-static + + - name: Upload Windows Artifacts + uses: actions/upload-artifact@v7 + with: + name: libserialport-windows-${{ matrix.msys_env }} + path: stage/