opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.32k stars 5.74k forks source link

cmake: find freetype and harfbuzz even on windows #1497

Open crackwitz opened 6 years ago

crackwitz commented 6 years ago

modules/freetype/CMakeLists.txt only works if PKG_CONFIG_FOUND. it could alternatively use find_package(Freetype) (which cmake comes with automatically).

additionally, it'd make sense to introduce a FindHarfbuzz.cmake that has a similar structure to FindFreetype.cmake, and bring it along.

right now I don't have the time to work on this and make a pull request, but I wanted to record this in case someone wants to work on low hanging fruit (or I find the time). CMake documentation about this stuff

note: I got freetype and harfbuzz built on win7 x64 with vs2015, and built into opencv 3.4 (+contrib).

Lyoko-Jeremie commented 5 years ago

Hi, i want to build the freetype module for working on CJK chars. but on windows, cmake GUI not have a config fields to let me set the freetype and harfbuzz path.

when i view the code, i got this:

https://github.com/opencv/opencv_contrib/blob/5eaa25c95051c3784cc144a9a4244512ef8f0aa0/modules/freetype/CMakeLists.txt#L6-L7

and seems like the ocv_check_modules only work on PkgConfig env :

https://github.com/opencv/opencv/blob/b2abd8ca4197744174d34d96e3053f8e0d7fad54/cmake/OpenCVUtils.cmake#L734-L751


i search on google and find this: https://answers.opencv.org/question/173247/how-to-build-opencv-with-freetype-and-harfbuzz/

it tell me that i must change the cmake file on the freetype modules. and then, i find the gist on this: https://gist.github.com/UnaNancyOwen/14c72a3f10a46d41c359ab6ea307a1d2

seems like only need add follow code to cmake file before the https://github.com/opencv/opencv_contrib/blob/5eaa25c95051c3784cc144a9a4244512ef8f0aa0/modules/freetype/CMakeLists.txt#L6-L7 , then it will work.

find_package(Freetype REQUIRED)

# find_package(HarfBuzz) is not included in cmake
set(HARFBUZZ_DIR "$ENV{HARFBUZZ_DIR}" CACHE PATH "HarfBuzz directory")
find_path(HARFBUZZ_INCLUDE_DIRS
    NAMES hb-ft.h PATH_SUFFIXES harfbuzz
    HINTS ${HARFBUZZ_DIR}/include)
find_library(HARFBUZZ_LIBRARIES
    NAMES harfbuzz
    HINTS ${HARFBUZZ_DIR}/lib)
find_package_handle_standard_args(HARFBUZZ
    DEFAULT_MSG HARFBUZZ_LIBRARIES HARFBUZZ_INCLUDE_DIRS)

so, did this way work for any platform ? i think maybe the other way is that add a FindHarfbuzz.cmake on the opencv cmake find like the other files , https://github.com/opencv/opencv/tree/master/cmake or add on this place , https://github.com/opencv/opencv_contrib/tree/master/modules/freetype .

and i find a similar work on google : https://github.com/SFTtech/openage/blob/master/buildsystem/modules/FindHarfBuzz.cmake , seems this is a old issue, hope it can help you.

alalek commented 5 years ago

cmake GUI

Use CMake regular command line and pass all required variables (including _FOUND):

P.S. ocv_check_modules(), CMake's find_package(), etc are just scripts for automatic searching of required dependency - they usually can by bypassed if you specify variables via the CMake's command-line.

Lyoko-Jeremie commented 5 years ago

thank you @alalek but i think that, if pass they by CMake's command-line, it dont have any check or warnning for people who pass a wrong path.

example, the HARFBUZZ not build hb-ft.h by defualt utill you tell it HB_HAVE_FREETYPE=1 , but the opencv freetype module need the hb-ft.h for work. so, if some one have wrong HARFBUZZ (like me), he have to build fail 3 times to find what happend and fix this. but if cmake have a check and warnning, this mistake will find when run the cmake command that like config Eigen or QT to opencv.

