diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml new file mode 100644 index 0000000..f21a025 --- /dev/null +++ b/.github/workflows/cmake-multi-platform.yml @@ -0,0 +1,75 @@ +# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform. +# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml +name: CMake on multiple platforms + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + runs-on: ${{ matrix.os }} + + strategy: + # Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable. + fail-fast: false + + # Set up a matrix to run the following 3 configurations: + # 1. + # 2. + # 3. + # + # To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list. + matrix: + os: [ubuntu-latest, windows-latest] + build_type: [Release] + c_compiler: [gcc, clang, cl] + include: + - os: windows-latest + c_compiler: cl + cpp_compiler: cl + - os: ubuntu-latest + c_compiler: gcc + cpp_compiler: g++ + - os: ubuntu-latest + c_compiler: clang + cpp_compiler: clang++ + exclude: + - os: windows-latest + c_compiler: gcc + - os: windows-latest + c_compiler: clang + - os: ubuntu-latest + c_compiler: cl + + steps: + - uses: actions/checkout@v4 + + - name: Set reusable strings + # Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file. + id: strings + shell: bash + run: | + echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT" + + - name: Configure CMake + # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. + # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type + run: > + cmake -B ${{ steps.strings.outputs.build-output-dir }} + -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }} + -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} + -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} + -S ${{ github.workspace }} + + - name: Build + # Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). + run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} + + - name: Test + working-directory: ${{ steps.strings.outputs.build-output-dir }} + # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest --build-config ${{ matrix.build_type }} diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b50c0f6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,90 @@ +cmake_minimum_required(VERSION 3.5...3.10) +project(CVector LANGUAGES C CXX) + +include(GNUInstallDirs) + +# Options +option(BUILD_TESTING "Build tests" ON) + +if(MSVC) + add_definitions(-D_CRT_SECURE_NO_WARNINGS) +endif() + +# Library target +add_library(cvector INTERFACE) +target_include_directories(cvector INTERFACE + $ + $ +) + +# Install rules +install(TARGETS cvector EXPORT CVectorTargets) +install(FILES cvector.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +install(EXPORT CVectorTargets + FILE CVectorTargets.cmake + NAMESPACE CVector:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CVector +) + +if(BUILD_TESTING) + # Try to find CUnit + find_path(CUNIT_INCLUDE_DIR NAMES CUnit/Automated.h) + find_library(CUNIT_LIBRARY NAMES cunit) + + if(CUNIT_INCLUDE_DIR AND CUNIT_LIBRARY) + message(STATUS "Found CUnit: ${CUNIT_LIBRARY}") + + # Test: cvector + add_executable(cvector_test main.c cvector_tests.c) + target_link_libraries(cvector_test PRIVATE cvector ${CUNIT_LIBRARY}) + target_include_directories(cvector_test PRIVATE ${CUNIT_INCLUDE_DIR}) + target_compile_definitions(cvector_test PRIVATE USE_CVECTOR_H) + if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(cvector_test PRIVATE -ansi -pedantic-errors -fno-strict-aliasing -Wunused-variable -Wreturn-type) + endif() + + # Test: cvec_strdup + add_executable(cvec_strdup main.c cvector_tests.c) + target_link_libraries(cvec_strdup PRIVATE cvector ${CUNIT_LIBRARY}) + target_include_directories(cvec_strdup PRIVATE ${CUNIT_INCLUDE_DIR}) + target_compile_definitions(cvec_strdup PRIVATE USE_CVECTOR_H USE_POSIX_STRDUP) + if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(cvec_strdup PRIVATE -pedantic-errors -fno-strict-aliasing -Wunused-variable -Wreturn-type) + endif() + + # Test: cvector_templates + add_executable(cvector_templates main.c cvector_tests.c cvector_all.c) + target_link_libraries(cvector_templates PRIVATE cvector ${CUNIT_LIBRARY}) + target_include_directories(cvector_templates PRIVATE ${CUNIT_INCLUDE_DIR}) + target_compile_definitions(cvector_templates PRIVATE USE_TEMPLATES) + if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(cvector_templates PRIVATE -ansi -pedantic-errors -fno-strict-aliasing -Wunused-variable -Wreturn-type) + endif() + + # Test: cvector_macro + add_executable(cvector_macro main.c cvector_tests.c cvector_all.c) + target_link_libraries(cvector_macro PRIVATE cvector ${CUNIT_LIBRARY}) + target_include_directories(cvector_macro PRIVATE ${CUNIT_INCLUDE_DIR}) + if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(cvector_macro PRIVATE -ansi -pedantic-errors -fno-strict-aliasing -Wunused-variable -Wreturn-type) + endif() + else() + message(WARNING "CUnit not found. CUnit tests will not be built.") + endif() + + # Test: cvectorcpp + add_executable(cvectorcpp main2.cpp) + target_link_libraries(cvectorcpp PRIVATE cvector) + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(cvectorcpp PRIVATE -ansi -pedantic-errors -fno-strict-aliasing -Wunused-variable -Wreturn-type) + endif() + + # Test: cvector_nolibc (POSIX only) + if(NOT WIN32) + add_executable(cvector_nolibc main_nolibc.c) + target_link_libraries(cvector_nolibc PRIVATE cvector) + if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(cvector_nolibc PRIVATE -std=gnu89 -nostdlib -pedantic-errors -fno-strict-aliasing -Wunused-variable -Wreturn-type) + endif() + endif() +endif()