source: pacpusframework/trunk/cmake/PacpusUtilities.cmake@ 228

Last change on this file since 228 was 228, checked in by Marek Kurdej, 11 years ago

Minor: added (commented) manifest SxS.

  • Property svn:executable set to *
File size: 14.1 KB
Line 
1#################################################
2# ___________ ____ ______ __ __ _____ #
3# \____ \__ \ _/ ___\\____ \| | \/ ___/ #
4# | |_> > __ \\ \___| |_> > | /\___ \ #
5# | __(____ /\___ > __/|____//____ > #
6# |__| \/ \/|__| \/ #
7# #
8#################################################
9
10## ========================================
11## Qt macro compatibility Qt4 / Qt5
12## ========================================
13function(qt_wrap_cpp OUT_MOC_SRCS)
14 set(FILES_TO_MOC ${ARGN})
15 if(QT4_FOUND)
16 qt4_wrap_cpp(MOC_SRCS ${FILES_TO_MOC})
17 elseif(Qt5Core_FOUND)
18 qt5_wrap_cpp(MOC_SRCS ${FILES_TO_MOC})
19 else()
20 message(WARNING "QT not found, cannot wrap moc files")
21 endif()
22
23 set(${OUT_MOC_SRCS} ${MOC_SRCS} PARENT_SCOPE)
24endfunction(qt_wrap_cpp)
25
26## ========================================
27## Qt macro compatibility Qt4 / Qt5
28## ========================================
29function(qt_wrap_ui OUT_UI_SRCS)
30 set(UI_FILES ${ARGN})
31 if(QT4_FOUND)
32 qt4_wrap_ui( UI_SRCS ${UI_FILES})
33 elseif(Qt5Core_FOUND)
34 qt5_wrap_ui( UI_SRCS ${UI_FILES})
35 else()
36 message(WARNING "QT not found, cannot wrap ui files")
37 endif()
38
39 set(${OUT_UI_SRCS} ${UI_SRCS} PARENT_SCOPE)
40endfunction()
41
42## ========================================
43## Qt macro compatibility Qt4 / Qt5
44## ========================================
45function(qt_add_resources OUT_RESOURCES)
46 set(RCS_FILES ${ARGN})
47 if(QT4_FOUND)
48 qt4_add_resources( RESOURCES ${RCS_FILES})
49 elseif(Qt5Core_FOUND)
50 qt5_add_resources( RESOURCES ${RCS_FILES})
51 else()
52 message(WARNING "QT not found, cannot add resources")
53 endif()
54
55 set(${OUT_RESOURCES} ${RESOURCES} PARENT_SCOPE)
56endfunction()
57
58# ========================================
59# Generic macro for Pacpus plugin
60# ========================================
61function(PACPUS_PLUGIN OUT_CPP OUT_H PLUGIN_NAME)
62 if(QT4_FOUND)
63 pacpus_plugin_qt4(PLUGIN_CPP PLUGIN_H ${PROJECT_NAME} )
64 elseif(Qt5Core_FOUND)
65 pacpus_plugin_qt5(PLUGIN_CPP PLUGIN_H ${PROJECT_NAME} )
66 else()
67 message(WARNING "QT not found, can't create Plugin")
68 endif()
69
70 set(${OUT_CPP} ${PLUGIN_CPP} PARENT_SCOPE)
71 set(${OUT_H} ${PLUGIN_H} PARENT_SCOPE)
72endfunction(PACPUS_PLUGIN)
73
74# ========================================
75# Create a Pacpus plugin QT4
76# ========================================
77function(PACPUS_PLUGIN_QT4 OUT_CPP OUT_H PLUGIN_NAME)
78 set(PLUGIN_NAME ${PLUGIN_NAME}Plugin)
79 set(${OUT_CPP} ${PLUGIN_NAME}.cpp PARENT_SCOPE)
80 set(${OUT_H} ${PLUGIN_NAME}.h PARENT_SCOPE)
81
82 string(TOUPPER ${PLUGIN_NAME} PLUGIN_NAME_UPPER)
83
84 # create the header file
85 file(WRITE ${PLUGIN_NAME}.h
86 "// Autogenerated file by PacpusPlugin.cmake\n"
87 "// DO NOT EDIT!!! ALL CHANGES WOULD BE REMOVED BY THE NEXT CALL OF CMAKE\n"
88 "\n"
89 "#ifndef __${PLUGIN_NAME_UPPER}_H__\n"
90 "#define __${PLUGIN_NAME_UPPER}_H__\n"
91 "\n"
92 "#include <QObject>\n"
93 "#include <qplugin.h>\n"
94 "\n"
95 "#include <Pacpus/kernel/PacpusPluginInterface.h>\n"
96 "\n"
97 "/// Auto-generated plugin class\n"
98 "class ${PLUGIN_NAME}\n"
99 " : public QObject\n"
100 " , public PacpusPluginInterface\n"
101 "{\n"
102 " Q_OBJECT\n"
103 " Q_INTERFACES(PacpusPluginInterface)\n"
104 "\n"
105 "public:\n"
106 " ${PLUGIN_NAME}();\n"
107 " ~${PLUGIN_NAME}();\n"
108 "\n"
109 "protected:\n"
110 " QString name();\n"
111 "};\n"
112 "\n"
113 "#endif // __${PLUGIN_NAME_UPPER}_H__\n"
114 )
115
116 # create the cpp file
117 file(WRITE ${PLUGIN_NAME}.cpp
118 "// Autogenerated file by PacpusPlugin.cmake\n"
119 "// DO NOT EDIT!!! ALL CHANGES WOULD BE REMOVED BY THE NEXT CALL OF CMAKE\n"
120 "\n"
121 "#include \"${PLUGIN_NAME}.h\"\n"
122 "\n"
123 "${PLUGIN_NAME}::${PLUGIN_NAME}()\n"
124 "{\n"
125 "}\n"
126 "\n"
127 "${PLUGIN_NAME}::~${PLUGIN_NAME}()\n"
128 "{\n"
129 "}\n"
130 "\n"
131 "QString ${PLUGIN_NAME}::name()\n"
132 "{\n"
133 " return \"${PLUGIN_NAME}\";\n"
134 "}\n"
135 "\n"
136 "Q_EXPORT_PLUGIN2(${PLUGIN_NAME}, ${PLUGIN_NAME})\n"
137 )
138endfunction(PACPUS_PLUGIN_QT4)
139
140# ========================================
141# Create a Pacpus plugin QT5
142# ========================================
143function(PACPUS_PLUGIN_QT5 OUT_CPP OUT_H PLUGIN_NAME)
144 set(PLUGIN_NAME ${PLUGIN_NAME}Plugin)
145 set(${OUT_CPP} ${PLUGIN_NAME}.cpp ${PLUGIN_NAME}.json PARENT_SCOPE)
146 set(${OUT_H} ${PLUGIN_NAME}.h PARENT_SCOPE)
147
148 string(TOUPPER ${PLUGIN_NAME} PLUGIN_NAME_UPPER)
149
150 # create the header file
151 file(WRITE ${PLUGIN_NAME}.h
152 "// Autogenerated file by PacpusUtilities.cmake\n"
153 "// DO NOT EDIT!!! ALL CHANGES WOULD BE REMOVED BY THE NEXT CALL OF CMAKE\n"
154 "\n"
155 "#ifndef __${PLUGIN_NAME_UPPER}_H__\n"
156 "#define __${PLUGIN_NAME_UPPER}_H__\n"
157 "\n"
158 "#include <QObject>\n"
159 "#include <qplugin.h>\n"
160 "\n"
161 "#include <Pacpus/kernel/PacpusPluginInterface.h>\n"
162 "\n"
163 "/// Auto-generated plugin class\n"
164 "class ${PLUGIN_NAME}\n"
165 " : public QObject\n"
166 " , public PacpusPluginInterface\n"
167 "{\n"
168 " Q_OBJECT\n"
169 " //Qt5 Plugins Macro\n"
170 " Q_PLUGIN_METADATA(IID \"pacpus." ${PLUGIN_NAME} "\" FILE \"" ${PLUGIN_NAME} ".json\")\n"
171 " Q_INTERFACES(PacpusPluginInterface)\n"
172 "\n"
173 "public:\n"
174 " ${PLUGIN_NAME}();\n"
175 " ~${PLUGIN_NAME}();\n"
176 "\n"
177 "protected:\n"
178 " QString name();\n"
179 "};\n"
180 "\n"
181 "#endif // __${PLUGIN_NAME_UPPER}_H__\n"
182 )
183
184 # create the cpp file
185 file(WRITE ${PLUGIN_NAME}.cpp
186 "// Autogenerated file by PacpusUtilities.cmake\n"
187 "// DO NOT EDIT!!! ALL CHANGES WOULD BE REMOVED BY THE NEXT CALL OF CMAKE\n"
188 "\n"
189 "#include \"${PLUGIN_NAME}.h\"\n"
190 "\n"
191 "${PLUGIN_NAME}::${PLUGIN_NAME}()\n"
192 "{\n"
193 "}\n"
194 "\n"
195 "${PLUGIN_NAME}::~${PLUGIN_NAME}()\n"
196 "{\n"
197 "}\n"
198 "\n"
199 "QString ${PLUGIN_NAME}::name()\n"
200 "{\n"
201 " return \"${PLUGIN_NAME}\";\n"
202 "}\n"
203 "\n"
204 "//Qt4 Plugin Macro desactivated\n"
205 "//Q_EXPORT_PLUGIN2(${PLUGIN_NAME}, ${PLUGIN_NAME})\n"
206 )
207
208 file(WRITE ${PLUGIN_NAME}.json
209 "{\n"
210 " \"Keys\": [\"${PLUGIN_NAME}\"]\n"
211 "}\n"
212 )
213endfunction(PACPUS_PLUGIN_QT5)
214
215# ========================================
216# Replace backslashes by slashes
217# ========================================
218macro(pacpus_purge_backslash var)
219 string(REGEX REPLACE "\\\\" "/" ${var} "${${var}}")
220endmacro()
221
222# ========================================
223# Get the version of MSVC as a string
224# ========================================
225macro(pacpus_get_msvc output)
226 # By default, unknown
227 set(${output} "unknown")
228 if(MSVC9)
229 set(${output} "msvc2008")
230 elseif(MSVC10)
231 set(${output} "msvc2010")
232 else(MSVC11)
233 set(${output} "msvc2012")
234 endif()
235endmacro()
236
237# ========================================
238# Print a debug message
239# ========================================
240function(pacpus_output_status msg)
241 message(STATUS "${msg}")
242 string(REPLACE "\\" "\\\\" msg "${msg}")
243 string(REPLACE "\"" "\\\"" msg "${msg}")
244endfunction()
245
246# ========================================
247# Pacpus log message
248# ========================================
249function(pacpus_info text)
250 set(status_cond)
251 set(status_then)
252 set(status_else)
253
254 set(status_current_name "cond")
255 foreach(arg ${ARGN})
256 if(arg STREQUAL "THEN")
257 set(status_current_name "then")
258 elseif(arg STREQUAL "ELSE")
259 set(status_current_name "else")
260 else()
261 list(APPEND status_${status_current_name} ${arg})
262 endif()
263 endforeach()
264
265 if(DEFINED status_cond)
266 set(status_placeholder_length 32)
267 string(RANDOM LENGTH ${status_placeholder_length} ALPHABET " " status_placeholder)
268 string(LENGTH "${text}" status_text_length)
269 if(status_text_length LESS status_placeholder_length)
270 string(SUBSTRING "${text}${status_placeholder}" 0 ${status_placeholder_length} status_text)
271 elseif(DEFINED status_then OR DEFINED status_else)
272 pacpus_output_status("${text}")
273 set(status_text "${status_placeholder}")
274 else()
275 set(status_text "${text}")
276 endif()
277
278 if(DEFINED status_then OR DEFINED status_else)
279 if(${status_cond})
280 string(REPLACE ";" " " status_then "${status_then}")
281 string(REGEX REPLACE "^[ \t]+" "" status_then "${status_then}")
282 pacpus_output_status("${status_text} ${status_then}")
283 else()
284 string(REPLACE ";" " " status_else "${status_else}")
285 string(REGEX REPLACE "^[ \t]+" "" status_else "${status_else}")
286 pacpus_output_status("${status_text} ${status_else}")
287 endif()
288 else()
289 string(REPLACE ";" " " status_cond "${status_cond}")
290 string(REGEX REPLACE "^[ \t]+" "" status_cond "${status_cond}")
291 pacpus_output_status("${status_text} ${status_cond}")
292 endif()
293 else()
294 pacpus_output_status("${text}")
295 endif()
296endfunction()
297
298# ========================================
299# Pacpus add library and set debug postfix
300# ========================================
301function(pacpus_add_library target)
302 add_library(
303 ${ARGV}
304 )
305 set_target_properties(
306 ${target}
307 PROPERTIES DEBUG_POSTFIX "_d"
308 )
309 # Add manifest to the target (Windows SxS)
310 # TODO: do the same for executable (change #2 to #1)
311 #if(MSVC)
312 # pacpus_info("\${CMAKE_CURRENT_SOURCE_DIR}/\${target}.manifest = ${CMAKE_CURRENT_SOURCE_DIR}/${target}.manifest")
313 # if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}\\${target}.manifest")
314 # pacpus_info("Exists: ${CMAKE_CURRENT_SOURCE_DIR}/${target}.manifest")
315 # add_custom_command(
316 # TARGET ${target}
317 # POST_BUILD
318 # COMMAND "mt.exe" -manifest \"${CMAKE_CURRENT_SOURCE_DIR}\\${target}.manifest\" -outputresource:\"$(TargetDir)\\$(TargetFileName)\"\;\#2
319 # COMMENT "Adding custom manifest containing MSVCRT80 dependency..."
320 # )
321 # endif()
322 #endif()
323endfunction()
324
325# ========================================
326# Pacpus add executable and set debug postfix
327# ========================================
328function(pacpus_add_executable target)
329 add_executable(
330 ${ARGV}
331 )
332 set_target_properties(
333 ${target}
334 PROPERTIES DEBUG_POSTFIX "_d"
335 )
336endfunction()
337
338# ========================================
339# Pacpus install
340# ========================================
341function(pacpus_install)
342 install(
343 TARGETS ${ARGV}
344 RUNTIME DESTINATION ${PACPUS_INSTALL_DIR}/bin
345 LIBRARY DESTINATION ${PACPUS_INSTALL_DIR}/lib
346 ARCHIVE DESTINATION ${PACPUS_INSTALL_DIR}/lib
347 )
348 if(MSVC)
349 # install debug information files .pdb
350 foreach(target ${ARGV})
351 get_target_property(target_filepath ${target} LOCATION_DEBUG)
352 get_filename_component(target_directory ${target_filepath} PATH)
353 get_filename_component(target_basename ${target_filepath} NAME_WE)
354
355 set(target_pdb "${target_directory}/${target_basename}.pdb")
356 install(FILES ${target_pdb}
357 CONFIGURATIONS Debug
358 DESTINATION ${PACPUS_INSTALL_DIR}/lib
359 )
360 endforeach()
361 endif(MSVC)
362endfunction()
363
364# ========================================
365# Pacpus folder
366# ========================================
367function(pacpus_folder target folder_name)
368 if(PACPUS_USE_SOLUTION_FOLDERS AND MSVC)
369 set_target_properties(${target}
370 PROPERTIES FOLDER ${folder_name}
371 )
372 endif()
373endfunction()
374
375# ========================================
376# Get sub directories
377# ========================================
378MACRO(SUBDIRLIST result curdir)
379 FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
380 SET(dirlist "")
381 FOREACH(child ${children})
382 IF(IS_DIRECTORY ${curdir}/${child})
383 SET(dirlist ${dirlist} ${child})
384 ENDIF()
385 ENDFOREACH()
386 SET(${result} ${dirlist})
387ENDMACRO()
388
389
390# ========================================
391# Create export header for plugins
392#
393# Usage: `create_export(PluginExportHeader "MyPluginName")`
394#
395# DLL export/import will depend on macro `${PLUGIN_NAME_UPPER}_EXPORTS`
396# which can be set using `add_definitions( -D${PLUGIN_NAME_UPPER}_EXPORTS )`
397# ========================================
398function(CREATE_EXPORT OUT_H PLUGIN_NAME)
399 # set output directory if given, otherwise use current directory
400 set(PLUGIN_OUTPUT_DIR "${ARGN}")
401 # set file name to the input variable
402 set(PLUGIN_CONFIG_FILE "${PLUGIN_NAME}Config.h")
403 if (NOT "${PLUGIN_OUTPUT_DIR}" STREQUAL "")
404 set(PLUGIN_CONFIG_FILE "${PLUGIN_OUTPUT_DIR}/${PLUGIN_CONFIG_FILE}")
405 endif()
406
407 set(${OUT_H} "${PLUGIN_CONFIG_FILE}" PARENT_SCOPE)
408
409 string(TOUPPER ${PLUGIN_NAME} PLUGIN_NAME_UPPER)
410 add_definitions( -D${PLUGIN_NAME_UPPER}_EXPORTS )
411
412 # create the header file
413 file(WRITE "${PLUGIN_CONFIG_FILE}"
414 "// Autogenerated file by PacpusUtilities.cmake\n"
415 "// DO NOT EDIT!!! ALL CHANGES WOULD BE REMOVED BY THE NEXT CALL OF CMAKE\n"
416 "\n"
417
418 "#ifndef __${PLUGIN_NAME_UPPER}_CONFIG_H__\n"
419 "#define __${PLUGIN_NAME_UPPER}_CONFIG_H__\n"
420 "\n"
421 "// Export macro for use DLL for Windows only\n"
422 "#ifdef WIN32\n"
423 " #ifdef ${PLUGIN_NAME_UPPER}_EXPORTS\n"
424 "// make DLL\n"
425 " #define ${PLUGIN_NAME_UPPER}_API __declspec(dllexport)\n"
426 " #else\n"
427 "// use DLL\n"
428 " #define ${PLUGIN_NAME_UPPER}_API __declspec(dllimport)\n"
429 " #endif\n"
430 "#else\n"
431 "// On other platforms, simply ignore this\n"
432 " #define ${PLUGIN_NAME_UPPER}_API\n"
433 "#endif\n\n"
434
435 "#endif // __${PLUGIN_NAME_UPPER}_CONFIG_H__\n"
436 )
437endfunction(CREATE_EXPORT)
Note: See TracBrowser for help on using the repository browser.