-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
94 lines (74 loc) · 1.99 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
cmake_minimum_required(VERSION 3.27)
project(blog_backend)
# Set test executable name
set(TEST_EXE ${PROJECT_NAME}_test)
# Set C++ standard
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(BUILD_SHARED_LIBS OFF)
# Check if the target platform is Linux
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(CMAKE_EXE_LINKER_FLAGS "-static")
endif()
# Set build type to Release
set(CMAKE_BUILD_TYPE Debug)
# Set policies
cmake_policy(SET CMP0135 NEW)
# Include Google Test
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)
FetchContent_MakeAvailable(googletest)
file(GLOB_RECURSE SOURECS
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
)
# Add your source files
add_executable(${PROJECT_NAME}
main.cpp
${SOURECS}
)
# Link the executable with Google Test
target_link_libraries(${PROJECT_NAME}
GTest::gtest
GTest::gtest_main
)
enable_testing()
file(GLOB_RECURSE TEST_SOURECS
"${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp"
)
# Add your test files
add_executable(${TEST_EXE}
${TEST_SOURECS}
${SOURECS}
)
target_include_directories(${TEST_EXE} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)
# Link the test executable with Google Test
target_link_libraries(${TEST_EXE}
GTest::gtest
GTest::gtest_main
)
# Add custom target to run the binary directly with "make run" command
add_custom_target(run
COMMAND ./${PROJECT_NAME}
DEPENDS ${PROJECT_NAME}
)
# Add custom target to run the binary directly with "make run" command
add_custom_target(tests
COMMAND ./${TEST_EXE}
DEPENDS ${TEST_EXE}
)
# Add a test
include(GoogleTest)
gtest_discover_tests(${TEST_EXE})
add_custom_target(lint
COMMAND find .. \( -path ../build/CMakeFiles -o -name include \) -prune -o \( -iname "*.h" -o -iname "*.cpp" \) -print | xargs clang-format --dry-run --Werror
COMMENT "Running clang-format linting"
VERBATIM
)