Skip to content

Commit

Permalink
Avoid conans import
Browse files Browse the repository at this point in the history
  • Loading branch information
partiallyderived committed Nov 7, 2022
1 parent 8311665 commit 1cde03f
Showing 1 changed file with 73 additions and 5 deletions.
78 changes: 73 additions & 5 deletions recipes/lzham/all/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import (
apply_conandata_patches, copy, export_conandata_patches, get, rmdir
apply_conandata_patches,
copy,
export_conandata_patches,
get,
replace_in_file,
rmdir
)
from conan.tools.layout import vs_layout
from conan.tools.microsoft import MSBuildDeps, MSBuildToolchain, VCVars, is_msvc
from conans import MSBuild
from conan.tools.microsoft import (
MSBuild, MSBuildDeps, MSBuildToolchain, VCVars, is_msvc
)

required_conan_version = ">=1.52.0"

SLN_FILE = "lzham.sln"


class PackageConan(ConanFile):
name = "lzham"
Expand All @@ -36,6 +44,63 @@ class PackageConan(ConanFile):
"disable_threading": False,
}

def _patch_sources(self):
apply_conandata_patches(self)

if not is_msvc(self):
# Remove lzhamtest from root CMakeLists.txt.
replace_in_file(
self,
os.path.join(self.source_folder, "CMakeLists.txt"),
"add_subdirectory(lzhamtest)\n",
""
)
return
new_sln = []
# Remove example and test projects from sln.
with open(os.path.join(
self.source_folder, "lzham.sln"
), encoding="utf-8") as f:
line = f.readline()
while line:
if (
line.startswith("Project(")
and ("lzhamtest" in line or "example" in line)
):
# Don't write the current line and skip the "EndProject"
# line.
f.readline()
else:
new_sln.append(line)
line = f.readline()
with open(os.path.join(
self.source_folder, "lzham.sln"
), "w", encoding="utf-8") as f:
f.write("".join(new_sln))

# Inject conantoolchain.props so that correct platform toolset is used.
projects = [(x, f"{x}.vcxproj") for x in (
"lzhamcomp",
"lzhamdecomp",
"lzhamlib",
)]
projects.append(("lzhamdll", "lzham.vcxproj"))
search_str = (
' <Import Project='
'"$(VCTargetsPath)\\Microsoft.Cpp.Default.props" />'
)

for p in projects:
replace_in_file(
self,
os.path.join(self.source_folder, *p),
search_str,
' <ImportGroup Label="PropertySheets">\n'
' <Import Project="..\\conan\\conantoolchain.props" />\n'
' </ImportGroup>\n'
+ search_str
)

def export_sources(self):
export_conandata_patches(self)

Expand Down Expand Up @@ -74,13 +139,16 @@ def generate(self):
tc.generate()

def build(self):
apply_conandata_patches(self)
self._patch_sources()
if is_msvc(self):
msbuild = MSBuild(self)
msbuild.build_type = (
"Debug" if self.settings.build_type == "Debug" else "Release"
)
msbuild.build("lzham.sln", platforms={"x86": "Win32"})
msbuild.platform = (
"Win32" if self.settings.arch == "x86" else msbuild.platform
)
msbuild.build(sln="lzham.sln")
else:
cmake = CMake(self)
cmake.configure()
Expand Down

0 comments on commit 1cde03f

Please sign in to comment.