Skip to content
Open
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 .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

- name: run cmake
working-directory: ${{github.workspace}}/
run: cmake -B./build .
run: cmake -B./build -DBUILD_TESTING=ON .

- name: make
working-directory: ${{github.workspace}}/build
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ _deps
.cache/*
.vscode/*
build/*
cmake-build-debug/*
.idea/

# Allow expected outputs for tests to be committed
!**/tests/*.out
!**/tests/**/*.out
!**/tests/**/*.out
63 changes: 48 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.16)

project(homeworks)
project(homeworks LANGUAGES CXX)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib)
option(BUILD_SANDBOX "Build sandbox targets" OFF)
option(BUILD_ADDITIONAL_TASKS "Build additional tasks" OFF)
set(BUILD_TASK "" CACHE STRING "Build only one task, for example task_07")

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sandbox)
set(HOMEWORK_TASKS
task_01
task_02
task_03
task_04
task_05
task_06
task_07
task_08
task_09
task_10
task_11
task_12
task_13
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тесты собираются только если передан флаг -DBUILD_TESTING=ON. Нужно убедиться, что GitHub Actions workflow явно передаёт этот флаг при вызове cmake, иначе тесты не будут собираться и CI будет «зелёным» без реальной проверки

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавил флаг -DBUILD_TESTING=ON в шаг run cmake в .github/workflows/tests.yaml, теперь тесты собираются и CI их гарантировано проверяет.

task_14)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/additional_tasks)
list(FIND HOMEWORK_TASKS "${BUILD_TASK}" build_task_index)

file(GLOB_RECURSE tasks_dirs LIST_DIRECTORIES true ".")
if(BUILD_TASK AND build_task_index EQUAL -1)
message(FATAL_ERROR "Unknown BUILD_TASK='${BUILD_TASK}'. Expected one of: ${HOMEWORK_TASKS}")
endif()

foreach(dir ${tasks_dirs})
IF(IS_DIRECTORY ${dir})
IF(${dir} MATCHES "task_[0-9][0-9]$" AND NOT ${dir} MATCHES "build")
add_subdirectory(${dir})
ENDIF()
ELSE()
CONTINUE()
ENDIF()
endforeach()
add_subdirectory(lib)

include(CTest)

if(BUILD_TESTING)
find_package(GTest REQUIRED)
include(GoogleTest)
endif()

if(BUILD_TASK)
add_subdirectory(${BUILD_TASK})
else()
foreach(task IN LISTS HOMEWORK_TASKS)
add_subdirectory(${task})
endforeach()

if(BUILD_SANDBOX)
add_subdirectory(sandbox)
endif()

if(BUILD_ADDITIONAL_TASKS)
add_subdirectory(additional_tasks)
endif()
endif()
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
### Советы при работе с codespaces

