Skip to content

Commit

Permalink
Configure installation of system libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
bernedom committed Apr 20, 2024
1 parent 1ab058f commit 5805d3a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
22 changes: 18 additions & 4 deletions chapter04/ex07_pack_nsis_standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
54 changes: 52 additions & 2 deletions chapter04/ex07_pack_nsis_standalone/executable/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,57 @@

#include <chapter4/ex07/lib.hpp>

int main(void) {
#include <iomanip>
#include <iostream>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <sstream>
#include <string>

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();
}

const std::string message{"CMake is awesome!"};

std::cout << "The sha256 hash of the message '" << message << "' is:\n";
std::cout << "\t" << sha256(message) << "\n";
}

0 comments on commit 5805d3a

Please sign in to comment.