chore: scaffold CMake build system

Add the CMake build for the Swift + C++ macOS port, mirroring the
refix layout: deps/ modules (Platform, Flags, Sanitizers, FindUnity),
per-module static libraries (usb, auth, mt76, gip, hid, api), a Swift
app entry point with a pure C bridge header, and a Unity test suite
behind BUILD_TESTING.

Swift requires the Ninja or Xcode generator; a guard in
CMakeLists.txt rejects anything else.

Co-Authored-By: qwen (qwen/qwen3.8-27b@q2_k_xl): scaffolded CMake build + tests
This commit is contained in:
portersky
2026-08-17 14:18:03 +02:00
parent 78f042ab84
commit a7d7d0f253
25 changed files with 742 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
# ==============================================================================
# Find Unity
# ==============================================================================
# This module fetches the Unity unit testing framework.
#
# Targets provided:
# Unity::Unity - The Unity library target
#
# Variables set:
# Unity_FOUND - TRUE if Unity is available
# Unity_LIBRARIES - The Unity library target (Unity::Unity)
# Unity_INCLUDE_DIR - Include directories for Unity
# Unity_VERSION - Version of Unity (if available)
# ==============================================================================
if (DEFINED _FINDUNITY_INCLUDED)
return()
endif()
set(_FINDUNITY_INCLUDED TRUE)
if (DEFINED Unity_FIND_VERSION AND NOT Unity_FIND_VERSION STREQUAL "")
set(UNITY_VERSION "${Unity_FIND_VERSION}")
else()
set(UNITY_VERSION "2.6.1")
endif()
message(STATUS "Fetching Unity ${UNITY_VERSION}")
include(FetchContent)
FetchContent_Declare(
unity
URL https://github.com/ThrowTheSwitch/Unity/archive/refs/tags/v${UNITY_VERSION}.zip
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)
FetchContent_MakeAvailable(unity)
# Unity sets INTERFACE_SYSTEM_INCLUDE_DIRECTORIES to a path inside the build
# tree, which CMake rejects on newer versions. The path stays in
# INTERFACE_INCLUDE_DIRECTORIES so headers are still found.
if (TARGET unity)
set_target_properties(unity PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "")
endif()
if (NOT TARGET Unity::Unity)
if (TARGET unity)
add_library(Unity::Unity ALIAS unity)
else()
message(FATAL_ERROR "Could not fetch Unity; no target unity or Unity::Unity available")
endif()
endif()
set(Unity_FOUND TRUE)
set(Unity_LIBRARIES Unity::Unity)
set(Unity_VERSION "${UNITY_VERSION}")
set(Unity_INCLUDE_DIR "${unity_SOURCE_DIR}/src")
if (TARGET unity)
target_compile_definitions(unity PUBLIC
UNITY_OUTPUT_COLOR
UNITY_INCLUDE_PRINT_FORMATTED
)
endif()
set(UNITY_LICENSE_FILE "${unity_SOURCE_DIR}/LICENSE.txt" CACHE FILEPATH "Path to Unity license file")
+82
View File
@@ -0,0 +1,82 @@
# ==============================================================================
# Compiler and Linker Flags
# ==============================================================================
# This module sets platform-specific and compiler-specific flags, libraries,
# and definitions used throughout the build system.
#
# Variables set:
# BASE_LIBRARIES - Libraries to link based on target platform
# BASE_DEFINITIONS - Preprocessor definitions based on target platform
# BASE_OPTIONS - Compiler warning flags (C/C++ only, not applied to Swift)
#
# Requires: Platform.cmake must be included first (for IS_* variables)
# ==============================================================================
# ------------------------------------------------------------------------------
# Framework Detection Helper
# ------------------------------------------------------------------------------
# find_and_link_framework(<OUT_VAR> <FRAMEWORK_NAME> <DISPLAY_NAME>)
#
# Searches for an Apple framework and, if found, appends -framework <name> to
# the output list.
#
# Parameters:
# - OUT_VAR Name of the list variable to append the flag to
# - FRAMEWORK_NAME The framework name to search for (e.g. "IOKit")
# - DISPLAY_NAME Human-readable name for status messages
function(find_and_link_framework OUT_VAR FRAMEWORK_NAME DISPLAY_NAME)
find_library(_FOUND_LIB ${FRAMEWORK_NAME})
if (_FOUND_LIB)
message(STATUS " [x] ${DISPLAY_NAME} (-framework ${FRAMEWORK_NAME})")
list(APPEND ${OUT_VAR} "-framework ${FRAMEWORK_NAME}")
set(${OUT_VAR} "${${OUT_VAR}}" PARENT_SCOPE)
else()
message(STATUS " [ ] ${DISPLAY_NAME} (-framework ${FRAMEWORK_NAME})")
endif()
unset(_FOUND_LIB CACHE)
endfunction()
# ------------------------------------------------------------------------------
# Platform-Specific Link Libraries
# ------------------------------------------------------------------------------
# These frameworks are required for USB access, crypto and (later) audio.
# They are linked into the final executable; static libraries reference the
# symbols and resolution happens at final link time.
set(BASE_LIBRARIES "")
if (IS_MACOS)
message(STATUS "macOS link libraries:")
find_and_link_framework(BASE_LIBRARIES "IOKit" "IOKit (USB transport)")
find_and_link_framework(BASE_LIBRARIES "CoreFoundation" "Core Foundation")
find_and_link_framework(BASE_LIBRARIES "Security" "Security (crypto/ECDH)")
elseif (IS_IOS)
message(FATAL_ERROR "iOS is not supported yet!")
endif()
# ------------------------------------------------------------------------------
# Platform-Specific Definitions
# ------------------------------------------------------------------------------
set(BASE_DEFINITIONS "")
# ------------------------------------------------------------------------------
# Compiler Warning Flags
# ------------------------------------------------------------------------------
# These are stored in BASE_OPTIONS and applied per-target via
# target_compile_options() to avoid polluting external dependencies.
# NOTE: Applied to C/C++ targets only; swiftc does not share these flags.
set(BASE_OPTIONS "")
if (IS_CLANG_OR_GCC)
set(BASE_OPTIONS
"-Wall" # Enable all common warnings
"-Wextra" # Enable extra warnings
"-Werror" # Treat warnings as errors
)
elseif (IS_MSVC)
set(BASE_OPTIONS
"/W4" # Warning level 4 (high)
"/WX" # Treat warnings as errors
"/utf-8" # Set source and execution character set to UTF-8
"/Zc:__cplusplus" # Report correct C++ standard version in __cplusplus
)
endif()
+54
View File
@@ -0,0 +1,54 @@
# ==============================================================================
# Platform Detection
# ==============================================================================
# This module detects the current platform and compiler, setting IS_* variables
# that can be used throughout the build system for conditional logic.
#
# Compiler flags set:
# IS_CLANG_OR_GCC - TRUE if using Clang or GCC compiler
# IS_MSVC - TRUE if using Microsoft Visual C++ compiler
#
# Platform flags set:
# IS_WINDOWS - TRUE if building for Windows
# IS_LINUX - TRUE if building for Linux
# IS_MACOS - TRUE if building for macOS
# IS_IOS - TRUE if building for iOS
# ==============================================================================
# ------------------------------------------------------------------------------
# Compiler Detection
# ------------------------------------------------------------------------------
set(IS_CLANG_OR_GCC FALSE)
set(IS_MSVC FALSE)
if(MSVC)
set(IS_MSVC TRUE)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
set(IS_CLANG_OR_GCC TRUE)
endif()
# ------------------------------------------------------------------------------
# Platform Detection
# ------------------------------------------------------------------------------
set(IS_WINDOWS FALSE)
set(IS_LINUX FALSE)
set(IS_MACOS FALSE)
set(IS_IOS FALSE)
if(ANDROID)
message(FATAL_ERROR "Android is not supported by xone_macos!")
elseif(APPLE)
if(IOS)
message(STATUS "Platform: iOS")
set(IS_IOS TRUE)
else()
message(STATUS "Platform: macOS")
set(IS_MACOS TRUE)
endif()
elseif(WIN32)
message(FATAL_ERROR "xone_macos only builds on macOS!")
elseif(UNIX)
message(FATAL_ERROR "xone_macos only builds on macOS!")
else()
message(FATAL_ERROR "Unknown platform!")
endif()
+28
View File
@@ -0,0 +1,28 @@
# ==============================================================================
# Sanitizers
# ==============================================================================
# AddressSanitizer (ASan) support.
# Works with GCC/Clang on Linux and macOS, Clang on Windows (requires
# compiler-rt with sanitizers), and MSVC on Windows (/fsanitize=address).
#
# Usage: cmake -DENABLE_ASAN=ON ...
# ==============================================================================
option(ENABLE_ASAN "Build with AddressSanitizer" OFF)
if (ENABLE_ASAN)
if (ENABLE_COVERAGE)
message(FATAL_ERROR "ENABLE_ASAN and ENABLE_COVERAGE cannot be used together")
endif()
if (MSVC)
add_compile_options(/fsanitize=address)
message(STATUS "ASan: enabled (MSVC)")
elseif (CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
add_link_options(-fsanitize=address)
message(STATUS "ASan: enabled (${CMAKE_C_COMPILER_ID})")
else()
message(FATAL_ERROR "ENABLE_ASAN: unsupported compiler ${CMAKE_C_COMPILER_ID}")
endif()
endif()