Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include/cpp_core/version.h
build/
.cache/

compile_commands.json
111 changes: 79 additions & 32 deletions CMakeLists.txt
Comment thread
Mqxx marked this conversation as resolved.
Comment thread
Mqxx marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,64 +1,111 @@
cmake_minimum_required(VERSION 3.14)
cmake_minimum_required(VERSION 3.30)

project(cpp-core
VERSION 0.1.0
DESCRIPTION "Cross-platform helper library shared by cpp-linux-bindings and cpp-windows-bindings"
LANGUAGES CXX)
# Export compile commands to root directory
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# ---------------------------------------------------------------------------
# Version information - can be overridden by parent project
# Version information from Git tags
# Version is determined automatically from Git tags and cannot be overridden
# ---------------------------------------------------------------------------
foreach(_part IN ITEMS MAJOR MINOR PATCH)
if(NOT DEFINED CPP_CORE_VERSION_${_part} OR CPP_CORE_VERSION_${_part} STREQUAL "")
set(CPP_CORE_VERSION_${_part} ${PROJECT_VERSION_${_part}})
endif()
endforeach()
include(${CMAKE_CURRENT_LIST_DIR}/cmake/get_version_from_git.cmake)

# Get version from Git tags
get_version_from_git()
set(CPP_CORE_VERSION_MAJOR ${VERSION_MAJOR} CACHE STRING "Major version number" FORCE)
set(CPP_CORE_VERSION_MINOR ${VERSION_MINOR} CACHE STRING "Minor version number" FORCE)
set(CPP_CORE_VERSION_PATCH ${VERSION_PATCH} CACHE STRING "Patch version number" FORCE)
set(CPP_CORE_VERSION_STRING ${VERSION_STRING} CACHE STRING "Full version string" FORCE)

# Set PROJECT_VERSION for CMake package config (only MAJOR.MINOR.PATCH, no suffix)
set(PROJECT_VERSION "${CPP_CORE_VERSION_MAJOR}.${CPP_CORE_VERSION_MINOR}.${CPP_CORE_VERSION_PATCH}")

project(
cpp-core
VERSION ${PROJECT_VERSION}
DESCRIPTION "Cross-platform helper library shared by cpp-linux-bindings and cpp-windows-bindings"
LANGUAGES CXX
)

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/include/cpp_core/version.h.in
${CMAKE_CURRENT_SOURCE_DIR}/include/cpp_core/version.h
@ONLY)
${CMAKE_CURRENT_SOURCE_DIR}/include/cpp_core/version.h.in
${CMAKE_CURRENT_SOURCE_DIR}/include/cpp_core/version.h
@ONLY
)

# Header-only library --------------------------------------------------------
add_library(cpp_core INTERFACE)
add_library(cpp_core::cpp_core ALIAS cpp_core)

# Public include directories
target_include_directories(cpp_core INTERFACE
target_include_directories(
cpp_core
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

# Set C++ standard
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
Comment thread
Katze719 marked this conversation as resolved.

# Require C++23
target_compile_features(cpp_core INTERFACE cxx_std_23)

# Enable C++23 module support
set(CMAKE_CXX_MODULE_STD 23)
set(CMAKE_CXX_MODULE_EXTENSIONS OFF)
Comment thread
Mqxx marked this conversation as resolved.
Comment thread
Mqxx marked this conversation as resolved.

# Install rules --------------------------------------------------------------
include(GNUInstallDirs)

install(TARGETS cpp_core
EXPORT cpp_coreTargets)
install(
TARGETS cpp_core
EXPORT cpp_coreTargets
)

install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(
DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

# CMake package configuration ------------------------------------------------
include(CMakePackageConfigHelpers)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)

configure_package_config_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/cpp_core_config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core)
${CMAKE_CURRENT_LIST_DIR}/cmake/cpp_core_config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core
)

install(EXPORT cpp_coreTargets
FILE cpp_coreTargets.cmake
NAMESPACE cpp_core::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core)
install(
EXPORT cpp_coreTargets
FILE cpp_coreTargets.cmake
NAMESPACE cpp_core::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core
)

install(FILES
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core)
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core
)

