CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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(MSDFGEN_DYNAMIC_RUNTIME "Link dynamic runtime library instead of static" OFF)
  11. option(BUILD_SHARED_LIBS "Generate dynamic library files instead of static" OFF)
  12. if(MSDFGEN_CORE_ONLY AND MSDFGEN_USE_VCPKG)
  13. message(STATUS "Option MSDFGEN_USE_VCPKG ignored due to MSDFGEN_CORE_ONLY - core has no dependencies")
  14. set(MSDFGEN_USE_VCPKG OFF)
  15. endif()
  16. get_property(MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  17. if(NOT MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE)
  18. message(STATUS "CMAKE_BUILD_TYPE not set, defaulting to Release")
  19. set(CMAKE_BUILD_TYPE Release)
  20. endif()
  21. if(MSDFGEN_USE_VCPKG)
  22. # Make sure that vcpkg toolchain file is set
  23. if(NOT CMAKE_TOOLCHAIN_FILE)
  24. if(DEFINED ENV{VCPKG_ROOT})
  25. set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
  26. else()
  27. 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")
  28. endif()
  29. endif()
  30. # Default to statically linked vcpkg triplet on Windows
  31. if(WIN32 AND NOT VCPKG_TARGET_TRIPLET AND NOT MSDFGEN_DYNAMIC_RUNTIME)
  32. if(CMAKE_GENERATOR_PLATFORM MATCHES "64$" AND NOT CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64")
  33. set(VCPKG_TARGET_TRIPLET "x64-windows-static")
  34. elseif(CMAKE_GENERATOR_PLATFORM MATCHES "32$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x86")
  35. set(VCPKG_TARGET_TRIPLET "x86-windows-static")
  36. else()
  37. if(CMAKE_GENERATOR_PLATFORM)
  38. message(WARNING "Vcpkg triplet not explicitly specified and could not be deduced. Recommend using -DVCPKG_TARGET_TRIPLET=x64-windows-static or similar")
  39. else()
  40. message(WARNING "Vcpkg triplet not explicitly specified and could not be deduced. Recommend using -A to explicitly select platform (Win32 or x64)")
  41. endif()
  42. endif()
  43. endif()
  44. # Select project features
  45. if(NOT MSDFGEN_VCPKG_FEATURES_SET)
  46. set(VCPKG_MANIFEST_NO_DEFAULT_FEATURES ON)
  47. if(NOT MSDFGEN_CORE_ONLY)
  48. list(APPEND VCPKG_MANIFEST_FEATURES "extensions")
  49. endif()
  50. if(MSDFGEN_BUILD_STANDALONE)
  51. list(APPEND VCPKG_MANIFEST_FEATURES "standalone")
  52. endif()
  53. if(MSDFGEN_USE_SKIA)
  54. list(APPEND VCPKG_MANIFEST_FEATURES "geometry-preprocessing")
  55. endif()
  56. if(MSDFGEN_USE_OPENMP)
  57. list(APPEND VCPKG_MANIFEST_FEATURES "openmp")
  58. endif()
  59. endif()
  60. endif()
  61. # Version is specified in vcpkg.json
  62. project(msdfgen VERSION ${MSDFGEN_VERSION} LANGUAGES CXX)
  63. if(MSDFGEN_DYNAMIC_RUNTIME)
  64. set(MSDFGEN_MSVC_RUNTIME "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
  65. else()
  66. set(MSDFGEN_MSVC_RUNTIME "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  67. endif()
  68. if(BUILD_SHARED_LIBS)
  69. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  70. endif()
  71. if(MAX_WARNING_LEVEL)
  72. if (MSVC)
  73. add_compile_options(/W4)
  74. else()
  75. add_compile_options(-Wall -Wextra -Wpedantic)
  76. endif()
  77. endif()
  78. file(GLOB_RECURSE MSDFGEN_CORE_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "core/*.h" "core/*.hpp")
  79. file(GLOB_RECURSE MSDFGEN_CORE_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "core/*.cpp")
  80. file(GLOB_RECURSE MSDFGEN_EXT_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "ext/*.h" "ext/*.hpp")
  81. file(GLOB_RECURSE MSDFGEN_EXT_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "ext/*.cpp" "lib/*.cpp")
  82. # Core library
  83. add_library(msdfgen-core "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen.h" ${MSDFGEN_CORE_HEADERS} ${MSDFGEN_CORE_SOURCES})
  84. add_library(msdfgen::msdfgen-core ALIAS msdfgen-core)
  85. set_target_properties(msdfgen-core PROPERTIES PUBLIC_HEADER "${MSDFGEN_CORE_HEADERS}")
  86. set_property(TARGET msdfgen-core PROPERTY MSVC_RUNTIME_LIBRARY "${MSDFGEN_MSVC_RUNTIME}")
  87. target_compile_definitions(msdfgen-core PUBLIC
  88. MSDFGEN_VERSION=${MSDFGEN_VERSION}
  89. MSDFGEN_VERSION_MAJOR=${MSDFGEN_VERSION_MAJOR}
  90. MSDFGEN_VERSION_MINOR=${MSDFGEN_VERSION_MINOR}
  91. MSDFGEN_VERSION_REVISION=${MSDFGEN_VERSION_REVISION}
  92. MSDFGEN_COPYRIGHT_YEAR=${MSDFGEN_COPYRIGHT_YEAR}
  93. )
  94. target_include_directories(msdfgen-core INTERFACE
  95. $<INSTALL_INTERFACE:include/msdfgen>
  96. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>
  97. )
  98. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT msdfgen-core)
  99. if(MSDFGEN_USE_CPP11)
  100. target_compile_features(msdfgen-core PUBLIC cxx_std_11)
  101. target_compile_definitions(msdfgen-core PUBLIC MSDFGEN_USE_CPP11)
  102. endif()
  103. if(MSDFGEN_USE_OPENMP)
  104. # Note: Clang doesn't support OpenMP by default...
  105. find_package(OpenMP REQUIRED COMPONENTS CXX)
  106. target_compile_definitions(msdfgen-core PUBLIC MSDFGEN_USE_OPENMP)
  107. target_link_libraries(msdfgen-core PUBLIC OpenMP::OpenMP_CXX)
  108. endif()
  109. if(BUILD_SHARED_LIBS AND WIN32)
  110. target_compile_definitions(msdfgen-core PRIVATE "MSDFGEN_PUBLIC=__declspec(dllexport)")
  111. target_compile_definitions(msdfgen-core INTERFACE "MSDFGEN_PUBLIC=__declspec(dllimport)")
  112. else()
  113. target_compile_definitions(msdfgen-core PUBLIC MSDFGEN_PUBLIC=)
  114. endif()
  115. # Extensions library
  116. if(NOT MSDFGEN_CORE_ONLY)
  117. if(NOT TARGET Freetype::Freetype)
  118. find_package(Freetype REQUIRED)
  119. endif()
  120. if(NOT MSDFGEN_DISABLE_SVG AND NOT TARGET tinyxml2::tinyxml2)
  121. find_package(tinyxml2 REQUIRED)
  122. endif()
  123. if(NOT MSDFGEN_DISABLE_PNG AND NOT TARGET PNG::PNG)
  124. find_package(PNG REQUIRED)
  125. endif()
  126. add_library(msdfgen-ext "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen-ext.h" ${MSDFGEN_EXT_HEADERS} ${MSDFGEN_EXT_SOURCES})
  127. add_library(msdfgen::msdfgen-ext ALIAS msdfgen-ext)
  128. set_target_properties(msdfgen-ext PROPERTIES PUBLIC_HEADER "${MSDFGEN_EXT_HEADERS}")
  129. set_property(TARGET msdfgen-ext PROPERTY MSVC_RUNTIME_LIBRARY "${MSDFGEN_MSVC_RUNTIME}")
  130. target_compile_definitions(msdfgen-ext INTERFACE MSDFGEN_EXTENSIONS)
  131. if(NOT MSDFGEN_DISABLE_SVG)
  132. target_link_libraries(msdfgen-ext PRIVATE tinyxml2::tinyxml2)
  133. else()
  134. target_compile_definitions(msdfgen-ext PUBLIC MSDFGEN_DISABLE_SVG)
  135. endif()
  136. if(NOT MSDFGEN_DISABLE_PNG)
  137. target_compile_definitions(msdfgen-ext PUBLIC MSDFGEN_USE_LIBPNG)
  138. target_link_libraries(msdfgen-ext PRIVATE PNG::PNG)
  139. else()
  140. target_compile_definitions(msdfgen-ext PUBLIC MSDFGEN_DISABLE_PNG)
  141. endif()
  142. if(MSDFGEN_DISABLE_VARIABLE_FONTS)
  143. target_compile_definitions(msdfgen-ext PUBLIC MSDFGEN_DISABLE_VARIABLE_FONTS)
  144. endif()
  145. target_link_libraries(msdfgen-ext PRIVATE Freetype::Freetype msdfgen::msdfgen-core)
  146. target_include_directories(msdfgen-ext
  147. PUBLIC
  148. $<INSTALL_INTERFACE:include/msdfgen>
  149. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  150. PRIVATE
  151. ${CMAKE_CURRENT_SOURCE_DIR}/include
  152. )
  153. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT msdfgen-ext)
  154. if(MSDFGEN_USE_SKIA)
  155. set(MSDFGEN_SKIA_LIB skia)
  156. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  157. set(THREADS_PREFER_PTHREAD_FLAG TRUE)
  158. find_package(Threads REQUIRED)
  159. if(NOT TARGET skia)
  160. if(MSDFGEN_USE_VCPKG)
  161. find_package(unofficial-skia REQUIRED)
  162. set(MSDFGEN_SKIA_LIB unofficial::skia::skia)
  163. else()
  164. find_package(skia REQUIRED)
  165. endif()
  166. endif()
  167. target_compile_features(msdfgen-ext PUBLIC cxx_std_17)
  168. target_compile_definitions(msdfgen-ext PUBLIC MSDFGEN_USE_SKIA)
  169. target_link_libraries(msdfgen-ext PRIVATE Threads::Threads ${MSDFGEN_SKIA_LIB})
  170. endif()
  171. if(BUILD_SHARED_LIBS AND WIN32)
  172. target_compile_definitions(msdfgen-ext PRIVATE "MSDFGEN_EXT_PUBLIC=__declspec(dllexport)")
  173. target_compile_definitions(msdfgen-ext INTERFACE "MSDFGEN_EXT_PUBLIC=__declspec(dllimport)")
  174. else()
  175. target_compile_definitions(msdfgen-ext PUBLIC MSDFGEN_EXT_PUBLIC=)
  176. endif()
  177. add_library(msdfgen-full INTERFACE)
  178. add_library(msdfgen::msdfgen ALIAS msdfgen-full)
  179. target_link_libraries(msdfgen-full INTERFACE msdfgen::msdfgen-core msdfgen::msdfgen-ext)
  180. else()
  181. add_library(msdfgen::msdfgen ALIAS msdfgen-core)
  182. endif()
  183. # Standalone executable
  184. if(MSDFGEN_BUILD_STANDALONE)
  185. set(MSDFGEN_STANDALONE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
  186. if(MSVC)
  187. set(MSDFGEN_STANDALONE_SOURCES ${MSDFGEN_STANDALONE_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen.rc")
  188. endif()
  189. add_executable(msdfgen ${MSDFGEN_STANDALONE_SOURCES})
  190. target_compile_definitions(msdfgen PUBLIC MSDFGEN_STANDALONE)
  191. target_compile_definitions(msdfgen PRIVATE MSDFGEN_VERSION_UNDERLINE=${MSDFGEN_VERSION_UNDERLINE})
  192. set_property(TARGET msdfgen PROPERTY MSVC_RUNTIME_LIBRARY "${MSDFGEN_MSVC_RUNTIME}")
  193. target_link_libraries(msdfgen PRIVATE msdfgen::msdfgen)
  194. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT msdfgen)
  195. endif()
  196. # Hide ZERO_CHECK and ALL_BUILD targets
  197. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  198. set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER meta)
  199. # Installation
  200. if(MSDFGEN_INSTALL)
  201. include(GNUInstallDirs)
  202. include(CMakePackageConfigHelpers)
  203. set(MSDFGEN_CONFIG_PATH "lib/cmake/msdfgen")
  204. # Generate msdfgen-config.h
  205. if(BUILD_SHARED_LIBS AND WIN32)
  206. set(MSDFGEN_PUBLIC_MACRO_VALUE " __declspec(dllimport)")
  207. else()
  208. set(MSDFGEN_PUBLIC_MACRO_VALUE "")
  209. endif()
  210. set(MSDFGEN_ADDITIONAL_DEFINES "")
  211. if(MSDFGEN_USE_CPP11)
  212. set(MSDFGEN_ADDITIONAL_DEFINES "${MSDFGEN_ADDITIONAL_DEFINES}\n#define MSDFGEN_USE_CPP11")
  213. endif()
  214. if(MSDFGEN_USE_OPENMP)
  215. set(MSDFGEN_ADDITIONAL_DEFINES "${MSDFGEN_ADDITIONAL_DEFINES}\n#define MSDFGEN_USE_OPENMP")
  216. endif()
  217. if(NOT MSDFGEN_CORE_ONLY)
  218. set(MSDFGEN_ADDITIONAL_DEFINES "${MSDFGEN_ADDITIONAL_DEFINES}\n#define MSDFGEN_EXTENSIONS")
  219. if(MSDFGEN_USE_SKIA)
  220. set(MSDFGEN_ADDITIONAL_DEFINES "${MSDFGEN_ADDITIONAL_DEFINES}\n#define MSDFGEN_USE_SKIA")
  221. endif()
  222. if(MSDFGEN_DISABLE_SVG)
  223. set(MSDFGEN_ADDITIONAL_DEFINES "${MSDFGEN_ADDITIONAL_DEFINES}\n#define MSDFGEN_DISABLE_SVG")
  224. endif()
  225. if(NOT MSDFGEN_DISABLE_PNG)
  226. set(MSDFGEN_ADDITIONAL_DEFINES "${MSDFGEN_ADDITIONAL_DEFINES}\n#define MSDFGEN_USE_LIBPNG")
  227. else()
  228. set(MSDFGEN_ADDITIONAL_DEFINES "${MSDFGEN_ADDITIONAL_DEFINES}\n#define MSDFGEN_DISABLE_PNG")
  229. endif()
  230. if(MSDFGEN_DISABLE_VARIABLE_FONTS)
  231. set(MSDFGEN_ADDITIONAL_DEFINES "${MSDFGEN_ADDITIONAL_DEFINES}\n#define MSDFGEN_DISABLE_VARIABLE_FONTS")
  232. endif()
  233. endif()
  234. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/msdfgen-config.h.in" msdfgen-config.h)
  235. write_basic_package_version_file(
  236. "${CMAKE_CURRENT_BINARY_DIR}/msdfgenConfigVersion.cmake"
  237. VERSION ${PROJECT_VERSION}
  238. COMPATIBILITY SameMajorVersion
  239. )
  240. configure_package_config_file(
  241. "${CMAKE_CURRENT_SOURCE_DIR}/cmake/msdfgenConfig.cmake.in"
  242. ${MSDFGEN_CONFIG_PATH}/msdfgenConfig.cmake
  243. INSTALL_DESTINATION ${MSDFGEN_CONFIG_PATH}
  244. NO_CHECK_REQUIRED_COMPONENTS_MACRO
  245. )
  246. configure_file(
  247. "${CMAKE_CURRENT_SOURCE_DIR}/cmake/msdfgenConfig.cmake.in"
  248. msdfgenConfig.cmake
  249. @ONLY
  250. )
  251. install(FILES "${CMAKE_CURRENT_BINARY_DIR}/msdfgen-config.h" DESTINATION include/msdfgen)
  252. install(FILES "${CMAKE_CURRENT_BINARY_DIR}/msdfgen-config.h" DESTINATION include/msdfgen/msdfgen)
  253. install(TARGETS msdfgen-core EXPORT msdfgenTargets
  254. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  255. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  256. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  257. FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR}
  258. PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/msdfgen/core
  259. )
  260. install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen.h" DESTINATION include/msdfgen)
  261. if(MSVC AND BUILD_SHARED_LIBS)
  262. install(FILES $<TARGET_PDB_FILE:msdfgen-core> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
  263. endif()
  264. if(NOT MSDFGEN_CORE_ONLY)
  265. install(TARGETS msdfgen-ext EXPORT msdfgenTargets
  266. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  267. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  268. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  269. FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR}
  270. PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/msdfgen/ext
  271. )
  272. install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen-ext.h" DESTINATION include/msdfgen)
  273. if(MSVC AND BUILD_SHARED_LIBS)
  274. install(FILES $<TARGET_PDB_FILE:msdfgen-ext> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
  275. endif()
  276. install(TARGETS msdfgen-full EXPORT msdfgenTargets)
  277. endif()
  278. export(EXPORT msdfgenTargets NAMESPACE msdfgen:: FILE "${CMAKE_CURRENT_BINARY_DIR}/msdfgenTargets.cmake")
  279. install(EXPORT msdfgenTargets FILE msdfgenTargets.cmake NAMESPACE msdfgen:: DESTINATION ${MSDFGEN_CONFIG_PATH})
  280. if(MSDFGEN_BUILD_STANDALONE)
  281. install(TARGETS msdfgen EXPORT msdfgenBinaryTargets DESTINATION ${CMAKE_INSTALL_BINDIR})
  282. if(MSVC)
  283. install(FILES $<TARGET_PDB_FILE:msdfgen> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
  284. endif()
  285. export(EXPORT msdfgenBinaryTargets NAMESPACE msdfgen-standalone:: FILE "${CMAKE_CURRENT_BINARY_DIR}/msdfgenBinaryTargets.cmake")
  286. install(EXPORT msdfgenBinaryTargets FILE msdfgenBinaryTargets.cmake NAMESPACE msdfgen-standalone:: DESTINATION ${MSDFGEN_CONFIG_PATH})
  287. endif()
  288. install(
  289. FILES
  290. "${CMAKE_CURRENT_BINARY_DIR}/${MSDFGEN_CONFIG_PATH}/msdfgenConfig.cmake"
  291. "${CMAKE_CURRENT_BINARY_DIR}/msdfgenConfigVersion.cmake"
  292. DESTINATION ${MSDFGEN_CONFIG_PATH}
  293. )
  294. endif()