Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tesseract/4.1.1: libarchive dep and portable build #4278

Merged
merged 17 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion recipes/tesseract/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.8)
cmake_minimum_required(VERSION 3.1)
project(cmake_wrapper)

include(conanbuildinfo.cmake)
Expand Down
6 changes: 6 additions & 0 deletions recipes/tesseract/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ sources:
"4.1.1":
url: "https://github.com/tesseract-ocr/tesseract/archive/4.1.1.tar.gz"
sha256: "2a66ff0d8595bff8f04032165e6c936389b1e5727c3ce5a27b3e059d218db1cb"
patches:
"4.1.1":
- patch_file: "patches/0001-Remove-config.patch"
base_path: "source_subfolder"
- patch_file: "patches/0002-Link-with-targets.patch"
base_path: "source_subfolder"
98 changes: 63 additions & 35 deletions recipes/tesseract/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from conans import ConanFile, CMake, tools
from conans.tools import Version
from conans.errors import ConanInvalidConfiguration


Expand All @@ -10,18 +11,33 @@ class TesseractConan(ConanFile):
topics = ("conan", "ocr", "image", "multimedia", "graphics")
license = "Apache-2.0"
homepage = "https://github.com/tesseract-ocr/tesseract"
exports_sources = ["CMakeLists.txt"]
exports_sources = ["CMakeLists.txt", "patches/*"]
generators = "cmake", "cmake_find_package"
settings = "os", "arch", "compiler", "build_type"
options = {"shared": [True, False],
"fPIC": [True, False],
"with_auto_optimize": [True, False],
"with_march_native": [True, False],
"with_training": [True, False]}
default_options = {'shared': False, 'fPIC': True, 'with_training': False}
_source_subfolder = "source_subfolder"
_build_subfolder = "build_subfolder"
default_options = {"shared": False,
"fPIC": True,
"with_auto_optimize": False,
"with_march_native": False,
"with_training": False}

_cmake = None

requires = "leptonica/1.79.0"
@property
def _source_subfolder(self):
return "source_subfolder"

@property
def _build_subfolder(self):
return "build_subfolder"

def requirements(self):
self.requires("leptonica/1.80.0")
self.requires("libarchive/3.5.1")

def source(self):
tools.get(**self.conan_data["sources"][self.version])
Expand All @@ -31,50 +47,54 @@ def source(self):
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
if self.options.shared:
del self.options.fPIC
theirix marked this conversation as resolved.
Show resolved Hide resolved
if self.options.with_training:
# do not enforce failure and allow user to build with system cairo, pango, fontconfig
self.output.warn("*** Build with training is not yet supported, continue on your own")

def configure(self):
# Exclude old compilers not supported by tesseract
compiler_version = tools.Version(self.settings.compiler.version)
if (self.settings.compiler == "gcc" and compiler_version < "5") or \
(self.settings.compiler == "clang" and compiler_version < "5"):
raise ConanInvalidConfiguration("tesseract/{} requires Clang >= 5".format(self.version))
compiler = str(self.settings.compiler)
compiler_version = Version(self.settings.compiler.version.value)

minimal_version = {
"Visual Studio": "14",
"gcc": "5",
"clang": "5",
"apple-clang": "6"
}

if compiler in minimal_version and \
theirix marked this conversation as resolved.
Show resolved Hide resolved
compiler_version < minimal_version[compiler]:
raise ConanInvalidConfiguration("{} requires a {} version >= {}".format(self.name, compiler, compiler_version))

def _configure_cmake(self):
if self._cmake:
return self._cmake
cmake = self._cmake = CMake(self)
cmake.definitions['BUILD_TRAINING_TOOLS'] = self.options.with_training
cmake.definitions["BUILD_TRAINING_TOOLS"] = self.options.with_training
cmake.definitions["STATIC"] = not self.options.shared
# Use CMake-based package build and dependency detection, not the pkg-config, cppan or SW
cmake.definitions['CPPAN_BUILD'] = False
cmake.definitions['SW_BUILD'] = False
cmake.definitions["CPPAN_BUILD"] = False
cmake.definitions["SW_BUILD"] = False

# avoid accidentally picking up system libarchive
cmake.definitions['CMAKE_DISABLE_FIND_PACKAGE_LIBARCHIVE'] = True
cmake.definitions["AUTO_OPTIMIZE"] = self.options.with_auto_optimize

# Set Leptonica_DIR to ensure that find_package will be called in original CMake file
cmake.definitions['Leptonica_DIR'] = self.deps_cpp_info['leptonica'].rootpath
cmake.definitions["Leptonica_DIR"] = self.deps_cpp_info["leptonica"].rootpath