* При работе с заданиями желательно создавать файлы .hpp и .cpp с заданием, причем .hpp - именно заголовочный файл, который будет импортироваться вами в test.cpp для тестирования
* Если вы добавили в `src` отдельный файл с реализацией (например, `algo.cpp`), не забудьте дописать его в `SOURCES` и, при необходимости, в `TEST_SOURCES` внутри `task_N/CMakeLists.txt`. Иначе проект может сконфигурироваться, но упасть на этапе линковки из-за “undefined reference”
* Если вы работаете через VS Code, то чтобы поменять цель сборки, нажмите Ctrl+p, и введите **>CMake: Set Launch/Debug Target**
* Если при запуске тестов консоль остается пустой, проверьте что int main() внутри main.cpp соответствующего задания пуст
* Если вся среда неожиданно зависла и очень долго пытается соединиться с сервером - попробуйте выключить интернет на 10 секунд и включить снова. Если не помогает - ищите более надежную сеть(
Expand Down
17 changes: 7 additions & 10 deletions additional_tasks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ cmake_minimum_required(VERSION 3.10)

project(additional_tasks)

file(GLOB_RECURSE tasks_dirs LIST_DIRECTORIES true ".")
file(GLOB additional_entries RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*")

foreach(dir ${tasks_dirs})
IF(IS_DIRECTORY ${dir})
IF(NOT ${dir} MATCHES ".*src.*")
add_subdirectory(${dir})
ENDIF()
ELSE()
CONTINUE()
ENDIF()
endforeach()
foreach(entry ${additional_entries})
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${entry}" AND
EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${entry}/CMakeLists.txt")
add_subdirectory(${entry})
endif()
endforeach()
37 changes: 37 additions & 0 deletions cmake/AddHomeworkTask.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function(add_homework_task)
set(options)
set(one_value_args NAME)
set(multi_value_args SOURCES TEST_SOURCES INCLUDE_DIRS)
cmake_parse_arguments(TASK "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})

if(NOT TASK_NAME)
message(FATAL_ERROR "add_homework_task requires NAME")
endif()

if(NOT TASK_SOURCES)
message(FATAL_ERROR "add_homework_task(${TASK_NAME}) requires SOURCES")
endif()

add_executable(${TASK_NAME} ${TASK_SOURCES})
target_compile_features(${TASK_NAME} PRIVATE cxx_std_23)
target_include_directories(
${TASK_NAME}
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/src"
${TASK_INCLUDE_DIRS}
)
target_link_libraries(${TASK_NAME} PRIVATE Utils)

if(BUILD_TESTING AND TASK_TEST_SOURCES)
add_executable(${TASK_NAME}_tests ${TASK_TEST_SOURCES})
target_compile_features(${TASK_NAME}_tests PRIVATE cxx_std_23)
target_include_directories(
${TASK_NAME}_tests
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/src"
${TASK_INCLUDE_DIRS}
)
target_link_libraries(${TASK_NAME}_tests PRIVATE GTest::gtest_main Utils)
gtest_discover_tests(${TASK_NAME}_tests)
endif()
endfunction()
14 changes: 5 additions & 9 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(Utils)
add_library(Utils
src/util.cpp
)

set(CMAKE_CXX_STANDARD 23)

file(GLOB_RECURSE lib_source_list "src/*.cpp" "src/*.hpp")

add_library(${PROJECT_NAME} ${lib_source_list})

target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_compile_features(Utils PUBLIC cxx_std_23)
target_include_directories(Utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
17 changes: 7 additions & 10 deletions sandbox/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ cmake_minimum_required(VERSION 3.10)

project(sendbox)

file(GLOB_RECURSE tasks_dirs LIST_DIRECTORIES true ".")
file(GLOB sandbox_entries RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*")

foreach(dir ${tasks_dirs})
IF(IS_DIRECTORY ${dir})
IF(NOT ${dir} MATCHES ".*src.*" AND NOT ${dir} MATCHES ".*tests.*")
IF(EXISTS ${dir}/CMakeLists.txt)
add_subdirectory(${dir})
ENDIF()
ENDIF()
ENDIF()
endforeach()
foreach(entry ${sandbox_entries})
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${entry}" AND
EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${entry}/CMakeLists.txt")
add_subdirectory(${entry})
endif()
endforeach()
49 changes: 10 additions & 39 deletions task_01/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@

cmake_minimum_required(VERSION 3.10)

get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME})
project(${PROJECT_NAME} C CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

file(GLOB_RECURSE source_list "src/*.cpp" "src/*.hpp")
file(GLOB_RECURSE main_source_list "src/main.cpp")
file(GLOB_RECURSE test_source_list "src/*.cpp")
file(GLOB_RECURSE test_list "src/*test.cpp")

list(REMOVE_ITEM test_source_list ${main_source_list})
list(REMOVE_ITEM source_list ${test_list})

include_directories(${PROJECT_NAME} PUBLIC src)

add_executable(${PROJECT_NAME} ${source_list})

# Locate GTest
enable_testing()
find_package(GTest REQUIRED)

include_directories(${GTEST_INCLUDE_DIRS})

target_link_libraries(${PROJECT_NAME} PUBLIC Utils)

# Link runTests with what we want to test and the GTest and pthread library
add_executable(${PROJECT_NAME}_tests ${test_source_list})
target_link_libraries(
${PROJECT_NAME}_tests
GTest::gtest_main
Utils
include(${CMAKE_SOURCE_DIR}/cmake/AddHomeworkTask.cmake)

# If you add extra implementation files in src/, list them in SOURCES and
# TEST_SOURCES explicitly. CMake will not pick new .cpp files automatically.
add_homework_task(
NAME task_01
SOURCES
src/main.cpp
TEST_SOURCES
src/test.cpp
)

include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME}_tests)
49 changes: 10 additions & 39 deletions task_02/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@

cmake_minimum_required(VERSION 3.10)

get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME})
project(${PROJECT_NAME} C CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

file(GLOB_RECURSE source_list "src/*.cpp" "src/*.hpp")
file(GLOB_RECURSE main_source_list "src/main.cpp")
file(GLOB_RECURSE test_source_list "src/*.cpp")
file(GLOB_RECURSE test_list "src/*test.cpp")

list(REMOVE_ITEM test_source_list ${main_source_list})
list(REMOVE_ITEM source_list ${test_list})

include_directories(${PROJECT_NAME} PUBLIC src)

add_executable(${PROJECT_NAME} ${source_list})

# Locate GTest
enable_testing()
find_package(GTest REQUIRED)

include_directories(${GTEST_INCLUDE_DIRS})

target_link_libraries(${PROJECT_NAME} PUBLIC Utils)

# Link runTests with what we want to test and the GTest and pthread library
add_executable(${PROJECT_NAME}_tests ${test_source_list})
target_link_libraries(
${PROJECT_NAME}_tests
GTest::gtest_main
Utils
include(${CMAKE_SOURCE_DIR}/cmake/AddHomeworkTask.cmake)

# If you add extra implementation files in src/, list them in SOURCES and
# TEST_SOURCES explicitly. CMake will not pick new .cpp files automatically.
add_homework_task(
NAME task_02
SOURCES
src/main.cpp
TEST_SOURCES
src/test.cpp
)

include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME}_tests)
49 changes: 10 additions & 39 deletions task_03/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@

cmake_minimum_required(VERSION 3.10)

get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME})
project(${PROJECT_NAME} C CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

file(GLOB_RECURSE source_list "src/*.cpp" "src/*.hpp")
file(GLOB_RECURSE main_source_list "src/main.cpp")
file(GLOB_RECURSE test_source_list "src/*.cpp")
file(GLOB_RECURSE test_list "src/*test.cpp")

list(REMOVE_ITEM test_source_list ${main_source_list})
list(REMOVE_ITEM source_list ${test_list})

include_directories(${PROJECT_NAME} PUBLIC src)

add_executable(${PROJECT_NAME} ${source_list})

# Locate GTest
enable_testing()
find_package(GTest REQUIRED)

include_directories(${GTEST_INCLUDE_DIRS})

target_link_libraries(${PROJECT_NAME} PUBLIC Utils)

# Link runTests with what we want to test and the GTest and pthread library
add_executable(${PROJECT_NAME}_tests ${test_source_list})
target_link_libraries(
${PROJECT_NAME}_tests
GTest::gtest_main
Utils
include(${CMAKE_SOURCE_DIR}/cmake/AddHomeworkTask.cmake)

# If you add extra implementation files in src/, list them in SOURCES and
# TEST_SOURCES explicitly. CMake will not pick new .cpp files automatically.
add_homework_task(
NAME task_03
SOURCES
src/main.cpp
TEST_SOURCES
src/test.cpp
)

include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME}_tests)
Loading
Loading