|
| 1 | +cmake_minimum_required(VERSION 3.25) |
| 2 | + |
| 3 | +project(tinyclib LANGUAGES C) |
| 4 | + |
| 5 | +# Set the output directories for libraries and executables |
| 6 | +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
| 7 | +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
| 8 | +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 9 | + |
| 10 | +# Set the C standard to C11 and fallback to C99 if not supported |
| 11 | +set(CMAKE_C_STANDARD 11) |
| 12 | +set(CMAKE_C_STANDARD_REQUIRED ON) |
| 13 | +set(CMAKE_C_EXTENSIONS OFF) |
| 14 | +include(CheckCCompilerFlag) |
| 15 | +check_c_compiler_flag("-std=c11" COMPILER_SUPPORTS_C11) |
| 16 | +if(NOT COMPILER_SUPPORTS_C11) |
| 17 | + message(WARNING "C11 is not supported by the compiler. Falling back to C99.") |
| 18 | + set(CMAKE_C_STANDARD 99) |
| 19 | +endif() |
| 20 | + |
| 21 | +# Add include directories |
| 22 | +include_directories(${CMAKE_SOURCE_DIR}/include) |
| 23 | + |
| 24 | +# Source files |
| 25 | +set(SOURCES |
| 26 | + src/tl_app.c |
| 27 | + src/tl_config.c |
| 28 | + src/tl_error.c |
| 29 | + src/tl_test.c |
| 30 | +) |
| 31 | + |
| 32 | +# Header files |
| 33 | +set(HEADERS |
| 34 | + include/tl_app.h |
| 35 | + include/tl_config.h |
| 36 | + include/tl_constants.h |
| 37 | + include/tl_debug.h |
| 38 | + include/tl_error.h |
| 39 | + include/tl_test.h |
| 40 | +) |
| 41 | + |
| 42 | +# Create a static library |
| 43 | +add_library(tinyclib STATIC ${SOURCES} ${HEADERS}) |
| 44 | + |
| 45 | +# Fetch dependencies |
| 46 | +include(FetchContent) |
| 47 | + |
| 48 | +# Fetch Unity test framework |
| 49 | +FetchContent_Declare( |
| 50 | + Unity |
| 51 | + GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git |
| 52 | + GIT_TAG v2.6.1 |
| 53 | +) |
| 54 | +FetchContent_GetProperties(Unity) |
| 55 | +if(NOT Unity_POPULATED) |
| 56 | + FetchContent_MakeAvailable(Unity) |
| 57 | + set(Unity_SOURCE_DIR ${CMAKE_BINARY_DIR}/_deps/unity-src) |
| 58 | + if(NOT TARGET Unity) |
| 59 | + add_library(Unity STATIC ${Unity_SOURCE_DIR}/src/unity.c) |
| 60 | + target_include_directories(Unity PUBLIC ${Unity_SOURCE_DIR}/src) |
| 61 | + endif() |
| 62 | +endif() |
| 63 | + |
| 64 | +# Build tests |
| 65 | +option(BUILD_TESTS "Build tests" ON) |
| 66 | +if(BUILD_TESTS) |
| 67 | + enable_testing() |
| 68 | + |
| 69 | + include_directories(${unity_SOURCE_DIR}/src) |
| 70 | + |
| 71 | + # Add test executable for tl_app |
| 72 | + add_executable(tl_app_test tests/arch/generic/tl_app_test.c) |
| 73 | + target_link_libraries(tl_app_test Unity tinyclib) |
| 74 | + add_test(NAME tl_app_test COMMAND tl_app_test) |
| 75 | + |
| 76 | + # Add test executable for tl_config |
| 77 | + add_executable(tl_config_test tests/arch/generic/tl_config_test.c) |
| 78 | + target_link_libraries(tl_config_test Unity tinyclib) |
| 79 | + add_test(NAME tl_config_test COMMAND tl_config_test) |
| 80 | + |
| 81 | + # Add test executable for tl_debug |
| 82 | + add_executable(tl_debug_test tests/arch/generic/tl_debug_test.c) |
| 83 | + target_link_libraries(tl_debug_test Unity tinyclib) |
| 84 | + add_test(NAME tl_debug_test COMMAND tl_debug_test) |
| 85 | + |
| 86 | + # Add test executable for tl_error |
| 87 | + add_executable(tl_error_test tests/arch/generic/tl_error_test.c) |
| 88 | + target_link_libraries(tl_error_test Unity tinyclib) |
| 89 | + add_test(NAME tl_error_test COMMAND tl_error_test) |
| 90 | + |
| 91 | + # Add test executable for tl_test |
| 92 | + add_executable(tl_test_test tests/arch/generic/tl_test_test.c) |
| 93 | + target_link_libraries(tl_test_test Unity tinyclib) |
| 94 | + add_test(NAME tl_test_test COMMAND tl_test_test) |
| 95 | +endif() |
0 commit comments