Debian Codegraph @

CMakeModules/CalamaresAddModuleSubdirectory.cmake

raw
1 # === This file is part of Calamares - <https://calamares.io> ===
2 #
3 # SPDX-FileCopyrightText: 2014 Teo Mrnjavac <[email protected]>
4 # SPDX-FileCopyrightText: 2017 Adriaan de Groot <[email protected]>
5 # SPDX-License-Identifier: BSD-2-Clause
6 #
7 # Calamares is Free Software: see the License-Identifier above.
8 #
9 #
10 ###
11 #
12 # Function and support code for adding a Calamares module (either a Qt / C++ plugin,
13 # or a Python module, or whatever) to the build.
14 #
15 # # Usage
16 #
17 # The public API is one single function:
18 #
19 # - calamares_add_module_subdirectory(subdirectory [skiplistvar])
20 # Adds a given *subdirectory* to the modules list, building the
21 # module that is there. The *subdirectory* must contain a `module.desc`
22 # (generally non-C++ modules) or a `CMakeLists.txt` (for C++ modules,
23 # or special cases). The module is assumed to be named after the
24 # (last component of) the subdirectory.
25 #
26 # If the module would be skipped (by the global SKIP_MODULES setting
27 # or a USE_* setting) or the module itself sets a reason to skip
28 # via the calamares_skip_module() function, the module is added to
29 # the list of skipped-modules in *skiplistvar*. If no variable is
30 # given, the reason is set in the parent scope variable
31 # SKIPPED_MODULES . Do **not** use SKIPPED_MODULES as the name of
32 # *skiplistvar*, things will get weird.
33 #
34 # Do note that the name of a module must be the same as the name of
35 # the directory containing it (as documented in src/modules/README.md).
36 # This applies to both C++ and Python modules, and allows the use of
37 # the subdirectory as a proxy for the module name inside.
38 #
39
40 include( CalamaresAddTranslations )
41 include( CalamaresCheckModuleSelection )
42
43 set( MODULE_DATA_DESTINATION share/calamares/modules )
44
45 # We look for Pylint (just once) so that unittests can be added that
46 # check the syntax / variables of Python modules. This should help
47 # avoid more typo's-in-releases.
48 if(BUILD_TESTING AND NOT PYLINT_COMMAND_SEARCHED)
49 set(PYLINT_COMMAND_SEARCHED TRUE)
50 find_program(
51 PYLINT_COMMAND
52 NAMES pylint3 pylint
53 PATHS $ENV{HOME}/.local/bin
54 )
55 endif()
56
57 function( _calamares_add_module_subdirectory_impl )
58 set( SUBDIRECTORY ${ARGV0} )
59
60 # Set SKIPPED_MODULES here, so CMake-based modules have a
61 # parent scope to set it in; this function, in turn sets it
62 # in **its** parent scope.
63 set( SKIPPED_MODULES "" )
64 set( MODULE_CONFIG_FILES "" )
65
66 # The module subdirectory may be given as a/b/c, but the module
67 # needs to be installed as "c", so we split off any intermediate
68 # directories.
69 #
70 # Compute _modulename (the last directory name) and _mod_dir
71 # (the full path to the module sources).
72 get_filename_component(_dirname "${SUBDIRECTORY}" DIRECTORY)
73 if( _dirname )
74 # Remove the dirname and any leftover leading /s
75 string( REGEX REPLACE "^${_dirname}/*" "" _modulename "${SUBDIRECTORY}" )
76 else()
77 set( _modulename ${SUBDIRECTORY} )
78 endif()
79 # Strip any remaining /
80 string( REGEX REPLACE "/" "" _modulename "${_modulename}" )
81 set( _mod_dir "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIRECTORY}" )
82
83 # Skip list check applies to all kinds of modules
84 calamares_check_skip( ${_modulename} SKIPPED_MODULES )
85 if ( SKIPPED_MODULES )
86 # If it's skipped by infrastucture, the message already includes the module
87 # name. We don't need to do any further checking.
88 set( SKIPPED_MODULES "${SKIPPED_MODULES}" PARENT_SCOPE )
89 return()
90 endif()
91
92 # If this subdirectory has a CMakeLists.txt, we add_subdirectory it...
93 if( EXISTS "${_mod_dir}/CMakeLists.txt" )
94 add_subdirectory( ${SUBDIRECTORY} )
95 file( GLOB MODULE_CONFIG_FILES RELATIVE ${_mod_dir} "${SUBDIRECTORY}/*.conf" )
96 # Module has indicated it should be skipped, show that in
97 # the calling CMakeLists (which is src/modules/CMakeLists.txt normally).
98 if ( SKIPPED_MODULES )
99 set( SKIPPED_MODULES ${SKIPPED_MODULES} PARENT_SCOPE )
100 set( MODULE_CONFIG_FILES "" )
101 else()
102 # The SKIPPED_MODULES may be set in the directory itself
103 get_directory_property( _skip DIRECTORY ${SUBDIRECTORY} DEFINITION SKIPPED_MODULES )
104 if ( _skip )
105 set( SKIPPED_MODULES ${_skip} PARENT_SCOPE )
106 set( MODULE_CONFIG_FILES "" )
107 endif()
108 endif()
109 if ( SKIPPED_MODULES )
110 return()
111 endif()
112 # ...otherwise, we look for a module.desc.
113 elseif( EXISTS "${_mod_dir}/module.desc" )
114 set( MODULES_DIR ${CMAKE_INSTALL_LIBDIR}/calamares/modules )
115 set( MODULE_DESTINATION ${MODULES_DIR}/${_modulename} )
116
117 # Read module.desc, check that the interface type is supported.
118 #
119 # _mod_enabled boolean if the module should be built (only if the interface is supported)
120 # _mod_reason is a human-readable explanation why it isn't built
121 # _mod_testing boolean if the module should be added to the loadmodule tests
122 file(STRINGS "${_mod_dir}/module.desc" MODULE_INTERFACE REGEX "^interface")
123 if ( MODULE_INTERFACE MATCHES "pythonqt" )
124 message( FATAL_ERROR "PythonQt is no longer supported" )
125 set( _mod_enabled OFF )
126 set( _mod_reason "No PythonQt support" )
127 set( _mod_testing OFF )
128 elseif ( MODULE_INTERFACE MATCHES "python" )
129 set( _mod_enabled ${Calamares_WITH_PYTHON} )
130 set( _mod_reason "No Python support" )
131 set( _mod_testing ON ) # Will check syntax and imports, at least
132 elseif ( MODULE_INTERFACE MATCHES "qtplugin" )
133 set( _mod_enabled OFF )
134 set( _mod_reason "C++ modules must have a CMakeLists.txt instead" )
135 set( _mod_testing OFF )
136 elseif ( MODULE_INTERFACE MATCHES "process" )
137 set( _mod_enabled ON )
138 set( _mod_reason "" )
139 set( _mod_testing OFF )
140 else()
141 set( _mod_enabled OFF )
142 set( _mod_reason "Unknown module interface '${MODULE_INTERFACE}'" )
143 set( _mod_testing OFF )
144 endif()
145
146 if ( _mod_enabled )
147 # We glob all the files inside the subdirectory, and we make sure they are
148 # synced with the bindir structure and installed.
149 file( GLOB MODULE_FILES RELATIVE ${_mod_dir} "${SUBDIRECTORY}/*" )
150 foreach( MODULE_FILE ${MODULE_FILES} )
151 if( NOT IS_DIRECTORY ${_mod_dir}/${MODULE_FILE} )
152 configure_file( ${SUBDIRECTORY}/${MODULE_FILE} ${SUBDIRECTORY}/${MODULE_FILE} COPYONLY )
153
154 get_filename_component( FLEXT ${MODULE_FILE} EXT )
155 if( "${FLEXT}" STREQUAL ".conf" )
156 message(STATUS "Config ${MODULE_FILE}")
157 list( APPEND MODULE_CONFIG_FILES ${MODULE_FILE} )
158 else()
159 message(STATUS "Non-Config ${MODULE_FILE}")
160 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${SUBDIRECTORY}/${MODULE_FILE}
161 DESTINATION ${MODULE_DESTINATION} )
162 endif()
163 endif()
164 endforeach()
165
166 message( "-- ${BoldYellow}Found ${CALAMARES_APPLICATION_NAME} module: ${BoldRed}${_modulename}${ColorReset}" )
167 message( " ${Green}TYPE:${ColorReset} jobmodule" )
168 message( " ${Green}MODULE_DESTINATION:${ColorReset} ${MODULE_DESTINATION}" )
169 if( MODULE_CONFIG_FILES )
170 if (INSTALL_CONFIG)
171 message(" ${Green}CONFIGURATION_FILES:${ColorReset} ${MODULE_CONFIG_FILES} => [Build directory and ${MODULE_DATA_DESTINATION}]")
172 foreach(_cf ${MODULE_CONFIG_FILES})
173 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${SUBDIRECTORY}/${_cf} DESTINATION ${MODULE_DATA_DESTINATION})
174 endforeach()
175 else()
176 message(" ${Green}CONFIGURATION_FILES:${ColorReset} ${MODULE_CONFIG_FILES} => [Build directory only]")
177 endif()
178 endif()
179 message( "" )
180 # We copy over the lang directory, if any
181 if( IS_DIRECTORY "${_mod_dir}/lang" )
182 install_calamares_gettext_translations(
183 ${SUBDIRECTORY}
184 SOURCE_DIR "${_mod_dir}/lang"
185 FILENAME ${SUBDIRECTORY}.mo
186 RENAME calamares-${SUBDIRECTORY}.mo
187 )
188 endif()
189 else()
190 # Module disabled due to missing dependencies / unsupported interface
191 set( SKIPPED_MODULES "${SUBDIRECTORY} (${_mod_reason})" PARENT_SCOPE )
192 endif()
193 else()
194 message( "-- ${BoldYellow}Warning:${ColorReset} tried to add module subdirectory ${BoldRed}${SUBDIRECTORY}${ColorReset} which has no CMakeLists.txt or module.desc." )
195 message( "" )
196 endif()
197
198 # Check any config files for basic correctness
199 if ( BUILD_TESTING AND MODULE_CONFIG_FILES )
200 set( _count 0 )
201 foreach( _config_file ${MODULE_CONFIG_FILES} )
202 set( _count_str "-${_count}" )
203 if ( _count EQUAL 0 )
204 set( _count_str "" )
205 endif()
206 add_test(
207 NAME config-${SUBDIRECTORY}${_count_str}
208 COMMAND test_conf ${CMAKE_CURRENT_BINARY_DIR}/${SUBDIRECTORY}/${_config_file} )
209 math( EXPR _count "${_count} + 1" )
210 endforeach()
211 endif()
212
213 # Adding general tests
214 #
215 # Add a check that the module can be loaded. Since this calls exec(), the module
216 # may try to do things to the running system. Needs work to make that a
217 # safe thing to do.
218 #
219 # If the module has a tests/ subdirectory with *.global and *.job
220 # files (YAML files holding global and job-configurations for
221 # testing purposes) then those files are used to drive additional
222 # tests. The files must be numbered (starting from 1) for this to work;
223 # 1.global and 1.job together make the configuration for test 1.
224 #
225 # If the module has a tests/CMakeLists.txt while it doesn't have its
226 # own CMakeLists.txt (e.g. a Python module), then the subdirectory
227 # for tests/ is added on its own.
228 #
229 if ( BUILD_TESTING AND _mod_enabled AND _mod_testing )
230 add_test(
231 NAME load-${SUBDIRECTORY}
232 COMMAND loadmodule ${SUBDIRECTORY}
233 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
234 )
235 # Try it with the tests/ configurations shipped with the module
236 set( _count 1 )
237 set( _testdir ${_mod_dir}/tests )
238 while ( EXISTS "${_testdir}/${_count}.global" OR EXISTS "${_testdir}/${_count}.job" )
239 set( _dash_g "" )
240 set( _dash_j "" )
241 if ( EXISTS "${_testdir}/${_count}.global" )
242 set( _dash_g -g ${_testdir}/${_count}.global )
243 endif()
244 if ( EXISTS "${_testdir}/${_count}.job" )
245 set( _dash_j -j ${_testdir}/${_count}.job )
246 endif()
247 add_test(
248 NAME load-${SUBDIRECTORY}-${_count}
249 COMMAND loadmodule ${_dash_g} ${_dash_j} ${SUBDIRECTORY}
250 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
251 )
252 math( EXPR _count "${_count} + 1" )
253 endwhile()
254 if ( EXISTS ${_testdir}/CMakeTests.txt AND NOT EXISTS ${_mod_dir}/CMakeLists.txt )
255 include( ${_testdir}/CMakeTests.txt )
256 endif()
257 if ( PYLINT_COMMAND AND MODULE_INTERFACE MATCHES "python" )
258 # Python modules get an additional test via pylint; this
259 # needs to run at top-level because the ci/libcalamares directory
260 # contains API stubs.
261 #
262 # TODO: the entry point is assumed to be `main.py`, but that is
263 # configurable through module.desc
264 add_test(
265 NAME lint-${SUBDIRECTORY}
266 COMMAND env PYTHONPATH=ci: ${PYLINT_COMMAND} -E ${_mod_dir}/main.py
267 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
268 )
269 endif()
270 endif()
271 endfunction()
272
273 function( calamares_add_module_subdirectory )
274 set( SUBDIRECTORY ${ARGV0} )
275 set( _ams_SKIP_LIST ${ARGV1} )
276
277 set( SKIPPED_MODULES "" )
278 _calamares_add_module_subdirectory_impl( ${SUBDIRECTORY} )
279 if ( SKIPPED_MODULES )
280 if ( _ams_SKIP_LIST )
281 list( APPEND ${_ams_SKIP_LIST} "${SKIPPED_MODULES}" )
282 set( ${_ams_SKIP_LIST} "${${_ams_SKIP_LIST}}" PARENT_SCOPE )
283 else()
284 set( SKIPPED_MODULES "${SKIPPED_MODULES}" PARENT_SCOPE )
285 endif()
286 endif()
287 endfunction()