BTW: in windows, if you want build OpenCV with Eigen/QT/nofree/extern/CUDA/TBB/MKL/OpenMP/OpenGL and other (like me) , cmake cannot auto find all lib path, you need pass all the sub value of every lib . you won't want to pass all the config by command. the only thing you want is that let the Cmake GUI do many check and find what lost and you check it and add a lost path after a cup of coffe or tea.

OgreTransporter commented 4 years ago

With find_package you can also see the missing paths in the gui, with ocv_check_modules you have to know what the variables are called.

Edit:

Use CMake regular command line and pass all required variables (including _FOUND):

-DFREETYPE_FOUND=1 -DFREETYPE_LIBRARIES=... -DFREETYPE_INCLUDE_DIRS=... -DHARFBUZZ_FOUND=1 -DHARFBUZZ_LIBRARIES=... -DHARFBUZZ_INCLUDE_DIRS=…

This is not working. The search script deletes the *_LIBRARIES variables defined on the command line. Include directories work, but the libraries are gone. For me only the way from Lyoko-Jeremie works with find_package.

xkszltl commented 4 years ago

@OgreTransporter You need to set a few more, see:

OgreTransporter commented 4 years ago

My Workarround: I added the line

ocv_target_link_libraries(${the_modules} ${BUGFIX_LIBRARIES})

to the file src\modules\world\CMakeLists.txt and then called CMake with

-D BUGFIX_LIBRARIES:FILEPATH=debug;D:\deps\freetype2\lib\freetyped.lib;debug;D:\deps\bzip2\lib\bzip2staticd.lib;debug;D:\deps\HarfBuzz\lib\harfbuzzd.lib;optimized;D:\deps\freetype2\lib\freetype.lib;optimized;D:\deps\bzip2\lib\bzip2static.lib;optimized;D:\deps\HarfBuzz\lib\harfbuzz.lib

It's working.

gansm255 commented 4 years ago

Hi, Any one worked on cross compiling freetype and harfbuzz for aarch64 target? ocv_check_modules(FREETYPE freetype2) in CMakeLists.txt file provided with freetype 2.10.2 does not work in OpenCV 4.3.0. same for harfbuzz module also. please guide me.

Plavit commented 3 years ago

As someone also said above, the issue is in:

_opencvcontrib/modules/freetype/CMakeLists.txt

This change should fix it:

set(the_description "FreeType module. It enables to draw strings with outlines and mono-bitmaps/gray-bitmaps.")

find_package(Freetype REQUIRED)

# find_package(HarfBuzz) is not included in cmake
set(HARFBUZZ_DIR "$ENV{HARFBUZZ_DIR}" CACHE PATH "HarfBuzz directory")
find_path(HARFBUZZ_INCLUDE_DIRS
    NAMES hb-ft.h PATH_SUFFIXES harfbuzz
    HINTS ${HARFBUZZ_DIR}/include)
find_library(HARFBUZZ_LIBRARIES
    NAMES harfbuzz
    HINTS ${HARFBUZZ_DIR}/lib)
find_package_handle_standard_args(HARFBUZZ
    DEFAULT_MSG HARFBUZZ_LIBRARIES HARFBUZZ_INCLUDE_DIRS)

if(NOT FREETYPE_FOUND)
  message(STATUS "freetype2:   NO")
else()
  message(STATUS "freetype2:   YES")
endif()

if(NOT HARFBUZZ_FOUND)
  message(STATUS "harfbuzz:   NO")
else()
  message(STATUS "harfbuzz:   YES")
endif()

if(FREETYPE_FOUND AND HARFBUZZ_FOUND)
  ocv_define_module(freetype opencv_core opencv_imgproc PRIVATE_REQUIRED ${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES} WRAP python)
  ocv_include_directories(${FREETYPE_INCLUDE_DIRS} ${HARFBUZZ_INCLUDE_DIRS})
else()
  ocv_module_disable(freetype)
endif()

Source: https://gist.github.com/UnaNancyOwen/14c72a3f10a46d41c359ab6ea307a1d2