From 40b57c71fce512bdbd98a55d5d3fa9c7480e3b76 Mon Sep 17 00:00:00 2001
From: h5p9sl <21267024+h5p9sl@users.noreply.github.com>
Date: Tue, 12 Apr 2022 19:05:28 -0600
Subject: [PATCH] Use more CMake functionality
Use CTest and create tests/ subdir
Use CMake to optionally build examples
Add .gitignore
---
.gitignore | 12 +++++++
CMakeLists.txt | 36 ++++++++++++++++---
examples/.gitignore | 12 +++++++
examples/CMakeLists.txt | 13 +++++++
.../{simple_example.c => hmac_c_example.c} | 0
...imple_example.cpp => hmac_cxx_example.cpp} | 0
tests/CMakeLists.txt | 7 ++++
{examples => tests}/tests.cpp | 0
8 files changed, 76 insertions(+), 4 deletions(-)
create mode 100644 .gitignore
create mode 100644 examples/.gitignore
create mode 100644 examples/CMakeLists.txt
rename examples/{simple_example.c => hmac_c_example.c} (100%)
rename examples/{simple_example.cpp => hmac_cxx_example.cpp} (100%)
create mode 100644 tests/CMakeLists.txt
rename {examples => tests}/tests.cpp (100%)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4650f9d
--- /dev/null
+++ b/.gitignore
@@ -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
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d07d1d7..752ee7a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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_
" 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()
diff --git a/examples/.gitignore b/examples/.gitignore
new file mode 100644
index 0000000..4650f9d
--- /dev/null
+++ b/examples/.gitignore
@@ -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
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
new file mode 100644
index 0000000..081df23
--- /dev/null
+++ b/examples/CMakeLists.txt
@@ -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)
diff --git a/examples/simple_example.c b/examples/hmac_c_example.c
similarity index 100%
rename from examples/simple_example.c
rename to examples/hmac_c_example.c
diff --git a/examples/simple_example.cpp b/examples/hmac_cxx_example.cpp
similarity index 100%
rename from examples/simple_example.cpp
rename to examples/hmac_cxx_example.cpp
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644
index 0000000..5be3a59
--- /dev/null
+++ b/tests/CMakeLists.txt
@@ -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)
diff --git a/examples/tests.cpp b/tests/tests.cpp
similarity index 100%
rename from examples/tests.cpp
rename to tests/tests.cpp