Skip to content

Commit 3ff7894

Browse files
Katze719CopilotMqxx
authored
Revamp project (#4)
* Remove sequential interface headers and related functionality - Deleted multiple sequential interface headers to streamline the codebase and eliminate unused functionality. - Updated `cpp_core.h` to remove references to the deleted headers, enhancing organization and maintainability. These changes are part of ongoing refactoring efforts to improve header management and code clarity. * Add CMake presets and toolchain files for GCC, Clang, and MSVC - Introduced `CMakePresets.json` to define build configurations for GCC, Clang, and MSVC compilers. - Added toolchain files for GCC (`gcc-toolchain.cmake`), Clang (`clang-toolchain.cmake`), and MSVC (`msvc-toolchain.cmake`) to specify compiler settings and flags. - Each toolchain file sets the C++ standard to C++23 and includes appropriate compiler flags for different build types (Debug and Release). These additions enhance the build system's flexibility and support for multiple compilers, facilitating easier project setup and configuration. * Update CMake configuration and add module support - Increased minimum CMake version requirement to 3.30. - Enabled export of compile commands and added support for C++23 modules in CMake configuration. - Introduced a new static library `hello_module` with a simple module implementation and a corresponding test executable. - Updated `.gitignore` to include `compile_commands.json` for better build management. These changes enhance the build system's capabilities and introduce modular programming support. * Remove unnecessary processor setting from toolchain files - Deleted the line setting CMAKE_SYSTEM_PROCESSOR in the GCC, Clang, and MSVC toolchain files to simplify configuration. - This change streamlines the toolchain setup without affecting the build process. These modifications enhance the clarity and maintainability of the CMake configuration. * Refactor CMake configuration to automate versioning and enhance toolchain files - Introduced a new script `get_version_from_git.cmake` to automatically determine versioning from Git tags, replacing manual version settings. - Updated `CMakeLists.txt` to utilize the new versioning system and removed redundant version definitions. - Simplified toolchain files for GCC, Clang, and MSVC by removing unnecessary settings and ensuring consistent C++ standard enforcement. - Updated `cpp_core_config.cmake.in` to set version variables for parent projects. These changes improve the maintainability and clarity of the CMake configuration while ensuring accurate versioning based on Git history. * Revise README.md for clarity and detail on library features - Updated the description to reflect the library's purpose as a header-only API definition for cross-platform serial communication. - Enhanced the features section to highlight key functionalities, including C++23 compatibility and centralized version information. - Improved instructions for obtaining platform-specific bindings and clarified versioning details. - Removed the outdated overview documentation file to streamline the repository. These changes aim to provide clearer guidance and improve the overall presentation of the library's capabilities. * Add serial port enumeration functionality - Introduced a new header file `serial_list_ports.h` to provide a function for enumerating available serial ports on the system. - Updated `serial.h` to include the new `serial_list_ports.h` header, enhancing the library's capabilities for serial communication. These changes expand the API to facilitate easier access to serial port information. * Update cmake/get_version_from_git.cmake Co-authored-by: Copilot <[email protected]> * Update CMakeLists.txt Co-authored-by: Copilot <[email protected]> * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> * Apply suggestions from code review Co-authored-by: Mqx <[email protected]> * fix: format --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Mqx <[email protected]>
1 parent a86472f commit 3ff7894

38 files changed

+339
-895
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
include/cpp_core/version.h
22
build/
33
.cache/
4-
4+
compile_commands.json

CMakeLists.txt

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,111 @@
1-
cmake_minimum_required(VERSION 3.14)
1+
cmake_minimum_required(VERSION 3.30)
22

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

86
# ---------------------------------------------------------------------------
9-
# Version information - can be overridden by parent project
7+
# Version information from Git tags
8+
# Version is determined automatically from Git tags and cannot be overridden
109
# ---------------------------------------------------------------------------
11-
foreach(_part IN ITEMS MAJOR MINOR PATCH)
12-
if(NOT DEFINED CPP_CORE_VERSION_${_part} OR CPP_CORE_VERSION_${_part} STREQUAL "")
13-
set(CPP_CORE_VERSION_${_part} ${PROJECT_VERSION_${_part}})
14-
endif()
15-
endforeach()
10+
include(${CMAKE_CURRENT_LIST_DIR}/cmake/get_version_from_git.cmake)
11+
12+
# Get version from Git tags
13+
get_version_from_git()
14+
set(CPP_CORE_VERSION_MAJOR ${VERSION_MAJOR} CACHE STRING "Major version number" FORCE)
15+
set(CPP_CORE_VERSION_MINOR ${VERSION_MINOR} CACHE STRING "Minor version number" FORCE)
16+
set(CPP_CORE_VERSION_PATCH ${VERSION_PATCH} CACHE STRING "Patch version number" FORCE)
17+
set(CPP_CORE_VERSION_STRING ${VERSION_STRING} CACHE STRING "Full version string" FORCE)
18+
19+
# Set PROJECT_VERSION for CMake package config (only MAJOR.MINOR.PATCH, no suffix)
20+
set(PROJECT_VERSION "${CPP_CORE_VERSION_MAJOR}.${CPP_CORE_VERSION_MINOR}.${CPP_CORE_VERSION_PATCH}")
21+
22+
project(
23+
cpp-core
24+
VERSION ${PROJECT_VERSION}
25+
DESCRIPTION "Cross-platform helper library shared by cpp-linux-bindings and cpp-windows-bindings"
26+
LANGUAGES CXX
27+
)
1628

1729
configure_file(
18-
${CMAKE_CURRENT_SOURCE_DIR}/include/cpp_core/version.h.in
19-
${CMAKE_CURRENT_SOURCE_DIR}/include/cpp_core/version.h
20-
@ONLY)
30+
${CMAKE_CURRENT_SOURCE_DIR}/include/cpp_core/version.h.in
31+
${CMAKE_CURRENT_SOURCE_DIR}/include/cpp_core/version.h
32+
@ONLY
33+
)
2134

