diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..ab401c6b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,56 @@ +cmake_minimum_required(VERSION 3.1) +project(rhash) + +file(READ "version.h" versionfile) +string(REGEX MATCH "#define VERSION \"([0-9]*)\.([0-9]*)\.([0-9]*)\"" _ ${versionfile}) +set(RHASH_VERSION_MAJOR ${CMAKE_MATCH_1}) +set(RHASH_VERSION_MINOR ${CMAKE_MATCH_2}) +set(RHASH_VERSION_PATCH ${CMAKE_MATCH_3}) +set(RHASH_VERSION "${RHASH_VERSION_MAJOR}.${RHASH_VERSION_MINOR}.${RHASH_VERSION_PATCH}") + +option(USE_GETTEXT "Enable gettext (localization) support") + +set(SOURCE_FILES "calc_sums.c" + "hash_print.c" + "common_func.c" + "hash_update.c" + "file.c" + "file_mask.c" + "file_set.c" + "find_file.c" + "hash_check.c" + "output.c" + "parse_cmdline.c" + "rhash_main.c" + "win_utils.c") + +set(HEADER_FILES "calc_sums.h" + "hash_print.h" + "common_func.h" + "hash_update.h" + "file.h" + "file_mask.h" + "file_set.h" + "find_file.h" + "hash_check.h" + "output.h" + "parse_cmdline.h" + "rhash_main.h" + "win_utils.h" + "platform.h" + "version.h") + +add_subdirectory("librhash") + +add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES} ${HEADER_FILES}) +target_link_libraries(${CMAKE_PROJECT_NAME} librhash) + +if (USE_GETTEXT) + find_package(Intl REQUIRED) + target_link_libraries(${CMAKE_PROJECT_NAME} ${Intl_LIBRARIES}) + target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${Intl_INCLUDE_DIRS}) + target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE USE_GETTEXT) +endif() + +install(TARGETS ${CMAKE_PROJECT_NAME} + RUNTIME DESTINATION bin) diff --git a/INSTALL.md b/INSTALL.md index ce6d4d9f..7a05d6e6 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -52,3 +52,52 @@ Example of installing RHash with shared and static LibRHash library: ./configure --enable-lib-static make install install-lib-so-link ``` + + +Optional Installation (CMake) +============================= + +Build Prerequisites +------------------- + - GCC or Intel Compiler for Linux / macOS / Unix. + - MinGW or MS VC++ for Windows. + - CMake 3.1 or later + - (optionally) gettext library for internationalization + - (optionally) OpenSSL for optimized algorithms + + Build and install +----------------- +To compile and install the program use command +```sh +cmake . -DCMAKE_BUILD_TYPE=Release && cmake --build . && cmake --build . --target install +``` + +Enabling features +----------------- +RHash can use optimized algorithms of MD5, SHA1, SHA2 from the OpenSSL library. +To link OpenSSL at run-time (preffered way), configure RHash as +```sh +cmake . -DOPENSSL_RUNTIME=ON +``` +To link it at load-time, use options +```sh +cmake . -DUSE_OPENSSL=ON +``` + +Internationalization support can be compiled and installed by commands +```sh +cmake . -DUSE_GETTEXT=ON +make install install-gmo +``` + +Building an OS native package +----------------------------- +When building a package for an OS Repository, one should correctly specify system directories, e.g.: +```sh +cmake . -DCMAKE_INSTALL_PREFIX=/usr +``` + +Example of installing RHash with shared LibRHash library: +```sh +cmake . -DBUILD_SHARED_LIBS=ON +``` diff --git a/librhash/CMakeLists.txt b/librhash/CMakeLists.txt new file mode 100644 index 00000000..ecc3ab9e --- /dev/null +++ b/librhash/CMakeLists.txt @@ -0,0 +1,110 @@ +cmake_minimum_required(VERSION 3.1) +project(librhash LANGUAGES C) + +include(GNUInstallDirs) + +set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON CACHE BOOL "Export all symbols when building shared library on Windows") + +option(USE_OPENSSL "Enable OpenSSL (optimized hash functions) support") +option(OPENSSL_RUNTIME "Load OpenSSL at runtime if present") + +set(SOURCE_FILES "algorithms.c" + "byte_order.c" + "plug_openssl.c" + "rhash.c" + "rhash_timing.c" + "rhash_torrent.c" + "aich.c" + "crc32.c" + "ed2k.c" + "edonr.c" + "hex.c" + "md4.c" + "md5.c" + "sha1.c" + "sha256.c" + "sha512.c" + "sha3.c" + "ripemd-160.c" + "gost12.c" + "gost94.c" + "has160.c" + "snefru.c" + "tiger.c" + "tiger_sbox.c" + "tth.c" + "torrent.c" + "whirlpool.c" + "whirlpool_sbox.c") + +set(HEADER_FILES "algorithms.h" + "byte_order.h" + "plug_openssl.h" + "rhash.h" + "rhash_timing.h" + "rhash_torrent.h" + "aich.h" + "crc32.h" + "ed2k.h" + "edonr.h" + "hex.h" + "md4.h" + "md5.h" + "sha1.h" + "sha256.h" + "sha512.h" + "sha3.h" + "ripemd-160.h" + "gost12.h" + "gost94.h" + "has160.h" + "snefru.h" + "tiger.h" + "tth.h" + "torrent.h" + "ustd.h" + "util.h" + "whirlpool.h") + +add_library(${PROJECT_NAME} ${SOURCE_FILES} ${HEADER_FILES}) +set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${CMAKE_PROJECT_NAME}") + +if(USE_OPENSSL) + find_package(OpenSSL REQUIRED) + target_link_libraries(${PROJECT_NAME} OpenSSL::Crypto) + target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENSSL) +endif() + +if(OPENSSL_RUNTIME) + target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_DL_LIBS}) + target_compile_definitions(${PROJECT_NAME} PRIVATE OPENSSL_RUNTIME) +endif() + +if(MSVC) + target_compile_definitions(${PROJECT_NAME} PRIVATE _CRT_SECURE_NO_DEPRECATE) +endif() + +set_target_properties(${PROJECT_NAME} PROPERTIES + COMPILE_DEFINITIONS IN_RHASH + DEFINE_SYMBOL RHASH_EXPORTS + PREFIX "" + IMPORT_PREFIX "" + VERSION ${RHASH_VERSION} + SOVERSION ${RHASH_VERSION_MAJOR}) + +export(TARGETS ${PROJECT_NAME} + NAMESPACE ${CMAKE_PROJECT_NAME}:: + FILE "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}Config.cmake") + +install(TARGETS ${PROJECT_NAME} + EXPORT ${CMAKE_PROJECT_NAME}Config + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + +install(FILES "rhash.h" "rhash_torrent.h" + DESTINATION include) + +install(EXPORT ${CMAKE_PROJECT_NAME}Config + DESTINATION "${CMAKE_INSTALL_LIBDIR}/${CMAKE_PROJECT_NAME}/cmake" + NAMESPACE ${CMAKE_PROJECT_NAME}::)