Skip to content

Commit

Permalink
(conan-io#11612) bzip3: add recipe
Browse files Browse the repository at this point in the history
* bzip3: add recipe

* set c_std_99 features for `restrict` keyword

* make restrict macro

* update cmake required version

Co-authored-by: SSE4 <tomskside@gmail.com>

* add const

* modernize CMakeLists.txt

* link pthread only on with_thread=True

Co-authored-by: Chris Mc <prince.chrismc@gmail.com>

Co-authored-by: SSE4 <tomskside@gmail.com>
Co-authored-by: Chris Mc <prince.chrismc@gmail.com>
  • Loading branch information
3 people authored and AndreyMlashkin committed Jul 29, 2022
1 parent 80dceb9 commit 4b82409
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 0 deletions.
57 changes: 57 additions & 0 deletions recipes/bzip3/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
cmake_minimum_required(VERSION 3.4)
project(bzip3 LANGUAGES C)

include(conanbuildinfo.cmake)
conan_basic_setup(KEEP_RPATHS)

option(WITH_THREAD "enable thread" OFF)
option(WITH_UTIL "build and install utility" OFF)

set(BZIP3_INC
source_subfolder/include/libbz3.h
)

add_library(bzip3)
target_sources(bzip3 PRIVATE
source_subfolder/src/cm.c
source_subfolder/src/crc32.c
source_subfolder/src/libbz3.c
source_subfolder/src/libsais.c
source_subfolder/src/lzp.c
source_subfolder/src/rle.c
)
target_include_directories(bzip3 PUBLIC source_subfolder/include)
set_target_properties(bzip3 PROPERTIES
PUBLIC_HEADER "${BZIP3_INC}"
WINDOWS_EXPORT_ALL_SYMBOLS ON
C_EXTENSIONS OFF
)
if (with_thread)
target_compile_definitions(bzip3 PRIVATE "PTHREAD")
endif()

if (WITH_UTIL)
add_executable(bzip3_cmd source_subfolder/src/main.c)
target_link_libraries(bzip3_cmd bzip3)
target_compile_definitions(bzip3_cmd PRIVATE "VERSION=\"${VERSION}\"")
set_target_properties(bzip3_cmd PROPERTIES OUTPUT_NAME "bzip3")
endif()

find_library(LIBM m)
target_link_libraries(bzip3 PRIVATE $<$<BOOL:${LIBM}>:${LIBM}>)

include(GNUInstallDirs)
install(
TARGETS bzip3
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

if (WITH_UTIL)
install(
TARGETS bzip3_cmd
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
endif()
9 changes: 9 additions & 0 deletions recipes/bzip3/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sources:
"1.1.4":
url: "https://github.com/kspalaiologos/bzip3/releases/download/1.1.4/bzip3-1.1.4.tar.bz2"
sha256: "e23a06ae17fc36192e79d0151950b3bbd4e26381af50ba4b4fd7a2d9797e498f"

patches:
"1.1.4":
- patch_file: "patches/0001-make-restrict-alias.patch"
base_path: "source_subfolder"
77 changes: 77 additions & 0 deletions recipes/bzip3/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import functools
from conans import ConanFile, CMake, tools

required_conan_version = ">=1.33.0"

class BZip3Conan(ConanFile):
name = "bzip3"
description = "A better and stronger spiritual successor to BZip2."
license = "LGPL-3.0"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/kspalaiologos/bzip3"
topics = ("bzip2", "lzma", "compression")
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"with_thread": [True, False],
"with_util": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"with_thread": True,
"with_util": False,
}
generators = "cmake"

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

def export_sources(self):
self.copy("CMakeLists.txt")
for patch in self.conan_data.get("patches", {}).get(self.version, []):
self.copy(patch["patch_file"])

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
if self.settings.os not in ["Linux", "FreeBSD"]:
del self.options.with_thread

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

@functools.lru_cache(1)
def _configure_cmake(self):
cmake = CMake(self)
cmake.definitions["WITH_PTHREAD"] = self.options.get_safe("with_thread", False)
cmake.definitions["WITH_UTIL"] = self.options.with_util
cmake.definitions["VERSION"] = self.version
cmake.configure()
return cmake

def build(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
cmake = self._configure_cmake()
cmake.build()

def package(self):
self.copy("LICENSE", src=self._source_subfolder, dst="licenses")
cmake = self._configure_cmake()
cmake.install()

def package_info(self):
self.cpp_info.libs = ["bzip3"]
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.system_libs.append("m")
if self.options.get_safe("with_thread", False):
self.cpp_info.system_libs.append("pthread")
114 changes: 114 additions & 0 deletions recipes/bzip3/all/patches/0001-make-restrict-alias.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
diff --git a/src/crc32.c b/src/crc32.c
index 856fcd4..d9fb87a 100644
--- a/src/crc32.c
+++ b/src/crc32.c
@@ -19,6 +19,14 @@

#include "crc32.h"

+#if defined(__GNUC__) || defined(__clang__)
+ #define RESTRICT __restrict__
+#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
+ #define RESTRICT __restrict
+#else
+ #error Your compiler, configuration or platform is not supported.
+#endif
+
static const u32 crc32Table[256] = {
0x00000000L, 0xF26B8303L, 0xE13B70F7L, 0x1350F3F4L, 0xC79A971FL, 0x35F1141CL, 0x26A1E7E8L, 0xD4CA64EBL, 0x8AD958CFL,
0x78B2DBCCL, 0x6BE22838L, 0x9989AB3BL, 0x4D43CFD0L, 0xBF284CD3L, 0xAC78BF27L, 0x5E133C24L, 0x105EC76FL, 0xE235446CL,
@@ -51,7 +59,7 @@ static const u32 crc32Table[256] = {
0xBE2DA0A5L, 0x4C4623A6L, 0x5F16D052L, 0xAD7D5351L
};

-u32 crc32sum(u32 crc, u8 * restrict buf, size_t size) {
+u32 crc32sum(u32 crc, u8 * RESTRICT buf, size_t size) {
while (size--) crc = crc32Table[(crc ^ *(buf++)) & 0xff] ^ (crc >> 8);
return crc;
}
diff --git a/src/lzp.c b/src/lzp.c
index c19580d..2206673 100644
--- a/src/lzp.c
+++ b/src/lzp.c
@@ -11,8 +11,16 @@

#define MATCH 0xf2

-static s32 lzp_encode_block(const u8 * restrict in, const u8 * in_end, u8 * restrict out, u8 * out_end,
- s32 * restrict lut, s32 mask, s32 m_len) {
+#if defined(__GNUC__) || defined(__clang__)
+ #define RESTRICT __restrict__
+#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
+ #define RESTRICT __restrict
+#else
+ #error Your compiler, configuration or platform is not supported.
+#endif
+
+static s32 lzp_encode_block(const u8 * RESTRICT in, const u8 * in_end, u8 * RESTRICT out, u8 * out_end,
+ s32 * RESTRICT lut, s32 mask, s32 m_len) {
const u8 *ins = in, *outs = out;
const u8 * out_eob = out_end - 8;
const u8 * heur = in;
@@ -28,7 +36,7 @@ static s32 lzp_encode_block(const u8 * restrict in, const u8 * in_end, u8 * rest
s32 val = lut[idx];
lut[idx] = in - ins;
if (val > 0) {
- const u8 * restrict ref = ins + val;
+ const u8 * RESTRICT ref = ins + val;
if (memcmp(in + m_len - 4, ref + m_len - 4, sizeof(u32)) == 0 && memcmp(in, ref, sizeof(u32)) == 0) {
if (heur > in && *(u32 *)heur != *(u32 *)(ref + (heur - in))) goto not_found;

@@ -85,7 +93,7 @@ static s32 lzp_encode_block(const u8 * restrict in, const u8 * in_end, u8 * rest
return out >= out_eob ? -1 : (s32)(out - outs);
}

-static s32 lzp_decode_block(const u8 * restrict in, const u8 * in_end, s32 * restrict lut, u8 * restrict out, s32 hash,
+static s32 lzp_decode_block(const u8 * RESTRICT in, const u8 * in_end, s32 * RESTRICT lut, u8 * RESTRICT out, s32 hash,
s32 m_len) {
if (in_end - in < 4) return -1;

@@ -129,7 +137,7 @@ static s32 lzp_decode_block(const u8 * restrict in, const u8 * in_end, s32 * res
return out - outs;
}

-s32 lzp_compress(const u8 * restrict in, u8 * restrict out, s32 n, s32 hash, s32 m_len, s32 * restrict lut) {
+s32 lzp_compress(const u8 * RESTRICT in, u8 * RESTRICT out, s32 n, s32 hash, s32 m_len, s32 * RESTRICT lut) {
if (n - m_len < 32) return -1;

memset(lut, 0, sizeof(s32) * (1 << hash));
@@ -137,6 +145,6 @@ s32 lzp_compress(const u8 * restrict in, u8 * restrict out, s32 n, s32 hash, s32
return lzp_encode_block(in, in + n, out, out + n, lut, (s32)(1 << hash) - 1, m_len);
}

-s32 lzp_decompress(const u8 * restrict in, u8 * restrict out, s32 n, s32 hash, s32 m_len, s32 * restrict lut) {
+s32 lzp_decompress(const u8 * RESTRICT in, u8 * RESTRICT out, s32 n, s32 hash, s32 m_len, s32 * RESTRICT lut) {
return lzp_decode_block(in, in + n, lut, out, hash, m_len);
}
diff --git a/src/rle.c b/src/rle.c
index b1f0340..2cb2e53 100644
--- a/src/rle.c
+++ b/src/rle.c
@@ -19,6 +19,14 @@

#include "rle.h"

+#if defined(__GNUC__) || defined(__clang__)
+ #define RESTRICT __restrict__
+#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
+ #define RESTRICT __restrict
+#else
+ #error Your compiler, configuration or platform is not supported.
+#endif
+
s32 mrlec(u8 * in, s32 inlen, u8 * out) {
u8 *ip = in, *in_end = in + inlen;
s32 op = 0;
@@ -57,7 +65,7 @@ s32 mrlec(u8 * in, s32 inlen, u8 * out) {
return op;
}

-void mrled(u8 * restrict in, u8 * restrict out, s32 outlen) {
+void mrled(u8 * RESTRICT in, u8 * RESTRICT out, s32 outlen) {
s32 op = 0, ip = 0;

s32 c, pc = -1;
10 changes: 10 additions & 0 deletions recipes/bzip3/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)

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

find_package(bzip3 REQUIRED CONFIG)

add_executable(${PROJECT_NAME} ${PROJECT_NAME}.c)
target_link_libraries(${PROJECT_NAME} bzip3::bzip3)
17 changes: 17 additions & 0 deletions recipes/bzip3/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
from conans import ConanFile, CMake, tools


class TestConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
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)
13 changes: 13 additions & 0 deletions recipes/bzip3/all/test_package/test_package.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
#include <string.h>

#include "libbz3.h"

int main() {
const unsigned int block_size = 8 * 1024 * 1024;
struct bz3_state * state = bz3_new(block_size);

bz3_free(state);

return 0;
}
4 changes: 4 additions & 0 deletions recipes/bzip3/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
versions:
"1.1.4":
folder: all

0 comments on commit 4b82409

Please sign in to comment.