if(CMAKE_EXPORT_COMPILE_COMMANDS AND EXISTS "${CMAKE_BINARY_DIR}/compile_commands.json")
add_custom_target(
copy-compile-commands
ALL
${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/compile_commands.json
${CMAKE_SOURCE_DIR}/compile_commands.json
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json
COMMENT "Copying compile_commands.json to project root"
)
endif()
82 changes: 82 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 30,
"patch": 0
},
"configurePresets": [
{
"name": "gcc",
"displayName": "GCC (g++)",
"description": "Build using GCC compiler",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/gcc",
"toolchainFile": "${sourceDir}/cmake/gcc-toolchain.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "clang",
"displayName": "Clang (clang++)",
"description": "Build using Clang compiler",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/clang",
"toolchainFile": "${sourceDir}/cmake/clang-toolchain.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "msvc",
"displayName": "MSVC",
"description": "Build using MSVC compiler",
"generator": "Visual Studio 17 2022",
"binaryDir": "${sourceDir}/build/msvc",
"toolchainFile": "${sourceDir}/cmake/msvc-toolchain.cmake",
"architecture": {
"value": "x64",
"strategy": "external"
}
}
],
"buildPresets": [
{
"name": "gcc-debug",
"displayName": "GCC Debug",
"configurePreset": "gcc",
"configuration": "Debug"
},
{
"name": "gcc-release",
"displayName": "GCC Release",
"configurePreset": "gcc",
"configuration": "Release"
},
{
"name": "clang-debug",
"displayName": "Clang Debug",
"configurePreset": "clang",
"configuration": "Debug"
},
{
"name": "clang-release",
"displayName": "Clang Release",
"configurePreset": "clang",
"configuration": "Release"
},
{
"name": "msvc-debug",
"displayName": "MSVC Debug",
"configurePreset": "msvc",
"configuration": "Debug"
},
{
"name": "msvc-release",
"displayName": "MSVC Release",
"configurePreset": "msvc",
"configuration": "Release"
}
]
}
97 changes: 46 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,76 @@
# C++ Core

Header-only C++ helper library that provides small, cross-platform utilities and a centrally maintained **Version** struct shared by the *cpp-bindings-linux*, *cpp-bindings-windows* and *cpp-bindings-macos* repositories.
Header-only API definition library for cross-platform serial communication. Defines the **API contract** and **version information** shared by all platform-specific binding implementations.

