helpers.cmake 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # CMake Windows helper functions module
  2. include_guard(GLOBAL)
  3. include(helpers_common)
  4. # set_target_properties_plugin: Set target properties for use in obs-studio
  5. function(set_target_properties_plugin target)
  6. set(options "")
  7. set(oneValueArgs "")
  8. set(multiValueArgs PROPERTIES)
  9. cmake_parse_arguments(PARSE_ARGV 0 _STPO "${options}" "${oneValueArgs}" "${multiValueArgs}")
  10. message(DEBUG "Setting additional properties for target ${target}...")
  11. while(_STPO_PROPERTIES)
  12. list(POP_FRONT _STPO_PROPERTIES key value)
  13. set_property(TARGET ${target} PROPERTY ${key} "${value}")
  14. endwhile()
  15. string(TIMESTAMP CURRENT_YEAR "%Y")
  16. set_target_properties(${target} PROPERTIES VERSION 0 SOVERSION ${PLUGIN_VERSION})
  17. install(TARGETS ${target} RUNTIME DESTINATION "${target}/bin/64bit" LIBRARY DESTINATION "${target}/bin/64bit")
  18. install(
  19. FILES "$<TARGET_PDB_FILE:${target}>"
  20. CONFIGURATIONS RelWithDebInfo Debug Release
  21. DESTINATION "${target}/bin/64bit"
  22. OPTIONAL
  23. )
  24. if(TARGET plugin-support)
  25. target_link_libraries(${target} PRIVATE plugin-support)
  26. endif()
  27. add_custom_command(
  28. TARGET ${target}
  29. POST_BUILD
  30. COMMAND "${CMAKE_COMMAND}" -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/rundir/$<CONFIG>"
  31. COMMAND
  32. "${CMAKE_COMMAND}" -E copy_if_different "$<TARGET_FILE:${target}>"
  33. "$<$<CONFIG:Debug,RelWithDebInfo,Release>:$<TARGET_PDB_FILE:${target}>>"
  34. "${CMAKE_CURRENT_BINARY_DIR}/rundir/$<CONFIG>"
  35. COMMENT "Copy ${target} to rundir"
  36. VERBATIM
  37. )
  38. target_install_resources(${target})
  39. get_target_property(target_sources ${target} SOURCES)
  40. set(target_ui_files ${target_sources})
  41. list(FILTER target_ui_files INCLUDE REGEX ".+\\.(ui|qrc)")
  42. source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" PREFIX "UI Files" FILES ${target_ui_files})
  43. configure_file(cmake/windows/resources/resource.rc.in "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.rc")
  44. target_sources(${CMAKE_PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.rc")
  45. endfunction()
  46. # Helper function to add resources into bundle
  47. function(target_install_resources target)
  48. message(DEBUG "Installing resources for target ${target}...")
  49. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/data")
  50. file(GLOB_RECURSE data_files "${CMAKE_CURRENT_SOURCE_DIR}/data/*")
  51. foreach(data_file IN LISTS data_files)
  52. cmake_path(
  53. RELATIVE_PATH
  54. data_file
  55. BASE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/data/"
  56. OUTPUT_VARIABLE relative_path
  57. )
  58. cmake_path(GET relative_path PARENT_PATH relative_path)
  59. target_sources(${target} PRIVATE "${data_file}")
  60. source_group("Resources/${relative_path}" FILES "${data_file}")
  61. endforeach()
  62. install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/data/" DESTINATION "${target}/data" USE_SOURCE_PERMISSIONS)
  63. add_custom_command(
  64. TARGET ${target}
  65. POST_BUILD
  66. COMMAND "${CMAKE_COMMAND}" -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/rundir/$<CONFIG>/${target}"
  67. COMMAND
  68. "${CMAKE_COMMAND}" -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/data"
  69. "${CMAKE_CURRENT_BINARY_DIR}/rundir/$<CONFIG>/${target}"
  70. COMMENT "Copy ${target} resources to rundir"
  71. VERBATIM
  72. )
  73. endif()
  74. endfunction()
  75. # Helper function to add a specific resource to a bundle
  76. function(target_add_resource target resource)
  77. message(DEBUG "Add resource '${resource}' to target ${target} at destination '${target_destination}'...")
  78. install(FILES "${resource}" DESTINATION "${target}/data" COMPONENT Runtime)
  79. add_custom_command(
  80. TARGET ${target}
  81. POST_BUILD
  82. COMMAND "${CMAKE_COMMAND}" -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/rundir/$<CONFIG>/${target}"
  83. COMMAND "${CMAKE_COMMAND}" -E copy "${resource}" "${CMAKE_CURRENT_BINARY_DIR}/rundir/$<CONFIG>/${target}"
  84. COMMENT "Copy ${target} resource ${resource} to rundir"
  85. VERBATIM
  86. )
  87. source_group("Resources" FILES "${resource}")
  88. endfunction()