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

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

Update: PacpusUtilities.cmake: create_export minor fix.

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