Skip to content

Commit

Permalink
(#18986) librhash: migrate to Conan v2
Browse files Browse the repository at this point in the history
* librhash: migrate to Conan v2

* librhash: use OpenSSL v1

* librhash: bump deps

* librhash: fix installation

* librhash: downgrade OpenSSL

* librhash: switch to custom CMakeLists.txt

The build scripts in the project are quite non-standard and not too portable.

Based on rhash/RHash#103

* librhash: bump openssl

* librhash: add v1.4.4

* librhash: fix the list of sources used in CMakeLists.txt

* librhash: fix missing lib prefix for libs

* librhash: don't use PROJECT_NAME
  • Loading branch information
valgur authored Jun 13, 2024
1 parent 5e81cfc commit 0ac284b
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 120 deletions.
48 changes: 48 additions & 0 deletions recipes/librhash/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Based on https://github.com/rhash/RHash/pull/103
cmake_minimum_required(VERSION 3.15)
project(rhash LANGUAGES C)

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_OPENSSL "Enable OpenSSL (optimized hash functions) support" ON)

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)

# Get the list of source files from the Makefile
# https://github.com/rhash/RHash/blob/v1.4.4/librhash/Makefile#L6
file(READ Makefile MAKEFILE_CONTENTS)
if("${MAKEFILE_CONTENTS}" MATCHES "SOURCES *= *([^\n]+)")
string(REPLACE " " ";" SOURCE_FILES ${CMAKE_MATCH_1})
else()
message(FATAL_ERROR "SOURCES line not found in Makefile")
endif()

add_library(rhash ${SOURCE_FILES})

if(USE_OPENSSL)
find_package(OpenSSL REQUIRED)
target_link_libraries(rhash OpenSSL::Crypto)
target_compile_definitions(rhash PUBLIC USE_OPENSSL)
endif()

if(MSVC)
target_compile_definitions(rhash PRIVATE _CRT_SECURE_NO_DEPRECATE)
endif()

set_target_properties(rhash PROPERTIES
COMPILE_DEFINITIONS IN_RHASH
DEFINE_SYMBOL RHASH_EXPORTS
VERSION ${RHASH_VERSION}
SOVERSION ${RHASH_VERSION_MAJOR})

install(TARGETS rhash
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)

install(FILES "rhash.h" "rhash_torrent.h" DESTINATION include)
7 changes: 3 additions & 4 deletions recipes/librhash/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
sources:
"1.4.4":
url: "https://downloads.sourceforge.net/project/rhash/rhash/1.4.4/rhash-1.4.4-src.tar.gz"
sha256: "8e7d1a8ccac0143c8fe9b68ebac67d485df119ea17a613f4038cda52f84ef52a"
"1.4.2":
url: "https://downloads.sourceforge.net/project/rhash/rhash/1.4.2/rhash-1.4.2-src.tar.gz"
sha256: "600d00f5f91ef04194d50903d3c79412099328c42f28ff43a0bdb777b00bec62"
"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"
base_path: "source_subfolder"
124 changes: 48 additions & 76 deletions recipes/librhash/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
from conans import AutoToolsBuildEnvironment, ConanFile, tools
from conans.errors import ConanInvalidConfiguration
import os

required_conan_version = ">=1.33.0"
from conan import ConanFile
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.scm import Version

required_conan_version = ">=1.53.0"


class LibRHashConan(ConanFile):
name = "librhash"
description = "Great utility for computing hash sums"
topics = ("rhash", "hash", "checksum")
license = "MIT"
url = "https://github.com/conan-io/conan-center-index"
homepage = "http://rhash.sourceforge.net/"
license = "MIT"
topics = ("rhash", "hash", "checksum")

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
Expand All @@ -24,98 +30,64 @@ class LibRHashConan(ConanFile):
"with_openssl": True,
}

exports_sources = "patches/*"
_autotools = None

@property
def _source_subfolder(self):
return "source_subfolder"

@property
def _settings_build(self):
return getattr(self, "settings_build", self.settings)

def export_sources(self):
copy(self, "CMakeLists.txt", self.recipe_folder, os.path.join(self.export_sources_folder, "src", "librhash"))

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
del self.options.fPIC
del self.settings.compiler.cppstd
del self.settings.compiler.libcxx
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")

def layout(self):
basic_layout(self, src_folder="src")

