Skip to content

Commit e834cd9

Browse files
author
Martin Valgur
committed
libidn: fix MSVC build, add shared MSVC build support
1 parent 03ac3af commit e834cd9

File tree

2 files changed

+58
-41
lines changed

2 files changed

+58
-41
lines changed

recipes/libidn/all/conanfile.py

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import os
22

33
from conan import ConanFile
4-
from conan.errors import ConanInvalidConfiguration
54
from conan.tools.build import cross_building
6-
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
7-
from conan.tools.files import get, rmdir, export_conandata_patches, apply_conandata_patches, copy, chdir, replace_in_file, rm
5+
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv, Environment
6+
from conan.tools.files import get, rmdir, export_conandata_patches, apply_conandata_patches, copy, replace_in_file, rm, save
87
from conan.tools.gnu import AutotoolsToolchain, Autotools, AutotoolsDeps
98
from conan.tools.layout import basic_layout
10-
from conan.tools.microsoft import unix_path, is_msvc, check_min_vs
9+
from conan.tools.microsoft import unix_path, is_msvc
1110

12-
required_conan_version = ">=1.33.0"
11+
required_conan_version = ">=1.53.0"
1312

1413

1514
class LibIdnConan(ConanFile):
@@ -56,37 +55,28 @@ def layout(self):
5655
def requirements(self):
5756
self.requires("libiconv/1.17")
5857

59-
def validate(self):
60-
if self.settings.os == "Windows" and self.options.shared:
61-
raise ConanInvalidConfiguration("Shared libraries are not supported on Windows due to libtool limitation")
62-
6358
def build_requirements(self):
6459
if self._settings_build.os == "Windows":
6560
self.win_bash = True
6661
if not self.conf.get("tools.microsoft.bash:path", check_type=str):
6762
self.tool_requires("msys2/cci.latest")
6863
if is_msvc(self):
69-
self.tool_requires("cccl/1.3")
64+
self.tool_requires("automake/1.16.5")
7065

7166
def source(self):
7267
get(self, **self.conan_data["sources"][self.version], strip_root=True)
7368

7469
def generate(self):
7570
env = VirtualBuildEnv(self)
7671
env.generate()
72+
7773
if not cross_building(self):
7874
env = VirtualRunEnv(self)
7975
env.generate(scope="build")
76+
8077
tc = AutotoolsToolchain(self)
8178
if not self.options.shared:
82-
tc.defines.append("LIBIDN_STATIC")
83-
env = tc.environment()
84-
if is_msvc(self):
85-
env.define("CC", "cccl")
86-
env.define("CXX", "cccl")
87-
env.define("LD", "cccl")
88-
if check_min_vs(self, 180, raise_invalid=False):
89-
tc.extra_cflags.append("-FS")
79+
tc.extra_defines.append("LIBIDN_STATIC")
9080
yes_no = lambda v: "yes" if v else "no"
9181
tc.configure_args += [
9282
"--enable-threads={}".format(yes_no(self.options.threads)),
@@ -95,18 +85,48 @@ def generate(self):
9585
"--disable-nls",
9686
"--disable-rpath",
9787
]
98-
tc.generate(env)
99-
deps = AutotoolsDeps(self)
100-
deps.generate()
88+
if is_msvc(self):
89+
tc.extra_cxxflags.append("-FS")
90+
tc.generate()
91+
92+
if is_msvc(self):
93+
env = Environment()
94+
dep_info = self.dependencies["libiconv"].cpp_info.aggregated_components()
95+
env.append("CPPFLAGS", [f"-I{unix_path(self, p)}" for p in dep_info.includedirs] + [f"-D{d}" for d in dep_info.defines])
96+
env.append("_LINK_", [lib if lib.endswith(".lib") else f"{lib}.lib" for lib in (dep_info.libs + dep_info.system_libs)])
97+
env.append("LDFLAGS", [f"-L{unix_path(self, p)}" for p in dep_info.libdirs] + dep_info.sharedlinkflags + dep_info.exelinkflags)
98+
env.append("CFLAGS", dep_info.cflags)
99+
env.vars(self).save_script("conanautotoolsdeps_cl_workaround")
100+
else:
101+
deps = AutotoolsDeps(self)
102+
deps.generate()
103+
104+
if is_msvc(self):
105+
env = Environment()
106+
automake_conf = self.dependencies.build["automake"].conf_info
107+
compile_wrapper = unix_path(self, automake_conf.get("user.automake:compile-wrapper", check_type=str))
108+
ar_wrapper = unix_path(self, automake_conf.get("user.automake:lib-wrapper", check_type=str))
109+
# Workaround for iconv.lib not being found due to linker flag order
110+
libiconv_libdir = unix_path(self, self.dependencies["libiconv"].cpp_info.aggregated_components().libdir)
111+
env.define("CC", f"{compile_wrapper} cl -nologo -L{libiconv_libdir}")
112+
env.define("CXX", f"{compile_wrapper} cl -nologo")
113+
env.define("LD", "link -nologo")
114+
env.define("AR", f'{ar_wrapper} lib')
115+
env.vars(self).save_script("conanbuild_msvc")
101116

