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

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