source: pacpusframework/branches/2.0-beta1/cmake/PacpusUtilities.cmake@ 169

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

Update: create_export: added optional 3rd argument -- output directory for the export header file.

  • Property svn:executable set to *
File size: 13.4 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 )
309endfunction()
310
311# ========================================
312# Pacpus add executable and set debug postfix
313# ========================================
314function(pacpus_add_executable target)
315 add_executable(
316 ${ARGV}
317 )
318 set_target_properties(
319 ${target}
320 PROPERTIES DEBUG_POSTFIX "_d"
321 )
322endfunction()
323
324# ========================================
325# Pacpus install
326# ========================================
327function(pacpus_install)
328 install(
329 TARGETS ${ARGV}
330 RUNTIME DESTINATION ${PACPUS_INSTALL_DIR}/bin
331 LIBRARY DESTINATION ${PACPUS_INSTALL_DIR}/lib
332 ARCHIVE DESTINATION ${PACPUS_INSTALL_DIR}/lib
333 )
334 if(MSVC)
335 # install debug information files .pdb
336 foreach(target ${ARGV})
337 get_target_property(target_filepath ${target} LOCATION_DEBUG)
338 get_filename_component(target_directory ${target_filepath} PATH)
339 get_filename_component(target_basename ${target_filepath} NAME_WE)
340
341 set(target_pdb "${target_directory}/${target_basename}.pdb")
342 install(FILES ${target_pdb}
343 CONFIGURATIONS Debug
344 DESTINATION ${PACPUS_INSTALL_DIR}/lib
345 )
346 endforeach()
347 endif(MSVC)
348endfunction()
349
350# ========================================
351# Pacpus folder
352# ========================================
353function(pacpus_folder target folder_name)
354 if(PACPUS_USE_SOLUTION_FOLDERS AND MSVC)
355 set_target_properties(${target}
356 PROPERTIES FOLDER ${folder_name}
357 )
358 endif()
359endfunction()
360
361# ========================================
362# Get sub directories
363# ========================================
364MACRO(SUBDIRLIST result curdir)
365 FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
366 SET(dirlist "")
367 FOREACH(child ${children})
368 IF(IS_DIRECTORY ${curdir}/${child})
369 SET(dirlist ${dirlist} ${child})
370 ENDIF()
371 ENDFOREACH()
372 SET(${result} ${dirlist})
373ENDMACRO()
374
375
376# ========================================
377# Create export header for plugins
378#
379# Usage: `create_export(PluginExportHeader "MyPluginName")`
380#
381# DLL export/import will depend on macro `${PLUGIN_NAME_UPPER}_EXPORTS`
382# which can be set using `add_definitions( -D${PLUGIN_NAME_UPPER}_EXPORTS )`
383# ========================================
384function(CREATE_EXPORT OUT_H PLUGIN_NAME)
385 # set output directory if given, otherwise use current directory
386 set(PLUGIN_OUTPUT_DIR "${ARGN}")
387 # set file name to the input variable
388 set(PLUGIN_CONFIG_FILE "${PLUGIN_NAME}Config.h")
389 if (NOT "${PLUGIN_OUTPUT_DIR}" STREQUAL "")
390 set(PLUGIN_CONFIG_FILE "${PLUGIN_OUTPUT_DIR}/${PLUGIN_CONFIG_FILE}")
391 endif()
392
393 set(${OUT_H} "${PLUGIN_CONFIG_FILE}" PARENT_SCOPE)
394
395 string(TOUPPER ${PLUGIN_NAME} PLUGIN_NAME_UPPER)
396 add_definitions( -D${PLUGIN_NAME_UPPER}_EXPORTS )
397
398 # create the header file
399 file(WRITE "${PLUGIN_CONFIG_FILE}"
400 "// Autogenerated file by PacpusUtilities.cmake\n"
401 "// DO NOT EDIT!!! ALL CHANGES WOULD BE REMOVED BY THE NEXT CALL OF CMAKE\n"
402 "\n"
403
404 "#ifndef __${PLUGIN_NAME_UPPER}_CONFIG_H__\n"
405 "#define __${PLUGIN_NAME_UPPER}_CONFIG_H__\n"
406 "\n"
407 "// Export macro for use DLL for Windows only\n"
408 "#ifdef WIN32\n"
409 " #ifdef ${PLUGIN_NAME_UPPER}_EXPORTS\n"
410 "// make DLL\n"
411 " #define ${PLUGIN_NAME_UPPER}_API __declspec(dllexport)\n"
412 " #else\n"
413 "// use DLL\n"
414 " #define ${PLUGIN_NAME_UPPER}_API __declspec(dllimport)\n"
415 " #endif\n"
416 "#else\n"
417 "// On other platforms, simply ignore this\n"
418 " #define ${PLUGIN_NAME_UPPER}_API\n"
419 "#endif\n\n"
420
421 "#endif // __${PLUGIN_NAME_UPPER}_CONFIG_H__\n"
422 )
423endfunction(CREATE_EXPORT)
Note: See TracBrowser for help on using the repository browser.