opencv-rust icon indicating copy to clipboard operation
opencv-rust copied to clipboard

Support for building and linking to static OpenCV

Open tylerhawkes opened this issue 5 years ago • 4 comments

This crate has come a long way since we last looked at an opencv wrapper! We've been using another rust port of opencv that we've forked at https://github.com/vivint-smarthome/rustcv/tree/dnn that also builds opencv for cross-compiling if needed. Would you be interested in a pull-request that adds a feature to have the opencv crate build the binaries? It would make it a lot more accessible to us. Part of what we need is to keep the binary size down so we only build it with the imgproc module on 3.4 and nothing else. I'd be happy to figure out if that can be done on the 4.x version as well though it might end up feature gating a lot of code. We use this on both x86-64 and aarch64 linux.

tylerhawkes avatar Mar 16 '20 15:03 tylerhawkes

Can you please elaborate on why you need to build OpenCV in the build.rs? I mean the library itself takes quite a while to build and any cargo clean will purge the results of the lengthy compilation for no good reason. What's the advantage for you over having it installed in a separate directory? And what about "linux mac" from the issue title? :)

twistedfall avatar Mar 16 '20 18:03 twistedfall

In our build system we either build aarch64 or x86_64 and we'd have to build one of them either way. This makes it easier to build and we can use an archive instead of a shared object. As far as cargo clean goes, we use sccache so most build steps are very fast and I usually only clean things out when I upgrade rust every 6 weeks.

The linux mac in the title is because we have a script that should work for both of them but not on Windows, so this would only be available on linux and mac.

tylerhawkes avatar Mar 17 '20 19:03 tylerhawkes

Well, I can't see why not then, the PR would be welcome. It would be nice to keep this functionality explicitly opt-in though so that there is little surprise to the consumers of the crate.

twistedfall avatar Mar 18 '20 13:03 twistedfall

This is pretty neat! I've roughly adapted your build.rs to work on Windows as well: https://github.com/framp/joystick-mapper/blob/master/opencv-sys/build.rs

The only changes needed were:

  • Using the correct separator depending on platform (I'm not 100% sure this is needed)
  • Using OsString's into_string() instead of to_bytes (which is platform specific)
  • Recursively linking library in the out folder obtained after building opencv: this is needed because on windows the lib folder is scoped by target. The proper approach would be to embed the logic implemented in the makefiles and calculate the correct path depending on platform
Screenshot 2020-12-10 at 02 30 24 This is the folder structure on Mac. On windows it's something like `/x64/mingw64/lib`

In terms of compiler requirements I had problems with Visual Studio and several mingw installations. g++ on x86_64_posix-seh ended up being the one. More instructions here: https://github.com/framp/joystick-mapper/blob/master/scripts/package.sh#L5

framp avatar Dec 10 '20 02:12 framp

The static build is now tested as part of the CI, so I think we can close this as done: https://github.com/twistedfall/opencv-rust#static-build

twistedfall avatar Dec 13 '22 15:12 twistedfall