Plugins.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. set( CC_PLUGIN_TARGET_LIST "" CACHE INTERNAL "Internal plugin list" )
  2. # AddPlugin should be called once for each plugin.
  3. # This function sets up a target for the plugin, sets up default properties, and sets the target
  4. # to link to the necessary libraries.
  5. #
  6. # Arguments:
  7. # NAME The name of the plugin (this is the target)
  8. # TYPE One of "gl", "io", or "standard". If TYPE is not specified, it will default to "standard".
  9. # SHADER_FOLDER If it is a gl plugin, this is the folder containing the shaders. Ignored otherwise.
  10. function( AddPlugin )
  11. cmake_parse_arguments(
  12. ADD_PLUGIN
  13. ""
  14. "NAME;TYPE;SHADER_FOLDER"
  15. ""
  16. ${ARGN}
  17. )
  18. # For readability
  19. set( PLUGIN_TARGET "${ADD_PLUGIN_NAME}" )
  20. set( CC_PLUGIN_TARGET_LIST "${CC_PLUGIN_TARGET_LIST};${PLUGIN_TARGET}" CACHE INTERNAL "Internal plugin list" )
  21. # First check our TYPE
  22. # If none given, make it "standard"
  23. if ( NOT ADD_PLUGIN_TYPE )
  24. set( ADD_PLUGIN_TYPE "standard" )
  25. endif()
  26. # Check that we were given a valid type
  27. set( VALID_TYPES "gl" "io" "standard" )
  28. if( NOT "${ADD_PLUGIN_TYPE}" IN_LIST VALID_TYPES )
  29. # In cmake 3.12:
  30. # list( JOIN VALID_TYPES ", " VALID_TYPES_STR )
  31. string( REPLACE ";" ", " VALID_TYPES_STR "${VALID_TYPES}" )
  32. message( FATAL_ERROR "AddPlugin: Did not find proper TYPE. Valid values are: ${VALID_TYPES_STR}" )
  33. endif()
  34. # Create our target
  35. add_library( ${PLUGIN_TARGET} SHARED )
  36. # Set default properties
  37. set_target_properties( ${PLUGIN_TARGET}
  38. PROPERTIES
  39. AUTOUIC ON # FIXME Remove after everything has moved to targets and we can set it globally
  40. AUTOUIC_SEARCH_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/ui
  41. PLUGIN_TYPE "${ADD_PLUGIN_TYPE}"
  42. )
  43. # Keep track of our shader folder if it was given
  44. if( ("${ADD_PLUGIN_TYPE}" STREQUAL "gl") AND ADD_PLUGIN_SHADER_FOLDER )
  45. # Because they are added relative to the CMakeLists file, add the path
  46. set( SHADER_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}/shaders/${ADD_PLUGIN_SHADER_FOLDER}" )
  47. # Check that the folder exists
  48. if( NOT EXISTS "${SHADER_FOLDER}" )
  49. message( FATAL_ERROR "AddPlugin: SHADER_FOLDER does not exist: ${SHADER_FOLDER}" )
  50. endif()
  51. # Add it as a property
  52. set_target_properties( ${PLUGIN_TARGET}
  53. PROPERTIES
  54. SHADER_FOLDER_NAME "${ADD_PLUGIN_SHADER_FOLDER}"
  55. SHADER_FOLDER_PATH "${SHADER_FOLDER}"
  56. )
  57. endif()
  58. if( WIN32 )
  59. # Plugins need the QT_NO_DEBUG preprocessor in release!
  60. target_compile_definitions( ${PLUGIN_TARGET} PRIVATE $<$<CONFIG:Release>:QT_NO_DEBUG> )
  61. endif()
  62. # Add .qrc file to sources
  63. get_filename_component( FOLDER_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME )
  64. set( QRC_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${FOLDER_NAME}.qrc" )
  65. if( EXISTS "${QRC_FILE}" )
  66. target_sources( ${PLUGIN_TARGET} PRIVATE ${QRC_FILE} )
  67. else()
  68. message( FATAL_ERROR "Missing qrc file: ${QRC_FILE}" )
  69. endif()
  70. # Add info.json to sources
  71. set( INFO_FILE "${CMAKE_CURRENT_SOURCE_DIR}/info.json" )
  72. if( EXISTS "${INFO_FILE}" )
  73. target_sources( ${PLUGIN_TARGET} PRIVATE ${INFO_FILE} )
  74. else()
  75. message( FATAL_ERROR "Missing info.json file: ${INFO_FILE}" )
  76. endif()
  77. # Link to required libraries
  78. target_link_libraries( ${PLUGIN_TARGET}
  79. CCCoreLib
  80. CCPluginAPI
  81. CCPluginStub
  82. )
  83. # On macOS, copy the plugin to the ccPlugins directory at the top level
  84. # post build so we can find it without installing everything.
  85. if( APPLE )
  86. set( PLUGINS_OUTPUT_DIR "${CMAKE_BINARY_DIR}/ccPlugins" )
  87. if( NOT EXISTS PLUGINS_OUTPUT_DIR )
  88. file( MAKE_DIRECTORY "${PLUGINS_OUTPUT_DIR}" )
  89. endif()
  90. add_custom_command( TARGET ${PLUGIN_TARGET} POST_BUILD
  91. COMMAND
  92. ${CMAKE_COMMAND} -E copy_if_different
  93. $<TARGET_FILE:${PLUGIN_TARGET}>
  94. ${PLUGINS_OUTPUT_DIR}
  95. )
  96. endif()
  97. message( STATUS "Added ${ADD_PLUGIN_TYPE} plugin: ${ADD_PLUGIN_NAME}")
  98. endfunction()
  99. # Documentation of custom properties
  100. define_property( TARGET
  101. PROPERTY
  102. PLUGIN_TYPE
  103. BRIEF_DOCS
  104. "Type of plugin: gl, io, or standard."
  105. FULL_DOCS
  106. "Type of plugin: gl, io, or standard. Used to decide where to install this plugin."
  107. )
  108. define_property( TARGET
  109. PROPERTY
  110. SHADER_FOLDER_NAME
  111. BRIEF_DOCS
  112. "Name of the shader folder for a gl plugin."
  113. FULL_DOCS
  114. "Name of the shader folder for a gl plugin. Used as the name of the folder when installing the shader files."
  115. )
  116. define_property( TARGET
  117. PROPERTY
  118. SHADER_FOLDER_PATH
  119. BRIEF_DOCS
  120. "Path to the folder containing the shader files for a gl plugin."
  121. FULL_DOCS
  122. "Path to the folder containing the shader files for a gl plugin. Files are copied from here when installing."
  123. )