Skip to content

Commit 26d33e0

Browse files
author
Martin Valgur
committed
libidn: migrate to Conan v2
1 parent adc2d85 commit 26d33e0

File tree

6 files changed

+118
-87
lines changed

6 files changed

+118
-87
lines changed

recipes/libidn/all/conandata.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ sources:
55
patches:
66
"1.36":
77
- patch_file: "patches/0001-unconditional-system-stdint-h.patch"
8-
base_path: "source_subfolder"

recipes/libidn/all/conanfile.py

Lines changed: 69 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import os
2+
13
from conan import ConanFile
24
from conan.errors import ConanInvalidConfiguration
3-
from conan.tools.files import get, rmdir
5+
from conan.tools.build import cross_building
6+
from conan.tools.env import Environment, VirtualBuildEnv, VirtualRunEnv
7+
from conan.tools.files import get, rmdir, export_conandata_patches, apply_conandata_patches, copy, chdir, replace_in_file, rm
8+
from conan.tools.gnu import AutotoolsToolchain, Autotools
9+
from conan.tools.microsoft import unix_path, is_msvc
410
from conan.tools.scm import Version
5-
from conans import AutoToolsBuildEnvironment, tools
6-
import contextlib
7-
import functools
8-
import os
911

1012
required_conan_version = ">=1.33.0"
1113

@@ -17,37 +19,26 @@ class LibIdnConan(ConanFile):
1719
topics = ("libidn", "encode", "decode", "internationalized", "domain", "name")
1820
license = "GPL-3.0-or-later"
1921
url = "https://github.com/conan-io/conan-center-index"
20-
settings = "os", "arch", "compiler", "build_type"
21-
options = {
22-
"shared": [True, False],
23-
"fPIC": [True, False],
24-
"threads": [True, False],
25-
}
26-
default_options = {
27-
"shared": False,
28-
"fPIC": True,
29-
"threads": True,
30-
}
3122

32-
@property
33-
def _source_subfolder(self):
34-
return "source_subfolder"
23+
package_type = "library"
24+
settings = "os", "arch", "compiler", "build_type"
25+
options = {"shared": [True, False], "fPIC": [True, False], "threads": [True, False]}
26+
default_options = {"shared": False, "fPIC": True, "threads": True}
3527

3628
@property
3729
def _settings_build(self):
3830
return getattr(self, "settings_build", self.settings)
3931

4032
def export_sources(self):
41-
for patch in self.conan_data.get("patches", {}).get(self.version, []):
42-
self.copy(patch["patch_file"])
33+
export_conandata_patches(self)
4334

4435
def config_options(self):
4536
if self.settings.os == "Windows":
4637
del self.options.fPIC
4738

4839
def configure(self):
4940
if self.options.shared:
50-
del self.options.fPIC
41+
self.options.rm_safe("fPIC")
5142
del self.settings.compiler.libcxx
5243
del self.settings.compiler.cppstd
5344

@@ -59,87 +50,90 @@ def validate(self):
5950
raise ConanInvalidConfiguration("Shared libraries are not supported on Windows due to libtool limitation")
6051

6152
def build_requirements(self):
62-
if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"):
63-
self.build_requires("msys2/cci.latest")
64-
if self.settings.compiler == "Visual Studio":
65-
self.build_requires("automake/1.16.5")
53+
if self._settings_build.os == "Windows":
54+
self.win_bash = True
55+
if not self.conf.get("tools.microsoft.bash:path", check_type=str):
56+
self.tool_requires("msys2/cci.latest")
57+
if is_msvc(self):
58+
self.tool_requires("automake/1.16.5")
6659

