Debian Codegraph @

CMakeModules/CalamaresAddPlugin.cmake

raw
1 # === This file is part of Calamares - <https://calamares.io> ===
2 #
3 # SPDX-FileCopyrightText: 2014 Teo Mrnjavac <[email protected]>
4 # SPDX-FileCopyrightText: 2019 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 # Convenience function for creating a C++ (qtplugin) module for Calamares.
13 # This function provides cmake-time feedback about the plugin, adds
14 # targets for compilation and boilerplate information, and creates
15 # a module.desc with standard values (if the module.desc file exists,
16 # that one is used instead, which happens only for unusual plugins).
17 #
18 # Usage:
19 #
20 # calamares_add_plugin(
21 # module-name
22 # TYPE <viewmodule|job>
23 # EXPORT_MACRO macro-name
24 # SOURCES source-file...
25 # UI ui-file...
26 # LINK_LIBRARIES lib...
27 # LINK_PRIVATE_LIBRARIES lib...
28 # [COMPILE_DEFINITIONS def...]
29 # [RESOURCES resource-file]
30 # [REQUIRES module-name...]
31 # [NO_INSTALL]
32 # [NO_CONFIG]
33 # [SHARED_LIB]
34 # [EMERGENCY]
35 # [WEIGHT w]
36 # )
37 #
38 # Function optional parameters:
39 # - COMPILE_DEFINITIONS
40 # Definitions are set on the resulting module with a suitable
41 # flag (i.e. `-D`) so only state the name (optionally, also the value)
42 # without a `-D` prefixed to it.
43 # - RESOURCES
44 # One (single!) filename for the RCC file for the plugin.
45 # - REQUIRES
46 # One or more names of modules which are added to the *requiredModules*
47 # key in the descriptor. See *Module Requirements* in the module
48 # documentation.
49 # - NO_INSTALL
50 # If this is set, the module is not installed by default; use this to
51 # build testing modules or unit-testing modules.
52 # - SHARED_LIB
53 # In unusual circumstances, this function is used to add a library
54 # rather than a normal Calamares module / plugin.
55 # - EMERGENCY
56 # If this is set, the module is marked as an *emergency* module in the
57 # descriptor. See *Emergency Modules* in the module documentation.
58 # - WEIGHT
59 # If this is set, writes an explicit weight into the module.desc;
60 # module weights are used in progress reporting.
61 #
62 #
63 # This function follows the global SKIP_MODULES and USE_* settings, so
64 # a plugin may be skipped -- then nothing will be built. In that case,
65 # SKIPPED_MODULES is set in the parent (i.e. caller's) scope with the
66 # reason why. This should rarely be a concern as AddModuleSubdirectory
67 # already handles skip-reasons and collects them for reporting.
68 #
69 # The target defined this way is called "calamares_<TYPE>_<module-name>",
70 # e.g. "calamares_viewmodule_packagechooserq". The function sets a variable
71 # in its **calling** scope, `<module-name>_TARGET` with the full name
72 # of the target.
73
74 include( CMakeParseArguments )
75
76 include( CalamaresAddLibrary )
77 include( CalamaresCheckModuleSelection )
78 include( CMakeColors )
79
80 function( calamares_add_plugin )
81 # parse arguments ( name needs to be saved before passing ARGN into the macro )
82 set( NAME ${ARGV0} )
83 set( options NO_CONFIG NO_INSTALL SHARED_LIB EMERGENCY )
84 set( oneValueArgs NAME TYPE EXPORT_MACRO RESOURCES WEIGHT )
85 set( multiValueArgs SOURCES UI LINK_LIBRARIES LINK_PRIVATE_LIBRARIES COMPILE_DEFINITIONS REQUIRES )
86 cmake_parse_arguments( PLUGIN "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
87 set( PLUGIN_NAME ${NAME} )
88 set( PLUGIN_DESTINATION ${CMAKE_INSTALL_LIBDIR}/calamares/modules/${PLUGIN_NAME} )
89 set( PLUGIN_DESC_FILE module.desc )
90 file( GLOB PLUGIN_CONFIG_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.conf" )
91 set( PLUGIN_DATA_DESTINATION share/calamares/modules )
92 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" )
93 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" )
94 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" )
95
96 calamares_check_skip( ${NAME} _skip)
97 if ( _skip )
98 set( SKIPPED_MODULES "${_skip}" PARENT_SCOPE )
99 return()
100 endif()
101
102 message( "-- ${BoldYellow}Found ${CALAMARES_APPLICATION_NAME} module: ${BoldRed}${PLUGIN_NAME}${ColorReset}" )
103 message( " ${Green}TYPE:${ColorReset} ${PLUGIN_TYPE}" )
104 message( " ${Green}LINK_LIBRARIES:${ColorReset} ${PLUGIN_LINK_LIBRARIES}" )
105 message( " ${Green}LINK_PRIVATE_LIBRARIES:${ColorReset} ${PLUGIN_LINK_PRIVATE_LIBRARIES}" )
106 message( " ${Green}PLUGIN_DESTINATION:${ColorReset} ${PLUGIN_DESTINATION}" )
107 if( PLUGIN_CONFIG_FILES )
108 if( PLUGIN_NO_CONFIG )
109 message( FATAL_ERROR "${Red}NO_CONFIG${ColorReset} is set, with configuration ${Red}${PLUGIN_CONFIG_FILES}${ColorReset}" )
110 endif()
111 set( _destination "(unknown)" )
112 if(INSTALL_CONFIG AND NOT PLUGIN_NO_INSTALL)
113 set(_destination "${PLUGIN_DATA_DESTINATION}")
114 elseif( NOT PLUGIN_NO_INSTALL )
115 set( _destination "[Build directory only]" )
116 else()
117 set( _destination "[Skipping installation]" )
118 endif()
119 message( " ${Green}CONFIGURATION_FILES:${ColorReset} ${PLUGIN_CONFIG_FILES} => ${_destination}" )
120 else()
121 if( NOT PLUGIN_NO_CONFIG )
122 message( " ${Red}NO_CONFIG${ColorReset} should be set." )
123 endif()
124 endif()
125 if( PLUGIN_RESOURCES )
126 message( " ${Green}RESOURCES:${ColorReset} ${PLUGIN_RESOURCES}" )
127 endif()
128 message( "" )
129
130 # create target name once for convenience
131 set( target "calamares_${PLUGIN_TYPE}_${PLUGIN_NAME}" )
132
133 # automatic library linkage
134 if(PLUGIN_TYPE STREQUAL "viewmodule")
135 list(APPEND PLUGIN_LINK_PRIVATE_LIBRARIES Calamares::calamaresui)
136 elseif(PLUGIN_TYPE STREQUAL "job")
137 list(APPEND PLUGIN_LINK_PRIVATE_LIBRARIES Calamares::calamares)
138 else()
139 message(FATAL_ERROR "Unknown plugin type ${PLUGIN_TYPE}")
140 endif()
141
142 # determine target type
143 if( NOT ${PLUGIN_SHARED_LIB} )
144 set( target_type "MODULE" )
145 else()
146 set( target_type "SHARED" )
147 endif()
148
149 set( calamares_add_library_args
150 "${target}"
151 "EXPORT_MACRO" "${PLUGIN_EXPORT_MACRO}"
152 "TARGET_TYPE" "${target_type}"
153 "SOURCES" "${PLUGIN_SOURCES}"
154 )
155
156 if( PLUGIN_UI )
157 list( APPEND calamares_add_library_args "UI" "${PLUGIN_UI}" )
158 endif()
159
160 if( PLUGIN_LINK_LIBRARIES )
161 list( APPEND calamares_add_library_args "LINK_LIBRARIES" "${PLUGIN_LINK_LIBRARIES}" )
162 endif()
163
164 if( PLUGIN_LINK_PRIVATE_LIBRARIES )
165 list( APPEND calamares_add_library_args "LINK_PRIVATE_LIBRARIES" "${PLUGIN_LINK_PRIVATE_LIBRARIES}" )
166 endif()
167
168 if( PLUGIN_COMPILE_DEFINITIONS )
169 list( APPEND calamares_add_library_args "COMPILE_DEFINITIONS" ${PLUGIN_COMPILE_DEFINITIONS} )
170 endif()
171
172 if ( PLUGIN_NO_INSTALL )
173 list( APPEND calamares_add_library_args "NO_INSTALL" )
174 endif()
175
176 list( APPEND calamares_add_library_args
177 "NO_VERSION"
178 "INSTALL_BINDIR" "${PLUGIN_DESTINATION}"
179 )
180
181 if( PLUGIN_RESOURCES )
182 list( APPEND calamares_add_library_args "RESOURCES" "${PLUGIN_RESOURCES}" )
183 endif()
184
185 calamares_add_library( ${calamares_add_library_args} )
186
187 if ( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${PLUGIN_DESC_FILE} )
188 configure_file( ${PLUGIN_DESC_FILE} ${PLUGIN_DESC_FILE} COPYONLY )
189 else()
190 set( _file ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_DESC_FILE} )
191 set( _type ${PLUGIN_TYPE} )
192 file( WRITE ${_file} "# AUTO-GENERATED metadata file\n# Syntax is YAML 1.2\n---\n" )
193 file( APPEND ${_file} "type: \"${_type}\"\nname: \"${PLUGIN_NAME}\"\ninterface: \"qtplugin\"\nload: \"lib${target}.so\"\n" )
194 if ( PLUGIN_REQUIRES )
195 file( APPEND ${_file} "requiredModules:\n" )
196 foreach( _r ${PLUGIN_REQUIRES} )
197 file( APPEND ${_file} " - ${_r}\n" )
198 endforeach()
199 endif()
200 if ( PLUGIN_EMERGENCY )
201 file( APPEND ${_file} "emergency: true\n" )
202 endif()
203 if ( PLUGIN_NO_CONFIG )
204 file( APPEND ${_file} "noconfig: true\n" )
205 endif()
206 if ( PLUGIN_WEIGHT )
207 file( APPEND ${_file} "weight: ${PLUGIN_WEIGHT}\n" )
208 endif()
209 endif()
210
211 if ( NOT PLUGIN_NO_INSTALL )
212 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_DESC_FILE}
213 DESTINATION ${PLUGIN_DESTINATION} )
214
215 set( _warned_config OFF )
216 foreach( PLUGIN_CONFIG_FILE ${PLUGIN_CONFIG_FILES} )
217 if( ${CMAKE_CURRENT_SOURCE_DIR}/${PLUGIN_CONFIG_FILE} IS_NEWER_THAN ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_CONFIG_FILE} )
218 configure_file( ${PLUGIN_CONFIG_FILE} ${PLUGIN_CONFIG_FILE} COPYONLY )
219 else()
220 message( " ${BoldYellow}Not updating${ColorReset} ${PLUGIN_CONFIG_FILE}" )
221 set( _warned_config ON )
222 endif()
223 if(INSTALL_CONFIG)
224 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_CONFIG_FILE} DESTINATION ${PLUGIN_DATA_DESTINATION})
225 endif()
226 endforeach()
227 if ( _warned_config )
228 message( "" )
229 endif()
230 endif()
231
232 set(${NAME}_TARGET ${target} PARENT_SCOPE)
233 endfunction()