cmake.configure(build_folder=self._build_subfolder)
return cmake

def _patch_sources(self):
# Use generated cmake module files
tools.replace_in_file(
os.path.join(self._source_subfolder, "CMakeLists.txt"),
"find_package(Leptonica ${MINIMUM_LEPTONICA_VERSION} REQUIRED CONFIG)",
"find_package(Leptonica ${MINIMUM_LEPTONICA_VERSION} REQUIRED)")
# Variable Leptonica_LIBRARIES does not know about its dependencies which are handled only
# by exported cmake/pc files which are not used by Conan.
# Therefore link with exported target from the autogenerated CMake file by the cmake_find_package
# that contains information about all dependencies
tools.replace_in_file(
os.path.join(self._source_subfolder, "CMakeLists.txt"),
"${Leptonica_LIBRARIES}",
"Leptonica::Leptonica")
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)

if not self.options.with_march_native:
tools.replace_in_file(
os.path.join(self._source_subfolder, "CMakeLists.txt"),
"if(COMPILER_SUPPORTS_MARCH_NATIVE)",
"if(False)")

def build(self):
self._patch_sources()
Expand All @@ -92,13 +112,21 @@ def package(self):
tools.rmdir(os.path.join(self.package_folder, 'lib', 'pkgconfig'))
# remove cmake
tools.rmdir(os.path.join(self.package_folder, 'cmake'))
# required for 5.0
tools.rmdir(os.path.join(self.package_folder, 'lib', 'cmake'))
Comment on lines +118 to +119
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no 5.0 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was experimenting in packaging 5.0.x-alpha. Of course, I can drop it now and add for the final version.


def package_info(self):
self.cpp_info.libs = tools.collect_libs(self)
if self.settings.os == "Linux":
self.cpp_info.system_libs = ["pthread"]
elif self.settings.compiler == "Visual Studio":
if not self.options.shared:
self.cpp_info.system_libs = ["ws2_32"]
self.cpp_info.names["cmake_find_package"] = "Tesseract"
self.cpp_info.names["cmake_find_package_multi"] = "Tesseract"

self.cpp_info.components["libtesseract"].libs = tools.collect_libs(self)
self.cpp_info.components["libtesseract"].requires = ["leptonica::leptonica", "libarchive::libarchive" ]

self.cpp_info.components["libtesseract"].names["cmake_find_package"] = "libtesseract"
self.cpp_info.components["libtesseract"].names["cmake_find_package_multi"] = "libtesseract"
self.cpp_info.components["libtesseract"].names["pkg_config"] = "libtesseract"

if self.settings.os == "Linux":
self.cpp_info.components["libtesseract"].system_libs = ["pthread"]
elif self.settings.compiler == "Visual Studio" and not self.options.shared:
self.cpp_info.components["libtesseract"].system_libs = ["ws2_32"]
theirix marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions recipes/tesseract/all/patches/0001-Remove-config.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Use generated cmake module files
diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -221,7 +221,7 @@ else()
pkg_check_modules(Leptonica REQUIRED lept>=${MINIMUM_LEPTONICA_VERSION})
link_directories(${Leptonica_LIBRARY_DIRS})
else()
- find_package(Leptonica ${MINIMUM_LEPTONICA_VERSION} REQUIRED CONFIG)
+ find_package(Leptonica ${MINIMUM_LEPTONICA_VERSION} REQUIRED)
endif()
if (NOT Leptonica_FOUND)
message(FATAL_ERROR "Cannot find required library Leptonica. Quitting!")
18 changes: 18 additions & 0 deletions recipes/tesseract/all/patches/0002-Link-with-targets.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Variable Leptonica_LIBRARIES does not know about its dependencies which are handled only
by exported cmake/pc files which are not used by Conan.
Therefore link with exported target from the autogenerated CMake file by the cmake_find_package
that contains information about all dependencies
diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -491,8 +491,8 @@ elseif (SW_BUILD)
export(TARGETS libtesseract APPEND FILE ${CMAKE_CURRENT_BINARY_DIR}/TesseractTargets.cmake)
else()
target_link_libraries (libtesseract PUBLIC
- ${Leptonica_LIBRARIES}
- ${LibArchive_LIBRARIES}
+ Leptonica::Leptonica
+ LibArchive::LibArchive
)
export(TARGETS libtesseract FILE ${CMAKE_CURRENT_BINARY_DIR}/TesseractTargets.cmake)
endif()
4 changes: 3 additions & 1 deletion recipes/tesseract/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project(test_package)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

find_package(Tesseract REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
target_link_libraries(${PROJECT_NAME} Tesseract::libtesseract)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
2 changes: 1 addition & 1 deletion recipes/tesseract/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
Expand Down