Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add all the lzham lib files for msvc builds. #23907

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 53 additions & 20 deletions recipes/lzham/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import (
apply_conandata_patches,
copy,
export_conandata_patches,
get,
replace_in_file,
rmdir
)
from conan.tools.microsoft import (
Expand Down Expand Up @@ -51,6 +53,28 @@ def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")

def validate(self):
if self.settings.os == "Windows" and self.settings.arch not in ["x86", "x86_64"]:
raise ConanInvalidConfiguration("On Windows, only x86 and x86_64 are supported")

def _msvc_libs(self, targets=False):
arch = "x86" if self.settings.arch == "x86" else "x64"
suffix = f"{arch}D" if self.settings.build_type == "Debug" else arch
if self.options.shared == True:
if targets:
# Note: this causes its dependencies to be built too
return ['lzhamdll']

files = [f"lzham_{suffix}.dll", f"lzham_{suffix}.lib"]
else:
libs = ['lzhamcomp', 'lzhamdecomp', 'lzhamlib']
if targets:
return libs

files = [f"{lib}_{suffix}.lib" for lib in libs]

return files

def layout(self):
if is_msvc(self):
vs_layout(self)
Expand Down Expand Up @@ -79,10 +103,21 @@ def generate(self):
# Honor BUILD_SHARED_LIBS from conan_toolchain (see
# https://github.com/conan-io/conan/issues/11840)
tc.cache_variables["CMAKE_POLICY_DEFAULT_CMP0077"] = "NEW"

# Build relocatable shared libraries on Apple OSs
tc.cache_variables["CMAKE_POLICY_DEFAULT_CMP0042"] = "NEW"
tc.generate()

def build(self):
apply_conandata_patches(self)
for project in ['lzhamcomp', 'lzhamdecomp', 'lzhamdll', 'lzhamlib']:
filename = 'lzham' if project == 'lzhamdll' else project
vcxproj_file = os.path.join(self.source_folder, project, f"{filename}.vcxproj")
# Avoid errors when the toolset on the consumer side is not exactly the same version
replace_in_file(self, vcxproj_file, "WholeProgramOptimization>true", "WholeProgramOptimization>false")
# Don't override the runtime library set by Conan's MSBuildToolchain
replace_in_file(self, vcxproj_file, "<RuntimeLibrary>MultiThreaded</RuntimeLibrary>", "")
replace_in_file(self, vcxproj_file, "<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>", "")
if is_msvc(self):
msbuild = MSBuild(self)
msbuild.build_type = (
Expand All @@ -91,7 +126,7 @@ def build(self):
msbuild.platform = (
"Win32" if self.settings.arch == "x86" else msbuild.platform
)
msbuild.build(sln="lzham.sln")
msbuild.build(sln="lzham.sln", targets=self._msvc_libs(targets=True))
else:
cmake = CMake(self)
cmake.configure()
Expand All @@ -106,21 +141,21 @@ def package(self):
)

if is_msvc(self):
suffix = "x64D" if self.settings.build_type == "Debug" else "x64"
copy(
self,
pattern=f"lzham_{suffix}.lib",
dst=os.path.join(self.package_folder, "lib"),
src=os.path.join(self.build_folder, "lib", "x64"),
keep_path=False
)
copy(
self,
pattern=f"lzham_{suffix}.dll",
dst=os.path.join(self.package_folder, "bin"),
src=os.path.join(self.build_folder, "bin"),
keep_path=False
)
arch = "x86" if self.settings.arch == "x86" else "x64"
for target in self._msvc_libs():
self.output.warning(target)
debug_suffix = "D" if self.settings.build_type == "Debug" and not self.options.shared else ""
arch_folder = f"{arch}{debug_suffix}"
subfolder = "lib" if not target.endswith('dll') else "bin"
subfolder_src = os.path.join("lib", arch_folder) if not target.endswith('dll') else "bin"
self.output.warning(subfolder_src)
copy(
self,
pattern=target,
dst=os.path.join(self.package_folder, subfolder),
src=os.path.join(self.build_folder, subfolder_src),
keep_path=False
)
copy(
self,
pattern="*.h",
Expand All @@ -141,10 +176,8 @@ def package_info(self):
self.cpp_info.system_libs.extend(["m", "pthread"])

if is_msvc(self):
lib_name = "lzham_x64"
if self.settings.build_type == "Debug":
lib_name += "D"
self.cpp_info.libs = [lib_name]
libs = list(set([filename[:-4] for filename in self._msvc_libs()]))
self.cpp_info.libs = libs
else:
self.cpp_info.libs = ["lzhamdll", "lzhamcomp", "lzhamdecomp"]
self.cpp_info.set_property("cmake_file_name", "lzham")
Expand Down
20 changes: 5 additions & 15 deletions recipes/lzham/all/test_package/test_package.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>

#include <lzham_static_lib.h>
#include <lzham.h>

int main() {
unsigned char in[] = "Hello Conan Center!";
unsigned char out[sizeof(in)];
const std::string version(lzham_z_version());

lzham_z_stream stream;
std::memset(&stream, 0, sizeof(stream));
stream.next_in = in;
stream.avail_in = sizeof(in);
stream.next_out = out;
stream.avail_out = sizeof(out);
if (lzham_z_deflateInit(&stream, LZHAM_Z_BEST_COMPRESSION) != LZHAM_Z_OK)
return EXIT_FAILURE;
std::cout << "lzham version: " << version << std::endl;

if (lzham_z_deflate(&stream, LZHAM_Z_FULL_FLUSH) != LZHAM_Z_OK)
return EXIT_FAILURE;

return EXIT_SUCCESS;
}
Loading