CalamaresAddPlugin.cmake 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # === This file is part of Calamares - <https://github.com/calamares> ===
  2. #
  3. # Calamares is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # Calamares is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Calamares. If not, see <http://www.gnu.org/licenses/>.
  15. #
  16. # SPDX-License-Identifier: GPL-3.0+
  17. # License-Filename: LICENSE
  18. #
  19. ###
  20. #
  21. # Convenience function for creating a C++ (qtplugin) module for Calamares.
  22. # This function provides cmake-time feedback about the plugin, adds
  23. # targets for compilation and boilerplate information, and creates
  24. # a module.desc with standard values if none is provided (which only
  25. # happens for very unusual plugins).
  26. #
  27. # Usage:
  28. #
  29. # calamares_add_plugin(
  30. # module-name
  31. # TYPE <view|job>
  32. # EXPORT_MACRO macro-name
  33. # SOURCES source-file...
  34. # UI ui-file...
  35. # LINK_LIBRARIES lib...
  36. # LINK_PRIVATE_LIBRARIES lib...
  37. # COMPILE_DEFINITIONS def...
  38. # RESOURCES resource-file
  39. # [NO_INSTALL]
  40. # [SHARED_LIB]
  41. # [EMERGENCY]
  42. # )
  43. #
  44. # The COMPILE_DEFINITIONS are set on the resulting module with a suitable
  45. # flag (i.e. `-D`) so only state the name (optionally, also the value)
  46. # without a `-D` prefixed to it.
  47. include( CMakeParseArguments )
  48. include( CalamaresAddLibrary )
  49. include( CMakeColors )
  50. function( calamares_add_plugin )
  51. # parse arguments ( name needs to be saved before passing ARGN into the macro )
  52. set( NAME ${ARGV0} )
  53. set( options NO_INSTALL SHARED_LIB EMERGENCY )
  54. set( oneValueArgs NAME TYPE EXPORT_MACRO RESOURCES )
  55. set( multiValueArgs SOURCES UI LINK_LIBRARIES LINK_PRIVATE_LIBRARIES COMPILE_DEFINITIONS )
  56. cmake_parse_arguments( PLUGIN "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  57. set( PLUGIN_NAME ${NAME} )
  58. set( PLUGIN_DESTINATION ${CMAKE_INSTALL_LIBDIR}/calamares/modules/${PLUGIN_NAME} )
  59. set( PLUGIN_DESC_FILE module.desc )
  60. file( GLOB PLUGIN_CONFIG_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.conf" )
  61. set( PLUGIN_DATA_DESTINATION share/calamares/modules )
  62. set( CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" )
  63. set( CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" )
  64. set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" )
  65. message( "-- ${BoldYellow}Found ${CALAMARES_APPLICATION_NAME} module: ${BoldRed}${PLUGIN_NAME}${ColorReset}" )
  66. message( " ${Green}TYPE:${ColorReset} ${PLUGIN_TYPE}" )
  67. message( " ${Green}LINK_LIBRARIES:${ColorReset} ${PLUGIN_LINK_LIBRARIES}" )
  68. message( " ${Green}LINK_PRIVATE_LIBRARIES:${ColorReset} ${PLUGIN_LINK_PRIVATE_LIBRARIES}" )
  69. message( " ${Green}PLUGIN_DESTINATION:${ColorReset} ${PLUGIN_DESTINATION}" )
  70. if( PLUGIN_CONFIG_FILES )
  71. set( _destination "(unknown)" )
  72. if ( INSTALL_CONFIG AND NOT PLUGIN_NO_INSTALL )
  73. set( _destination "${PLUGIN_DATA_DESTINATION}" )
  74. elseif( NOT PLUGIN_NO_INSTALL )
  75. # Not INSTALL_CONFIG
  76. set( _destination "[Build directory only]" )
  77. else()
  78. set( _destination "[Skipping installation]" )
  79. endif()
  80. message( " ${Green}CONFIGURATION_FILES:${ColorReset} ${PLUGIN_CONFIG_FILES} => ${_destination}" )
  81. endif()
  82. if( PLUGIN_RESOURCES )
  83. message( " ${Green}RESOURCES:${ColorReset} ${PLUGIN_RESOURCES}" )
  84. endif()
  85. message( "" )
  86. # create target name once for convenience
  87. set( target "calamares_${PLUGIN_TYPE}_${PLUGIN_NAME}" )
  88. # determine target type
  89. if( NOT ${PLUGIN_SHARED_LIB} )
  90. set( target_type "MODULE" )
  91. else()
  92. set( target_type "SHARED" )
  93. endif()
  94. set( calamares_add_library_args
  95. "${target}"
  96. "EXPORT_MACRO" "${PLUGIN_EXPORT_MACRO}"
  97. "TARGET_TYPE" "${target_type}"
  98. "SOURCES" "${PLUGIN_SOURCES}"
  99. )
  100. if( PLUGIN_UI )
  101. list( APPEND calamares_add_library_args "UI" "${PLUGIN_UI}" )
  102. endif()
  103. if( PLUGIN_LINK_LIBRARIES )
  104. list( APPEND calamares_add_library_args "LINK_LIBRARIES" "${PLUGIN_LINK_LIBRARIES}" )
  105. endif()
  106. if( PLUGIN_LINK_PRIVATE_LIBRARIES )
  107. list( APPEND calamares_add_library_args "LINK_PRIVATE_LIBRARIES" "${PLUGIN_LINK_PRIVATE_LIBRARIES}" )
  108. endif()
  109. if( PLUGIN_COMPILE_DEFINITIONS )
  110. list( APPEND calamares_add_library_args "COMPILE_DEFINITIONS" ${PLUGIN_COMPILE_DEFINITIONS} )
  111. endif()
  112. if ( PLUGIN_NO_INSTALL )
  113. list( APPEND calamares_add_library_args "NO_INSTALL" )
  114. endif()
  115. list( APPEND calamares_add_library_args
  116. "NO_VERSION"
  117. "INSTALL_BINDIR" "${PLUGIN_DESTINATION}"
  118. )
  119. if( PLUGIN_RESOURCES )
  120. list( APPEND calamares_add_library_args "RESOURCES" "${PLUGIN_RESOURCES}" )
  121. endif()
  122. calamares_add_library( ${calamares_add_library_args} )
  123. if ( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${PLUGIN_DESC_FILE} )
  124. configure_file( ${PLUGIN_DESC_FILE} ${PLUGIN_DESC_FILE} COPYONLY )
  125. else()
  126. set( _file ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_DESC_FILE} )
  127. set( _type ${PLUGIN_TYPE} )
  128. file( WRITE ${_file} "# AUTO-GENERATED metadata file\n# Syntax is YAML 1.2\n---\n" )
  129. file( APPEND ${_file} "type: \"${_type}\"\nname: \"${PLUGIN_NAME}\"\ninterface: \"qtplugin\"\nload: \"lib${target}.so\"\n" )
  130. if ( PLUGIN_EMERGENCY )
  131. file( APPEND ${_file} "emergency: true\n" )
  132. endif()
  133. endif()
  134. if ( NOT PLUGIN_NO_INSTALL )
  135. install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_DESC_FILE}
  136. DESTINATION ${PLUGIN_DESTINATION} )
  137. foreach( PLUGIN_CONFIG_FILE ${PLUGIN_CONFIG_FILES} )
  138. configure_file( ${PLUGIN_CONFIG_FILE} ${PLUGIN_CONFIG_FILE} COPYONLY )
  139. if ( INSTALL_CONFIG )
  140. install(
  141. FILES ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_CONFIG_FILE}
  142. DESTINATION ${PLUGIN_DATA_DESTINATION} )
  143. endif()
  144. endforeach()
  145. endif()
  146. endfunction()