|
| 1 | +# Get the exercise name from the current directory |
| 2 | +get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME) |
| 3 | + |
| 4 | +# Basic CMake project |
| 5 | +cmake_minimum_required(VERSION 3.5.1) |
| 6 | + |
| 7 | +# Name the project after the exercise |
| 8 | +project(${exercise} CXX) |
| 9 | + |
| 10 | +# Get a source filename from the exercise name by replacing -'s with _'s |
| 11 | +string(REPLACE "-" "_" file ${exercise}) |
| 12 | + |
| 13 | +# Implementation could be only a header |
| 14 | +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp) |
| 15 | + set(exercise_cpp ${file}.cpp) |
| 16 | +else() |
| 17 | + set(exercise_cpp "") |
| 18 | +endif() |
| 19 | + |
| 20 | +# Use the common Catch library? |
| 21 | +if(EXERCISM_COMMON_CATCH) |
| 22 | + # For Exercism track development only |
| 23 | + add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>) |
| 24 | +elseif(EXERCISM_TEST_SUITE) |
| 25 | + # The Exercism test suite is being run, the Docker image already |
| 26 | + # includes a pre-built version of Catch. |
| 27 | + find_package(Catch2 REQUIRED) |
| 28 | + add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h) |
| 29 | + target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain) |
| 30 | + # When Catch is installed system wide we need to include a different |
| 31 | + # header, we need this define to use the correct one. |
| 32 | + target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE) |
| 33 | +else() |
| 34 | + # Build executable from sources and headers |
| 35 | + add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp) |
| 36 | +endif() |
| 37 | + |
| 38 | +set_target_properties(${exercise} PROPERTIES |
| 39 | + CXX_STANDARD 17 |
| 40 | + CXX_STANDARD_REQUIRED OFF |
| 41 | + CXX_EXTENSIONS OFF |
| 42 | +) |
| 43 | + |
| 44 | +set(CMAKE_BUILD_TYPE Debug) |
| 45 | + |
| 46 | +if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)") |
| 47 | + set_target_properties(${exercise} PROPERTIES |
| 48 | + COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror" |
| 49 | + ) |
| 50 | +endif() |
| 51 | + |
| 52 | +# Configure to run all the tests? |
| 53 | +if(${EXERCISM_RUN_ALL_TESTS}) |
| 54 | + target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS) |
| 55 | +endif() |
| 56 | + |
| 57 | +# Tell MSVC not to warn us about unchecked iterators in debug builds |
| 58 | +if(${MSVC}) |
| 59 | + set_target_properties(${exercise} PROPERTIES |
| 60 | + COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS) |
| 61 | +endif() |
| 62 | + |
| 63 | +# Run the tests on every build |
| 64 | +add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise}) |
0 commit comments