CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. cmake_minimum_required(VERSION 3.15)
  2. include(cmake/version.cmake)
  3. option(MSDFGEN_CORE_ONLY "Only build the core library with no dependencies" OFF)
  4. option(MSDFGEN_BUILD_STANDALONE "Build the msdfgen standalone executable" ON)
  5. option(MSDFGEN_USE_VCPKG "Use vcpkg package manager to link project dependencies" ON)
  6. option(MSDFGEN_USE_OPENMP "Build with OpenMP support for multithreaded code" OFF)
  7. option(MSDFGEN_USE_CPP11 "Build with C++11 enabled" ON)
  8. option(MSDFGEN_USE_SKIA "Build with the Skia library" ON)
  9. option(MSDFGEN_INSTALL "Generate installation target" OFF)
  10. option(BUILD_SHARED_LIBS "Generate dynamic library files instead of static" OFF)
  11. if(MSDFGEN_CORE_ONLY AND MSDFGEN_BUILD_STANDALONE)
  12. message(WARNING "Option MSDFGEN_CORE_ONLY ignored - extensions are required for standalone executable")
  13. set(MSDFGEN_CORE_ONLY OFF)
  14. endif()
  15. if(MSDFGEN_CORE_ONLY AND MSDFGEN_USE_VCPKG)
  16. message(STATUS "Option MSDFGEN_USE_VCPKG ignored due to MSDFGEN_CORE_ONLY - core has no dependencies")
  17. set(MSDFGEN_USE_VCPKG OFF)
  18. endif()
  19. get_property(MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  20. if(NOT MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE)
  21. message(STATUS "CMAKE_BUILD_TYPE not set, defaulting to Release")
  22. set(CMAKE_BUILD_TYPE Release)
  23. endif()
  24. if(BUILD_SHARED_LIBS)
  25. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  26. endif()
  27. if(MSDFGEN_USE_VCPKG)
  28. # Make sure that vcpkg toolchain file is set
  29. if(NOT CMAKE_TOOLCHAIN_FILE)
  30. if(DEFINED ENV{VCPKG_ROOT})
  31. set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
  32. else()
  33. message(SEND_ERROR "Vcpkg toolchain not configured. Either set VCPKG_ROOT environment variable or pass -DCMAKE_TOOLCHAIN_FILE=VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake to cmake")
  34. endif()
  35. endif()
  36. # Default to statically linked vcpkg triplet on Windows
  37. if(WIN32 AND NOT VCPKG_TARGET_TRIPLET AND NOT BUILD_SHARED_LIBS)
  38. if(CMAKE_GENERATOR_PLATFORM MATCHES "64$")
  39. set(VCPKG_TARGET_TRIPLET "x64-windows-static")
  40. elseif(CMAKE_GENERATOR_PLATFORM MATCHES "32$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x86")
  41. set(VCPKG_TARGET_TRIPLET "x86-windows-static")
  42. else()
  43. message(WARNING "Vcpkg triplet not explicitly specified and could not be deduced. Recommend using -DVCPKG_TARGET_TRIPLET=x86-windows-static or similar")
  44. endif()
  45. endif()
  46. # Select project features
  47. if(NOT MSDFGEN_VCPKG_FEATURES_SET)
  48. set(VCPKG_MANIFEST_NO_DEFAULT_FEATURES ON)
  49. if(NOT MSDFGEN_CORE_ONLY)
  50. list(APPEND VCPKG_MANIFEST_FEATURES "extensions")
  51. endif()
  52. if(MSDFGEN_BUILD_STANDALONE)
  53. list(APPEND VCPKG_MANIFEST_FEATURES "standalone")
  54. endif()
  55. if(MSDFGEN_USE_SKIA)
  56. list(APPEND VCPKG_MANIFEST_FEATURES "geometry-preprocessing")
  57. endif()
  58. if(MSDFGEN_USE_OPENMP)
  59. list(APPEND VCPKG_MANIFEST_FEATURES "openmp")
  60. endif()
  61. endif()
  62. endif()
  63. # Version is specified in vcpkg.json
  64. project(msdfgen VERSION ${MSDFGEN_VERSION} LANGUAGES CXX)
  65. file(GLOB_RECURSE MSDFGEN_CORE_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "core/*.h" "core/*.hpp")
  66. file(GLOB_RECURSE MSDFGEN_CORE_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "core/*.cpp")
  67. file(GLOB_RECURSE MSDFGEN_EXT_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "ext/*.h" "ext/*.hpp")
  68. file(GLOB_RECURSE MSDFGEN_EXT_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "ext/*.cpp" "lib/*.cpp")
  69. # Core library
  70. add_library(msdfgen-core "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen.h" ${MSDFGEN_CORE_HEADERS} ${MSDFGEN_CORE_SOURCES})
  71. add_library(msdfgen::msdfgen-core ALIAS msdfgen-core)
  72. set_target_properties(msdfgen-core PROPERTIES PUBLIC_HEADER "${MSDFGEN_CORE_HEADERS}")
  73. set_property(TARGET msdfgen-core PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  74. target_compile_definitions(msdfgen-core PUBLIC
  75. MSDFGEN_VERSION=${MSDFGEN_VERSION}
  76. MSDFGEN_VERSION_MAJOR=${MSDFGEN_VERSION_MAJOR}
  77. MSDFGEN_VERSION_MINOR=${MSDFGEN_VERSION_MINOR}
  78. MSDFGEN_VERSION_REVISION=${MSDFGEN_VERSION_REVISION}
  79. MSDFGEN_COPYRIGHT_YEAR=${MSDFGEN_COPYRIGHT_YEAR}
  80. )
  81. target_include_directories(msdfgen-core INTERFACE
  82. $<INSTALL_INTERFACE:include/msdfgen>
  83. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>
  84. )
  85. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT msdfgen-core)
  86. if(MSDFGEN_USE_CPP11)
  87. target_compile_features(msdfgen-core PUBLIC cxx_std_11)
  88. target_compile_definitions(msdfgen-core PUBLIC MSDFGEN_USE_CPP11)
  89. endif()
  90. if(MSDFGEN_USE_OPENMP)
  91. # Note: Clang doesn't support OpenMP by default...
  92. find_package(OpenMP REQUIRED COMPONENTS CXX)
  93. target_compile_definitions(msdfgen-core PUBLIC MSDFGEN_USE_OPENMP)
  94. target_link_libraries(msdfgen-core PUBLIC OpenMP::OpenMP_CXX)
  95. endif()
  96. if(BUILD_SHARED_LIBS AND WIN32)
  97. target_compile_definitions(msdfgen-core PRIVATE "MSDFGEN_PUBLIC=__declspec(dllexport)")
  98. target_compile_definitions(msdfgen-core INTERFACE "MSDFGEN_PUBLIC=__declspec(dllimport)")
  99. else()
  100. target_compile_definitions(msdfgen-core PUBLIC MSDFGEN_PUBLIC=)
  101. endif()
  102. # Extensions library
  103. if(NOT MSDFGEN_CORE_ONLY)
  104. if(NOT TARGET Freetype::Freetype)
  105. find_package(Freetype REQUIRED)
  106. endif()
  107. if(NOT TARGET tinyxml2::tinyxml2)
  108. find_package(tinyxml2 REQUIRED)
  109. endif()
  110. if(NOT TARGET PNG::PNG)
  111. find_package(PNG REQUIRED)
  112. endif()
  113. add_library(msdfgen-ext "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen-ext.h" ${MSDFGEN_EXT_HEADERS} ${MSDFGEN_EXT_SOURCES})
  114. add_library(msdfgen::msdfgen-ext ALIAS msdfgen-ext)
  115. set_target_properties(msdfgen-ext PROPERTIES PUBLIC_HEADER "${MSDFGEN_EXT_HEADERS}")
  116. set_property(TARGET msdfgen-ext PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  117. target_compile_definitions(msdfgen-ext PUBLIC MSDFGEN_USE_LIBPNG)
  118. target_link_libraries(msdfgen-ext PRIVATE msdfgen::msdfgen-core Freetype::Freetype tinyxml2::tinyxml2 PNG::PNG)
  119. target_include_directories(msdfgen-ext
  120. PUBLIC
  121. $<INSTALL_INTERFACE:include/msdfgen>
  122. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  123. PRIVATE
  124. ${CMAKE_CURRENT_SOURCE_DIR}/include
  125. )
  126. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT msdfgen-ext)
  127. if(MSDFGEN_USE_SKIA)
  128. set(MSDFGEN_SKIA_LIB skia)
  129. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  130. set(THREADS_PREFER_PTHREAD_FLAG TRUE)
  131. find_package(Threads REQUIRED)
  132. if(NOT TARGET skia)
  133. if(MSDFGEN_USE_VCPKG)
  134. find_package(unofficial-skia REQUIRED)
  135. set(MSDFGEN_SKIA_LIB unofficial::skia::skia)
  136. else()
  137. find_package(skia REQUIRED)
  138. endif()
  139. endif()
  140. target_compile_features(msdfgen-ext PUBLIC cxx_std_17)
  141. target_compile_definitions(msdfgen-ext PUBLIC MSDFGEN_USE_SKIA)
  142. target_link_libraries(msdfgen-ext PRIVATE Threads::Threads ${MSDFGEN_SKIA_LIB})
  143. endif()
  144. add_library(msdfgen-full INTERFACE)
  145. add_library(msdfgen::msdfgen ALIAS msdfgen-full)
  146. target_link_libraries(msdfgen-full INTERFACE msdfgen::msdfgen-core msdfgen::msdfgen-ext)
  147. endif()
  148. # Standalone executable
  149. if(MSDFGEN_BUILD_STANDALONE)
  150. set(MSDFGEN_STANDALONE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
  151. if(MSVC)
  152. set(MSDFGEN_STANDALONE_SOURCES ${MSDFGEN_STANDALONE_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen.rc")
  153. endif()
  154. add_executable(msdfgen ${MSDFGEN_STANDALONE_SOURCES})
  155. target_compile_definitions(msdfgen PUBLIC MSDFGEN_STANDALONE)
  156. target_compile_definitions(msdfgen PRIVATE MSDFGEN_VERSION_UNDERLINE=${MSDFGEN_VERSION_UNDERLINE})
  157. set_property(TARGET msdfgen PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  158. target_link_libraries(msdfgen PRIVATE msdfgen::msdfgen)
  159. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT msdfgen)
  160. endif()
  161. # Installation
  162. if(MSDFGEN_INSTALL)
  163. include(GNUInstallDirs)
  164. include(CMakePackageConfigHelpers)
  165. set(MSDFGEN_CONFIG_PATH "lib/cmake/msdfgen")
  166. # install tree package config
  167. write_basic_package_version_file(
  168. "${CMAKE_CURRENT_BINARY_DIR}/msdfgenConfigVersion.cmake"
  169. VERSION ${PROJECT_VERSION}
  170. COMPATIBILITY SameMajorVersion
  171. )
  172. configure_package_config_file(
  173. cmake/msdfgenConfig.cmake.in
  174. ${MSDFGEN_CONFIG_PATH}/msdfgenConfig.cmake
  175. INSTALL_DESTINATION ${MSDFGEN_CONFIG_PATH}
  176. NO_CHECK_REQUIRED_COMPONENTS_MACRO
  177. )
  178. # build tree package config
  179. configure_file(
  180. cmake/msdfgenConfig.cmake.in
  181. msdfgenConfig.cmake
  182. @ONLY
  183. )
  184. install(TARGETS msdfgen-core EXPORT msdfgenTargets
  185. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  186. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  187. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  188. FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR}
  189. PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/msdfgen/core
  190. )
  191. install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen.h" DESTINATION include/msdfgen)
  192. if(NOT MSDFGEN_CORE_ONLY)
  193. install(TARGETS msdfgen-ext EXPORT msdfgenTargets
  194. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  195. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  196. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  197. FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR}
  198. PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/msdfgen/ext
  199. )
  200. install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen-ext.h" DESTINATION include/msdfgen)
  201. install(TARGETS msdfgen-full EXPORT msdfgenTargets)
  202. endif()
  203. export(EXPORT msdfgenTargets NAMESPACE msdfgen:: FILE "${CMAKE_CURRENT_BINARY_DIR}/msdfgenTargets.cmake")
  204. install(EXPORT msdfgenTargets FILE msdfgenTargets.cmake NAMESPACE msdfgen:: DESTINATION ${MSDFGEN_CONFIG_PATH})
  205. if(MSDFGEN_BUILD_STANDALONE)
  206. install(TARGETS msdfgen EXPORT msdfgenBinaryTargets DESTINATION ${CMAKE_INSTALL_BINDIR})
  207. export(EXPORT msdfgenBinaryTargets NAMESPACE msdfgen-standalone:: FILE "${CMAKE_CURRENT_BINARY_DIR}/msdfgenBinaryTargets.cmake")
  208. install(EXPORT msdfgenBinaryTargets FILE msdfgenBinaryTargets.cmake NAMESPACE msdfgen-standalone:: DESTINATION ${MSDFGEN_CONFIG_PATH})
  209. endif()
  210. install(
  211. FILES
  212. "${CMAKE_CURRENT_BINARY_DIR}/${MSDFGEN_CONFIG_PATH}/msdfgenConfig.cmake"
  213. "${CMAKE_CURRENT_BINARY_DIR}/msdfgenConfigVersion.cmake"
  214. DESTINATION ${MSDFGEN_CONFIG_PATH}
  215. )
  216. endif()