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 2 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 "${CMAKE_CURRENT_SOURCE_DIR}/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 "${CMAKE_CURRENT_SOURCE_DIR}/calc_sums.c"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for CMAKE_CURRENT_SOURCE_DIR, files are always relative to the source dir

"${CMAKE_CURRENT_SOURCE_DIR}/hash_print.c"
"${CMAKE_CURRENT_SOURCE_DIR}/common_func.c"
"${CMAKE_CURRENT_SOURCE_DIR}/hash_update.c"
"${CMAKE_CURRENT_SOURCE_DIR}/file.c"
"${CMAKE_CURRENT_SOURCE_DIR}/file_mask.c"
"${CMAKE_CURRENT_SOURCE_DIR}/file_set.c"
"${CMAKE_CURRENT_SOURCE_DIR}/find_file.c"
"${CMAKE_CURRENT_SOURCE_DIR}/hash_check.c"
"${CMAKE_CURRENT_SOURCE_DIR}/output.c"
"${CMAKE_CURRENT_SOURCE_DIR}/parse_cmdline.c"
"${CMAKE_CURRENT_SOURCE_DIR}/rhash_main.c"
"${CMAKE_CURRENT_SOURCE_DIR}/win_utils.c")

set(HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/calc_sums.h"
"${CMAKE_CURRENT_SOURCE_DIR}/hash_print.h"
"${CMAKE_CURRENT_SOURCE_DIR}/common_func.h"
"${CMAKE_CURRENT_SOURCE_DIR}/hash_update.h"
"${CMAKE_CURRENT_SOURCE_DIR}/file.h"
"${CMAKE_CURRENT_SOURCE_DIR}/file_mask.h"
"${CMAKE_CURRENT_SOURCE_DIR}/file_set.h"
"${CMAKE_CURRENT_SOURCE_DIR}/find_file.h"
"${CMAKE_CURRENT_SOURCE_DIR}/hash_check.h"
"${CMAKE_CURRENT_SOURCE_DIR}/output.h"
"${CMAKE_CURRENT_SOURCE_DIR}/parse_cmdline.h"
"${CMAKE_CURRENT_SOURCE_DIR}/rhash_main.h"
"${CMAKE_CURRENT_SOURCE_DIR}/win_utils.h"
"${CMAKE_CURRENT_SOURCE_DIR}/platform.h"
"${CMAKE_CURRENT_SOURCE_DIR}/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
```
115 changes: 115 additions & 0 deletions librhash/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
cmake_minimum_required(VERSION 3.1)
project(librhash LANGUAGES C)

include(GNUInstallDirs)

if(MSVC OR MSVC90 OR MSVC10)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSVC90/MSVC10 already imply MSVC

set(MSVC ON)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uneeded

if(BUILD_SHARED_LIBS)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
endif()

option(USE_OPENSSL "Enable OpenSSL (optimized hash functions) support")
option(OPENSSL_RUNTIME "Load OpenSSL at runtime if present")

set(SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/algorithms.c"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

"${CMAKE_CURRENT_SOURCE_DIR}/byte_order.c"
"${CMAKE_CURRENT_SOURCE_DIR}/plug_openssl.c"
"${CMAKE_CURRENT_SOURCE_DIR}/rhash.c"
"${CMAKE_CURRENT_SOURCE_DIR}/rhash_timing.c"
"${CMAKE_CURRENT_SOURCE_DIR}/rhash_torrent.c"
"${CMAKE_CURRENT_SOURCE_DIR}/aich.c"
"${CMAKE_CURRENT_SOURCE_DIR}/crc32.c"
"${CMAKE_CURRENT_SOURCE_DIR}/ed2k.c"
"${CMAKE_CURRENT_SOURCE_DIR}/edonr.c"
"${CMAKE_CURRENT_SOURCE_DIR}/hex.c"
"${CMAKE_CURRENT_SOURCE_DIR}/md4.c"
"${CMAKE_CURRENT_SOURCE_DIR}/md5.c"
"${CMAKE_CURRENT_SOURCE_DIR}/sha1.c"
"${CMAKE_CURRENT_SOURCE_DIR}/sha256.c"
"${CMAKE_CURRENT_SOURCE_DIR}/sha512.c"
"${CMAKE_CURRENT_SOURCE_DIR}/sha3.c"
"${CMAKE_CURRENT_SOURCE_DIR}/ripemd-160.c"
"${CMAKE_CURRENT_SOURCE_DIR}/gost12.c"
"${CMAKE_CURRENT_SOURCE_DIR}/gost94.c"
"${CMAKE_CURRENT_SOURCE_DIR}/has160.c"
"${CMAKE_CURRENT_SOURCE_DIR}/snefru.c"
"${CMAKE_CURRENT_SOURCE_DIR}/tiger.c"
"${CMAKE_CURRENT_SOURCE_DIR}/tiger_sbox.c"
"${CMAKE_CURRENT_SOURCE_DIR}/tth.c"
"${CMAKE_CURRENT_SOURCE_DIR}/torrent.c"
"${CMAKE_CURRENT_SOURCE_DIR}/whirlpool.c"
"${CMAKE_CURRENT_SOURCE_DIR}/whirlpool_sbox.c")

set(HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/algorithms.h"
"${CMAKE_CURRENT_SOURCE_DIR}/byte_order.h"
"${CMAKE_CURRENT_SOURCE_DIR}/plug_openssl.h"
"${CMAKE_CURRENT_SOURCE_DIR}/rhash.h"
"${CMAKE_CURRENT_SOURCE_DIR}/rhash_timing.h"
"${CMAKE_CURRENT_SOURCE_DIR}/rhash_torrent.h"
"${CMAKE_CURRENT_SOURCE_DIR}/aich.h"
"${CMAKE_CURRENT_SOURCE_DIR}/crc32.h"
"${CMAKE_CURRENT_SOURCE_DIR}/ed2k.h"
"${CMAKE_CURRENT_SOURCE_DIR}/edonr.h"
"${CMAKE_CURRENT_SOURCE_DIR}/hex.h"
"${CMAKE_CURRENT_SOURCE_DIR}/md4.h"
"${CMAKE_CURRENT_SOURCE_DIR}/md5.h"
"${CMAKE_CURRENT_SOURCE_DIR}/sha1.h"
"${CMAKE_CURRENT_SOURCE_DIR}/sha256.h"
"${CMAKE_CURRENT_SOURCE_DIR}/sha512.h"
"${CMAKE_CURRENT_SOURCE_DIR}/sha3.h"
"${CMAKE_CURRENT_SOURCE_DIR}/ripemd-160.h"
"${CMAKE_CURRENT_SOURCE_DIR}/gost12.h"
"${CMAKE_CURRENT_SOURCE_DIR}/gost94.h"
"${CMAKE_CURRENT_SOURCE_DIR}/has160.h"
"${CMAKE_CURRENT_SOURCE_DIR}/snefru.h"
"${CMAKE_CURRENT_SOURCE_DIR}/tiger.h"
"${CMAKE_CURRENT_SOURCE_DIR}/tth.h"
"${CMAKE_CURRENT_SOURCE_DIR}/torrent.h"
"${CMAKE_CURRENT_SOURCE_DIR}/ustd.h"
"${CMAKE_CURRENT_SOURCE_DIR}/util.h"
"${CMAKE_CURRENT_SOURCE_DIR}/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 bin
LIBRARY DESTINATION lib
Copy link

@jschueller jschueller Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you use gnuinstalldirs then you can use CMAKE_INSTALL_LIBDIR/BINDIR/INCLUDEDIR

ARCHIVE DESTINATION lib)

install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/rhash.h" "${CMAKE_CURRENT_SOURCE_DIR}/rhash_torrent.h"
DESTINATION include)

install(EXPORT ${CMAKE_PROJECT_NAME}Config
DESTINATION "${CMAKE_INSTALL_LIBDIR}/${CMAKE_PROJECT_NAME}/cmake"
NAMESPACE ${CMAKE_PROJECT_NAME}::)