1# CMakeLists.txt
2#
3# Copyright 2013-2018 by
4# David Turner, Robert Wilhelm, and Werner Lemberg.
5#
6# Written originally by John Cary <cary@txcorp.com>
7#
8# This file is part of the FreeType project, and may only be used, modified,
9# and distributed under the terms of the FreeType project license,
10# LICENSE.TXT.  By continuing to use, modify, or distribute this file you
11# indicate that you have read the license and understand and accept it
12# fully.
13#
14#
15# The following will 1. create a build directory and 2. change into it and
16# call cmake to configure the build with default parameters as a static
17# library.
18#
19#   cmake -E make_directory build
20#   cmake -E chdir build cmake ..
21#
22# For a dynamic library, use
23#
24#   cmake -E chdir build cmake -D BUILD_SHARED_LIBS:BOOL=true ..
25#
26# For a framework on OS X, use
27#
28#   cmake -E chdir build cmake -G Xcode -D BUILD_FRAMEWORK:BOOL=true ..
29#
30# For an iOS static library, use
31#
32#   cmake -E chdir build cmake -G Xcode -D IOS_PLATFORM=OS ..
33#
34# or
35#
36#   cmake -E chdir build cmake -G Xcode -D IOS_PLATFORM=SIMULATOR ..
37#
38# or
39#
40#   cmake -E chdir build cmake -G Xcode -D IOS_PLATFORM=SIMULATOR64 ..
41#
42# Finally, build the project with:
43#
44#   cmake --build build
45#
46# Install it with
47#
48#   (sudo) cmake --build build --target install
49#
50# A binary distribution can be made with
51#
52#   cmake --build build --config Release --target package
53#
54# Please refer to the cmake manual for further options, in particular, how
55# to modify compilation and linking parameters.
56#
57# Some notes.
58#
59# . `cmake' creates configuration files in
60#
61#     <build-directory>/include/freetype/config
62#
63#   which should be further modified if necessary.
64#
65# . You can use `cmake' directly on a freshly cloned FreeType git
66#   repository.
67#
68# . `CMakeLists.txt' is provided as-is since it is normally not used by the
69#   developer team.
70#
71# . Set the `FT_WITH_ZLIB', `FT_WITH_BZIP2', `FT_WITH_PNG', and
72#   `FT_WITH_HARFBUZZ' CMake variables to `ON' to force using a dependency.
73#   Leave a variable undefined (which is the default) to use the dependency
74#   only if it is available.  Set `CMAKE_DISABLE_FIND_PACKAGE_HarfBuzz=TRUE' to
75#   disable a dependency completely (CMake package name, so `BZip2' instead of
76#   `BZIP2'). Example:
77#
78#     cmake -DFT_WITH_ZLIB=ON -DCMAKE_DISABLE_FIND_PACKAGE_HarfBuzz=TRUE [...]
79#
80# . Installation of FreeType can be controlled with the CMake variables
81#   `SKIP_INSTALL_HEADERS', `SKIP_INSTALL_LIBRARIES', and `SKIP_INSTALL_ALL'
82#   (this is compatible with the same CMake variables in zlib's CMake
83#   support).
84
85# FreeType explicitly marks the API to be exported and relies on the compiler
86# to hide all other symbols. CMake supports a C_VISBILITY_PRESET property
87# starting with 2.8.12.
88cmake_minimum_required(VERSION 2.8.12)
89
90if (NOT CMAKE_VERSION VERSION_LESS 3.3)
91  # Allow symbol visibility settings also on static libraries. CMake < 3.3
92  # only sets the propery on a shared library build.
93  cmake_policy(SET CMP0063 NEW)
94endif ()
95
96include(CheckIncludeFile)
97
98# CMAKE_TOOLCHAIN_FILE must be set before `project' is called, which
99# configures the base build environment and references the toolchain file
100if (APPLE)
101  if (DEFINED IOS_PLATFORM)
102    if (NOT "${IOS_PLATFORM}" STREQUAL "OS"
103        AND NOT "${IOS_PLATFORM}" STREQUAL "SIMULATOR"
104        AND NOT "${IOS_PLATFORM}" STREQUAL "SIMULATOR64")
105      message(FATAL_ERROR
106        "IOS_PLATFORM must be set to either OS, SIMULATOR, or SIMULATOR64")
107    endif ()
108    if (NOT "${CMAKE_GENERATOR}" STREQUAL "Xcode")
109      message(AUTHOR_WARNING
110        "You should use Xcode generator with IOS_PLATFORM enabled to get Universal builds.")
111    endif ()
112    if (BUILD_SHARED_LIBS)
113      message(FATAL_ERROR
114        "BUILD_SHARED_LIBS can not be on with IOS_PLATFORM enabled")
115    endif ()
116    if (BUILD_FRAMEWORK)
117      message(FATAL_ERROR
118        "BUILD_FRAMEWORK can not be on with IOS_PLATFORM enabled")
119    endif ()
120
121    # iOS only uses static libraries
122    set(BUILD_SHARED_LIBS OFF)
123
124    set(CMAKE_TOOLCHAIN_FILE
125      ${CMAKE_SOURCE_DIR}/builds/cmake/iOS.cmake)
126  endif ()
127else ()
128  if (DEFINED IOS_PLATFORM)
129    message(FATAL_ERROR "IOS_PLATFORM is not supported on this platform")
130  endif ()
131endif ()
132
133
134project(freetype C)
135
136set(VERSION_MAJOR "2")
137set(VERSION_MINOR "9")
138set(VERSION_PATCH "1")
139
140# SOVERSION scheme: CURRENT.AGE.REVISION
141#   If there was an incompatible interface change:
142#     Increment CURRENT. Set AGE and REVISION to 0
143#   If there was a compatible interface change:
144#     Increment AGE. Set REVISION to 0
145#   If the source code was changed, but there were no interface changes:
146#     Increment REVISION.
147set(LIBRARY_VERSION "6.16.0")
148set(LIBRARY_SOVERSION "6")
149
150# These options mean "require x and complain if not found". They'll get
151# optionally found anyway. Use `-DCMAKE_DISABLE_FIND_PACKAGE_x=TRUE` to disable
152# searching for a packge entirely (x is the CMake package name, so "BZip2"
153# instead of "BZIP2").
154option(FT_WITH_ZLIB "Use system zlib instead of internal library." OFF)
155option(FT_WITH_BZIP2 "Support bzip2 compressed fonts." OFF)
156option(FT_WITH_PNG "Support PNG compressed OpenType embedded bitmaps." OFF)
157option(FT_WITH_HARFBUZZ "Improve auto-hinting of OpenType fonts." OFF)
158
159
160# Disallow in-source builds
161if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
162  message(FATAL_ERROR
163    "In-source builds are not permitted!  Make a separate folder for"
164    " building, e.g.,\n"
165    "  cmake -E make_directory build\n"
166    "  cmake -E chdir build cmake ..\n"
167    "Before that, remove the files created by this failed run with\n"
168    "  cmake -E remove CMakeCache.txt\n"
169    "  cmake -E remove_directory CMakeFiles")
170endif ()
171
172
173# Add local cmake modules
174list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/builds/cmake)
175
176
177if (BUILD_FRAMEWORK)
178  if (NOT "${CMAKE_GENERATOR}" STREQUAL "Xcode")
179    message(FATAL_ERROR
180      "You should use Xcode generator with BUILD_FRAMEWORK enabled")
181  endif ()
182  set(CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD_32_64_BIT)")
183  set(BUILD_SHARED_LIBS ON)
184endif ()
185
186
187# Find dependencies
188if (FT_WITH_HARFBUZZ)
189  find_package(HarfBuzz 1.3.0 REQUIRED)
190else ()
191  find_package(HarfBuzz 1.3.0)
192endif ()
193
194if (FT_WITH_PNG)
195  find_package(PNG REQUIRED)
196else ()
197  find_package(PNG)
198endif ()
199
200if (FT_WITH_ZLIB)
201  find_package(ZLIB REQUIRED)
202else ()
203  find_package(ZLIB)
204endif ()
205
206if (FT_WITH_BZIP2)
207  find_package(BZip2 REQUIRED)
208else ()
209  find_package(BZip2)
210endif ()
211
212# Create the configuration file
213if (UNIX)
214  check_include_file("unistd.h" HAVE_UNISTD_H)
215  check_include_file("fcntl.h" HAVE_FCNTL_H)
216  check_include_file("stdint.h" HAVE_STDINT_H)
217
218  file(READ "${PROJECT_SOURCE_DIR}/builds/unix/ftconfig.in"
219    FTCONFIG_H)
220  if (HAVE_UNISTD_H)
221    string(REGEX REPLACE
222      "#undef +(HAVE_UNISTD_H)" "#define \\1 1"
223      FTCONFIG_H "${FTCONFIG_H}")
224  endif ()
225  if (HAVE_FCNTL_H)
226    string(REGEX REPLACE
227      "#undef +(HAVE_FCNTL_H)" "#define \\1 1"
228      FTCONFIG_H "${FTCONFIG_H}")
229  endif ()
230  if (HAVE_STDINT_H)
231    string(REGEX REPLACE
232      "#undef +(HAVE_STDINT_H)" "#define \\1 1"
233      FTCONFIG_H "${FTCONFIG_H}")
234  endif ()
235  string(REPLACE "/undef " "#undef "
236    FTCONFIG_H "${FTCONFIG_H}")
237else()
238  file(READ "${PROJECT_SOURCE_DIR}/include/freetype/config/ftconfig.h"
239    FTCONFIG_H)
240endif ()
241file(WRITE "${PROJECT_BINARY_DIR}/include/freetype/config/ftconfig.h"
242  "${FTCONFIG_H}")
243
244
245# Create the options file
246file(READ "${PROJECT_SOURCE_DIR}/include/freetype/config/ftoption.h"
247  FTOPTION_H)
248if (ZLIB_FOUND)
249  string(REGEX REPLACE
250    "/\\* +(#define +FT_CONFIG_OPTION_SYSTEM_ZLIB) +\\*/" "\\1"
251    FTOPTION_H "${FTOPTION_H}")
252endif ()
253if (BZIP2_FOUND)
254  string(REGEX REPLACE
255    "/\\* +(#define +FT_CONFIG_OPTION_USE_BZIP2) +\\*/" "\\1"
256    FTOPTION_H "${FTOPTION_H}")
257endif ()
258if (PNG_FOUND)
259  string(REGEX REPLACE
260    "/\\* +(#define +FT_CONFIG_OPTION_USE_PNG) +\\*/" "\\1"
261    FTOPTION_H "${FTOPTION_H}")
262endif ()
263if (HARFBUZZ_FOUND)
264  string(REGEX REPLACE
265    "/\\* +(#define +FT_CONFIG_OPTION_USE_HARFBUZZ) +\\*/" "\\1"
266    FTOPTION_H "${FTOPTION_H}")
267endif ()
268file(WRITE "${PROJECT_BINARY_DIR}/include/freetype/config/ftoption.h"
269  "${FTOPTION_H}")
270
271
272file(GLOB PUBLIC_HEADERS "include/ft2build.h" "include/freetype/*.h")
273file(GLOB PUBLIC_CONFIG_HEADERS "include/freetype/config/*.h")
274file(GLOB PRIVATE_HEADERS "include/freetype/internal/*.h")
275
276
277set(BASE_SRCS
278  src/autofit/autofit.c
279  src/base/ftbase.c
280  src/base/ftbbox.c
281  src/base/ftbdf.c
282  src/base/ftbitmap.c
283  src/base/ftcid.c
284  src/base/ftfstype.c
285  src/base/ftgasp.c
286  src/base/ftglyph.c
287  src/base/ftgxval.c
288  src/base/ftinit.c
289  src/base/ftmm.c
290  src/base/ftotval.c
291  src/base/ftpatent.c
292  src/base/ftpfr.c
293  src/base/ftstroke.c
294  src/base/ftsynth.c
295  src/base/ftsystem.c
296  src/base/fttype1.c
297  src/base/ftwinfnt.c
298  src/bdf/bdf.c
299  src/bzip2/ftbzip2.c
300  src/cache/ftcache.c
301  src/cff/cff.c
302  src/cid/type1cid.c
303  src/gzip/ftgzip.c
304  src/lzw/ftlzw.c
305  src/pcf/pcf.c
306  src/pfr/pfr.c
307  src/psaux/psaux.c
308  src/pshinter/pshinter.c
309  src/psnames/psnames.c
310  src/raster/raster.c
311  src/sfnt/sfnt.c
312  src/smooth/smooth.c
313  src/truetype/truetype.c
314  src/type1/type1.c
315  src/type42/type42.c
316  src/winfonts/winfnt.c
317)
318
319if (WIN32)
320  enable_language(RC)
321  list(APPEND BASE_SRCS builds/windows/ftdebug.c
322                        src/base/ftver.rc)
323elseif (WINCE)
324  list(APPEND BASE_SRCS builds/wince/ftdebug.c)
325else ()
326  list(APPEND BASE_SRCS src/base/ftdebug.c)
327endif ()
328
329if (BUILD_FRAMEWORK)
330  list(APPEND BASE_SRCS builds/mac/freetype-Info.plist)
331endif ()
332
333
334if (NOT DISABLE_FORCE_DEBUG_POSTFIX)
335  set(CMAKE_DEBUG_POSTFIX d)
336endif()
337
338
339add_library(freetype
340  ${PUBLIC_HEADERS}
341  ${PUBLIC_CONFIG_HEADERS}
342  ${PRIVATE_HEADERS}
343  ${BASE_SRCS}
344)
345
346set_target_properties(
347  freetype PROPERTIES
348    C_VISIBILITY_PRESET hidden)
349
350target_compile_definitions(
351  freetype PRIVATE FT2_BUILD_LIBRARY)
352
353if (WIN32)
354  target_compile_definitions(
355    freetype PRIVATE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_WARNINGS)
356  if (BUILD_SHARED_LIBS)
357    target_compile_definitions(
358      freetype PRIVATE DLL_EXPORT)
359  endif ()
360endif ()
361
362if (BUILD_SHARED_LIBS)
363  set_target_properties(freetype PROPERTIES
364    VERSION ${LIBRARY_VERSION}
365    SOVERSION ${LIBRARY_SOVERSION})
366endif ()
367
368# Pick up ftconfig.h and ftoption.h generated above, first.
369target_include_directories(
370  freetype
371    PUBLIC
372      $<INSTALL_INTERFACE:include/freetype2>
373      $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
374      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
375    PRIVATE
376      ${CMAKE_CURRENT_BINARY_DIR}/include
377      ${CMAKE_CURRENT_SOURCE_DIR}/include)
378
379
380if (BUILD_FRAMEWORK)
381  set_property(SOURCE ${PUBLIC_CONFIG_HEADERS}
382    PROPERTY MACOSX_PACKAGE_LOCATION Headers/config
383  )
384  set_target_properties(freetype PROPERTIES
385    FRAMEWORK TRUE
386    MACOSX_FRAMEWORK_INFO_PLIST builds/mac/freetype-Info.plist
387    PUBLIC_HEADER "${PUBLIC_HEADERS}"
388    XCODE_ATTRIBUTE_INSTALL_PATH "@rpath"
389  )
390endif ()
391
392
393set(PKG_CONFIG_REQUIRED_PRIVATE "")
394
395if (ZLIB_FOUND)
396  target_link_libraries(freetype PRIVATE ${ZLIB_LIBRARIES})
397  target_include_directories(freetype PRIVATE ${ZLIB_INCLUDE_DIRS})
398  list(APPEND PKG_CONFIG_REQUIRED_PRIVATE zlib)
399endif ()
400if (BZIP2_FOUND)
401  target_link_libraries(freetype PRIVATE ${BZIP2_LIBRARIES})
402  target_include_directories(freetype PRIVATE ${BZIP2_INCLUDE_DIR}) # not BZIP2_INCLUDE_DIRS
403  list(APPEND PKG_CONFIG_REQUIRED_PRIVATE bzip2)
404endif ()
405if (PNG_FOUND)
406  target_link_libraries(freetype PRIVATE ${PNG_LIBRARIES})
407  target_compile_definitions(freetype PRIVATE ${PNG_DEFINITIONS})
408  target_include_directories(freetype PRIVATE ${PNG_INCLUDE_DIRS})
409  list(APPEND PKG_CONFIG_REQUIRED_PRIVATE libpng)
410endif ()
411if (HARFBUZZ_FOUND)
412  target_link_libraries(freetype PRIVATE ${HARFBUZZ_LIBRARIES})
413  target_include_directories(freetype PRIVATE ${HARFBUZZ_INCLUDE_DIRS})
414  list(APPEND PKG_CONFIG_REQUIRED_PRIVATE harfbuzz)
415endif ()
416
417
418# Installation
419include(GNUInstallDirs)
420
421if (NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL)
422  install(
423    # Note the trailing slash in the argument to `DIRECTORY'!
424    DIRECTORY ${PROJECT_SOURCE_DIR}/include/
425      DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/freetype2
426      COMPONENT headers
427      PATTERN "internal" EXCLUDE
428      PATTERN "ftconfig.h" EXCLUDE
429      PATTERN "ftoption.h" EXCLUDE)
430  install(
431    FILES ${PROJECT_BINARY_DIR}/include/freetype/config/ftconfig.h
432          ${PROJECT_BINARY_DIR}/include/freetype/config/ftoption.h
433      DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/freetype2/freetype/config
434      COMPONENT headers)
435endif ()
436
437if (NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL)
438  # Generate the pkg-config file
439  if (UNIX)
440    file(READ ${PROJECT_SOURCE_DIR}/builds/unix/freetype2.in FREETYPE2_PC_IN)
441
442    string(REPLACE ";" ", " PKG_CONFIG_REQUIRED_PRIVATE "${PKG_CONFIG_REQUIRED_PRIVATE}")
443
444    string(REPLACE "%prefix%" ${CMAKE_INSTALL_PREFIX}
445           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
446    string(REPLACE "%exec_prefix%" "\${prefix}"
447           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
448    string(REPLACE "%libdir%" "\${prefix}/${CMAKE_INSTALL_LIBDIR}"
449           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
450    string(REPLACE "%includedir%" "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}"
451           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
452    string(REPLACE "%ft_version%" "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
453           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
454    string(REPLACE "%REQUIRES_PRIVATE%" "${PKG_CONFIG_REQUIRED_PRIVATE}"
455           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
456    string(REPLACE "%LIBS_PRIVATE%" ""  # All libs support pkg-config
457           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
458
459    file(WRITE ${PROJECT_BINARY_DIR}/freetype2.pc ${FREETYPE2_PC_IN})
460
461    install(
462      FILES ${PROJECT_BINARY_DIR}/freetype2.pc
463      DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
464      COMPONENT pkgconfig)
465  endif ()
466
467  install(
468    TARGETS freetype
469      EXPORT freetype-targets
470      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
471      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
472      FRAMEWORK DESTINATION Library/Frameworks
473      COMPONENT libraries)
474  install(
475    EXPORT freetype-targets
476      DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/freetype
477      FILE freetype-config.cmake
478      COMPONENT headers)
479endif ()
480
481
482# Packaging
483set(CPACK_PACKAGE_NAME ${CMAKE_PROJECT_NAME})
484set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "The FreeType font rendering library.")
485set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README")
486set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/docs/LICENSE.TXT")
487
488set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
489set(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
490set(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH})
491set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
492
493if (WIN32)
494  set(CPACK_GENERATOR ZIP)
495else()
496  set(CPACK_GENERATOR TGZ)
497endif()
498
499set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries")
500set(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C/C++ Headers")
501set(CPACK_COMPONENT_LIBRARIES_DESCRIPTION
502  "Library used to build programs which use FreeType")
503set(CPACK_COMPONENT_HEADERS_DESCRIPTION
504  "C/C++ header files for use with FreeType")
505set(CPACK_COMPONENT_HEADERS_DEPENDS libraries)
506set(CPACK_COMPONENT_LIBRARIES_GROUP "Development")
507set(CPACK_COMPONENT_HEADERS_GROUP "Development")
508
509include(CPack)
510