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

Last change on this file since 374 was 321, checked in by Marek Kurdej, 10 years ago

[cmake] Fix small CMake problem. Change defaults CMake parameters.

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