Skip to content

Commit

Permalink
fix msvc build
Browse files Browse the repository at this point in the history
  • Loading branch information
shun2wang committed Sep 15, 2024
1 parent 791e9e2 commit fbd9dfa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
6 changes: 4 additions & 2 deletions recipes/readstat/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
sources:
"1.1.9":
url: "https://github.com/WizardMac/ReadStat/releases/download/v1.1.9/readstat-1.1.9.tar.gz"
sha256: "3a232b9e852d10173e2f25da9155afe2e129a30d1fc6c9aac142cdc5cbfe527e"
- url: "https://github.com/WizardMac/ReadStat/archive/refs/tags/v1.1.9.zip"
sha256: "2a52026ae6e2348780060f4a769e272e0e1c04dd918b8e76d66e8c9861a81cbc"
- url: "https://github.com/WizardMac/ReadStat/releases/download/v1.1.9/readstat-1.1.9.tar.gz"
sha256: "3a232b9e852d10173e2f25da9155afe2e129a30d1fc6c9aac142cdc5cbfe527e"
74 changes: 57 additions & 17 deletions recipes/readstat/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from conan import ConanFile
from conan import ConanFile, conan_version
from conan.tools.build import cross_building
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
from conan.tools.files import apply_conandata_patches, chdir, copy, export_conandata_patches, get, rm, rmdir
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, rm, rmdir, load, save
from conan.tools.gnu import Autotools, AutotoolsDeps, AutotoolsToolchain
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc, NMakeDeps, NMakeToolchain
from conan.tools.microsoft import is_msvc, MSBuildDeps, MSBuildToolchain, MSBuild
import os
import platform


required_conan_version = ">=1.55.0"
required_conan_version = ">=1.57.0"

class ReadstatConan(ConanFile):
name = "readstat"
Expand Down Expand Up @@ -54,19 +55,41 @@ def requirements(self):

# if another tool than the compiler or autotools is required to build the project (pkgconf, bison, flex etc)
def build_requirements(self):
if self._settings_build.os == "Windows":
self.win_bash = True
if not self.conf.get("tools.microsoft.bash:path", check_type=str):
self.tool_requires("msys2/cci.latest")
# if self._settings_build.os == "Windows":
# self.win_bash = True
# if not self.conf.get("tools.microsoft.bash:path", check_type=str):
# self.tool_requires("msys2/cci.latest")
if self._settings_build.os == "Macos":
self.tool_requires("libtool/2.4.7")

def _sys_compiler(self):
return self.info.settings.compiler

@property
def _is_windows_msvc(self):
try:
return self.settings.os == "Windows"
except:
return self.info.settings.os == "Windows"

def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)
if platform.system() == "Windows": # 'self.settings' access in 'source()' method was forbidden
print("微软Windows")
print(self.conan_data["sources"][self.version][0])
get(self, **self.conan_data["sources"][self.version][0], strip_root=True)
else:
get(self, **self.conan_data["sources"][self.version][1], strip_root=True)

def _msbuild_configuration(self):
return "Debug" if self.settings.build_type == "Debug" else "Release"

def generate(self):
if is_msvc(self):
tc = NMakeToolchain(self)
tc = MSBuildToolchain(self)
tc.configuration = self._msbuild_configuration
tc.generate()
deps = NMakeDeps(self)
deps = MSBuildDeps(self)
deps.configuration = self._msbuild_configuration
deps.generate()
else:
env = VirtualBuildEnv(self)
Expand All @@ -78,23 +101,40 @@ def generate(self):
tc.generate()
deps = AutotoolsDeps(self)
deps.generate()

def _print_directory_structure(self, folder):
self.output.info(f"Directory structure for: {folder}")
for root, dirs, files in os.walk(folder):
level = root.replace(folder, '').count(os.sep)
indent = ' ' * 4 * level
self.output.info(f"{indent}{os.path.basename(root)}/")
subindent = ' ' * 4 * (level + 1)
for f in files:
self.output.info(f"{subindent}{f}")

def build(self):
apply_conandata_patches(self)
if is_msvc(self):
args = "readstat_i.lib READSTAT_EXPORT=-DDLL_EXPORT" if self.options.shared else "readstat.lib"
with chdir(self, self.source_folder):
self.run(f"nmake -f makefile.vc {args}")
msbuild = MSBuild(self)
msbuild.build_type = self._msbuild_configuration

self._print_directory_structure(self.source_folder)
self.output.info(f"Using solution file at: {self.source_folder}")

msbuild.build(sln=os.path.join(self.source_folder, "VS17", "ReadStat.sln"))
else:
autotools = Autotools(self)
# autotools.autoreconf()
autotools.configure()
autotools.make()

def package(self):
copy(self, pattern="LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
# upstream didn't pack license file into distribution
copy(self, "NEWS", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
if is_msvc(self):
copy(self, "readstat.h", src=os.path.join(self.source_folder, "headers"), dst=os.path.join(self.package_folder, "include"))
copy(self, "*.a", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
copy(self, "*.so", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
copy(self, "*.dylib", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
copy(self, "*.lib", src=self.source_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
copy(self, "*.dll", src=self.source_folder, dst=os.path.join(self.package_folder, "bin"), keep_path=False)
else:
Expand Down

0 comments on commit fbd9dfa

Please sign in to comment.