I have a program that relies on the freetype6-dev package. When installing this package, you get different versions when doing it on Ubuntu 14.04 vs 12.02. When doing freetype-config --ftversion
, you get 2.5.2 and 2.4.8 respectively. The problem I have is when I am trying to find these libraries using cmake. With 2.4.8, it finds ft2build.h then goes into <freetype/config/ftheader.h>
to find the rest of the header. In 2.5.2, it goes into <config/ftheader.h>
. I am not using the built in FindFreeType in cmake since there are some builds which already have ft2build.h included and I want it to use the one in that directory instead of the system. Here is what my custom FindFreeTypeTwo.cmake looks like.
1 # - Try to find Freetype2
2 # Once done this will define
3 #
4 # FREETYPE2_FOUND - system has FREETYPE2
5 # FREETYPE2_INCLUDE_DIR - the Freetype2 include directory
6 # FREETYPE2_LIBRARIES - Link these to use Freetype2
7
8 #handle the QUIETLY and REQUIRED arguments and set FREETYPE2_FOUND to TRUE if
9 #all listed variables are TRUE
10 FIND_PATH(_FREETYPE2_INCLUDE_DIR ft2build.h PATH_SUFFIXES freetype2)
11
12 FIND_LIBRARY(_FREETYPE2_LIBRARIES NAMES freetype)
13
14 INCLUDE(FindPackageHandleStandardArgs)
15 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Freetype2 DEFAULT_MSG _FREETYPE2_LIBRARIES _FREETYPE2_INCLUDE_DIR)
16
17 MARK_AS_ADVANCED(FREETYPE2_INCLUDE_DIR FREETYPE2_LIBRARIES)
18
19 # Set up output variables
20 if (FREETYPE2_FOUND)
21 set (FREETYPE2_INCLUDE_DIR ${_FREETYPE2_INCLUDE_DIR})
22 set (FREETYPE2_LIBRARIES ${_FREETYPE2_LIBRARIES})
23 else(FREETYPE2_FOUND)
24 set (FREETYPE2_INCLUDE_DIR)
25 set (FREETYPE2_LIBRARIES)
26 endif (FREETYPE2_FOUND)
So that works if freetype-config --ftversion
is 2.5.2 and not for 2.4.8. If I change it to
FIND_PATH(_FREETYPE2_INCLUDE_DIR ft2build.h)
...
if(FREETYPE2_FOUND)
set (FREETYPE2_INCLUDE_DIR ${_FREETYPE2_INCLUDE_DIR}/freetype2)
set (FREETYPE2_LIBRARIES ${_FREETYPE2_LIBRARIES})
else
...
My program can find the headers for freetype in 2.4.8, but 2.5.2 fails.
How can I modify cmake so that it will work regardless of the freetype version?
You can execute freetype-config --ftversion
during configuration, parse its output and choose FREETYPE2_INCLUDE_DIR
accordingly:
if(_FREETYPE2_INCLUDE_DIR)
execute_process (COMMAND "freetype-config" "--ftversion"
OUTPUT_VARIABLE version_output)
string (REGEX MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)"
FREETYPE2_VERSION "${version_output}")
set(FREETYPE2_VERSION_MAJOR ${CMAKE_MATCH_1})
set(FREETYPE2_VERSION_MINOR ${CMAKE_MATCH_2})
set(FREETYPE2_VERSION_TWEAK ${CMAKE_MATCH_3})
if(FREETYPE2_VERSION_MINOR EQUAL "4")
set (FREETYPE2_INCLUDE_DIR ${_FREETYPE2_INCLUDE_DIR}/freetype2)
else()
set (FREETYPE2_INCLUDE_DIR ${_FREETYPE2_INCLUDE_DIR})
endif()
endif()
By iniializing versions-related variables you also allow version
request in find_package()
call.