Skip to content

Commit c14677a

Browse files
authored
10 provide a compiled version of library (#11)
* created compiled version of library * Changed Header Guard Fix #5 * Introduced Single-Header Only and compiled version shared library. Updated Reademe Updated Workflow Added Tests for singleheader version Fix #10 * correction to CmakeLists * other fix * Fixed Cmake * Fixed test ci * Try to fix
1 parent 61ad65b commit c14677a

25 files changed

+657
-419
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
run: sudo apt-get update && sudo apt-get install -y lcov
2222

2323
- name: Configure CMake with coverage
24-
run: cmake -S . -B build -DENABLE_COVERAGE=ON
24+
run: cmake -S . -B build -DENABLE_TEST=ON -DENABLE_COVERAGE=ON
2525

2626
- name: Build
2727
run: cmake --build build --config Debug

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
uses: jwlawson/actions-setup-cmake@v1
2626

2727
- name: Configure CMake
28-
run: cmake -S . -B build
28+
run: cmake -DENABLE_TEST=ON -DENABLE_SINGLE_HEADER=ON -DSTATIC_LIB=ON -S . -B build
2929

3030
- name: Build
3131
run: cmake --build build --config Release

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ cmake_install.cmake
4949
Makefile
5050
build/
5151

52+
# Single Include
53+
single_include/
54+

CMakeLists.txt

Lines changed: 119 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,139 @@ project(CXXStateTree VERSION 0.4.0 LANGUAGES CXX)
44
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

7-
add_library(CXXStateTree INTERFACE)
8-
target_include_directories(CXXStateTree INTERFACE include)
7+
file (GLOB_RECURSE SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
98

10-
add_executable(basic examples/basic.cpp)
11-
target_link_libraries(basic PRIVATE CXXStateTree)
9+
option(STATIC_LIB "Enable Static Library instead of Dynamic" OFF)
1210

11+
if(STATIC_LIB)
12+
add_library(CXXStateTree STATIC ${SRC_FILES})
13+
target_include_directories(CXXStateTree INTERFACE include ${CMAKE_CURRENT_SOURCE_DIR}/include )
14+
else()
15+
add_library(CXXStateTree SHARED ${SRC_FILES})
16+
target_include_directories(CXXStateTree INTERFACE include ${CMAKE_CURRENT_SOURCE_DIR}/include )
17+
endif()
1318

14-
add_executable(nested examples/nested.cpp)
15-
target_link_libraries(nested PRIVATE CXXStateTree)
1619

17-
add_executable(export_dot_example examples/export_dot.cpp)
18-
target_include_directories(export_dot_example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
19-
add_executable(export_dot_nested_example examples/export_dot_nested.cpp)
20-
target_include_directories(export_dot_nested_example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
21-
add_executable(export_dot_context_example examples/export_dot_context.cpp)
22-
target_include_directories(export_dot_context_example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
2320

24-
add_executable(context_example examples/context_example.cpp)
25-
target_include_directories(context_example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
26-
27-
# GoogleTest setup
28-
include(FetchContent)
29-
FetchContent_Declare(
30-
googletest
31-
URL https://github.com/google/googletest/archive/refs/heads/main.zip
21+
set_target_properties(CXXStateTree PROPERTIES
22+
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/Release"
23+
ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/Release"
24+
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/Release"
3225
)
33-
# For Windows: Prevent overriding the parent project's compiler/linker settings
34-
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
35-
FetchContent_MakeAvailable(googletest)
3626

27+
option(ENABLE_TEST "Enable Test" OFF)
28+
29+
if(ENABLE_TEST)
30+
# GoogleTest setup
31+
include(FetchContent)
32+
FetchContent_Declare(
33+
googletest
34+
URL https://github.com/google/googletest/archive/refs/heads/main.zip
35+
DOWNLOAD_EXTRACT_TIMESTAMP true
36+
)
37+
# For Windows: Prevent overriding the parent project's compiler/linker settings
38+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
39+
FetchContent_MakeAvailable(googletest)
40+
41+
42+
enable_testing()
43+
add_executable(state_tree_test tests/state_tree_test.cpp)
44+
target_link_libraries(state_tree_test PRIVATE CXXStateTree gtest_main)
45+
46+
include(GoogleTest)
47+
gtest_discover_tests(state_tree_test)
48+
49+
endif()
3750

38-
enable_testing()
39-
add_executable(state_tree_test tests/state_tree_test.cpp)
40-
target_link_libraries(state_tree_test PRIVATE CXXStateTree gtest_main)
4151

42-
include(GoogleTest)
43-
gtest_discover_tests(state_tree_test)
52+
option(ENABLE_EXAMPLE "Enable Example" OFF)
53+
54+
if(ENABLE_EXAMPLE)
55+
add_executable(basic examples/basic.cpp)
56+
target_include_directories(basic PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
57+
target_link_libraries(basic PRIVATE CXXStateTree)
58+
59+
add_executable(nested examples/nested.cpp)
60+
target_include_directories(basic PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
61+
target_link_libraries(nested PRIVATE CXXStateTree)
62+
63+
add_executable(export_dot_example examples/export_dot.cpp)
64+
target_include_directories(export_dot_example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
65+
target_link_libraries(export_dot_example PRIVATE CXXStateTree)
66+
add_executable(export_dot_nested_example examples/export_dot_nested.cpp)
67+
target_include_directories(export_dot_nested_example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
68+
target_link_libraries(export_dot_nested_example PRIVATE CXXStateTree)
69+
add_executable(export_dot_context_example examples/export_dot_context.cpp)
70+
target_include_directories(export_dot_context_example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
71+
target_link_libraries(export_dot_context_example PRIVATE CXXStateTree)
72+
73+
add_executable(context_example examples/context_example.cpp)
74+
target_include_directories(context_example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
75+
target_link_libraries(context_example PRIVATE CXXStateTree)
76+
77+
endif()
4478

4579
option(ENABLE_COVERAGE "Enable coverage reporting" OFF)
4680

4781
if(ENABLE_COVERAGE)
4882
message(STATUS "Building with coverage flags")
4983
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage")
5084
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
85+
endif()
86+
87+
option(ENABLE_SINGLE_HEADER "Enable Single Header Generation" OFF)
88+
89+
if(ENABLE_SINGLE_HEADER)
90+
find_package(Python3 REQUIRED COMPONENTS Interpreter)
91+
include(FetchContent)
92+
93+
# ----- Download the script (configure‑time, once, cached in build dir) -----
94+
FetchContent_Declare(
95+
edlund_amalgamate
96+
GIT_REPOSITORY https://github.com/edlund/amalgamate.git
97+
GIT_TAG master # ↔ pin a commit / tag for reproducible builds
98+
)
99+
FetchContent_MakeAvailable(edlund_amalgamate) # populates edlund_amalgamate_SOURCE_DIR
100+
101+
set(AMALGAMATE_PY "${edlund_amalgamate_SOURCE_DIR}/amalgamate.py") # :contentReference[oaicite:0]{index=0}
102+
103+
set(AMALGAMATE_CFG "${CMAKE_CURRENT_SOURCE_DIR}/config_CXXStateTree.json")
104+
set(AMALGAMATE_PRO "${CMAKE_CURRENT_SOURCE_DIR}/config_CXXStateTree.prologue")
105+
set(AMALGAMATE_OUT "${CMAKE_CURRENT_SOURCE_DIR}/single_include/CXXStateTree.hpp")
106+
107+
add_custom_command(
108+
OUTPUT ${AMALGAMATE_OUT}
109+
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_SOURCE_DIR}/single_include # ensure /dist
110+
COMMAND python3 ${AMALGAMATE_PY} -c ${AMALGAMATE_CFG} -s ${CMAKE_CURRENT_SOURCE_DIR} -p ${AMALGAMATE_PRO} --verbose yes
111+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
112+
DEPENDS ${AMALGAMATE_PY} ${AMALGAMATE_CFG}
113+
COMMENT "Generating single‑header CXXStateTree.hpp with edlund/amalgamate"
114+
VERBATIM
115+
)
116+
117+
add_custom_target(amalgamate ALL DEPENDS ${AMALGAMATE_OUT})
118+
119+
if(ENABLE_TEST)
120+
# GoogleTest setup
121+
include(FetchContent)
122+
FetchContent_Declare(
123+
googletest
124+
URL https://github.com/google/googletest/archive/refs/heads/main.zip
125+
DOWNLOAD_EXTRACT_TIMESTAMP true
126+
)
127+
# For Windows: Prevent overriding the parent project's compiler/linker settings
128+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
129+
FetchContent_MakeAvailable(googletest)
130+
131+
132+
enable_testing()
133+
add_executable(state_tree_singleheader_test tests/state_tree_singleheader_test.cpp)
134+
target_include_directories(state_tree_singleheader_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/single_include)
135+
target_link_libraries(state_tree_singleheader_test PRIVATE CXXStateTree gtest_main)
136+
137+
include(GoogleTest)
138+
gtest_discover_tests(state_tree_singleheader_test)
139+
140+
endif()
141+
51142
endif()

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* 🧪 Google Test integration
1515
* 📈 Code coverage with Codecov
1616
* 🌳 Designed for extensibility: nested states, DOT export coming soon
17+
* 🔧 Deployed as Shared Library and as Single Header-Only library
1718

1819
---
1920

@@ -26,7 +27,7 @@
2627
using namespace CXXStateTree;
2728

2829
int main() {
29-
auto machine = Builder()
30+
auto machine = StateTree::Builder()
3031
.initial("Idle")
3132
.state("Idle", [](State& s) {
3233
s.on("Start", "Running", nullptr, []() {
@@ -47,10 +48,36 @@ int main() {
4748

4849
---
4950

51+
## 🛠️ Building Shared Library
52+
53+
```bash
54+
cmake -S . -B build
55+
cmake --build build
56+
```
57+
58+
After these command the Shared Library can be found in `build` directory
59+
60+
Please Note: in future release cmake will have the ability to install the library automatically, for now it is necessary to do it manually
61+
62+
---
63+
64+
## 🛠️ Building Single Header-Only Library
65+
66+
```bash
67+
cmake -S . -B build -DENABLE_SINGLE_HEADER=ON
68+
cmake --build build
69+
```
70+
71+
After these command the Single Header-Only Library can be found in `single_include` directory with the name CXXStateTree.hpp
72+
73+
Please Note: in future release cmake will have the ability to install the library automatically, for now it is necessary to do it manually
74+
75+
---
76+
5077
## 🧪 Running Tests
5178

5279
```bash
53-
cmake -S . -B build -DENABLE_COVERAGE=ON
80+
cmake -S . -B build -DENABLE_TEST=ON -DENABLE_COVERAGE=ON
5481
cmake --build build
5582
cd build && ctest
5683
```

config_CXXStateTree.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"project": "CXXStateTree",
3+
"target": "single_include/CXXStateTree.hpp",
4+
"sources": [
5+
"src/CXXStateTree/State.cpp",
6+
"src/CXXStateTree/StateTree.cpp"
7+
],
8+
"include_paths": [
9+
"include"
10+
]
11+
}

config_CXXStateTree.prologue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/**
2+
* CXXStateTree Single Header (c) 2025 ZigRazor
3+
*/

examples/basic.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// File: examples/basic.cpp
22
#include <iostream>
3-
#include "CXXStateTree/StateTree.hpp"
4-
#include "CXXStateTree/Builder.hpp"
3+
#include "CXXStateTree/StateTree.h"
54

65
using namespace CXXStateTree;
76

87
int main()
98
{
10-
auto machine = Builder()
9+
auto machine = StateTree::Builder()
1110
.initial("Idle")
1211
.state("Idle", [](State &s)
1312
{ s.on("Start", "Running", nullptr, [](const std::any &)

examples/context_example.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "CXXStateTree/Builder.hpp"
1+
#include "CXXStateTree/StateTree.h"
22
#include <iostream>
33
#include <string>
44
#include <any>
@@ -24,7 +24,7 @@ int main()
2424
{
2525
UserAuthorizedGuard auth_guard;
2626

27-
auto sm = Builder()
27+
auto sm = StateTree::Builder()
2828
.initial("Idle")
2929
.state("Idle", [&](State &s)
3030
{ s.on("login", "Dashboard", &auth_guard, [](const std::any &ctx)

examples/export_dot.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
#include "CXXStateTree/Builder.hpp"
2-
#include "CXXStateTree/StateTree.hpp"
1+
#include "CXXStateTree/StateTree.h"
32
#include <iostream>
43
#include <fstream>
54

65
using namespace CXXStateTree;
76

87
int main()
98
{
10-
auto tree = Builder()
9+
auto tree = StateTree::Builder()
1110
.initial("App")
1211
.state("App", [](State &app)
1312
{ app.initial_substate("Idle")

0 commit comments

Comments
 (0)