Skip to content

Commit

Permalink
librhash: switch to custom CMakeLists.txt
Browse files Browse the repository at this point in the history
The build scripts in the project are quite non-standard and not too portable.

Based on rhash/RHash#103
  • Loading branch information
valgur committed Jan 19, 2024
1 parent 9b142c8 commit 0b200ba
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 94 deletions.
47 changes: 47 additions & 0 deletions recipes/librhash/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Based on https://github.com/rhash/RHash/pull/103
cmake_minimum_required(VERSION 3.15)
project(librhash LANGUAGES C)

file(READ "${CMAKE_CURRENT_SOURCE_DIR}/src/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_OPENSSL "Enable OpenSSL (optimized hash functions) support" ON)

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)

set(SRC_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/src/librhash")
file(GLOB SOURCE_FILES "${SRC_ROOT}/*.c")
file(GLOB HEADER_FILES "${SRC_ROOT}/*.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(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})

install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)

install(FILES "${SRC_ROOT}/rhash.h" "${SRC_ROOT}/rhash_torrent.h"
DESTINATION include)
3 changes: 0 additions & 3 deletions recipes/librhash/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@ sources:
"1.3.9":
url: "https://downloads.sourceforge.net/project/rhash/rhash/1.3.9/rhash-1.3.9-src.tar.gz"
sha256: "42b1006f998adb189b1f316bf1a60e3171da047a85c4aaded2d0d26c1476c9f6"
patches:
"1.4.2":
- patch_file: "patches/0001-1.4.2-fix-compiler-detection.patch"
82 changes: 20 additions & 62 deletions recipes/librhash/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import os

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import cross_building
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
from conan.tools.files import apply_conandata_patches, chdir, copy, export_conandata_patches, get, rmdir
from conan.tools.gnu import Autotools, AutotoolsToolchain, AutotoolsDeps
from conan.tools.cmake import CMakeToolchain, CMakeDeps, CMake
from conan.tools.files import copy, get
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc, unix_path
from conan.tools.scm import Version

required_conan_version = ">=1.53.0"

Expand Down Expand Up @@ -38,7 +35,7 @@ def _settings_build(self):
return getattr(self, "settings_build", self.settings)

def export_sources(self):
export_conandata_patches(self)
copy(self, "CMakeLists.txt", src=self.recipe_folder, dst=self.export_sources_folder)

def config_options(self):
if self.settings.os == "Windows":
Expand All @@ -57,78 +54,39 @@ def requirements(self):
if self.options.with_openssl:
self.requires("openssl/1.1.1w")

def validate(self):
if is_msvc(self):
raise ConanInvalidConfiguration("Visual Studio is not supported")

def build_requirements(self):
if self._settings_build.os == "Windows":
self.win_bash = True
if not self.conf.get("tools.microsoft.bash:path", check_type=str):
self.tool_requires("msys2/cci.latest")

def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)

@property
def _xversion(self):
# https://github.com/rhash/RHash/blob/v1.4.4/configure#L339
version = Version(self.version)
return f"0x{version.major.value:02x}{version.minor.value:02x}{version.patch.value:02x}{0:02x}"

def generate(self):
env = VirtualBuildEnv(self)
env.generate()

if not cross_building(self):
env = VirtualRunEnv(self)
env.generate(scope="build")

tc = AutotoolsToolchain(self)
if self.settings.compiler in ("apple-clang",):
if self.settings.arch == "armv7":
tc.build_type_link_flags.append("-arch armv7")
elif self.settings.arch == "armv8":
tc.build_type_link_flags.append("-arch arm64")
tc.configure_args = [
# librhash's configure script does not understand `--enable-opt1=yes`
"--{}-openssl".format("enable" if self.options.with_openssl else "disable"),
"--disable-gettext",
]
if self.options.shared:
tc.configure_args += ["--enable-lib-shared", "--disable-lib-static"]
else:
tc.configure_args += ["--disable-lib-shared", "--enable-lib-static"]
tc.make_args += [
f"INCDIR={unix_path(self, os.path.join(self.package_folder, 'include'))}",
f"BINDIR={unix_path(self, os.path.join(self.package_folder, 'bin'))}",
f"LIBDIR={unix_path(self, os.path.join(self.package_folder, 'lib'))}",
f"SO_DIR={unix_path(self, os.path.join(self.package_folder, 'lib'))}",
]
tc = CMakeToolchain(self)
tc.cache_variables["USE_OPENSSL"] = self.options.with_openssl
tc.preprocessor_definitions["RHASH_XVERSION"] = self._xversion
tc.generate()

deps = AutotoolsDeps(self)
deps = CMakeDeps(self)
deps.generate()

def build(self):
apply_conandata_patches(self)
with chdir(self, self.source_folder):
autotools = Autotools(self)
autotools.configure()
autotools.make()
cmake = CMake(self)
cmake.configure(build_script_folder=self.source_path.parent)
cmake.build()

def package(self):
copy(self, "COPYING", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
with chdir(self, self.source_folder):
autotools = Autotools(self)
autotools.install()
autotools.make(target="install-lib-headers")
with chdir(self, "librhash"):
if self.options.shared:
autotools.make(target="install-so-link")
for path in self.package_path.iterdir():
if path.is_dir() and path.name not in ["include", "lib", "licenses"]:
rmdir(self, path)
cmake = CMake(self)
cmake.install()

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "LibRHash")
self.cpp_info.set_property("cmake_target_name", "LibRHash::LibRHash")
self.cpp_info.set_property("pkg_config_name", "librhash")
self.cpp_info.libs = ["rhash"]
self.cpp_info.defines.append(f"RHASH_XVERSION={self._xversion}")

# TODO: to remove in conan v2 once cmake_find_package_* generators removed
self.cpp_info.names["cmake_find_package"] = "LibRHash"
Expand Down

This file was deleted.

0 comments on commit 0b200ba

Please sign in to comment.