houqp / leptess

Productive and safe Rust binding for leptonica and tesseract
https://houqp.github.io/leptess/leptess/index.html
MIT License
258 stars 29 forks source link

run under Windows #3

Closed blr21560 closed 4 years ago

blr21560 commented 5 years ago

Hi,

I can't link under Windows. The linker search lept.lib. Where can i find this library for windows ? Thanks,

houqp commented 5 years ago

I am not familiar with windows, could you try the Linux subsystem?

TheRadioGuy commented 4 years ago

Yeah, I need help with Windows too

ccouzens commented 4 years ago

I'll give it a go and see what I can figure out this weekend.

Lept is the leptonica library http://www.leptonica.org/ https://github.com/DanBloomberg/leptonica

ccouzens commented 4 years ago

These instructions are a work in progress. They don't actually get this running, but I feel like I made progress towards it.

Please install:

Please run:

vcpkg install leptonica tesseract

Set your CPATH for clang.

# Modify to be your vcpkg installation directory
set CPATH=C:\Users\ccouz\Documents\git\github.com\Microsoft\vcpkg\installed\x64-windows\include

⚠️ But this doesn't make it link. Leptonica.lib is installed at C:\Users\ccouz\Documents\git\github.com\Microsoft\vcpkg\installed\x64-windows\lib\leptonica-1.78.0.lib on my computer. I can't work out what environment variable to set to make it pick this up. Renaming it to lept.lib didn't seem to help.

ccouzens commented 4 years ago

It's not a very satisfactory thing to do, but by copying

tesseract.lib (renamed from tesseract41.lib)
lept.lib (renamed from leptonica-1.78.0.lib)

from C:\Users\ccouz\Documents\git\github.com\Microsoft\vcpkg\installed\x64-windows\lib to the leptess directory, and with this minor patch

diff --git a/src/leptonica.rs b/src/leptonica.rs
index 1cea6eb7..0766ed88 100644
--- a/src/leptonica.rs
+++ b/src/leptonica.rs
@@ -103,7 +103,7 @@ impl Boxa {
         unsafe { (*self.raw).n as usize }
     }

