From 5805d3a69d3c211b63d3c8bfedfab55f5c74ab70 Mon Sep 17 00:00:00 2001 From: Dominik Berner Date: Sat, 20 Apr 2024 12:06:35 +0000 Subject: [PATCH] Configure installation of system libraries --- .../ex07_pack_nsis_standalone/CMakeLists.txt | 22 ++++++-- .../executable/CMakeLists.txt | 3 +- .../executable/src/main.cpp | 54 ++++++++++++++++++- 3 files changed, 72 insertions(+), 7 deletions(-) diff --git a/chapter04/ex07_pack_nsis_standalone/CMakeLists.txt b/chapter04/ex07_pack_nsis_standalone/CMakeLists.txt index 447f8af..8fe5c98 100644 --- a/chapter04/ex07_pack_nsis_standalone/CMakeLists.txt +++ b/chapter04/ex07_pack_nsis_standalone/CMakeLists.txt @@ -15,16 +15,31 @@ if(NOT PROJECT_IS_TOP_LEVEL) message(FATAL_ERROR "The chapter-4, ex07_pack project is intended to be a standalone, top-level project. Do not include this directory.") endif() +# Search the package OpenSSL and its component SSL, configuration fails if not found +# this is used by the executable target +find_package(OpenSSL REQUIRED COMPONENTS SSL) + add_subdirectory(executable) add_subdirectory(library) -include(InstallRequiredSystemLibraries) + + + +if(WIN32) + # if it is a debug build, we want to install debug libraries with the InstallRequiredSystemLibraries module + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(CMAKE_INSTALL_DEBUG_LIBRARIES TRUE) + endif() + # this includes compiler provided runtime libraries on windows + include(InstallRequiredSystemLibraries) +endif() + +# this includes the runtime directories of the executable and the library install(TARGETS ch4_ex07_executable RUNTIME_DEPENDENCIES PRE_EXCLUDE_REGEXES "api-ms-.*" "ext-ms-.*" POST_EXCLUDE_REGEXES ".*system32/.*\\.dll" - DIRECTORIES "${RUNTIME_DIRS}") - + ) # We will not explicitly specify project name and version here and # let CPack to get project name and version from the project() @@ -35,6 +50,5 @@ set(CPACK_GENERATOR "NSIS") set(CPACK_THREADS 0) # The DEB generator requires CPACK_DEBIAN_PACKAGE_MAINTAINER # value to be set. -set(CPACK_DEBIAN_PACKAGE_MAINTAINER "CBP Authors") # Enable packaging support for the project include(CPack) \ No newline at end of file diff --git a/chapter04/ex07_pack_nsis_standalone/executable/CMakeLists.txt b/chapter04/ex07_pack_nsis_standalone/executable/CMakeLists.txt index eb182de..8c43323 100644 --- a/chapter04/ex07_pack_nsis_standalone/executable/CMakeLists.txt +++ b/chapter04/ex07_pack_nsis_standalone/executable/CMakeLists.txt @@ -18,6 +18,7 @@ add_executable(ch4_ex07_executable src/main.cpp) target_compile_features(ch4_ex07_executable PRIVATE cxx_std_11) # Link executable target to library target -target_link_libraries(ch4_ex07_executable PRIVATE ch4_ex07_library) +target_link_libraries(ch4_ex07_executable PRIVATE ch4_ex07_library OpenSSL::SSL) + install(TARGETS ch4_ex07_executable) \ No newline at end of file diff --git a/chapter04/ex07_pack_nsis_standalone/executable/src/main.cpp b/chapter04/ex07_pack_nsis_standalone/executable/src/main.cpp index f902d71..d075bee 100644 --- a/chapter04/ex07_pack_nsis_standalone/executable/src/main.cpp +++ b/chapter04/ex07_pack_nsis_standalone/executable/src/main.cpp @@ -8,7 +8,57 @@ #include -int main(void) { +#include +#include +#include +#include +#include +#include + +std::string sha256(const std::string &str) { + unsigned char hash[SHA256_DIGEST_LENGTH]; + EVP_MD_CTX *mdctx; + + if (!(mdctx = EVP_MD_CTX_new())) { + // Handle error + return ""; + } + + if (1 != EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) { + // Handle error + EVP_MD_CTX_free(mdctx); + return ""; + } + + if (1 != EVP_DigestUpdate(mdctx, str.c_str(), str.size())) { + // Handle error + EVP_MD_CTX_free(mdctx); + return ""; + } + + unsigned int hash_len; + if (1 != EVP_DigestFinal_ex(mdctx, hash, &hash_len)) { + // Handle error + EVP_MD_CTX_free(mdctx); + return ""; + } + + EVP_MD_CTX_free(mdctx); + + std::stringstream ss; + for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { + ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; + } + + return ss.str(); +} + +int main(int, char **) { chapter4::ex07::greeter g; g.greet(); -} \ No newline at end of file + + const std::string message{"CMake is awesome!"}; + + std::cout << "The sha256 hash of the message '" << message << "' is:\n"; + std::cout << "\t" << sha256(message) << "\n"; +}