def requirements(self):
if self.options.with_openssl:
self.requires("openssl/1.1.1q")

def build_requirements(self):
if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"):
self.build_requires("msys2/cci.latest")

def validate(self):
if self.settings.compiler == "Visual Studio":
raise ConanInvalidConfiguration("Visual Studio is not supported")
self.requires("openssl/[>=1.1 <4]")

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

def _configure_autotools(self):
if self._autotools:
return self._autotools
self._autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows)
if self.settings.compiler in ("apple-clang", ):
if self.settings.arch in ("armv7", ):
self._autotools.link_flags.append("-arch armv7")
elif self.settings.arch in ("armv8", ):
self._autotools.link_flags.append("-arch arm64")
vars = self._autotools.vars
conf_args = [
# librhash's configure script does not understand `--enable-opt1=yes`
"--enable-openssl" if self.options.with_openssl else "--disable-openssl",
"--disable-gettext",
# librhash's configure script is custom and does not understand "--bindir=${prefix}/bin" arguments
"--prefix={}".format(tools.unix_path(self.package_folder)),
"--bindir={}".format(tools.unix_path(os.path.join(self.package_folder, "bin"))),
"--libdir={}".format(tools.unix_path(os.path.join(self.package_folder, "lib"))),
# the configure script does not use CPPFLAGS, so add it to CFLAGS/CXXFLAGS
"--extra-cflags={}".format("{} {}".format(vars["CFLAGS"], vars["CPPFLAGS"])),
"--extra-ldflags={}".format(vars["LDFLAGS"]),
]
if self.options.shared:
conf_args.extend(["--enable-lib-shared", "--disable-lib-static"])
else:
conf_args.extend(["--disable-lib-shared", "--enable-lib-static"])
get(self, **self.conan_data["sources"][self.version], strip_root=True)

with tools.environment_append({
"BUILD_TARGET": tools.get_gnu_triplet(str(self.settings.os), str(self.settings.arch), str(self.settings.compiler)),
}):
self._autotools.configure(args=conf_args, use_default_install_dirs=False, build=False, host=False)
return self._autotools
@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):
tc = CMakeToolchain(self)
tc.cache_variables["USE_OPENSSL"] = self.options.with_openssl
tc.preprocessor_definitions["RHASH_XVERSION"] = self._xversion
tc.generate()
deps = CMakeDeps(self)
deps.generate()

def build(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
with tools.chdir(self._source_subfolder):
autotools = self._configure_autotools()
autotools.make()
cmake = CMake(self)
cmake.configure(build_script_folder=os.path.join(self.source_folder, "librhash"))
cmake.build()

def package(self):
self.copy("COPYING", src=self._source_subfolder, dst="licenses")
with tools.chdir(self._source_subfolder):
autotools = self._configure_autotools()
autotools.install()
autotools.make(target="install-lib-headers")
with tools.chdir("librhash"):
if self.options.shared:
autotools.make(target="install-so-link")
tools.rmdir(os.path.join(self.package_folder, "bin"))
tools.rmdir(os.path.join(self.package_folder, "etc"))
tools.rmdir(os.path.join(self.package_folder, "share"))
copy(self, "COPYING", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
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"
self.cpp_info.names["cmake_find_package_multi"] = "LibRHash"
self.cpp_info.names["pkg_config"] = "librhash"
self.cpp_info.libs = ["rhash"]

This file was deleted.

5 changes: 1 addition & 4 deletions recipes/librhash/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
cmake_minimum_required(VERSION 3.3)
cmake_minimum_required(VERSION 3.15)
project(test_package C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(LibRHash CONFIG REQUIRED)

add_executable(${PROJECT_NAME} test_package.c)
Expand Down
23 changes: 16 additions & 7 deletions recipes/librhash/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


class SolaceTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"
class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
8 changes: 8 additions & 0 deletions recipes/librhash/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.15)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package/
${CMAKE_CURRENT_BINARY_DIR}/test_package/)
17 changes: 17 additions & 0 deletions recipes/librhash/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from conans import ConanFile, CMake, tools
import os


class SolaceTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
2 changes: 2 additions & 0 deletions recipes/librhash/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
versions:
1.4.4:
folder: all
1.4.2:
folder: all
1.3.9:
Expand Down

0 comments on commit 0ac284b

Please sign in to comment.