Browse Source

feat: build dynamic library

master
Dnomd343 8 months ago
parent
commit
8e0b1b2298
  1. 43
      CMakeLists.txt
  2. 8
      src/md5.h

43
CMakeLists.txt

@ -1,42 +1,67 @@
cmake_minimum_required(VERSION 3.12)
project(md5sum LANGUAGES CXX)
# ------------------------------------------------------------------------------------ #
option(MD5_SHARED_LIB "Built as a dynamic library." OFF)
option(MD5_ENABLE_TESTING "Enable testing of the md5sum library." ON)
option(MD5_ENABLE_BENCHMARK "Enable benchmark of the md5sum library." ON)
# ------------------------------------------------------------------------------------ #
set(CXX_STANDARDS)
foreach (CXX_FEATURE ${CMAKE_CXX_COMPILE_FEATURES})
if (CXX_FEATURE MATCHES "^cxx_std_[0-9]+$")
string(REGEX MATCH "[0-9]+$" CXX_STANDARD ${CXX_FEATURE})
list(APPEND CXX_STANDARDS ${CXX_STANDARD}) # Get supported c++ standards
list(APPEND CXX_STANDARDS ${CXX_STANDARD}) # supported c++ standards
endif()
endforeach()
if (NOT 17 IN_LIST CXX_STANDARDS)
message(FATAL_ERROR "MD5 projects require at least C++17 support.")
message(FATAL_ERROR "MD5 project require at least C++17.")
endif()
list(GET CXX_STANDARDS -1 MD5_CXX_STANDARD) # Select the latest C++ standard
list(GET CXX_STANDARDS -1 MD5_CXX_STANDARD) # select the latest C++ standard
message(STATUS "MD5 CXX Standard: ${MD5_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD ${MD5_CXX_STANDARD})
# ------------------------------------------------------------------------------------ #
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release) # using release build in default
endif()
add_compile_options(-Wall -Wextra)
add_library(md5sum STATIC src/impl/core.cc src/impl/wrapper.cc)
target_compile_options(md5sum PRIVATE -fno-rtti -fno-exceptions)
file(GLOB MD5_SRC src/impl/*.cc)
if (NOT MD5_SHARED_LIB)
add_library(md5sum STATIC ${MD5_SRC})
else()
add_library(md5sum SHARED ${MD5_SRC})
target_compile_options(md5sum PRIVATE -fvisibility=hidden)
endif()
target_include_directories(md5sum PUBLIC src/)
include(third_party/ThirdParty.cmake)
set(MD5_COMPILE_OPTIONS -fno-rtti -fno-exceptions -pedantic)
target_compile_options(md5sum PRIVATE ${MD5_COMPILE_OPTIONS})
add_library(md5sum::md5sum ALIAS md5sum)
# ------------------------------------------------------------------------------------ #
include(third_party/ThirdParty.cmake)
if (MD5_ENABLE_TESTING)
enable_testing()
add_executable(md5_test test/hash.cc test/assert.cc test/simple.cc test/stream.cc)
target_link_libraries(md5_test PRIVATE md5sum::md5sum GTest::gtest_main)
file(GLOB MD5_TEST_SRC test/*.cc)
add_executable(md5_test ${MD5_TEST_SRC})
target_link_libraries(md5_test PRIVATE md5sum GTest::gtest_main)
add_test(NAME md5_test COMMAND md5_test)
endif()
if (MD5_ENABLE_BENCHMARK)
add_executable(md5_benchmark benchmark.cc)
target_link_libraries(md5_benchmark PRIVATE md5sum::md5sum benchmark::benchmark_main)
target_link_libraries(md5_benchmark PRIVATE md5sum benchmark::benchmark_main)
target_compile_options(md5_benchmark PRIVATE -fno-rtti -fno-exceptions)
endif()
# ------------------------------------------------------------------------------------ #

8
src/md5.h

@ -10,6 +10,8 @@ static_assert(sizeof(uintptr_t) == 8,
static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__,
"Project only works on little-endian architecture.");
#define MD5_EXPORT __attribute__ ((visibility ("default")))
#include "impl/value.inc"
#include "impl/constexpr.inc"
@ -26,13 +28,13 @@ public:
MD5& Update(const std::string_view &data);
/// Update md5 hash with specified data.
MD5& Update(const void *data, uint64_t len);
MD5_EXPORT MD5& Update(const void *data, uint64_t len);
/// Stop streaming updates and calculate result.
MD5& Final();
/// Get the string result of md5.
[[nodiscard]] std::string Digest() const;
[[nodiscard]] MD5_EXPORT std::string Digest() const;
/// Calculate the md5 hash value of the specified data.
static std::string Hash(const std::string_view &data);
@ -63,7 +65,7 @@ private:
const void* UpdateImpl(const void *data, uint64_t len);
/// Update and final the md5 hash with the specified data.
void FinalImpl(const void *data, uint64_t len);
MD5_EXPORT void FinalImpl(const void *data, uint64_t len);
};
} // namespace md5

Loading…
Cancel
Save