6760
def source(self):
68-
get(self, **self.conan_data["sources"][self.version],
69-
destination=self._source_subfolder, strip_root=True)
70-
71-
@contextlib.contextmanager
72-
def _build_context(self):
73-
if self.settings.compiler == "Visual Studio":
74-
with tools.vcvars(self):
75-
env = {
76-
"CC": "{} cl -nologo".format(tools.unix_path(self.deps_user_info["automake"].compile)),
77-
"CXX": "{} cl -nologo".format(tools.unix_path(self.deps_user_info["automake"].compile)),
78-
"LD": "{} link -nologo".format(tools.unix_path(self.deps_user_info["automake"].compile)),
79-
"AR": "{} lib".format(tools.unix_path(self.deps_user_info["automake"].ar_lib)),
80-
}
81-
with tools.environment_append(env):
82-
yield
83-
else:
84-
yield
85-
86-
@functools.lru_cache(1)
87-
def _configure_autotools(self):
88-
autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows)
89-
autotools.libs = []
61+
get(self, **self.conan_data["sources"][self.version], strip_root=True)
62+
63+
def generate(self):
64+
env = VirtualBuildEnv(self)
65+
env.generate()
66+
if not cross_building(self):
67+
env = VirtualRunEnv(self)
68+
env.generate(scope="build")
69+
tc = AutotoolsToolchain(self)
70+
tc.libs = []
9071
if not self.options.shared:
91-
autotools.defines.append("LIBIDN_STATIC")
92-
if self.settings.compiler == "Visual Studio":
72+
tc.defines.append("LIBIDN_STATIC")
73+
if is_msvc(self):
9374
if Version(self.settings.compiler.version) >= "12":
94-
autotools.flags.append("-FS")
95-
autotools.link_flags.extend("-L{}".format(p.replace("\\", "/")) for p in self.deps_cpp_info.lib_paths)
75+
tc.extra_cflags.append("-FS")
76+
tc.extra_ldflags += ["-L{}".format(p.replace("\\", "/")) for p in self.deps_cpp_info.lib_paths]
9677
yes_no = lambda v: "yes" if v else "no"
97-
conf_args = [
98-
"--enable-shared={}".format(yes_no(self.options.shared)),
99-
"--enable-static={}".format(yes_no(not self.options.shared)),
78+
tc.configure_args += [
10079
"--enable-threads={}".format(yes_no(self.options.threads)),
101-
"--with-libiconv-prefix={}".format(tools.unix_path(self.deps_cpp_info["libiconv"].rootpath)),
80+
"--with-libiconv-prefix={}".format(unix_path(self, self.dependencies["libiconv"].cpp_info.libdirs[0])), # FIXME
10281
"--disable-nls",
10382
"--disable-rpath",
10483
]
105-
autotools.configure(args=conf_args, configure_dir=self._source_subfolder)
106-
return autotools
107-
108-
def build(self):
109-
for patch in self.conan_data.get("patches", {}).get(self.version, []):
110-
tools.patch(**patch)
111-
if self.settings.compiler == "Visual Studio":
84+
tc.generate()
85+
86+
if is_msvc(self):
87+
env = Environment()
88+
automake_conf = self.dependencies.build["automake"].conf_info
89+
compile_wrapper = unix_path(self, automake_conf.get("user.automake:compile-wrapper", check_type=str))
90+
ar_wrapper = unix_path(self, automake_conf.get("user.automake:lib-wrapper", check_type=str))
91+
env.define("CC", f"{compile_wrapper} cl -nologo")
92+
env.define("CXX", f"{compile_wrapper} cl -nologo")
93+
env.define("LD", "link -nologo")
94+
env.define("AR", f'{ar_wrapper} "lib -nologo"')
95+
env.define("NM", "dumpbin -symbols")
96+
env.define("OBJDUMP", ":")
97+
env.define("RANLIB", ":")
98+
env.define("STRIP", ":")
99+
env.vars(self).save_script("conanbuild_msvc")
100+
101+
def _patch_sources(self):
102+
apply_conandata_patches(self)
103+
if is_msvc(self):
112104
if self.settings.arch in ("x86_64", "armv8", "armv8.3"):
113105
ssize = "signed long long int"
114106
else:
115107
ssize = "signed long int"
116-
tools.replace_in_file(os.path.join(self._source_subfolder, "lib", "stringprep.h"),
117-
"ssize_t", ssize)
118-
with self._build_context():
119-
autotools = self._configure_autotools()
108+
replace_in_file(self, os.path.join(self.source_folder, "lib", "stringprep.h"), "ssize_t", ssize)
109+
110+
def build(self):
111+
self._patch_sources()
112+
with chdir(self, self.source_folder):
113+
autotools = Autotools(self)
114+
autotools.configure()
120115
autotools.make(args=["V=1"])
121116

122117
def package(self):
123-
self.copy("COPYING", src=self._source_subfolder, dst="licenses")
124-
with self._build_context():
125-
autotools = self._configure_autotools()
118+
copy(self, "COPYING", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
119+
with chdir(self, self.source_folder):
120+
autotools = Autotools(self)
126121
autotools.install()
127-
128122
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
129123
rmdir(self, os.path.join(self.package_folder, "share"))
130-
tools.remove_files_by_mask(os.path.join(self.package_folder, "lib"), "*.la")
124+
rm(self, os.path.join(self.package_folder, "lib"), "*.la", recursive=True)
131125

132126
def package_info(self):
133127
self.cpp_info.libs = ["idn"]
134-
self.cpp_info.names["pkg_config"] = "libidn"
128+
self.cpp_info.set_property("pkg_config_name", "libidn")
135129
if self.settings.os in ["Linux", "FreeBSD"]:
136130
if self.options.threads:
137131
self.cpp_info.system_libs = ["pthread"]
138132
if self.settings.os == "Windows":
139133
if not self.options.shared:
140134
self.cpp_info.defines = ["LIBIDN_STATIC"]
141135

136+
# TODO: to remove in conan v2
142137
bin_path = os.path.join(self.package_folder, "bin")
143-
self.output.info("Appending PATH environment variable: {}".format(bin_path))
138+
self.output.info(f"Appending PATH environment variable: {bin_path}")
144139
self.env_info.PATH.append(bin_path)
145-
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
cmake_minimum_required(VERSION 3.1)
1+
cmake_minimum_required(VERSION 3.15)
22
project(test_package C)
33

4-
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
5-
conan_basic_setup()
4+
find_package(libidn REQUIRED CONFIG)
65

76
add_executable(${PROJECT_NAME} test_package.c)
8-
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
7+
target_link_libraries(${PROJECT_NAME} PRIVATE libidn::libidn)
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
from conans import ConanFile, CMake, tools
1+
from conan import ConanFile
2+
from conan.tools.build import can_run
3+
from conan.tools.cmake import cmake_layout, CMake
24
import os
35

46

57
class TestPackageConan(ConanFile):
6-
settings = "os", "compiler", "build_type", "arch"
7-
generators = "cmake"
8+
settings = "os", "arch", "compiler", "build_type"
9+
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
10+
test_type = "explicit"
11+
12+
def requirements(self):
13+
self.requires(self.tested_reference_str)
14+
15+
def build_requirements(self):
16+
self.tool_requires(self.tested_reference_str)
17+
18+
def layout(self):
19+
cmake_layout(self)
820

921
def build(self):
1022
cmake = CMake(self)
1123
cmake.configure()
1224
cmake.build()
1325

1426
def test(self):
15-
if not tools.cross_building(self):
16-
self.run("idn --help", run_environment=True)
27+
if can_run(self):
28+
self.run("idn --help")
1729

18-
bin_path = os.path.join("bin", "test_package")
19-
self.run(bin_path, run_environment=True)
30+
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
31+
self.run(bin_path, env="conanrun")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(test_package)
3+
4+
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
5+
conan_basic_setup(TARGETS)
6+
7+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package/
8+
${CMAKE_CURRENT_BINARY_DIR}/test_package/)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from conans import ConanFile, CMake, tools
2+
import os
3+
4+
5+
class TestPackageConan(ConanFile):
6+
settings = "os", "compiler", "build_type", "arch"
7+
generators = "cmake", "cmake_find_package_multi"
8+
9+
def build(self):
10+
cmake = CMake(self)
11+
cmake.configure()
12+
cmake.build()
13+
14+
def test(self):
15+
if not tools.cross_building(self):
16+
self.run("idn --help", run_environment=True)
17+
18+
bin_path = os.path.join("bin", "test_package")
19+
self.run(bin_path, run_environment=True)

0 commit comments

Comments
 (0)