Use more CMake functionality

Use CTest and create tests/ subdir
Use CMake to optionally build examples
Add .gitignore
This commit is contained in:
h5p9sl 2022-04-12 19:05:28 -06:00
parent e47cc1112b
commit 40b57c71fc
8 changed files with 76 additions and 4 deletions

12
.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
# CMake
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps

View File

@ -1,8 +1,36 @@
cmake_minimum_required(VERSION 3.14)
project(hmac-sha256)
set(CMAKE_C_STANDARD 99)
add_library(hmac-sha256 STATIC hmac_sha256.c sha256.c )
# For "CMAKE_INSTALL_<dir>" standard installation directories
include(GNUInstallDirs)
install(TARGETS hmac-sha256 DESTINATION lib)
install(FILES hmac-sha256.h DESTINATION include)
project(hmac_sha256 LANGUAGES C CXX)
option(HMAC_SHA256_TESTS "Build unit tests" FALSE)
option(HMAC_SHA256_EXAMPLES "Build the examples" FALSE)
add_library(hmac_sha256 STATIC
${CMAKE_CURRENT_SOURCE_DIR}/sha256.c
${CMAKE_CURRENT_SOURCE_DIR}/hmac_sha256.c
)
target_include_directories(hmac_sha256 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
install(TARGETS hmac_sha256
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/hmac_sha256.h
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
if (HMAC_SHA256_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
if(HMAC_SHA256_EXAMPLES)
add_subdirectory(examples)
endif()

12
examples/.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
# CMake
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps

13
examples/CMakeLists.txt Normal file
View File

@ -0,0 +1,13 @@
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
add_executable(hmac_cxx_example
hmac_cxx_example.cpp
)
add_executable(hmac_c_example
hmac_c_example.c
)
target_link_libraries(hmac_cxx_example hmac_sha256)
target_link_libraries(hmac_c_example hmac_sha256)

7
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,7 @@
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
add_executable(tests tests.cpp)
target_link_libraries(tests hmac_sha256)
add_test(hmac_sha256-tests tests)