|
1 | 1 | cmake_minimum_required(VERSION 3.25)
|
2 | 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() |
| 3 | +project(tinyclib VERSION 0.1.0 LANGUAGES C) |
| 4 | + |
| 5 | +# Set the output directories |
| 6 | +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) |
| 7 | +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) |
| 8 | +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin) |
| 9 | + |
| 10 | +# Sources |
| 11 | +file(GLOB SOURCES CONFIGURE_DEPENDS src/*.c) |
| 12 | + |
| 13 | +# Add the library (BUILD_SHARED_LIBS is handled by CMake) |
| 14 | +add_library(tinyclib ${SOURCES}) |
| 15 | + |
| 16 | +# Set the C standard to C11 for now |
| 17 | +set_property(TARGET tinyclib PROPERTY C_STANDARD 11) |
| 18 | +set_property(TARGET tinyclib PROPERTY C_STANDARD_REQUIRED ON) |
| 19 | +set_property(TARGET tinyclib PROPERTY C_EXTENSIONS OFF) |
| 20 | + |
| 21 | +# Set include directories for build and install |
| 22 | +target_include_directories( |
| 23 | + tinyclib |
| 24 | + PUBLIC |
| 25 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 26 | + $<INSTALL_INTERFACE:include> |
| 27 | +) |
20 | 28 |
|
21 |
| -# Add include directories |
22 |
| -include_directories(${CMAKE_SOURCE_DIR}/include) |
| 29 | +# Install targets and headers |
| 30 | +include(GNUInstallDirs) |
23 | 31 |
|
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 |
| 32 | +install( |
| 33 | + TARGETS tinyclib |
| 34 | + EXPORT tinyclibTargets |
| 35 | + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 36 | + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 37 | + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} |
30 | 38 | )
|
31 | 39 |
|
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 | +install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) |
| 41 | + |
| 42 | +# Package configuration |
| 43 | +include(CMakePackageConfigHelpers) |
| 44 | + |
| 45 | +configure_package_config_file( |
| 46 | + cmake/tinyclibConfig.cmake.in |
| 47 | + ${CMAKE_CURRENT_BINARY_DIR}/tinyclibConfig.cmake |
| 48 | + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/tinyclib |
40 | 49 | )
|
41 | 50 |
|
42 |
| -# Create a static library |
43 |
| -add_library(tinyclib STATIC ${SOURCES} ${HEADERS}) |
| 51 | +write_basic_package_version_file( |
| 52 | + ${CMAKE_CURRENT_BINARY_DIR}/tinyclibConfigVersion.cmake |
| 53 | + VERSION ${PROJECT_VERSION} |
| 54 | + COMPATIBILITY AnyNewerVersion |
| 55 | +) |
44 | 56 |
|
45 |
| -# Fetch dependencies |
46 |
| -include(FetchContent) |
| 57 | +install( |
| 58 | + FILES |
| 59 | + ${CMAKE_CURRENT_BINARY_DIR}/tinyclibConfig.cmake |
| 60 | + ${CMAKE_CURRENT_BINARY_DIR}/tinyclibConfigVersion.cmake |
| 61 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/tinyclib |
| 62 | +) |
47 | 63 |
|
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 |
| 64 | +install( |
| 65 | + EXPORT tinyclibTargets |
| 66 | + FILE tinyclibTargets.cmake |
| 67 | + NAMESPACE tinyclib:: |
| 68 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/tinyclib |
53 | 69 | )
|
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() |
| 70 | + |
| 71 | +# Add a namespace alias for modern linking |
| 72 | +add_library(tinyclib::tinyclib ALIAS tinyclib) |
| 73 | + |
| 74 | +# Support shared libraries |
| 75 | +set_target_properties(tinyclib PROPERTIES POSITION_INDEPENDENT_CODE ON) |
63 | 76 |
|
64 | 77 | # Build tests
|
65 |
| -option(BUILD_TESTS "Build tests" ON) |
66 | 78 | 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) |
| 79 | + include(CTest) |
| 80 | + include(FetchContent) |
| 81 | + |
| 82 | + # FetchContent for Unity testing framework |
| 83 | + FetchContent_Declare( |
| 84 | + Unity |
| 85 | + GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git |
| 86 | + GIT_TAG v2.6.1 |
| 87 | + ) |
| 88 | + FetchContent_MakeAvailable(Unity) |
| 89 | + FetchContent_GetProperties(Unity) |
| 90 | + |
| 91 | + if(NOT TARGET Unity) |
| 92 | + add_library(Unity STATIC ${unity_SOURCE_DIR}/src/unity.c) |
| 93 | + endif() |
| 94 | + target_include_directories(Unity PUBLIC ${unity_SOURCE_DIR}/src) |
| 95 | + |
| 96 | + enable_testing() |
| 97 | + foreach(t app config debug error test) |
| 98 | + add_executable(tl_${t}_test tests/arch/generic/tl_${t}_test.c) |
| 99 | + target_link_libraries(tl_${t}_test Unity tinyclib) |
| 100 | + add_test(NAME tl_${t}_test COMMAND tl_${t}_test) |
| 101 | + endforeach() |
95 | 102 | endif()
|
0 commit comments