-    pub fn get_box(&self, i: usize, flag: u32) -> Option<Box> {
+    pub fn get_box(&self, i: usize, flag: i32) -> Option<Box> {
         unsafe {
             let b = capi::boxaGetBox(self.raw, i as i32, flag as i32);
             if b.is_null() {

I was able to run cargo test.

I'd like the input of someone more familiar with building on Windows, so that we can make it far more seamless.

TheRadioGuy commented 4 years ago

Anyway it's kind of progress

ccouzens commented 4 years ago

I suspect the cause is that the 2 sys libraries used by leptess do not compile nicely on windows:

I'll try and update them with the suggestions from this article. Maybe next weekend. It might mean some big changes to them (submoduling and compiling leptonica and tesseract for example).

ccouzens commented 4 years ago

I'll try and update them with the suggestions from this article. Maybe next weekend. It might mean some big changes to them (submoduling and compiling leptonica and tesseract for example).

This is currently beyond me, but (as always) I'll accept patches.

rucoder commented 4 years ago

I figured it out for msvc toolchain! I'm building using VS 2019 community edition.

  1. You should build from VS 2019 x64 native tools command prompt
  2. set LIBCLANG_PATH=C:\'**your path to LLVM distro**'\bin
  3. set INCLUDE=%INCLUDE%; '**your path**'\vcpkg\packages\leptonica_x64-windows\include
  4. do 'cargo build' in your project directory

note, that vcpkg build x86 libs by default. If you need x64 libs (like in my case) set VCPKG_DEFAULT_TRIPLET=x64-windows before installing them using vcpkg

EDIT: forgot to add tesseract set INCLUDE=%INCLUDE%;**your path**\vcpkg\packages\leptonica_x64-windows\include;**your path**\vcpkg\packages\tesseract_x64-windows\include

EDIT 2: Sorry, writing this late at night. forgot the most important part set LIBPATH=%LIBPATH%;**your path**\vcpkg\packages\tesseract_x64-windows\lib\;**your path**\vcpkg\packages\leptonica_x64-windows\lib\

and make symbolic links in corresponding library directories mklink lept.lib leptonica-1.78.0.lib mklink tesseract.lib tesseract41.lib

rucoder commented 4 years ago

I'll try to come up with fixes for both *-sys crates. I'm going to use vcpkg crate for this purpose and conditionally use it on win platforms

Shaddy commented 4 years ago

Thanks @rucoder for the update and @ccouzens for your work. Hope this leads into a usable workaround for Windows :). I'll try tonight these steps.

TheRadioGuy commented 4 years ago

@rucoder What is LLVM ?

ccouzens commented 4 years ago

@rucoder What is LLVM ?

@DuckerMan it is a tool used to help building compilers. The Rust compiler uses it. It's also used by the compilers of many other languages. https://en.wikipedia.org/wiki/LLVM

The C compiler that uses LLVM is called Clang. I suspect Clang and LLVM are distributed together because C is a very important language.

I guess you're meant to download the Windows prebuilt binary from http://releases.llvm.org/download.html in order to follow @rucoder's instructions.

set LIBCLANG_PATH=C:\'your path to LLVM distro'\bin

ccouzens commented 4 years ago

I'll try to come up with fixes for both *-sys crates. I'm going to use vcpkg crate for this purpose and conditionally use it on win platforms

Thank you @rucoder

rucoder commented 4 years ago

Ok, the fixes for both -sys crates are ready. This is my first time contributing to Rust libraries. Here is the question: I see that leptess depends on not the latest version of sys crates. Should I just create PRS for the latest versions and then owners will integrate them back to earlier versions or... that is the process?

ccouzens commented 4 years ago

Ok, the fixes for both -sys crates are ready. This is my first time contributing to Rust libraries. Here is the question: I see that leptess depends on not the latest version of sys crates. Should I just create PRS for the latest versions and then owners will integrate them back to earlier versions or... that is the process?

First submit PRs for both *-sys crates. Once those are merged and packaged, then any of us can update the dependencies of leptess.

Actually, I'm not sure we'll even have to update the dependencies of leptess, because they're flexible.

Leptess will currently try and install the newest version of leptonica-sys 0.3.x, and tesseract-sys 0.5.x.

rucoder commented 4 years ago

I've submitted two PRs. Please note that following settings must be added to tasks.json in VS code if you use it

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cargo",
            "subcommand": "build",
            "problemMatcher": [
                "$rustc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            // These two environment variables MUST be defined for successfull build
            // if you need staically linked libraries then set
           // RUSTFLAGS=-Ctarget-feature=+crt-static instead of VCPKGRS_DYNAMIC
            "options": {
                "env": {
                    "LIBCLANG_PATH": "C:\\LLVM-OFF\\bin",
                    "VCPKGRS_DYNAMIC": "1"
                }
            }
        }
    ]
}

From vcpkg documentation

Note: You must set one of RUSTFLAGS=-Ctarget-feature=+crt-static or VCPKGRS_DYNAMIC=1 in your environment or the vcpkg-rs helper will not find any libraries. If VCPKGRS_DYNAMIC is set, cargo install will generate dynamically linked binaries, in which case you will have to arrange for dlls from your Vcpkg installation to be available in your path.

https://docs.rs/vcpkg/0.2.8/vcpkg/

I think README files should be updated as well

ccouzens commented 4 years ago

Thanks to the excellent work @rucoder has done with tesseract-sys and leptonica-sys, I've been able to bring it all together and make a PR for running leptess on Windows.

Feel free to try it out and leave feedback if required https://github.com/houqp/leptess/pull/11

rucoder commented 4 years ago

As I can see the work is not finished. The package version should be bumped to 0.8.2 and it should be re-released on craits.io. It is not enough to just bump leptonia-sys and teserract-sys craits version in cargo.toml

ccouzens commented 4 years ago

Hi @rucoder , thanks for prompting me. I'll do that now.

rucoder commented 4 years ago

Hi @rucoder , thanks for prompting me. I'll do that now.

I'll be glad o be a first tester for this :)

ccouzens commented 4 years ago

leptess = "0.8.2"

has been published. @rucoder let me know how you get on and thanks for your patience

rucoder commented 4 years ago

I confirm that 0.8.2 builds and runs just fine on my PC. The issue can be closed I guess