Skip to content

Commit

Permalink
lzham: fix handling of static/shared on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jcar87 committed May 15, 2024
1 parent a8a54d8 commit 9ca33a0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 51 deletions.
86 changes: 50 additions & 36 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 @@ -83,6 +107,14 @@ def generate(self):

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 +123,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,36 +138,21 @@ def package(self):
)

if is_msvc(self):
suffix = "x64D" if self.settings.build_type == "Debug" else "x64"
build_folder = "x64D" if self.settings.build_type == "Debug" else "x64"
copy(
self,
pattern=f"lzhamlib_{suffix}.lib",
dst=os.path.join(self.package_folder, "lib"),
src=os.path.join(self.build_folder, "lib", build_folder),
keep_path=False
)
copy(
self,
pattern=f"lzhamcomp_{suffix}.lib",
dst=os.path.join(self.package_folder, "lib"),
src=os.path.join(self.build_folder, "lib", build_folder),
keep_path=False
)
copy(
self,
pattern=f"lzhamdecomp_{suffix}.lib",
dst=os.path.join(self.package_folder, "lib"),
src=os.path.join(self.build_folder, "lib", build_folder),
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 @@ -156,11 +173,8 @@ def package_info(self):
self.cpp_info.system_libs.extend(["m", "pthread"])

if is_msvc(self):
lib_names = ["lzhamlib_x64", "lzhamcomp_x64", "lzhamdecomp_x64"]
if self.settings.build_type == "Debug":
# add D to the end of all the names in the lib_names list
lib_names = [f"{lib_name}D" for lib_name in lib_names]
self.cpp_info.libs = lib_names
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;
}

0 comments on commit 9ca33a0

Please sign in to comment.