2235
# Header-only library --------------------------------------------------------
2336
add_library(cpp_core INTERFACE)
2437
add_library(cpp_core::cpp_core ALIAS cpp_core)
2538

2639
# Public include directories
27-
target_include_directories(cpp_core INTERFACE
40+
target_include_directories(
41+
cpp_core
42+
INTERFACE
2843
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
29-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
44+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
45+
)
46+
47+
# Set C++ standard
48+
set(CMAKE_CXX_STANDARD 23)
49+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
50+
set(CMAKE_CXX_EXTENSIONS OFF)
3051

3152
# Require C++23
3253
target_compile_features(cpp_core INTERFACE cxx_std_23)
3354

55+
# Enable C++23 module support
56+
set(CMAKE_CXX_MODULE_STD 23)
57+
set(CMAKE_CXX_MODULE_EXTENSIONS OFF)
58+
3459
# Install rules --------------------------------------------------------------
3560
include(GNUInstallDirs)
3661

37-
install(TARGETS cpp_core
38-
EXPORT cpp_coreTargets)
62+
install(
63+
TARGETS cpp_core
64+
EXPORT cpp_coreTargets
65+
)
3966

40-
install(DIRECTORY include/
41-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
67+
install(
68+
DIRECTORY include/
69+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
70+
)
4271

4372
# CMake package configuration ------------------------------------------------
4473
include(CMakePackageConfigHelpers)
4574

4675
write_basic_package_version_file(
47-
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfigVersion.cmake
48-
VERSION ${PROJECT_VERSION}
49-
COMPATIBILITY SameMajorVersion)
76+
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfigVersion.cmake
77+
VERSION ${PROJECT_VERSION}
78+
COMPATIBILITY SameMajorVersion
79+
)
5080

5181
configure_package_config_file(
52-
${CMAKE_CURRENT_LIST_DIR}/cmake/cpp_core_config.cmake.in
53-
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfig.cmake
54-
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core)
82+
${CMAKE_CURRENT_LIST_DIR}/cmake/cpp_core_config.cmake.in
83+
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfig.cmake
84+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core
85+
)
5586

