CMakeLists.txt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. cmake_minimum_required(VERSION 3.1)
  2. project(nanovg_example)
  3. set(CMAKE_CXX_STANDARD 20)
  4. set (BUILD_SHARED_LIBS OFF)
  5. set( GLFW_BUILD_DOCS OFF CACHE BOOL "" )
  6. set( GLFW_BUILD_TESTS OFF CACHE BOOL "" )
  7. set( GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" )
  8. set( GLFW_BUILD_INSTALL OFF CACHE BOOL "" )
  9. add_subdirectory(../glfw glfw)
  10. option(BUILD_USE_GLAD "Use glad instead of glew" ON)
  11. option(BUILD_EXAMPLE_FBO "Build FBO Example (GL3)" OFF)
  12. option(BUILD_EXAMPLE_GL2 "Build Example using OpenGL2" OFF)
  13. option(BUILD_EXAMPLE_GL3 "Build Example using OpenGL3" ON)
  14. option(BUILD_EXAMPLE_VULKAN "Build Example using Vulkan GLFW" ON)
  15. option(BUILD_EXAMPLE_VULKAN_MIN "Build minimal Example using Vulkan without GLFW" ON)
  16. option(DEMO_ANTIALIAS "Use NVG_ANTIALIAS" ON)
  17. option(DEMO_MSAA "Use MSAA instead of NVG_ANTIALIAS" OFF)
  18. option(DEMO_STENCIL_STROKES "NVG_STENCIL_STROKES" ON)
  19. add_compile_definitions(DEMO_MSAA=$<BOOL:${DEMO_MSAA}>)
  20. add_compile_definitions(DEMO_ANTIALIAS=$<BOOL:${DEMO_ANTIALIAS}>)
  21. add_compile_definitions(DEMO_STENCIL_STROKES=$<BOOL:${DEMO_STENCIL_STROKES}>)
  22. set(COMMON_INCLUDE ../src)
  23. set(COMMON_SRC
  24. ../src/fontstash.h
  25. ../src/nanovg.c
  26. ../src/nanovg.h
  27. ../src/stb_image.h
  28. ../src/stb_truetype.h
  29. ../src/nanovg.c
  30. demo.c
  31. perf.c
  32. )
  33. if (BUILD_EXAMPLE_VULKAN)
  34. find_package(Vulkan)
  35. if (Vulkan_FOUND)
  36. message(STATUS "Vulkan found ${Vulkan_LIBRARIES}")
  37. else()
  38. message(FATAL "Vulkan not found")
  39. endif()
  40. add_executable(
  41. example-vk
  42. ${COMMON_SRC}
  43. ../src/nanovg_vk.h
  44. example_vulkan.c
  45. vulkan_util.h
  46. )
  47. target_include_directories(example-vk PRIVATE ${COMMON_INCLUDE} ${Vulkan_INCLUDE_DIR})
  48. target_compile_definitions(example-vk PRIVATE NANOVG_VULKAN_IMPLEMENTATION)
  49. target_link_libraries(example-vk PRIVATE glfw ${Vulkan_LIBRARY})
  50. if (BUILD_EXAMPLE_VULKAN_MIN)
  51. add_executable(
  52. example-vk_min_no_glfw
  53. ${COMMON_SRC}
  54. ../src/nanovg_vk.h
  55. example_vulkan_min_no_glfw.c
  56. vulkan_util.h
  57. )
  58. if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
  59. add_definitions(-DVK_USE_PLATFORM_WIN32_KHR )
  60. target_include_directories(example-vk_min_no_glfw PRIVATE ${COMMON_INCLUDE} ${Vulkan_INCLUDE_DIR})
  61. target_compile_definitions(example-vk_min_no_glfw PRIVATE NANOVG_VULKAN_IMPLEMENTATION)
  62. target_link_libraries(example-vk_min_no_glfw PRIVATE ${Vulkan_LIBRARY})
  63. elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
  64. add_definitions(-DVK_USE_PLATFORM_XCB_KHR -O3)
  65. target_include_directories(example-vk_min_no_glfw PRIVATE ${COMMON_INCLUDE} ${Vulkan_INCLUDE_DIR})
  66. target_compile_definitions(example-vk_min_no_glfw PRIVATE NANOVG_VULKAN_IMPLEMENTATION)
  67. target_link_libraries(example-vk_min_no_glfw PRIVATE m xcb ${Vulkan_LIBRARY})
  68. else()
  69. message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!")
  70. endif()
  71. endif()
  72. endif()
  73. set(GL_EXAMPLE_SRC
  74. ${COMMON_SRC}
  75. ../src/nanovg_gl.h
  76. ../src/nanovg_gl_utils.h
  77. )
  78. set(GL_EXAMPLE_LIBS glfw)
  79. set(GL_EXAMPLE_INCLUDES ${COMMON_INCLUDE})
  80. if (BUILD_EXAMPLE_GL3 OR BUILD_EXAMPLE_GL2)
  81. if (BUILD_USE_GLAD)
  82. set(GL_EXAMPLE_DEFS NANOVG_GLAD)
  83. list(APPEND GL_EXAMPLE_SRC ../glad-3.2-compat/src/glad.c)
  84. list(APPEND GL_EXAMPLE_INCLUDES ../glad-3.2-compat/include)
  85. else()
  86. # Build glew
  87. set( ONLY_LIBS ON CACHE BOOL "" )
  88. set( glew-cmake_BUILD_SHARED OFF CACHE BOOL "" )
  89. set( glew-cmake_BUILD_STATIC ON CACHE BOOL "" )
  90. set(GL_EXAMPLE_DEFS NANVG_GLEW)
  91. add_subdirectory(glew-cmake glew)
  92. list(APPEND GL_EXAMPLE_LIBS libglew_static)
  93. endif()
  94. endif()
  95. if (BUILD_EXAMPLE_GL3)
  96. add_executable(
  97. example-gl3
  98. example_gl3.c
  99. ${GL_EXAMPLE_SRC}
  100. )
  101. target_include_directories(example-gl3 PRIVATE ${GL_EXAMPLE_INCLUDES})
  102. target_compile_definitions(example-gl3 PRIVATE ${GL_EXAMPLE_DEFS})
  103. target_link_libraries(example-gl3 PRIVATE ${GL_EXAMPLE_LIBS})
  104. endif()
  105. if (BUILD_EXAMPLE_GL2)
  106. add_executable(
  107. example-gl2
  108. example_gl2.c
  109. ${GL_EXAMPLE_SRC}
  110. )
  111. target_include_directories(example-gl2 PRIVATE ${GL_EXAMPLE_INCLUDES})
  112. target_compile_definitions(example-gl2 PRIVATE ${GL_EXAMPLE_DEFS})
  113. target_link_libraries(example-gl2 PRIVATE ${GL_EXAMPLE_LIBS})
  114. endif()
  115. if (BUILD_EXAMPLE_FBO)
  116. add_executable(
  117. example-fbo
  118. example_fbo.c
  119. ${GL_EXAMPLE_SRC}
  120. )
  121. target_include_directories(example-fbo PRIVATE ${GL_EXAMPLE_INCLUDES})
  122. target_compile_definitions(example-fbo PRIVATE ${GL_EXAMPLE_DEFS})
  123. target_link_libraries(example-fbo PRIVATE ${GL_EXAMPLE_LIBS})
  124. endif()
  125. if (MSVC)
  126. add_compile_definitions(_CRT_SECURE_NO_WARNINGS )
  127. set_source_files_properties(
  128. example_vulkan.c
  129. example_vulkan_min_no_glfw.c
  130. example_gl3.c
  131. example_gl2.c
  132. example_fbo.c
  133. demo.c
  134. PROPERTIES COMPILE_FLAGS
  135. /wd4244
  136. )
  137. set(BUILD_EXE_POSTFIX "-$<LOWER_CASE:${CMAKE_CXX_COMPILER_ID}-$<CONFIG>>" CACHE STRING "Append to executable name")
  138. set(ALL_TARGETS example-vk example-vk_min_no_glfw example-gl3 example-gl2)
  139. foreach(TARGET_NAME ${ALL_TARGETS})
  140. if (TARGET ${TARGET_NAME})
  141. set_property(TARGET ${TARGET_NAME} PROPERTY OUTPUT_NAME ${TARGET_NAME}${BUILD_EXE_POSTFIX})
  142. set_property(TARGET ${TARGET_NAME} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}$<0:...>)
  143. endif()
  144. endforeach()
  145. endif()
  146. set(CMAKE_VERBOSE_MAKEFILE ON)