Skip to content

Commit

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

* libb2: move DLLs from lib to bin

* libb2: restore VirtualRunEnv in test_package
  • Loading branch information
valgur authored Sep 6, 2023
1 parent a558cc0 commit a1f99f3
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 77 deletions.
52 changes: 24 additions & 28 deletions recipes/libb2/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
cmake_minimum_required(VERSION 3.4)
cmake_minimum_required(VERSION 3.12)
project(b2 C)

include("conanbuildinfo.cmake")
conan_basic_setup()

if(WIN32 AND BUILD_SHARED_LIBS AND MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()

option(USE_SSE "Optimized for speed on CPUs supporting SSE2, SSSE3, SSE4.1, AVX, or XOP" OFF)
option(USE_NEON "Optimiced for arm CPUs" OFF)

set(src ${CMAKE_CURRENT_LIST_DIR}/src)

if (USE_NEON)
message(STATUS "Using neon sources")
set(SOURCE_FILES source_subfolder/neon/blake2b-neon.c
source_subfolder/neon/blake2bp.c
source_subfolder/neon/blake2s-neon.c
source_subfolder/neon/blake2xb.c
source_subfolder/neon/blake2sp.c
source_subfolder/neon/blake2xs.c)
set(SOURCE_FILES ${src}/neon/blake2b-neon.c
${src}/neon/blake2bp.c
${src}/neon/blake2s-neon.c
${src}/neon/blake2xb.c
${src}/neon/blake2sp.c
${src}/neon/blake2xs.c)
elseif (USE_SSE)
message(STATUS "Using sse sources")
set(SOURCE_FILES source_subfolder/sse/blake2b.c
source_subfolder/sse/blake2bp.c
source_subfolder/sse/blake2s.c
source_subfolder/sse/blake2sp.c
source_subfolder/sse/blake2xb.c
source_subfolder/sse/blake2xs.c)
set(SOURCE_FILES ${src}/sse/blake2b.c
${src}/sse/blake2bp.c
${src}/sse/blake2s.c
${src}/sse/blake2sp.c
${src}/sse/blake2xb.c
${src}/sse/blake2xs.c)
else ()
message(STATUS "Using ref sources")
set(SOURCE_FILES source_subfolder/ref/blake2bp-ref.c
source_subfolder/ref/blake2b-ref.c
source_subfolder/ref/blake2sp-ref.c
source_subfolder/ref/blake2s-ref.c
source_subfolder/ref/blake2xb-ref.c
source_subfolder/ref/blake2xs-ref.c)
set(SOURCE_FILES ${src}/ref/blake2bp-ref.c
${src}/ref/blake2b-ref.c
${src}/ref/blake2sp-ref.c
${src}/ref/blake2s-ref.c
${src}/ref/blake2xb-ref.c
${src}/ref/blake2xs-ref.c)
endif()

add_library(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES PUBLIC_HEADER source_subfolder/ref/blake2.h)
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES PUBLIC_HEADER ${src}/ref/blake2.h)

include(GNUInstallDirs)
install(TARGETS ${CMAKE_PROJECT_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libb2
)
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libb2)
96 changes: 58 additions & 38 deletions recipes/libb2/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,85 @@
import os
import glob
from conans import ConanFile, tools, CMake
from conans.errors import ConanInvalidConfiguration

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, rename

required_conan_version = ">=1.53.0"


class libb2Conan(ConanFile):
name = "libb2"
description = (
"libb2 is a library that implements the BLAKE2 cryptographic hash function, which is faster than MD5, "
"SHA-1, SHA-2, and SHA-3, yet is at least as secure as the latest standard SHA-3"
)
license = ["CC0-1.0", "OpenSSL", "APSL-2.0"]
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/BLAKE2/BLAKE2"
description = ("libb2 is a library that implemets the BLAKE2 cryptographic hash function, which is faster than MD5, \
SHA-1, SHA-2, and SHA-3, yet is at least as secure as the latest standard SHA-3")
topics = ("blake2", "hash")

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
topics = ("conan", "blake2", "hash")
exports_sources = ["CMakeLists.txt"]
generators = ["cmake"]
options = {"fPIC": [True, False], "shared": [True, False], "use_sse": [True, False], "use_neon": [True, False]}
default_options = {"fPIC": True, "shared": False, "use_sse": False, "use_neon": False}
_cmake = None
options = {
"shared": [True, False],
"fPIC": [True, False],
"use_sse": [True, False],
"use_neon": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"use_sse": False,
"use_neon": False,
}

@property
def _source_subfolder(self):
return "source_subfolder"
def export_sources(self):
copy(self, "CMakeLists.txt", src=self.recipe_folder, dst=self.export_sources_folder)

@property
def _build_subfolder(self):
return "build_subfolder"
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

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

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

def validate(self):
if self.settings.compiler.get_safe("cppstd"):
check_min_cppstd(self, 11)
if self.options.use_neon and not "arm" in self.settings.arch:
raise ConanInvalidConfiguration("Neon sources only supported on arm-based CPUs")
if self.options.use_neon and self.options.use_sse:
raise ConanInvalidConfiguration("Neon and SSE can not be used together.")

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

def source(self):
tools.get(**self.conan_data["sources"][self.version])
extracted_dir = glob.glob("BLAKE2-*")[0]
os.rename(extracted_dir, self._source_subfolder)
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def _configure_cmake(self):
if not self._cmake:
self._cmake = CMake(self)
self._cmake.definitions["USE_SSE"] = self.options.use_sse
self._cmake.definitions["USE_NEON"] = self.options.use_neon
self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake
def generate(self):
tc = CMakeToolchain(self)
tc.variables["USE_SSE"] = self.options.use_sse
tc.variables["USE_NEON"] = self.options.use_neon
tc.generate()

def build(self):
cmake = self._configure_cmake()
cmake = CMake(self)
cmake.configure(build_script_folder=self.export_sources_folder)
cmake.build()

def package(self):
self.copy("COPYING", src=self._source_subfolder, dst="licenses")
cmake = self._configure_cmake()
copy(self, "COPYING", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
cmake = CMake(self)
cmake.install()
for dll in (self.package_path / "lib").glob("*.dll"):
rename(self, dll, self.package_path / "bin" / dll.name)

def package_info(self):
self.cpp_info.libs = tools.collect_libs(self)
self.cpp_info.includedirs = ["include", os.path.join("include","libb2")]
self.cpp_info.libs = ["b2"]
self.cpp_info.includedirs = ["include", os.path.join("include", "libb2")]
7 changes: 3 additions & 4 deletions recipes/libb2/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.15)
project(PackageTest CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_package(libb2 REQUIRED CONFIG)

add_executable(example example.cpp)
target_link_libraries(example ${CONAN_LIBS})
target_link_libraries(example libb2::libb2)
set_property(TARGET example PROPERTY CXX_STANDARD 11)
24 changes: 17 additions & 7 deletions recipes/libb2/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os
from conans import ConanFile, CMake, tools

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

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.settings):
bin_path = os.path.join("bin", "example")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "example")
self.run(bin_path, env="conanrun")
8 changes: 8 additions & 0 deletions recipes/libb2/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/)
16 changes: 16 additions & 0 deletions recipes/libb2/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
from conans import ConanFile, CMake, tools

class blake2TestConan(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.settings):
bin_path = os.path.join("bin", "example")
self.run(bin_path, run_environment=True)

0 comments on commit a1f99f3

Please sign in to comment.