102117
def _patch_sources(self):
103118
apply_conandata_patches(self)
119+
# Disable examples and tests
120+
for subdir in ["examples", "tests", "fuzz", "gltests", os.path.join("lib", "gltests"), "doc"]:
121+
save(self, os.path.join(self.source_folder, subdir, "Makefile.in"), "all:\ninstall:\n")
122+
104123
if is_msvc(self):
105124
if self.settings.arch in ("x86_64", "armv8", "armv8.3"):
106125
ssize = "signed long long int"
107126
else:
108127
ssize = "signed long int"
109128
replace_in_file(self, os.path.join(self.source_folder, "lib", "stringprep.h"), "ssize_t", ssize)
129+
110130
if self.settings.os == "Windows":
111131
# Otherwise tries to create a symlink from GNUmakefile to itself, which fails on Windows
112132
replace_in_file(self, os.path.join(self.source_folder, "configure"),
@@ -116,22 +136,27 @@ def _patch_sources(self):
116136

117137
def build(self):
118138
self._patch_sources()
119-
with chdir(self, self.source_folder):
120-
autotools = Autotools(self)
121-
autotools.configure()
122-
autotools.make(args=["V=1"])
139+
autotools = Autotools(self)
140+
autotools.configure()
141+
autotools.make()
123142

124143
def package(self):
125-
copy(self, "COPYING", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
126-
with chdir(self, self.source_folder):
127-
autotools = Autotools(self)
128-
autotools.install()
144+
copy(self, "COPYING", self.source_folder, os.path.join(self.package_folder, "licenses"))
145+
autotools = Autotools(self)
146+
autotools.install()
129147
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
130148
rmdir(self, os.path.join(self.package_folder, "share"))
131149
rm(self, "*.la", os.path.join(self.package_folder, "lib"), recursive=True)
132150

151+
if is_msvc(self) and self.options.shared:
152+
os.rename(os.path.join(self.package_folder, "lib", "idn.dll.lib"),
153+
os.path.join(self.package_folder, "lib", "idn-12.lib"))
154+
133155
def package_info(self):
134-
self.cpp_info.libs = ["idn"]
156+
if is_msvc(self) and self.options.shared:
157+
self.cpp_info.libs = ["idn-12"]
158+
else:
159+
self.cpp_info.libs = ["idn"]
135160
self.cpp_info.set_property("pkg_config_name", "libidn")
136161
if self.settings.os in ["Linux", "FreeBSD"]:
137162
if self.options.threads:

recipes/libidn/all/test_package/conanfile.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,27 @@
33
from conan import ConanFile
44
from conan.tools.build import can_run
55
from conan.tools.cmake import cmake_layout, CMake
6-
from conan.tools.env import VirtualRunEnv
76

87

98
class TestPackageConan(ConanFile):
109
settings = "os", "arch", "compiler", "build_type"
11-
generators = "CMakeDeps", "CMakeToolchain"
10+
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
1211
test_type = "explicit"
1312

1413
def requirements(self):
15-
self.requires(self.tested_reference_str)
16-
17-
def build_requirements(self):
18-
self.tool_requires(self.tested_reference_str)
14+
self.requires(self.tested_reference_str, run=True)
1915

2016
def layout(self):
2117
cmake_layout(self)
2218

23-
def generate(self):
24-
VirtualRunEnv(self).generate(scope="build")
25-
VirtualRunEnv(self).generate(scope="run")
26-
2719
def build(self):
2820
cmake = CMake(self)
2921
cmake.configure()
3022
cmake.build()
3123

3224
def test(self):
3325
if can_run(self):
34-
self.run("idn --help")
26+
self.run("idn --help", env="conanrun")
3527

3628
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
3729
self.run(bin_path, env="conanrun")

0 commit comments

Comments
 (0)