StormByte - Build Master
Loading...
Searching...
No Matches
helpers.cmake
Go to the documentation of this file.
1# Include GNUInstallDirs for standard installation directory variables
2include(GNUInstallDirs)
3
4
17function(library_import_hint _lib_full_path _lib_name)
18 if(ARGC GREATER 2)
19 set(_full_prefix_path "${ARGV2}")
20 else()
21 set(_full_prefix_path "${BUILDMASTER_INSTALL_LIBDIR}")
22 endif()
23
24 if (MSVC)
25 set(_prefix "${_full_prefix_path}/${CMAKE_IMPORT_LIBRARY_PREFIX}")
26 set(_suffix "${CMAKE_IMPORT_LIBRARY_SUFFIX}")
27 else()
28 set(_prefix "${_full_prefix_path}/${CMAKE_SHARED_LIBRARY_PREFIX}")
29 set(_suffix "${CMAKE_SHARED_LIBRARY_SUFFIX}")
30 endif()
31
32 set(${_lib_full_path} "${_prefix}${_lib_name}${_suffix}" PARENT_SCOPE)
33endfunction()
34
35
44function(library_import_static_hint _lib_full_path _lib_name)
45 if(ARGC GREATER 2)
46 set(_full_prefix_path "${ARGV2}")
47 else()
48 set(_full_prefix_path "${BUILDMASTER_INSTALL_LIBDIR}")
49 endif()
50
51 set(_prefix "${_full_prefix_path}/${CMAKE_STATIC_LIBRARY_PREFIX}")
52 set(_suffix "${CMAKE_STATIC_LIBRARY_SUFFIX}")
53
54 set(${_lib_full_path} "${_prefix}${_lib_name}${_suffix}" PARENT_SCOPE)
55endfunction()
56
57
68function(library_dll_hint _lib_full_path _lib_name)
69 if(NOT MSVC)
70 message(FATAL_ERROR "library_dll_hint is only applicable on MSVC platforms")
71 endif()
72 if(ARGC GREATER 2)
73 set(_full_prefix_path "${ARGV2}")
74 else()
75 set(_full_prefix_path "${BUILDMASTER_INSTALL_BINDIR}")
76 endif()
77
78 set(_prefix "${_full_prefix_path}/${CMAKE_SHARED_LIBRARY_PREFIX}")
79 set(_suffix "${CMAKE_SHARED_LIBRARY_SUFFIX}")
80
81 set(${_lib_full_path} "${_prefix}${_lib_name}${_suffix}" PARENT_SCOPE)
82endfunction()
83
84
113function(create_component _library_create_file _component _component_title _srcdir _builddir _options _library_mode _build_system _subcomponents _dependency)
114 # Optional indent level
115 if(ARGC GREATER 10)
116 set(_indent_level "${ARGV10}")
117 else()
118 set(_indent_level 0)
119 endif()
120
121 # Common variables
122 set(_LIBRARY_NAME "${_component}")
123 string(TOLOWER "${_library_mode}" _library_mode)
124 set(_LIBRARY_STAGE_INSTALL "${_component}_install")
125 if(NOT _dependency STREQUAL "")
126 set(_LIBRARY_CONFIGURE_TARGET "${_component}_configure")
127 set(_LIBRARY_BUILD_TARGET "${_component}_build")
128 set(_component_suffix "_dependant")
129 else()
130 set(_component_suffix "")
131 endif()
132 if(_library_mode STREQUAL "static")
133 set(_LIBRARY_GENERATOR_FILE "component_static${_component_suffix}.cmake.in")
134 set(_LIBRARY_COMPONENT_NAMES "")
135 set(_LIBRARY_COMPONENT_FILES "")
136 foreach(_subcomponent IN LISTS _subcomponents)
137 list(APPEND _LIBRARY_COMPONENT_NAMES "${_subcomponent}_component")
138 library_import_static_hint(_LIBRARY_FILE_SUB "${_subcomponent}")
139 list(APPEND _LIBRARY_COMPONENT_FILES "${_LIBRARY_FILE_SUB}")
140 endforeach()
141 elseif(_library_mode STREQUAL "shared")
142 set(_LIBRARY_GENERATOR_FILE "component_shared${_component_suffix}.cmake.in")
143 set(_LIBRARY_COMPONENT_NAMES "")
144 set(_LIBRARY_COMPONENT_FILES "")
145 set(_LIBRARY_COMPONENT_DLL_FILES "")
146 foreach(_subcomponent IN LISTS _subcomponents)
147 list(APPEND _LIBRARY_COMPONENT_NAMES "${_subcomponent}_component")
148 library_import_hint(_LIBRARY_FILE_SUB "${_subcomponent}")
149 list(APPEND _LIBRARY_COMPONENT_FILES "${_LIBRARY_FILE_SUB}")
150 if(MSVC)
151 library_dll_hint(_LIBRARY_DLL_FILE_SUB "${_subcomponent}")
152 list(APPEND _LIBRARY_COMPONENT_DLL_FILES "${_LIBRARY_DLL_FILE_SUB}")
153 endif()
154 endforeach()
155 else()
156 message(FATAL_ERROR "Unknown library mode '${_library_mode}' in create_library")
157 endif()
158
159 if(_build_system STREQUAL "cmake")
160 create_cmake_stages(_LIBRARY_CONFIGURE_FILE _LIBRARY_BUILD_FILE _LIBRARY_INSTALL_FILE "${_component}" "${_component_title}" "${_srcdir}" "${_builddir}" "${_options}" "${_library_mode}" "${_LIBRARY_COMPONENT_FILES}" "${_indent_level}")
161 elseif(_build_system STREQUAL "meson")
162 create_meson_stages(_LIBRARY_CONFIGURE_FILE _LIBRARY_BUILD_FILE _LIBRARY_INSTALL_FILE "${_component}" "${_component_title}" "${_srcdir}" "${_builddir}" "${_options}" "${_library_mode}" "${_LIBRARY_COMPONENT_FILES}" "${_indent_level}")
163 else()
164 message(FATAL_ERROR "Unknown build system '${_build_system}' in create_library")
165 endif()
166
167 # Set needed variables for template
168 sanitize_for_filename(_LIBRARY_COMPONENT_SAFE "${_component}")
169 set(_LIBRARY_CREATE_FILE "${BUILDMASTER_SCRIPTS_COMPONENTDIR}/${_LIBRARY_COMPONENT_SAFE}_library.cmake")
170
171 # Expose dependency list to the template (may be empty)
172 set(_LIBRARY_DEPENDENCIES "${_dependency}")
173
174 configure_file(
175 "${BUILDMASTER_COMPONENT_SRCDIR}/${_LIBRARY_GENERATOR_FILE}"
176 "${_LIBRARY_CREATE_FILE}"
177 @ONLY
178 )
179
180 set(${_library_create_file} "${_LIBRARY_CREATE_FILE}" PARENT_SCOPE)
181endfunction()
182
183
195function(create_cmake_component _library_create_file _component _component_title _srcdir _builddir _options _library_mode _subcomponents)
196 # Optional indent level
197 if(ARGC GREATER 8)
198 set(_indent_level "${ARGV8}")
199 else()
200 set(_indent_level 0)
201 endif()
202
203 # Llamamos a create_component, que deja el resultado en *este* scope
204 create_component(
205 ${_library_create_file}
206 "${_component}"
207 "${_component_title}"
208 "${_srcdir}"
209 "${_builddir}"
210 "${_options}"
211 "${_library_mode}"
212 "cmake"
213 "${_subcomponents}"
214 ""
215 ${_indent_level}
216 )
217
218 # Reexpone la variable al scope del llamador real
219 set(${_library_create_file} "${${_library_create_file}}" PARENT_SCOPE)
220endfunction()
221
222
234function(create_meson_component _library_create_file _component _component_title _srcdir _builddir _options _library_mode _subcomponents)
235 if(ARGC GREATER 8)
236 set(_indent_level "${ARGV8}")
237 else()
238 set(_indent_level 0)
239 endif()
240
241 create_component(
242 ${_library_create_file}
243 "${_component}"
244 "${_component_title}"
245 "${_srcdir}"
246 "${_builddir}"
247 "${_options}"
248 "${_library_mode}"
249 "meson"
250 "${_subcomponents}"
251 ""
252 ${_indent_level}
253 )
254
255 set(${_library_create_file} "${${_library_create_file}}" PARENT_SCOPE)
256endfunction()
257
258
273function(create_cmake_dependant_component _library_create_file _component _component_title _srcdir _builddir _options _library_mode _subcomponents _dependency)
274 # Optional indent level
275 if(ARGC GREATER 9)
276 set(_indent_level "${ARGV9}")
277 else()
278 set(_indent_level 0)
279 endif()
280
281 # Llamamos a create_component, que deja el resultado en *este* scope
282 create_component(
283 ${_library_create_file}
284 "${_component}"
285 "${_component_title}"
286 "${_srcdir}"
287 "${_builddir}"
288 "${_options}"
289 "${_library_mode}"
290 "cmake"
291 "${_subcomponents}"
292 "${_dependency}"
293 ${_indent_level}
294 )
295
296 # Reexpone la variable al scope del llamador real
297 set(${_library_create_file} "${${_library_create_file}}" PARENT_SCOPE)
298endfunction()
299
300
315function(create_meson_dependant_component _library_create_file _component _component_title _srcdir _builddir _options _library_mode _subcomponents _dependency)
316 if(ARGC GREATER 9)
317 set(_indent_level "${ARGV9}")
318 else()
319 set(_indent_level 0)
320 endif()
321
322 create_component(
323 ${_library_create_file}
324 "${_component}"
325 "${_component_title}"
326 "${_srcdir}"
327 "${_builddir}"
328 "${_options}"
329 "${_library_mode}"
330 "meson"
331 "${_subcomponents}"
332 "${_dependency}"
333 ${_indent_level}
334 )
335
336 set(${_library_create_file} "${${_library_create_file}}" PARENT_SCOPE)
337endfunction()
338
339
357function(rename_static_library _rename_file _component _badname)
358 set(_LIBRARY_NAME "${_component}")
359 set(_LIBRARY_BAD_PATH "${BUILDMASTER_INSTALL_LIBDIR}/${_badname}")
360 library_import_static_hint(_LIBRARY_GOOD_PATH "${_component}")
361 set(_LIBRARY_STAGE_INSTALL "${_component}_install")
362 set(_LIBRARY_RENAME_FILE "${BUILDMASTER_SCRIPTS_COMPONENTDIR}/${_badname}_rename.cmake")
363
364 configure_file(
365 "${BUILDMASTER_COMPONENT_SRCDIR}/rename_static_library.cmake.in"
366 "${_LIBRARY_RENAME_FILE}"
367 @ONLY
368 )
369
370 set(${_rename_file} "${_LIBRARY_RENAME_FILE}" PARENT_SCOPE)
371endfunction()
372
373
389function(create_bundle_static_libraries _bundle_file _component _libraries)
390 # Generate safe filename
391 sanitize_for_filename(_BUNDLE_COMPONENT_SAFE "${_component}")
392
393 # Compute output path
394 library_import_static_hint(LIBRARY_PATH "${_component}")
395
396 # Configure bundler script
397 if(MSVC)
398 set(_BUNDLE_SCRIPT_FILE "${BUILDMASTER_SCRIPTS_COMPONENTDIR}/${_BUNDLE_COMPONENT_SAFE}_bundler.bat")
399 # For MSVC we expand the list into a space-separated string
400 set(ADD_LIBRARIES "")
401 foreach(lib IN LISTS _libraries)
402 string(APPEND ADD_LIBRARIES "${lib} ")
403 endforeach()
404 configure_file(
405 "${BUILDMASTER_COMPONENT_SRCDIR}/bundler.bat.in"
406 "${_BUNDLE_SCRIPT_FILE}"
407 @ONLY
408 )
409 else()
410 set(_BUNDLE_SCRIPT_FILE "${BUILDMASTER_SCRIPTS_COMPONENTDIR}/${_BUNDLE_COMPONENT_SAFE}_bundler.sh")
411 # In linux we expect ADDLIB lib\n
412 set(ADD_LIBRARIES "")
413 foreach(lib IN LISTS _libraries)
414 string(APPEND ADD_LIBRARIES "ADDLIB ${lib}") # Real line break
415 endforeach()
416 configure_file(
417 "${BUILDMASTER_COMPONENT_SRCDIR}/bundler.sh.in"
418 "${_BUNDLE_SCRIPT_FILE}"
419 @ONLY
420 )
421
422 # Ensure the generated runner has execute permissions so it can be
423 # invoked directly by execute_process(). Some platforms require the
424 # executable bit even when a shebang is present.
425 execute_process(
426 COMMAND ${ENV_RUNNER_SILENT} chmod +x "${_BUNDLE_SCRIPT_FILE}"
427 RESULT_VARIABLE _chmod_result
428 OUTPUT_QUIET
429 ERROR_QUIET
430 )
431 endif()
432
433 # Set output variables
434 set(${_bundle_file} "${_BUNDLE_SCRIPT_FILE}" PARENT_SCOPE)
435endfunction()