Skip to content

Commit 31e81f9

Browse files
authored
Merge pull request #259 from kkzi/master
chore(build): add CMake build system for examples and tests
2 parents 3331c98 + 2ce0852 commit 31e81f9

14 files changed

Lines changed: 212 additions & 4 deletions

File tree

CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,5 +302,26 @@ elseif( ${CMAKE_HOST_WIN32} )
302302
set( CPACK_NSIS_URL_INFO_ABOUT ${CPACK_PACKAGE_HOMEPAGE_URL} )
303303
endif()
304304

305+
# ===================================================================
306+
# Examples (optional)
307+
308+
option( ${PROJECT_NAME}_BUILD_EXAMPLES "Build examples" OFF )
309+
310+
if ( ${PROJECT_NAME}_BUILD_EXAMPLES )
311+
add_subdirectory( examples )
312+
endif()
313+
314+
315+
# ===================================================================
316+
# Tests (optional)
317+
318+
option( ${PROJECT_NAME}_BUILD_TESTS "Build tests" OFF )
319+
320+
if ( ${PROJECT_NAME}_BUILD_TESTS )
321+
enable_testing()
322+
add_subdirectory( tests )
323+
endif()
324+
325+
305326
# This must always be last!
306327
include(CPack)

examples/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
3+
# Add subdirectories for examples
4+
add_subdirectory(qmqtt)

examples/qmqtt/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
3+
# Add client example
4+
add_subdirectory(client)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
3+
# Create the example executable
4+
add_executable(qmqtt_example example.cpp)
5+
6+
# Link against the qmqtt library
7+
target_link_libraries(qmqtt_example
8+
PRIVATE
9+
qmqtt
10+
Qt${QT_VERSION_MAJOR}::Core
11+
)
12+
13+
# Set C++ standard to match the library
14+
set_target_properties(qmqtt_example
15+
PROPERTIES
16+
CXX_STANDARD 11
17+
CXX_STANDARD_REQUIRED OFF
18+
)
19+
20+
# Enable Qt MOC for this target
21+
set_target_properties(qmqtt_example PROPERTIES AUTOMOC ON)
22+
23+
# Optional: Install the example
24+
install(TARGETS qmqtt_example
25+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}/examples
26+
COMPONENT Examples
27+
)

tests/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
3+
# Add auto tests (simple module tests)
4+
add_subdirectory(auto)
5+
6+
# Add Google Test based unit tests
7+
if(NOT WIN32 OR NOT NO_UNIT_TESTS)
8+
add_subdirectory(gtest)
9+
endif()
10+
11+
# Add benchmarks (only in release mode)
12+
if(CMAKE_BUILD_TYPE STREQUAL "Release")
13+
add_subdirectory(benchmarks)
14+
endif()

tests/auto/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
3+
# Add cmake module test
4+
add_subdirectory(cmake)

tests/auto/cmake/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 2.8)
1+
cmake_minimum_required(VERSION 3.9)
22

33
project(qmake_cmake_files)
44

tests/benchmarks/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
3+
# Add message benchmark
4+
add_subdirectory(message)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
3+
# Create the benchmark executable
4+
add_executable(tst_bench_message main.cpp)
5+
6+
# Link against required libraries
7+
target_link_libraries(tst_bench_message
8+
PRIVATE
9+
qmqtt
10+
Qt${QT_VERSION_MAJOR}::Core
11+
Qt${QT_VERSION_MAJOR}::Test
12+
)
13+
14+
# Set properties
15+
set_target_properties(tst_bench_message
16+
PROPERTIES
17+
CXX_STANDARD 11
18+
CXX_STANDARD_REQUIRED OFF
19+
AUTOMOC ON
20+
)
21+
22+
# Set release mode for benchmarks
23+
target_compile_definitions(tst_bench_message
24+
PRIVATE
25+
QT_NO_DEBUG
26+
)
27+
28+
# Add as test (benchmark)
29+
add_test(NAME qmqtt_message_benchmark COMMAND tst_bench_message)

tests/gtest/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
3+
# Set policies for Google Test compatibility
4+
if(POLICY CMP0077)
5+
cmake_policy(SET CMP0077 NEW)
6+
endif()
7+
if(POLICY CMP0079)
8+
cmake_policy(SET CMP0079 NEW)
9+
endif()
10+
11+
# Disable Google Test installation and examples
12+
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
13+
set(BUILD_GMOCK ON CACHE BOOL "" FORCE)
14+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
15+
16+
# Silence TR1 deprecation warnings for Google Test
17+
add_compile_definitions(_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING)
18+
19+
# Only build Google Mock (it will include Google Test automatically)
20+
add_subdirectory(gtest/googletest/googlemock EXCLUDE_FROM_ALL)
21+
22+
# Add the actual tests
23+
add_subdirectory(tests)

0 commit comments

Comments
 (0)