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

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

Update: minor formatting.

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