CalamaresAddModuleSubdirectory.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. # Function and support code for adding a Calamares module (either a Qt / C++ plugin,
  22. # or a Python module, or whatever) to the build.
  23. #
  24. include( CalamaresAddTranslations )
  25. set( MODULE_DATA_DESTINATION share/calamares/modules )
  26. # Convenience function to indicate that a module has been skipped
  27. # (optionally also why). Call this in the module's CMakeLists.txt
  28. macro( calamares_skip_module )
  29. set( SKIPPED_MODULES ${SKIPPED_MODULES} ${ARGV} PARENT_SCOPE )
  30. endmacro()
  31. function( calamares_explain_skipped_modules )
  32. if ( ARGN )
  33. message( "${ColorReset}-- Skipped modules:" )
  34. foreach( SUBDIRECTORY ${ARGN} )
  35. message( "${ColorReset}-- Skipped ${BoldRed}${SUBDIRECTORY}${ColorReset}." )
  36. endforeach()
  37. message( "" )
  38. endif()
  39. endfunction()
  40. function( calamares_add_module_subdirectory )
  41. set( SUBDIRECTORY ${ARGV0} )
  42. set( SKIPPED_MODULES )
  43. set( MODULE_CONFIG_FILES "" )
  44. set( _mod_dir "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIRECTORY}" )
  45. # If this subdirectory has a CMakeLists.txt, we add_subdirectory it...
  46. if( EXISTS "${_mod_dir}/CMakeLists.txt" )
  47. add_subdirectory( ${SUBDIRECTORY} )
  48. file( GLOB MODULE_CONFIG_FILES RELATIVE ${_mod_dir} "${SUBDIRECTORY}/*.conf" )
  49. # Module has indicated it should be skipped, show that in
  50. # the calling CMakeLists (which is src/modules/CMakeLists.txt normally).
  51. if ( SKIPPED_MODULES )
  52. set( SKIPPED_MODULES ${SKIPPED_MODULES} PARENT_SCOPE )
  53. set( MODULE_CONFIG_FILES "" )
  54. endif()
  55. # ...otherwise, we look for a module.desc.
  56. elseif( EXISTS "${_mod_dir}/module.desc" )
  57. set( MODULES_DIR ${CMAKE_INSTALL_LIBDIR}/calamares/modules )
  58. set( MODULE_DESTINATION ${MODULES_DIR}/${SUBDIRECTORY} )
  59. # Read module.desc, check that the interface type is supported.
  60. #
  61. # _mod_enabled boolean if the module should be built (only if the interface is supported)
  62. # _mod_reason is a human-readable explanation why it isn't built
  63. # _mod_testing boolean if the module should be added to the loadmodule tests
  64. file(STRINGS "${_mod_dir}/module.desc" MODULE_INTERFACE REGEX "^interface")
  65. if ( MODULE_INTERFACE MATCHES "pythonqt" )
  66. set( _mod_enabled ${WITH_PYTHONQT} )
  67. set( _mod_reason "No PythonQt support" )
  68. set( _mod_testing OFF )
  69. elseif ( MODULE_INTERFACE MATCHES "python" )
  70. set( _mod_enabled ${WITH_PYTHON} )
  71. set( _mod_reason "No Python support" )
  72. set( _mod_testing ON ) # Will check syntax and imports, at least
  73. elseif ( MODULE_INTERFACE MATCHES "qtplugin" )
  74. set( _mod_enabled OFF )
  75. set( _mod_reason "C++ modules must have a CMakeLists.txt instead" )
  76. set( _mod_testing OFF )
  77. elseif ( MODULE_INTERFACE MATCHES "process" )
  78. set( _mod_enabled ON )
  79. set( _mod_reason "" )
  80. set( _mod_testing OFF )
  81. else()
  82. set( _mod_enabled OFF )
  83. set( _mod_reason "Unknown module interface '${MODULE_INTERFACE}'" )
  84. set( _mod_testing OFF )
  85. endif()
  86. if ( _mod_enabled )
  87. # We glob all the files inside the subdirectory, and we make sure they are
  88. # synced with the bindir structure and installed.
  89. file( GLOB MODULE_FILES RELATIVE ${_mod_dir} "${SUBDIRECTORY}/*" )
  90. foreach( MODULE_FILE ${MODULE_FILES} )
  91. if( NOT IS_DIRECTORY ${_mod_dir}/${MODULE_FILE} )
  92. configure_file( ${SUBDIRECTORY}/${MODULE_FILE} ${SUBDIRECTORY}/${MODULE_FILE} COPYONLY )
  93. get_filename_component( FLEXT ${MODULE_FILE} EXT )
  94. if( "${FLEXT}" STREQUAL ".conf" )
  95. if( INSTALL_CONFIG )
  96. install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${SUBDIRECTORY}/${MODULE_FILE}
  97. DESTINATION ${MODULE_DATA_DESTINATION} )
  98. endif()
  99. list( APPEND MODULE_CONFIG_FILES ${MODULE_FILE} )
  100. else()
  101. install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${SUBDIRECTORY}/${MODULE_FILE}
  102. DESTINATION ${MODULE_DESTINATION} )
  103. endif()
  104. endif()
  105. endforeach()
  106. message( "-- ${BoldYellow}Found ${CALAMARES_APPLICATION_NAME} module: ${BoldRed}${SUBDIRECTORY}${ColorReset}" )
  107. message( " ${Green}TYPE:${ColorReset} jobmodule" )
  108. message( " ${Green}MODULE_DESTINATION:${ColorReset} ${MODULE_DESTINATION}" )
  109. if( MODULE_CONFIG_FILES )
  110. if ( INSTALL_CONFIG )
  111. set( _destination "${MODULE_DATA_DESTINATION}" )
  112. else()
  113. set( _destination "[Build directory only]" )
  114. endif()
  115. message( " ${Green}CONFIGURATION_FILES:${ColorReset} ${MODULE_CONFIG_FILES} => ${_destination}" )
  116. endif()
  117. message( "" )
  118. # We copy over the lang directory, if any
  119. if( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIRECTORY}/lang" )
  120. install_calamares_gettext_translations(
  121. ${SUBDIRECTORY}
  122. SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIRECTORY}/lang"
  123. FILENAME ${SUBDIRECTORY}.mo
  124. RENAME calamares-${SUBDIRECTORY}.mo
  125. )
  126. endif()
  127. else()
  128. # Module disabled due to missing dependencies / unsupported interface
  129. set( SKIPPED_MODULES "${SUBDIRECTORY} (${_mod_reason})" PARENT_SCOPE )
  130. endif()
  131. else()
  132. message( "-- ${BoldYellow}Warning:${ColorReset} tried to add module subdirectory ${BoldRed}${SUBDIRECTORY}${ColorReset} which has no CMakeLists.txt or module.desc." )
  133. message( "" )
  134. endif()
  135. # Check any config files for basic correctness
  136. if ( BUILD_TESTING AND MODULE_CONFIG_FILES )
  137. set( _count 0 )
  138. foreach( _config_file ${MODULE_CONFIG_FILES} )
  139. set( _count_str "-${_count}" )
  140. if ( _count EQUAL 0 )
  141. set( _count_str "" )
  142. endif()
  143. add_test(
  144. NAME config-${SUBDIRECTORY}${_count_str}
  145. COMMAND test_conf ${CMAKE_CURRENT_BINARY_DIR}/${SUBDIRECTORY}/${_config_file} )
  146. math( EXPR _count "${_count} + 1" )
  147. endforeach()
  148. endif()
  149. # Check that the module can be loaded. Since this calls exec(), the module
  150. # may try to do things to the running system. Needs work to make that a
  151. # safe thing to do.
  152. #
  153. if ( BUILD_TESTING AND _mod_enabled AND _mod_testing )
  154. add_test(
  155. NAME load-${SUBDIRECTORY}
  156. COMMAND loadmodule ${SUBDIRECTORY}
  157. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  158. )
  159. endif()
  160. endfunction()