> [!NOTE]
> [!IMPORTANT]
>
> This repository contains **headers only**. To obtain a working native library (SO / DLL / dylib) build one of the platform-specific projects instead:
> - [C++ bindings (Windows)](https://github.com/Serial-IO/cpp-bindings-windows)
> - [C++ bindings (Linux)](https://github.com/Serial-IO/cpp-bindings-linux)
>
> These repositories compile *cpp-core* for you and provide the ready-to-use shared library.
> **API definitions only** (headers). For implementations and ready-to-use shared libraries:
> - [cpp-bindings-windows](https://github.com/Serial-IO/cpp-bindings-windows) - Windows (DLL)
> - [cpp-bindings-linux](https://github.com/Serial-IO/cpp-bindings-linux) - Linux (SO)
> - [cpp-bindings-macos](https://github.com/Serial-IO/cpp-bindings-macos) - macOS (dylib)

## Documentation
## Features

* [Overview](docs/overview.md): Architecture, Build & Quick Start.
* C++23, zero runtime dependencies
* Delivered as an INTERFACE target `cpp_core::cpp_core`
* Fetchable via [CPM.cmake](https://github.com/cpm-cmake/CPM.cmake) or regular `find_package`
* **C++23** header-only API definitions
* **Cross-platform serial I/O API** (POSIX/Windows compatible)
* **Centralized version information** from Git tags (`cpp_core::kVersion`)
* **Status codes** enum for error handling
* **C-compatible API** for FFI bindings (Rust, Python, Deno, etc.)

## Requirements

* CMake ≥ 3.14
* CMake ≥ 3.30
* A C++23 compatible compiler (GCC 13+, Clang 16+, MSVC 2022+)
* Git (for automatic version detection)

## Quick Start
## Version Information

1. Add *cpp-core* with CPM.cmake (recommended way)
The version is **automatically extracted from Git tags** during CMake configuration and cannot be overridden:

```cmake
# Set the project version once via **cache variables** –
# ensures the values end up in cpp-core regardless of when CPM configures.
set(CPP_CORE_VERSION_MAJOR 1 CACHE STRING "")
set(CPP_CORE_VERSION_MINOR 0 CACHE STRING "")
set(CPP_CORE_VERSION_PATCH 3 CACHE STRING "")
* **With Git tag** (e.g., `v1.2.3`): Version is `1.2.3` (or `1.2.3-dirty` if working tree has uncommitted changes)
* **Without Git tag**: Version is `0.0.0` (or `0.0.0-dirty` if working tree has uncommitted changes)

CPMAddPackage(
NAME cpp_core
GITHUB_REPOSITORY Serial-IO/cpp-core # Fork / Upstream
GIT_TAG main # or e.g. v0.1.0
)

add_executable(my_app src/main.cpp)
target_link_libraries(my_app PRIVATE cpp_core::cpp_core)
```

> Why cache variables?
> They guarantee the values exist **before** the `CPMAddPackage()` call. If the variables are set later (or via
> `OPTIONS ... "CPP_CORE_VERSION_MAJOR=${FOO}"`) and `FOO` is empty at that moment, *cpp-core* falls back to its default version (0.1.0).

2. Use in code
All binding repositories that include this repository will use the same version information, ensuring consistency across platforms.

```cpp
#include <cpp_core/version.h>
#include <cpp_core/helpers.h>

int main() {
constexpr auto v = cpp_core::VERSION; // {1,4,0}
}
constexpr auto v = cpp_core::kVersion; // v.major, v.minor, v.patch
```

## Alternative: add_subdirectory
## Usage in Binding Repositories

If you include the source code directly as a sub-repository, simply do:
Binding repositories typically include this repository as a dependency:

```cmake
add_subdirectory(externals/cpp-core)
# the same version variables can be set before the call
CPMAddPackage(
NAME cpp_core
GITHUB_REPOSITORY Serial-IO/cpp-core
GIT_TAG v1.2.3 # Version tag determines the API version
)

target_link_libraries(my_binding_library PRIVATE cpp_core::cpp_core)
```

## Using the package with `find_package`
The binding implementation then uses the API definitions:

After running `cmake --install` (or `make install`) you can locate the package system-wide:
```cpp
#include <cpp_core/serial.h>
#include <cpp_core/version.h>

```cmake
find_package(cpp_core REQUIRED)
add_executable(my_app src/main.cpp)
target_link_libraries(my_app PRIVATE cpp_core::cpp_core)
// Implement platform-specific functions matching the API
intptr_t serialOpen(
void *port,
int baudrate,
int data_bits,
int parity,
int stop_bits,
ErrorCallbackT error_callback
) {
// Platform-specific implementation (Windows/Linux/macOS)
}
```

## License
`Apache-2.0` Check [LICENSE](https://github.com/Serial-IO/cpp-core/blob/main/LICENSE) for more details.

`Apache-2.0` - Check [LICENSE](LICENSE) for more details.
12 changes: 12 additions & 0 deletions cmake/clang-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Clang Toolchain file for cpp-core
# Usage: cmake -DCMAKE_TOOLCHAIN_FILE=cmake/clang-toolchain.cmake ..

# Set the C++ compiler
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_C_COMPILER "clang")

# Compiler-specific flags
set(CMAKE_CXX_FLAGS_INIT "-Wall -Wextra -Wpedantic")
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -DNDEBUG")

6 changes: 6 additions & 0 deletions cmake/cpp_core_config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

include("${CMAKE_CURRENT_LIST_DIR}/cpp_coreTargets.cmake")

# Set version variables for parent projects
set(CPP_CORE_VERSION_MAJOR @CPP_CORE_VERSION_MAJOR@)
set(CPP_CORE_VERSION_MINOR @CPP_CORE_VERSION_MINOR@)
set(CPP_CORE_VERSION_PATCH @CPP_CORE_VERSION_PATCH@)
set(CPP_CORE_VERSION_STRING "@CPP_CORE_VERSION_STRING@")

check_required_components(cpp_core)
11 changes: 11 additions & 0 deletions cmake/gcc-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# GCC Toolchain file for cpp-core
# Usage: cmake -DCMAKE_TOOLCHAIN_FILE=cmake/gcc-toolchain.cmake ..

# Set the C++ compiler
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_C_COMPILER "gcc")

# Compiler-specific flags
set(CMAKE_CXX_FLAGS_INIT "-Wall -Wextra -Wpedantic")
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -DNDEBUG")
Loading