Repository: conan-io/conan-center-index

Issue:
[request] brunsli/0.1
### Package Details
  * Package Name/Version: **brunsli/0.1**
  * Website: **https://github.com/google/brunsli**
  * Source code: **https://github.com/google/brunsli**


### Description Of The Library / Tool

Brunsli is a lossless JPEG repacking library.


Discussion:

Resolving patch:
diff --git a/recipes/brunsli/all/conanfile.py b/recipes/brunsli/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/brunsli/all/conanfile.py
@@ -0,0 +1,100 @@
+import os
+
+from conan import ConanFile
+from conan.errors import ConanInvalidConfiguration
+from conan.tools.build import check_min_cppstd
+from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
+from conan.tools.files import apply_conandata_patches, export_conandata_patches, get, copy, replace_in_file, save
+
+required_conan_version = ">=1.53.0"
+
+class PackageConan(ConanFile):
+    name = "brunsli"
+    description = "Practical JPEG Repacker"
+    license = "MIT"
+    url = "https://github.com/conan-io/conan-center-index"
+    homepage = "https://github.com/google/brunsli"
+    topics = ("jpeg", "repacker", "codec", "brotli", "wasm")
+
+    package_type = "library"
+    settings = "os", "arch", "compiler", "build_type"
+    options = {
+        "shared": [True, False],
+        "fPIC": [True, False],
+    }
+    default_options = {
+        "shared": False,
+        "fPIC": True,
+    }
+
+    def export_sources(self):
+        export_conandata_patches(self)
+
+    def config_options(self):
+        if self.settings.os == "Windows":
+            del self.options.fPIC
+
+    def configure(self):
+        if self.options.shared:
+            self.options.rm_safe("fPIC")
+        self.options["brotli"].shared = False
+
+    def layout(self):
+        cmake_layout(self, src_folder="src")
+
+    def requirements(self):
+        self.requires("brotli/1.0.9", transitive_libs=True)
+
+    def validate(self):
+        if self.settings.compiler.cppstd:
+            check_min_cppstd(self, 11)
+        if self.dependencies["brotli"].options.shared:
+            raise ConanInvalidConfiguration("brotli must be built as a static library")
+
+    def source(self):
+        get(self, **self.conan_data["sources"][self.version], strip_root=True)
+
+    def generate(self):
+        tc = CMakeToolchain(self)
+        tc.cache_variables["BUILD_TESTING"] = False
+        # TODO: add WASM support
+        tc.cache_variables["BRUNSLI_EMSCRIPTEN"] = False
+        tc.variables["CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS"] = True
+        tc.generate()
+
+        deps = CMakeDeps(self)
+        deps.set_property("brotli::brotlidec", "cmake_target_name", "brotlidec-static")
+        deps.set_property("brotli::brotlienc", "cmake_target_name", "brotlienc-static")
+        deps.generate()
+
+    def _patch_sources(self):
+        apply_conandata_patches(self)
+        # Allow Conan to control the linkage type of the output libraries
+        if not self.options.shared:
+            replace_in_file(self, os.path.join(self.source_folder, "brunsli.cmake"), " SHARED", "")
+            save(self, os.path.join(self.source_folder, "brunsli.cmake"),
+                 "\ninstall(TARGETS brunslicommon-static brunslidec-static brunslienc-static)",
+                 append=True)
+        # Fix DLL installation
+        replace_in_file(self, os.path.join(self.source_folder, "brunsli.cmake"),
+                        'LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"',
+                        'LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" '
+                        'RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"')
+
+    def build(self):
+        self._patch_sources()
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def package(self):
+        copy(self, "LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
+        cmake = CMake(self)
+        cmake.install()
+
+    def package_info(self):
+        self.cpp_info.components["brunslidec-c"].libs = ["brunslidec-c"]
+        self.cpp_info.components["brunslienc-c"].libs = ["brunslienc-c"]
+        if not self.options.shared:
+            self.cpp_info.components["brunslidec-c"].libs += ["brunslicommon-static", "brunslidec-static"]
+            self.cpp_info.components["brunslienc-c"].libs += ["brunslicommon-static", "brunslienc-static"]


Test patch:
diff --git a/recipes/brunsli/all/test_package/CMakeLists.txt b/recipes/brunsli/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/brunsli/all/test_package/CMakeLists.txt
@@ -0,0 +1,9 @@
+cmake_minimum_required(VERSION 3.15)
+
+project(test_package CXX)
+
+find_package(brunsli REQUIRED CONFIG)
+
+add_executable(${PROJECT_NAME} test_package.cpp)
+target_link_libraries(${PROJECT_NAME} PRIVATE brunsli::brunsli)
+target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)
diff --git a/recipes/brunsli/all/test_package/conanfile.py b/recipes/brunsli/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/brunsli/all/test_package/conanfile.py
@@ -0,0 +1,26 @@
+from conan import ConanFile
+from conan.tools.build import can_run
+from conan.tools.cmake import cmake_layout, CMake
+import os
+
+
+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 can_run(self):
+            bin_path = os.path.join(self.cpp.build.bindir, "test_package")
+            self.run(bin_path, env="conanrun")
diff --git a/recipes/brunsli/all/test_package/test_package.cpp b/recipes/brunsli/all/test_package/test_package.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/brunsli/all/test_package/test_package.cpp
@@ -0,0 +1,10 @@
+#include <brunsli/jpeg_data.h>
+#include <brunsli/types.h>
+#include <brunsli/jpeg_data_reader.h>
+
+int main() {
+    brunsli::JPEGData jpg;
+    const size_t input_size = 100;
+    const uint8_t input_data[input_size] = {};
+    bool ok = brunsli::ReadJpeg(input_data, input_size, brunsli::JPEG_READ_ALL, &jpg);
+}