Skip to content

Commit

Permalink
(#23410) libdatrie: new recipe
Browse files Browse the repository at this point in the history
* libdatrie: new recipe

* libdatrie: shared macOS builds are broken

* libdatrie: fix Windows build

* libdatrie: make libiconv visible

test_package tries to find libiconv.so and fails otherwise

* libdatrie: add /FS
  • Loading branch information
valgur authored Sep 18, 2024
1 parent abb7a21 commit 5ef4fe3
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 0 deletions.
9 changes: 9 additions & 0 deletions recipes/libdatrie/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sources:
"0.2.13":
url: "https://linux.thai.net/pub/ThaiLinux/software/libthai/libdatrie-0.2.13.tar.xz"
sha256: "12231bb2be2581a7f0fb9904092d24b0ed2a271a16835071ed97bed65267f4be"
patches:
"0.2.13":
- patch_file: "patches/fix-exports.patch"
patch_type: "portability"
patch_description: "Fix .def exports for Windows"
138 changes: 138 additions & 0 deletions recipes/libdatrie/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import os

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.apple import fix_apple_shared_install_name, is_apple_os
from conan.tools.build import cross_building
from conan.tools.env import Environment, VirtualBuildEnv, VirtualRunEnv
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, rm, rmdir, save, rename
from conan.tools.gnu import Autotools, AutotoolsDeps, AutotoolsToolchain
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc, unix_path

required_conan_version = ">=1.54.0"


class LibdatrieConan(ConanFile):
name = "libdatrie"
description = "Implementation of double-array structure for representing tries"
license = "LGPL-2.1-or-later"
homepage = "https://linux.thai.net/projects/datrie"
url = "https://github.com/conan-io/conan-center-index"
topics = ("trie", "double-array-trie", "datrie")

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"tools": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"tools": False,
}

@property
def _settings_build(self):
return getattr(self, "settings_build", self.settings)

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

def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")

def layout(self):
basic_layout(self, src_folder="src")

def export_sources(self):
export_conandata_patches(self)

def requirements(self):
self.requires("libiconv/1.17")

def validate(self):
if is_apple_os(self) and self.options.shared:
# Fails due to build script bugs
raise ConanInvalidConfiguration("shared builds on Apple OS-s are not supported. Contributions are welcome!")

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 is_msvc(self):
self.tool_requires("automake/1.16.5")

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

def generate(self):
env = VirtualBuildEnv(self)
env.generate()

if not cross_building(self):
env = VirtualRunEnv(self)
env.generate(scope="build")

tc = AutotoolsToolchain(self)
tc.configure_args.extend([
"--disable-doxygen-doc",
])
if is_msvc(self):
tc.extra_cflags.append("-FS")
tc.extra_cxxflags.append("-FS")
tc.generate()

if is_msvc(self):
env = Environment()
iconv_info = self.dependencies["libiconv"].cpp_info
env.append("CPPFLAGS", [f"-I{unix_path(self, iconv_info.includedir)}"] + [f"-D{d}" for d in iconv_info.defines])
env.append("_LINK_", [lib if lib.endswith(".lib") else f"{lib}.lib" for lib in iconv_info.libs])
env.append("LDFLAGS", [f"-L{unix_path(self, iconv_info.libdir)}"])
env.vars(self).save_script("conanautotoolsdeps_cl_workaround")
else:
tc = AutotoolsDeps(self)
tc.generate()

if is_msvc(self):
env = Environment()
automake_conf = self.dependencies.build["automake"].conf_info
compile_wrapper = unix_path(self, automake_conf.get("user.automake:compile-wrapper", check_type=str))
ar_wrapper = unix_path(self, automake_conf.get("user.automake:lib-wrapper", check_type=str))
env.define("CC", f"{compile_wrapper} cl -nologo")
env.define("LD", "link -nologo")
env.define("AR", f"{ar_wrapper} lib")
env.vars(self).save_script("conanbuild_msvc")

def build(self):
apply_conandata_patches(self)
autotools = Autotools(self)
autotools.configure()
if not self.options.tools:
save(self, os.path.join(self.build_folder, "tools", "Makefile"), "all:\n\t\ninstall:\n\t\n")
# Unnecessary and breaks MSVC build
save(self, os.path.join(self.build_folder, "man", "Makefile"), "all:\n\t\ninstall:\n\t\n")
autotools.make()

def package(self):
copy(self, "COPYING", self.source_folder, os.path.join(self.package_folder, "licenses"))
autotools = Autotools(self)
autotools.install()
rm(self, "*.la", os.path.join(self.package_folder, "lib"))
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
rmdir(self, os.path.join(self.package_folder, "share"))
fix_apple_shared_install_name(self)
if is_msvc(self) and self.options.shared:
rename(self, os.path.join(self.package_folder, "lib", "datrie.dll.lib"),
os.path.join(self.package_folder, "lib", "datrie.lib"))

def package_info(self):
self.cpp_info.set_property("pkg_config_name", "datrie-0.2")
self.cpp_info.libs = ["datrie"]
7 changes: 7 additions & 0 deletions recipes/libdatrie/all/patches/fix-exports.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--- datrie/libdatrie.def
+++ datrie/libdatrie.def
@@ -1,3 +1,4 @@
+EXPORTS
alpha_map_new
alpha_map_clone
alpha_map_free
8 changes: 8 additions & 0 deletions recipes/libdatrie/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.15)
project(test_package LANGUAGES C)

find_package(libdatrie REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE libdatrie::libdatrie)
target_compile_features(${PROJECT_NAME} PRIVATE c_std_99)
26 changes: 26 additions & 0 deletions recipes/libdatrie/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"

def layout(self):
cmake_layout(self)

def requirements(self):
self.requires(self.tested_reference_str)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
15 changes: 15 additions & 0 deletions recipes/libdatrie/all/test_package/test_package.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <datrie/trie.h>

int main(void) {
AlphaMap *en_map = alpha_map_new();
if (!en_map)
return 1;
if (alpha_map_add_range(en_map, 0x0061, 0x007a) != 0)
return 1;
Trie *test_trie = trie_new(en_map);
if (!test_trie)
return 1;
trie_free(test_trie);
alpha_map_free(en_map);
return 0;
}
3 changes: 3 additions & 0 deletions recipes/libdatrie/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
versions:
"0.2.13":
folder: all

0 comments on commit 5ef4fe3

Please sign in to comment.