56-
install(EXPORT cpp_coreTargets
57-
FILE cpp_coreTargets.cmake
58-
NAMESPACE cpp_core::
59-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core)
87+
install(
88+
EXPORT cpp_coreTargets
89+
FILE cpp_coreTargets.cmake
90+
NAMESPACE cpp_core::
91+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core
92+
)
6093

61-
install(FILES
94+
install(
95+
FILES
6296
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfig.cmake
6397
${CMAKE_CURRENT_BINARY_DIR}/cpp_coreConfigVersion.cmake
64-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core)
98+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cpp_core
99+
)
100+
101+
if(CMAKE_EXPORT_COMPILE_COMMANDS AND EXISTS "${CMAKE_BINARY_DIR}/compile_commands.json")
102+
add_custom_target(
103+
copy-compile-commands
104+
ALL
105+
${CMAKE_COMMAND} -E copy_if_different
106+
${CMAKE_BINARY_DIR}/compile_commands.json
107+
${CMAKE_SOURCE_DIR}/compile_commands.json
108+
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json
109+
COMMENT "Copying compile_commands.json to project root"
110+
)
111+
endif()

CMakePresets.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"version": 3,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 30,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "gcc",
11+
"displayName": "GCC (g++)",
12+
"description": "Build using GCC compiler",
13+
"generator": "Ninja",
14+
"binaryDir": "${sourceDir}/build/gcc",
15+
"toolchainFile": "${sourceDir}/cmake/gcc-toolchain.cmake",
16+
"cacheVariables": {
17+
"CMAKE_BUILD_TYPE": "Debug"
18+
}
19+
},
20+
{
21+
"name": "clang",
22+
"displayName": "Clang (clang++)",
23+
"description": "Build using Clang compiler",
24+
"generator": "Ninja",
25+
"binaryDir": "${sourceDir}/build/clang",
26+
"toolchainFile": "${sourceDir}/cmake/clang-toolchain.cmake",
27+
"cacheVariables": {
28+
"CMAKE_BUILD_TYPE": "Debug"
29+
}
30+
},
31+
{
32+
"name": "msvc",
33+
"displayName": "MSVC",
34+
"description": "Build using MSVC compiler",
35+
"generator": "Visual Studio 17 2022",
36+
"binaryDir": "${sourceDir}/build/msvc",
37+
"toolchainFile": "${sourceDir}/cmake/msvc-toolchain.cmake",
38+
"architecture": {
39+
"value": "x64",
40+
"strategy": "external"
41+
}
42+
}
43+
],
44+
"buildPresets": [
45+
{
46+
"name": "gcc-debug",
47+
"displayName": "GCC Debug",
48+
"configurePreset": "gcc",
49+
"configuration": "Debug"
50+
},
51+
{
52+
"name": "gcc-release",
53+
"displayName": "GCC Release",
54+
"configurePreset": "gcc",
55+
"configuration": "Release"
56+
},
57+
{
58+
"name": "clang-debug",
59+
"displayName": "Clang Debug",
60+
"configurePreset": "clang",
61+
"configuration": "Debug"
62+
},
63+
{
64+
"name": "clang-release",
65+
"displayName": "Clang Release",
66+
"configurePreset": "clang",
67+
"configuration": "Release"
68+
},
69+
{
70+
"name": "msvc-debug",
71+
"displayName": "MSVC Debug",
72+
"configurePreset": "msvc",
73+
"configuration": "Debug"
74+
},
75+
{
76+
"name": "msvc-release",
77+
"displayName": "MSVC Release",
78+
"configurePreset": "msvc",
79+
"configuration": "Release"
80+
}
81+
]
82+
}

README.md

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,76 @@
11
# C++ Core
22

3-
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.
3+
Header-only API definition library for cross-platform serial communication. Defines the **API contract** and **version information** shared by all platform-specific binding implementations.
44

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

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

2020
## Requirements
2121

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

25-
## Quick Start
26+
## Version Information
2627

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

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

