Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMake support for RHash #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
49 changes: 49 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
110 changes: 110 additions & 0 deletions librhash/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}::)