36-
CPMAddPackage(
37-
NAME cpp_core
38-
GITHUB_REPOSITORY Serial-IO/cpp-core # Fork / Upstream
39-
GIT_TAG main # or e.g. v0.1.0
40-
)
41-
42-
add_executable(my_app src/main.cpp)
43-
target_link_libraries(my_app PRIVATE cpp_core::cpp_core)
44-
```
45-
46-
> Why cache variables?
47-
> They guarantee the values exist **before** the `CPMAddPackage()` call. If the variables are set later (or via
48-
> `OPTIONS ... "CPP_CORE_VERSION_MAJOR=${FOO}"`) and `FOO` is empty at that moment, *cpp-core* falls back to its default version (0.1.0).
49-
50-
2. Use in code
33+
All binding repositories that include this repository will use the same version information, ensuring consistency across platforms.
5134

5235
```cpp
5336
#include <cpp_core/version.h>
54-
#include <cpp_core/helpers.h>
5537

56-
int main() {
57-
constexpr auto v = cpp_core::VERSION; // {1,4,0}
58-
}
38+
constexpr auto v = cpp_core::kVersion; // v.major, v.minor, v.patch
5939
```
6040

61-
## Alternative: add_subdirectory
41+
## Usage in Binding Repositories
6242

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

6545
```cmake
66-
add_subdirectory(externals/cpp-core)
67-
# the same version variables can be set before the call
46+
CPMAddPackage(
47+
NAME cpp_core
48+
GITHUB_REPOSITORY Serial-IO/cpp-core
49+
GIT_TAG v1.2.3 # Version tag determines the API version
50+
)
51+
52+
target_link_libraries(my_binding_library PRIVATE cpp_core::cpp_core)
6853
```
6954

70-
## Using the package with `find_package`
55+
The binding implementation then uses the API definitions:
7156

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

74-
```cmake
75-
find_package(cpp_core REQUIRED)
76-
add_executable(my_app src/main.cpp)
77-
target_link_libraries(my_app PRIVATE cpp_core::cpp_core)
61+
// Implement platform-specific functions matching the API
62+
intptr_t serialOpen(
63+
void *port,
64+
int baudrate,
65+
int data_bits,
66+
int parity,
67+
int stop_bits,
68+
ErrorCallbackT error_callback
69+
) {
70+
// Platform-specific implementation (Windows/Linux/macOS)
71+
}
7872
```
7973

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

cmake/clang-toolchain.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Clang Toolchain file for cpp-core
2+
# Usage: cmake -DCMAKE_TOOLCHAIN_FILE=cmake/clang-toolchain.cmake ..
3+
4+
# Set the C++ compiler
5+
set(CMAKE_CXX_COMPILER "clang++")
6+
set(CMAKE_C_COMPILER "clang")
7+
8+
# Compiler-specific flags
9+
set(CMAKE_CXX_FLAGS_INIT "-Wall -Wextra -Wpedantic")
10+
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-g -O0")
11+
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -DNDEBUG")
12+

cmake/cpp_core_config.cmake.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22

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

5+
# Set version variables for parent projects
6+
set(CPP_CORE_VERSION_MAJOR @CPP_CORE_VERSION_MAJOR@)
7+
set(CPP_CORE_VERSION_MINOR @CPP_CORE_VERSION_MINOR@)
8+
set(CPP_CORE_VERSION_PATCH @CPP_CORE_VERSION_PATCH@)
9+
set(CPP_CORE_VERSION_STRING "@CPP_CORE_VERSION_STRING@")
10+
511
check_required_components(cpp_core)

cmake/gcc-toolchain.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# GCC Toolchain file for cpp-core
2+
# Usage: cmake -DCMAKE_TOOLCHAIN_FILE=cmake/gcc-toolchain.cmake ..
3+
4+
# Set the C++ compiler
5+
set(CMAKE_CXX_COMPILER "g++")
6+
set(CMAKE_C_COMPILER "gcc")
7+
8+
# Compiler-specific flags
9+
set(CMAKE_CXX_FLAGS_INIT "-Wall -Wextra -Wpedantic")
10+
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-g -O0")
11+
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -DNDEBUG")

0 commit comments

Comments
 (0)